{"version":3,"sources":["../src/index.ts"],"sourcesContent":["import { Unkey } from \"@unkey/api\";\nimport type { Context, MiddlewareHandler } from \"hono\";\nimport { HTTPException } from \"hono/http-exception\";\nimport * as errors from \"@unkey/api/models/errors\";\n\ntype VerifyResponse = Awaited<\n  ReturnType<InstanceType<typeof Unkey>[\"keys\"][\"verifyKey\"]>\n>;\nexport type UnkeyContext = VerifyResponse;\n\nexport type UnkeyConfig = {\n  /**\n   * The root key is required to verify keys.\n   */\n  rootKey: string;\n  /**\n   * \tArbitrary tags you may add during the verification to filter later.\n   */\n  tags?: string[];\n\n  /**\n   * Require keys to have these permissions to be valid.\n   */\n  permissions?: string;\n\n  /**\n   * How to get the key from the request\n   * Usually the key is provided in an `Authorization` header, but you can do what you want.\n   *\n   * Return the key as string, or undefined if it doesn't exist.\n   *\n   * You can also override the response given to the caller by returning a `Response`\n   *\n   * @default `c.req.header(\"Authorization\")?.replace(\"Bearer \", \"\")`\n   */\n  getKey?: (c: Context) => string | undefined | Response;\n\n  /**\n   * Automatically return a custom response when a key is invalid\n   */\n  handleInvalidKey?: (\n    c: Context,\n    result: UnkeyContext,\n  ) => Response | Promise<Response>;\n\n  /**\n   * What to do if things go wrong\n   */\n  onError?: (c: Context, err: errors.APIError) => Response | Promise<Response>;\n};\n\nexport function unkey(config: UnkeyConfig): MiddlewareHandler {\n  const unkey = new Unkey({\n    rootKey: config.rootKey,\n  });\n\n  return async (c, next) => {\n    const key = config.getKey\n      ? config.getKey(c)\n      : (c.req.header(\"Authorization\")?.replace(\"Bearer \", \"\") ?? null);\n    if (!key) {\n      return c.json({ error: \"unauthorized\" }, { status: 401 });\n    }\n    if (typeof key !== \"string\") {\n      return key;\n    }\n\n    try {\n      const res = await unkey.keys.verifyKey({\n        key,\n        permissions: config.permissions,\n        tags: config.tags,\n      });\n\n      if (!res.data.valid && config.handleInvalidKey) {\n        return config.handleInvalidKey(c, res);\n      }\n      c.set(\"unkey\", res);\n    } catch (err) {\n      if (err instanceof errors.APIError) {\n        if (config.onError) {\n          return config.onError(c, err);\n        }\n        throw new HTTPException(500, {\n          message: `unkey error: [CODE: ${err.statusCode}] - ${err.message}`,\n        });\n      }\n\n      throw err;\n    }\n    await next();\n  };\n}\n"],"mappings":";AAAA,SAAS,aAAa;AAEtB,SAAS,qBAAqB;AAC9B,YAAY,YAAY;AAgDjB,SAAS,MAAM,QAAwC;AAC5D,QAAMA,SAAQ,IAAI,MAAM;AAAA,IACtB,SAAS,OAAO;AAAA,EAClB,CAAC;AAED,SAAO,OAAO,GAAG,SAAS;AACxB,UAAM,MAAM,OAAO,SACf,OAAO,OAAO,CAAC,IACd,EAAE,IAAI,OAAO,eAAe,GAAG,QAAQ,WAAW,EAAE,KAAK;AAC9D,QAAI,CAAC,KAAK;AACR,aAAO,EAAE,KAAK,EAAE,OAAO,eAAe,GAAG,EAAE,QAAQ,IAAI,CAAC;AAAA,IAC1D;AACA,QAAI,OAAO,QAAQ,UAAU;AAC3B,aAAO;AAAA,IACT;AAEA,QAAI;AACF,YAAM,MAAM,MAAMA,OAAM,KAAK,UAAU;AAAA,QACrC;AAAA,QACA,aAAa,OAAO;AAAA,QACpB,MAAM,OAAO;AAAA,MACf,CAAC;AAED,UAAI,CAAC,IAAI,KAAK,SAAS,OAAO,kBAAkB;AAC9C,eAAO,OAAO,iBAAiB,GAAG,GAAG;AAAA,MACvC;AACA,QAAE,IAAI,SAAS,GAAG;AAAA,IACpB,SAAS,KAAK;AACZ,UAAI,eAAsB,iBAAU;AAClC,YAAI,OAAO,SAAS;AAClB,iBAAO,OAAO,QAAQ,GAAG,GAAG;AAAA,QAC9B;AACA,cAAM,IAAI,cAAc,KAAK;AAAA,UAC3B,SAAS,uBAAuB,IAAI,UAAU,OAAO,IAAI,OAAO;AAAA,QAClE,CAAC;AAAA,MACH;AAEA,YAAM;AAAA,IACR;AACA,UAAM,KAAK;AAAA,EACb;AACF;","names":["unkey"]}