@unkey/hono
Version:
<div align="center"> <h1 align="center">@unkey/hono</h1> <h5>Hono.js middleware for authenticating API keys</h5> </div>
43 lines (40 loc) • 1.48 kB
TypeScript
import { Unkey } from '@unkey/api';
import { Context, MiddlewareHandler } from 'hono';
import * as errors from '@unkey/api/models/errors';
type VerifyResponse = Awaited<ReturnType<InstanceType<typeof Unkey>["keys"]["verifyKey"]>>;
type UnkeyContext = VerifyResponse;
type UnkeyConfig = {
/**
* The root key is required to verify keys.
*/
rootKey: string;
/**
* Arbitrary tags you may add during the verification to filter later.
*/
tags?: string[];
/**
* Require keys to have these permissions to be valid.
*/
permissions?: string;
/**
* 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: errors.APIError) => Response | Promise<Response>;
};
declare function unkey(config: UnkeyConfig): MiddlewareHandler;
export { type UnkeyConfig, type UnkeyContext, unkey };