@arcjet/bun
Version:
Arcjet SDK for Bun
122 lines (121 loc) • 4.1 kB
TypeScript
import type { ArcjetDecision, ArcjetOptions as CoreOptions, Primitive, Product, ExtraProps, CharacteristicProps } from "arcjet";
import type { Server } from "bun";
export * from "arcjet";
type Simplify<T> = {
[KeyType in keyof T]: T[KeyType];
} & {};
declare const emptyObjectSymbol: unique symbol;
type WithoutCustomProps = {
[emptyObjectSymbol]?: never;
};
type PlainObject = {
[key: string]: unknown;
};
/**
* Configuration for {@linkcode createRemoteClient}.
*/
export type RemoteClientOptions = {
/**
* Base URI for HTTP requests to Decide API (optional).
*
* Defaults to the environment variable `ARCJET_BASE_URL` (if that value
* is known and allowed) and the standard production API otherwise.
*/
baseUrl?: string;
/**
* Timeout in milliseconds for the Decide API (optional).
*
* Defaults to `500` in production and `1000` in development.
*/
timeout?: number;
};
/**
* Create a remote client.
*
* @param options
* Configuration (optional).
* @returns
* Client.
*/
export declare function createRemoteClient(options?: RemoteClientOptions): import("@arcjet/protocol/client.js").Client;
/**
* Configuration for the Bun integration of Arcjet.
*
* @template Rules
* List of rules.
* @template Characteristics
* Characteristics to track a user by.
*/
export type ArcjetOptions<Rules extends [...Array<Primitive | Product>], Characteristics extends readonly string[]> = Simplify<CoreOptions<Rules, Characteristics> & {
/**
* IP addresses and CIDR ranges of trusted load balancers and proxies
* (optional, example: `["100.100.100.100", "100.100.100.0/24"]`).
*/
proxies?: Array<string>;
}>;
/**
* Instance of the Bun integration of Arcjet.
*
* Primarily has a `protect()` method to make a decision about how a Bun request
* should be handled.
*
* @template Props
* Configuration.
*/
export interface ArcjetBun<Props extends PlainObject> {
/**
* Make a decision about how to handle a request.
*
* This will analyze the request locally where possible and otherwise call
* the Arcjet decision API.
*
* @param ctx
* Additional context for this function call.
* @param request
* Details about the {@linkcode Request} that Arcjet needs to make a
* decision.
* @param props
* Additional properties required for running rules against a request.
* @returns
* Promise that resolves to an {@linkcode ArcjetDecision} indicating
* Arcjet’s decision about the request.
*/
protect(request: Request, ...props: Props extends WithoutCustomProps ? [] : [Props]): Promise<ArcjetDecision>;
/**
* Augment the client with another rule.
*
* Useful for varying rules based on criteria in your handler such as
* different rate limit for logged in users.
*
* @template Rule
* Type of rule.
* @param rule
* Rule to add to Arcjet.
* @returns
* Arcjet instance augmented with the given rule.
*/
withRule<Rule extends Primitive | Product>(rule: Rule): ArcjetBun<Simplify<Props & ExtraProps<Rule>>>;
/**
* Wrap the Bun `fetch` handler to provide additional details when calling
* the `protect()` method.
*
* @param fetch
* Original `fetch` from Bun.
* @returns
* Wrapped `fetch` handler.
*/
handler(fetch: (this: Server, request: Request, server: Server) => Response | Promise<Response>): (this: Server, request: Request, server: Server) => Response | Promise<Response>;
}
/**
* Create a new Bun integration of Arcjet.
*
* @template Rules
* List of rules.
* @template Characteristics
* Characteristics to track a user by.
* @param options
* Configuration.
* @returns
* Bun integration of Arcjet.
*/
export default function arcjet<const Rules extends (Primitive | Product)[], const Characteristics extends readonly string[]>(options: ArcjetOptions<Rules, Characteristics>): ArcjetBun<Simplify<ExtraProps<Rules> & CharacteristicProps<Characteristics>>>;