UNPKG

@unkey/hono

Version:

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

46 lines (44 loc) 1.3 kB
// src/index.ts import { Unkey } from "@unkey/api"; import { HTTPException } from "hono/http-exception"; // package.json var version = "1.5.0"; // src/index.ts function unkey(config) { return async (c, next) => { const key = config.getKey ? config.getKey(c) : c.req.header("Authorization")?.replace("Bearer ", "") ?? null; if (!key) { return c.json({ error: "unauthorized" }, { status: 401 }); } if (typeof key !== "string") { return key; } const unkeyInstance = new Unkey({ rootKey: "public", disableTelemetry: config.disableTelemetry, wrapperSdkVersion: `@unkey/hono@${version}` }); const res = await unkeyInstance.keys.verify({ key, apiId: config.apiId, tags: config.tags }); if (res.error) { if (config.onError) { return config.onError(c, res.error); } throw new HTTPException(500, { message: `unkey error: [CODE: ${res.error.code}] - [REQUEST_ID: ${res.error.requestId}] - ${res.error.message} - read more at ${res.error.docs}` }); } if (!res.result.valid && config.handleInvalidKey) { return config.handleInvalidKey(c, res.result); } c.set("unkey", res.result); await next(); }; } export { unkey }; //# sourceMappingURL=index.mjs.map