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 (43 loc) 1.59 kB
import { Unkey, ErrorResponse } from '@unkey/api'; import { Context, MiddlewareHandler } from 'hono'; type VerifyResponse = Awaited<ReturnType<InstanceType<typeof Unkey>["keys"]["verify"]>>; type UnkeyContext = VerifyResponse["result"]; type UnkeyConfig = { /** * The apiId to verify against. Only keys belonging to this api will be valid. */ apiId: string; /** * Arbitrary tags you may add during the verification to filter later. */ tags?: string[]; /** * * By default telemetry data is enabled, and sends: * runtime (Node.js / Edge) * platform (Node.js / Vercel / AWS) * SDK version */ disableTelemetry?: boolean; /** * How to get the key from the request * Usually the key is provided in an `Authorization` header, but you can do what you want. * * Return the key as string, or undefined if it doesn't exist. * * You can also override the response given to the caller by returning a `Response` * * @default `c.req.header("Authorization")?.replace("Bearer ", "")` */ getKey?: (c: Context) => string | undefined | Response; /** * Automatically return a custom response when a key is invalid */ handleInvalidKey?: (c: Context, result: UnkeyContext) => Response | Promise<Response>; /** * What to do if things go wrong */ onError?: (c: Context, err: ErrorResponse["error"]) => Response | Promise<Response>; }; declare function unkey(config: UnkeyConfig): MiddlewareHandler; export { type UnkeyConfig, type UnkeyContext, unkey };