winterspec
Version:
Write Winter-CG compatible routes with filesystem routing and tons of features
34 lines (33 loc) • 1.99 kB
TypeScript
import type { Middleware } from "../middleware/types.js";
import { type WinterSpecRouteFn, type WinterSpecRouteParams } from "./web-handler.js";
import type { ReadonlyDeep } from "type-fest";
export type WinterSpecRouteMatcher = (pathname: string) => {
matchedRoute: string;
routeParams: WinterSpecRouteParams;
} | undefined | null;
export type WinterSpecRouteMap = Record<string, WinterSpecRouteFn>;
export interface WinterSpecOptions {
handle404?: WinterSpecRouteFn;
}
export interface MakeRequestOptions {
/**
* Defaults to true. When true, we will attempt to automatically remove any pathname prefix from the request. This is useful when you're hosting an WinterSpec service on a subpath of your application.
*
* For example, if you're hosting an WinterSpec service "Foo" at /foo/[...path], then this option will automatically remove the /foo prefix from the request so that the Foo service only sees /[...path].
*
* This currently only works if your parent application is also an WinterSpec service and it's hosting the child service on a wildcard route (/foo/[...path]).
*/
automaticallyRemovePathnamePrefix?: boolean;
/**
* If you want to manually remove a pathname prefix, you can specify it here. `automaticallyRemovePathnamePrefix` must be false when specifying this option.
*/
removePathnamePrefix?: string;
middleware?: Middleware[];
}
export type WinterSpecRouteBundle = ReadonlyDeep<WinterSpecOptions & {
routeMatcher: WinterSpecRouteMatcher;
routeMapWithHandlers: WinterSpecRouteMap;
makeRequest: (request: Request, options?: MakeRequestOptions) => Promise<Response>;
}>;
export type WinterSpecAdapter<Options extends Array<unknown> = [], ReturnValue = void> = (winterSpec: WinterSpecRouteBundle, ...options: Options) => ReturnValue;
export declare function makeRequestAgainstWinterSpec(winterSpec: WinterSpecRouteBundle, options?: MakeRequestOptions): (request: Request) => Promise<Response>;