@unkey/nextjs
Version:
<div align="center"> <h1 align="center">@unkey/nextjs</h1> <h5>`@unkey/nextjs` the official SDK for Next.js. Just use it in your route handlers a direct and type-safe method to verify API keys.</h5> </div>
49 lines (46 loc) • 2.05 kB
text/typescript
import { Unkey } from '@unkey/api';
import * as errors from '@unkey/api/models/errors';
import { NextRequest, NextResponse } from 'next/server';
type WithUnkeyConfig = {
/**
* 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 null if it doesn't exist.
*
* You can also override the response given to the caller by returning a `NextResponse`
*
* @default `req.headers.get("authorization")?.replace("Bearer ", "") ?? null`
*/
getKey?: (req: NextRequest) => string | null | Response | NextResponse;
/**
* Automatically return a custom response when a key is invalid
*/
handleInvalidKey?: (req: NextRequest, result: UnkeyContext) => Response | NextResponse | Promise<Response> | Promise<NextResponse>;
/**
* What to do if things go wrong
*/
onError?: (req: NextRequest, err: errors.APIError) => Response | NextResponse | Promise<Response> | Promise<NextResponse>;
};
type VerifyResponse = Awaited<ReturnType<InstanceType<typeof Unkey>["keys"]["verifyKey"]>>;
type UnkeyContext = VerifyResponse;
type NextContext = {
params: Promise<Record<string, string | string[]>>;
};
type NextRequestWithUnkeyContext = NextRequest & {
unkey: UnkeyContext;
};
declare function withUnkey<TContext extends NextContext = NextContext>(handler: (req: NextRequestWithUnkeyContext, context: TContext) => Response | NextResponse | Promise<Response | NextResponse>, config: WithUnkeyConfig): (req: NextRequest, context: TContext) => Promise<Response>;
export { type NextContext, type NextRequestWithUnkeyContext, type UnkeyContext, type WithUnkeyConfig, withUnkey };