UNPKG

@unkey/hono

Version:

<div align="center"> <h1 align="center">@unkey/hono</h1> <h5>Hono.js middleware for authenticating API keys</h5> </div>

1 lines 4.82 kB
{"version":3,"sources":["../src/index.ts","../package.json"],"sourcesContent":["import { type ErrorResponse, Unkey } from \"@unkey/api\";\nimport type { Context, MiddlewareHandler } from \"hono\";\nimport { HTTPException } from \"hono/http-exception\";\nimport { version } from \"../package.json\";\n\ntype VerifyResponse = Awaited<ReturnType<InstanceType<typeof Unkey>[\"keys\"][\"verify\"]>>;\nexport type UnkeyContext = VerifyResponse[\"result\"];\n\nexport type UnkeyConfig = {\n /**\n * The apiId to verify against. Only keys belonging to this api will be valid.\n */\n apiId: string;\n\n /**\n * \tArbitrary tags you may add during the verification to filter later.\n */\n tags?: string[];\n\n /**\n *\n * By default telemetry data is enabled, and sends:\n * runtime (Node.js / Edge)\n * platform (Node.js / Vercel / AWS)\n * SDK version\n */\n disableTelemetry?: boolean;\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?: (c: Context, result: UnkeyContext) => Response | Promise<Response>;\n\n /**\n * What to do if things go wrong\n */\n onError?: (c: Context, err: ErrorResponse[\"error\"]) => Response | Promise<Response>;\n};\n\nexport function unkey(config: UnkeyConfig): MiddlewareHandler {\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 const unkeyInstance = new Unkey({\n rootKey: \"public\",\n disableTelemetry: config.disableTelemetry,\n wrapperSdkVersion: `@unkey/hono@${version}`,\n });\n\n const res = await unkeyInstance.keys.verify({\n key,\n apiId: config.apiId,\n tags: config.tags,\n });\n if (res.error) {\n if (config.onError) {\n return config.onError(c, res.error);\n }\n throw new HTTPException(500, {\n message: `unkey error: [CODE: ${res.error.code}] - [REQUEST_ID: ${res.error.requestId}] - ${res.error.message} - read more at ${res.error.docs}`,\n });\n }\n\n if (!res.result.valid && config.handleInvalidKey) {\n return config.handleInvalidKey(c, res.result);\n }\n\n c.set(\"unkey\", res.result);\n await next();\n };\n}\n","{\n \"name\": \"@unkey/hono\",\n \"version\": \"1.5.0\",\n \"main\": \"./dist/index.js\",\n \"types\": \"./dist/index.d.ts\",\n \"license\": \"MIT\",\n \"private\": false,\n \"publishConfig\": {\n \"access\": \"public\"\n },\n \"keywords\": [\n \"unkey\",\n \"client\",\n \"api\",\n \"hono\"\n ],\n \"bugs\": {\n \"url\": \"https://github.com/unkeyed/unkey/issues\"\n },\n \"homepage\": \"https://github.com/unkeyed/unkey#readme\",\n \"files\": [\n \"./dist/**\"\n ],\n \"author\": \"Andreas Thomas <andreas@chronark.com>\",\n \"scripts\": {\n \"build\": \"tsup\",\n \"test\": \"vitest run\"\n },\n \"devDependencies\": {\n \"@types/node\": \"^20.14.9\",\n \"hono\": \"^4.6.3\",\n \"msw\": \"^2.8.7\",\n \"tsup\": \"^8.0.2\",\n \"typescript\": \"^5.5.3\",\n \"zod\": \"^3.23.5\"\n },\n \"dependencies\": {\n \"@unkey/api\": \"workspace:^\"\n },\n \"peerDependencies\": {\n \"hono\": \"^4.6.3\"\n }\n}\n"],"mappings":";AAAA,SAA6B,aAAa;AAE1C,SAAS,qBAAqB;;;ACA5B,cAAW;;;ADiDN,SAAS,MAAM,QAAwC;AAC5D,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,aA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