@kaito-http/core 4.0.0-beta.3 → 4.0.0-beta.30

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.d.cts CHANGED
@@ -1,5 +1,7 @@
1
- import { z } from 'zod';
2
- import { KaitoSSEResponse } from './stream/stream.cjs';
1
+ import { JSONValue, BaseSchema, AnySchemaFor } from './schema/schema.cjs';
2
+ export { ArrayChecks, ArrayDef, BaseSchemaDef, BooleanDef, Issue, JSONPrimitive, KArray, KBoolean, KLazy, KLiteral, KNativeEnum, KNull, KNumber, KObject, KObjectFromURLSearchParams, KRecord, KRef, KScalar, KString, KUnion, LazyDef, LiteralDef, NativeEnumDef, NullDef, NumberChecks, NumberDef, NumberFormat, ObjectDef, ParseContext, ParseResult, RecordDef, RefDef, STRING_FORMAT_REGEXES, ScalarDef, ScalarOptions, SchemaError, StringChecks, StringDef, StringFormat, UnionDef, isPrimitiveJSONValue, k } from './schema/schema.cjs';
3
+ import * as OpenAPI from 'openapi3-ts/oas31';
4
+ import { KaitoSSEResponse, SSEEvent } from './stream/stream.cjs';
3
5
 
4
6
  declare class WrappedError<T> extends Error {
5
7
  static maybe<T>(maybeError: T): (T & Error) | WrappedError<T>;
@@ -21,14 +23,12 @@ declare class KaitoRequest {
21
23
  arrayBuffer(): Promise<ArrayBuffer>;
22
24
  blob(): Promise<Blob>;
23
25
  formData(): Promise<FormData>;
24
- bytes(): Promise<Uint8Array>;
26
+ bytes(): Promise<Uint8Array<ArrayBuffer>>;
25
27
  json(): Promise<unknown>;
26
28
  text(): Promise<string>;
27
29
  get request(): Request;
28
30
  }
29
31
 
30
- type KaitoMethod = 'GET' | 'POST' | 'PUT' | 'PATCH' | 'DELETE' | 'HEAD' | 'OPTIONS';
31
-
32
32
  /**
33
33
  * This class is merely a wrapper around a `Headers` object and a status code.
34
34
  * It's used while the router is executing a route to store any mutations to the status
@@ -67,139 +67,168 @@ declare class KaitoHead {
67
67
  * @param body The Kaito JSON format to be sent as the response body
68
68
  * @returns A Response instance, ready to be sent
69
69
  */
70
- toResponse<T>(body: APIResponse<T>): Response;
70
+ toResponse<T extends JSONValue>(data: T): Response;
71
71
  /**
72
72
  * Whether this KaitoHead instance has been touched/modified
73
73
  */
74
74
  get touched(): boolean;
75
75
  }
76
76
 
77
- /**
78
- * A helper to check if the environment is Node.js-like and the NODE_ENV is development
79
- */
80
- declare const isNodeLikeDev: boolean;
81
- type ErroredAPIResponse = {
82
- success: false;
83
- data: null;
84
- message: string;
85
- };
86
- type SuccessfulAPIResponse<T> = {
87
- success: true;
88
- data: T;
77
+ type PrefixRoutesPathInner<R extends AnyRoute, Prefix extends `/${string}`> = R extends Route<infer ContextFrom, infer ContextTo, infer RouterInput, infer ResultOutput, infer Path, infer AdditionalParams, infer Method, infer Query, infer BodyOutput> ? Route<ContextFrom, ContextTo, RouterInput, ResultOutput, `${Prefix}${Path extends '/' ? '' : Path}`, AdditionalParams, Method, Query, BodyOutput> : never;
78
+ type PrefixRoutesPath<Prefix extends `/${string}`, R extends AnyRoute> = R extends R ? PrefixRoutesPathInner<R, Prefix> : never;
79
+ type RouterState<ContextFrom, ContextTo, RequiredParams extends string, Routes extends AnyRoute, Input extends readonly unknown[]> = {
80
+ routes: Set<Routes>;
81
+ through: (context: ContextFrom, params: Record<RequiredParams, string>) => Promise<ContextTo> | ContextTo;
82
+ config: KaitoConfig<ContextFrom, Input>;
89
83
  };
90
- type APIResponse<T> = ErroredAPIResponse | SuccessfulAPIResponse<T>;
91
- type AnyResponse = APIResponse<unknown>;
92
- type MakeOptional<T, K extends keyof T> = T extends T ? Omit<T, K> & Partial<Pick<T, K>> : never;
93
- type MaybePromise<T> = T | Promise<T>;
94
- type ExtractRouteParams<T extends string> = string extends T ? Record<string, string> : T extends `${string}:${infer Param}/${infer Rest}` ? {
95
- [k in Param | keyof ExtractRouteParams<Rest>]: string;
96
- } : T extends `${string}:${infer Param}` ? {
97
- [k in Param]: string;
98
- } : {};
99
- /**
100
- * A function that is called to get the context for a request.
101
- *
102
- * This is useful for things like authentication, to pass in a database connection, etc.
103
- *
104
- * It's fine for this function to throw; if it does, the error is passed to the `onError` function.
105
- *
106
- * @param req - The kaito request object, which contains the request method, url, headers, etc
107
- * @param head - The kaito head object, which contains getters and setters for headers and status
108
- * @returns The context for your routes
109
- */
110
- type GetContext<Result> = (req: KaitoRequest, head: KaitoHead) => MaybePromise<Result>;
84
+ declare class Router<ContextFrom, ContextTo, RequiredParams extends string, Routes extends AnyRoute, Input extends readonly unknown[]> {
85
+ #private;
86
+ static create: <Context = null, Input_1 extends readonly unknown[] = []>(config?: KaitoConfig<Context, Input_1>) => Router<Context, Context, never, never, Input_1>;
87
+ protected constructor(state: RouterState<ContextFrom, ContextTo, RequiredParams, Routes, Input>);
88
+ get routes(): Set<Routes>;
89
+ private readonly add;
90
+ readonly params: [RequiredParams] extends [never] ? <NextParams extends string>() => Router<ContextFrom, ContextTo, NextParams, Routes, Input> : () => Router<ContextFrom, ContextTo, RequiredParams, Routes, Input>;
91
+ readonly merge: <PathPrefix extends `/${string}`, NextRequiredParams extends string, OtherRoutes extends AnyRoute>(pathPrefix: [NextRequiredParams] extends [ExtractRouteParams<PathPrefix> | RequiredParams] ? PathPrefix : `/:${Exclude<NextRequiredParams, ExtractRouteParams<PathPrefix> | RequiredParams>}`, other: Router<ContextFrom, ContextTo, NextRequiredParams, OtherRoutes, Input>) => Router<ContextFrom, ContextTo, RequiredParams, Routes | PrefixRoutesPath<PathPrefix, OtherRoutes>, Input>;
92
+ protected static getFindRoute: <R>(routes: Map<KaitoMethod, Map<string, R>>) => (method: KaitoMethod, path: string) => {
93
+ route?: never;
94
+ params?: never;
95
+ } | {
96
+ route: R;
97
+ params: Record<string, string>;
98
+ };
99
+ serve: () => (request: Request, ...args: Input) => Promise<Response>;
100
+ /**
101
+ * Create a `/openapi.json` route on this router.
102
+ *
103
+ * Any routes defined AFTER this method call will NOT be included in the
104
+ * file. This is because all methods in Kaito are immutable, so there's no
105
+ * way for the router to know about routes that were created in the future
106
+ * on another router.
107
+ *
108
+ * @example
109
+ * ```ts
110
+ * router.get("/", () => "hey").openapi({
111
+ * info: {
112
+ * title: "My API",
113
+ * version: "1.0.0",
114
+ * },
115
+ * });
116
+ * ```
117
+ *
118
+ * @param options Options object
119
+ * @returns
120
+ */
121
+ openapi: ({ info, servers, }: {
122
+ info: OpenAPI.InfoObject;
123
+ servers?: Partial<Record<(`https://` | `http://`) | ({} & string), string>>;
124
+ }) => Router<ContextFrom, ContextTo, RequiredParams, Routes | Route<ContextFrom, ContextTo, Input, Response, "/openapi.json", RequiredParams, "GET", {}, never>, Input>;
125
+ private readonly method;
126
+ readonly get: <Path extends string, ResultOutput = never, Query extends AnyQuery = {}, Body extends JSONValue = never>(path: Path, route: ((data: RouteRunData<RequiredParams | ExtractRouteParams<Path>, ContextTo, Query, Body>) => ResultOutput | Promise<ResultOutput>) | (Omit<Route<ContextFrom, ContextTo, Input, ResultOutput, Path, RequiredParams, "GET", Query, Body>, "body" | "path" | "method" | "router" | "openapi"> & {
127
+ openapi?: OpenAPISpecFor<ResultOutput>;
128
+ })) => Router<ContextFrom, ContextTo, RequiredParams, Routes | Route<ContextFrom, ContextTo, Input, ResultOutput, Path, RequiredParams, "GET", Query, Body>, Input>;
129
+ readonly post: <Path extends string, ResultOutput = never, Query extends AnyQuery = {}, Body extends JSONValue = never>(path: Path, route: ((data: RouteRunData<RequiredParams | ExtractRouteParams<Path>, ContextTo, Query, Body>) => ResultOutput | Promise<ResultOutput>) | (Omit<Route<ContextFrom, ContextTo, Input, ResultOutput, Path, RequiredParams, "POST", Query, Body>, "path" | "method" | "router" | "openapi"> & {
130
+ openapi?: OpenAPISpecFor<ResultOutput>;
131
+ })) => Router<ContextFrom, ContextTo, RequiredParams, Routes | Route<ContextFrom, ContextTo, Input, ResultOutput, Path, RequiredParams, "POST", Query, Body>, Input>;
132
+ readonly put: <Path extends string, ResultOutput = never, Query extends AnyQuery = {}, Body extends JSONValue = never>(path: Path, route: ((data: RouteRunData<RequiredParams | ExtractRouteParams<Path>, ContextTo, Query, Body>) => ResultOutput | Promise<ResultOutput>) | (Omit<Route<ContextFrom, ContextTo, Input, ResultOutput, Path, RequiredParams, "PUT", Query, Body>, "path" | "method" | "router" | "openapi"> & {
133
+ openapi?: OpenAPISpecFor<ResultOutput>;
134
+ })) => Router<ContextFrom, ContextTo, RequiredParams, Routes | Route<ContextFrom, ContextTo, Input, ResultOutput, Path, RequiredParams, "PUT", Query, Body>, Input>;
135
+ readonly patch: <Path extends string, ResultOutput = never, Query extends AnyQuery = {}, Body extends JSONValue = never>(path: Path, route: ((data: RouteRunData<RequiredParams | ExtractRouteParams<Path>, ContextTo, Query, Body>) => ResultOutput | Promise<ResultOutput>) | (Omit<Route<ContextFrom, ContextTo, Input, ResultOutput, Path, RequiredParams, "PATCH", Query, Body>, "path" | "method" | "router" | "openapi"> & {
136
+ openapi?: OpenAPISpecFor<ResultOutput>;
137
+ })) => Router<ContextFrom, ContextTo, RequiredParams, Routes | Route<ContextFrom, ContextTo, Input, ResultOutput, Path, RequiredParams, "PATCH", Query, Body>, Input>;
138
+ readonly delete: <Path extends string, ResultOutput = never, Query extends AnyQuery = {}, Body extends JSONValue = never>(path: Path, route: ((data: RouteRunData<RequiredParams | ExtractRouteParams<Path>, ContextTo, Query, Body>) => ResultOutput | Promise<ResultOutput>) | (Omit<Route<ContextFrom, ContextTo, Input, ResultOutput, Path, RequiredParams, "DELETE", Query, Body>, "path" | "method" | "router" | "openapi"> & {
139
+ openapi?: OpenAPISpecFor<ResultOutput>;
140
+ })) => Router<ContextFrom, ContextTo, RequiredParams, Routes | Route<ContextFrom, ContextTo, Input, ResultOutput, Path, RequiredParams, "DELETE", Query, Body>, Input>;
141
+ readonly head: <Path extends string, ResultOutput = never, Query extends AnyQuery = {}, Body extends JSONValue = never>(path: Path, route: ((data: RouteRunData<RequiredParams | ExtractRouteParams<Path>, ContextTo, Query, Body>) => ResultOutput | Promise<ResultOutput>) | (Omit<Route<ContextFrom, ContextTo, Input, ResultOutput, Path, RequiredParams, "HEAD", Query, Body>, "path" | "method" | "router" | "openapi"> & {
142
+ openapi?: OpenAPISpecFor<ResultOutput>;
143
+ })) => Router<ContextFrom, ContextTo, RequiredParams, Routes | Route<ContextFrom, ContextTo, Input, ResultOutput, Path, RequiredParams, "HEAD", Query, Body>, Input>;
144
+ readonly options: <Path extends string, ResultOutput = never, Query extends AnyQuery = {}, Body extends JSONValue = never>(path: Path, route: ((data: RouteRunData<RequiredParams | ExtractRouteParams<Path>, ContextTo, Query, Body>) => ResultOutput | Promise<ResultOutput>) | (Omit<Route<ContextFrom, ContextTo, Input, ResultOutput, Path, RequiredParams, "OPTIONS", Query, Body>, "path" | "method" | "router" | "openapi"> & {
145
+ openapi?: OpenAPISpecFor<ResultOutput>;
146
+ })) => Router<ContextFrom, ContextTo, RequiredParams, Routes | Route<ContextFrom, ContextTo, Input, ResultOutput, Path, RequiredParams, "OPTIONS", Query, Body>, Input>;
147
+ through: <NextContext>(through: (context: ContextTo, params: Record<RequiredParams, string>) => MaybePromise<NextContext>) => Router<ContextFrom, NextContext, RequiredParams, Routes, Input>;
148
+ }
111
149
 
112
- type RouteRunData<Path extends string, Context, QueryOutput, BodyOutput> = {
150
+ type RouteRunData<Params extends string, Context, QueryOutput, BodyOutput> = {
151
+ params: Record<Params, string>;
113
152
  ctx: Context;
114
- body: BodyOutput;
115
153
  query: QueryOutput;
116
- params: ExtractRouteParams<Path>;
154
+ body: BodyOutput;
117
155
  };
118
156
  type AnyQuery = {
119
157
  [key in string]: any;
120
158
  };
121
- type Through<From, To> = (context: From) => Promise<To>;
122
- type SSEOutputSpec<Result> = {
159
+ type Through<From, To, RequiredParams extends string> = (context: From, params: Record<RequiredParams, string>) => Promise<To>;
160
+ type SSEOutputSpecWithSchema = {
123
161
  type: 'sse';
124
- schema: z.Schema<Result>;
125
- description?: string;
162
+ schema: BaseSchema<any, any, any>;
163
+ description?: string | undefined;
164
+ };
165
+ type SSEOutputSpecWithoutSchema = {
166
+ type: 'sse';
167
+ schema?: undefined;
168
+ description?: string | undefined;
126
169
  };
127
- type JSONOutputSpec<Result> = {
170
+ type SSEOutputSpec = SSEOutputSpecWithSchema | SSEOutputSpecWithoutSchema;
171
+ type JSONOutputSpec = {
128
172
  type: 'json';
129
- schema: z.Schema<Result>;
130
- description?: string;
173
+ schema: BaseSchema<any, any, any>;
174
+ description?: string | undefined;
175
+ };
176
+ type ResponseOutputSpec = {
177
+ type: 'response';
178
+ description?: string | undefined;
131
179
  };
132
- type OutputSpec<Result> = {
180
+ type OutputSpec = SSEOutputSpec | JSONOutputSpec | ResponseOutputSpec;
181
+ type OpenAPISpec<Body extends OutputSpec = OutputSpec> = {
182
+ summary?: string;
133
183
  description?: string;
134
- body: NoInfer<Result extends KaitoSSEResponse<infer R> ? SSEOutputSpec<R> : JSONOutputSpec<Result>>;
184
+ body: Body;
135
185
  };
136
- type Route<ContextTo, Result, Path extends string, Method extends KaitoMethod, Query, Body> = {
137
- through: Through<unknown, ContextTo>;
138
- body?: z.Schema<Body>;
186
+ type OpenAPISpecFor<ResultOutput> = 0 extends 1 & ResultOutput ? OpenAPISpec : [ResultOutput] extends [never] ? OpenAPISpec : [ResultOutput] extends [KaitoSSEResponse<any>] ? [ResultOutput extends KaitoSSEResponse<SSEEvent<infer U, any>> ? U : never] extends [JSONValue] ? OpenAPISpec<SSEOutputSpec> : OpenAPISpec<SSEOutputSpecWithSchema> : [ResultOutput] extends [Response] ? OpenAPISpec<ResponseOutputSpec> : OpenAPISpec<JSONOutputSpec>;
187
+ type Route<ContextFrom, ContextTo, RouterInput extends readonly unknown[], ResultOutput, Path extends string, AdditionalParams extends string, Method extends KaitoMethod, Query extends Record<string, JSONValue>, Body extends JSONValue> = {
188
+ body?: AnySchemaFor<Body>;
139
189
  query?: {
140
- [Key in keyof Query]: z.Schema<Query[Key]>;
190
+ [Key in keyof Query]: AnySchemaFor<Query[Key]>;
141
191
  };
142
192
  path: Path;
143
193
  method: Method;
144
- openapi?: OutputSpec<NoInfer<Result>>;
145
- run(data: RouteRunData<Path, ContextTo, Query, Body>): Promise<Result> | Result;
194
+ openapi?: OpenAPISpec;
195
+ router: Router<ContextFrom, ContextTo, AdditionalParams, AnyRoute, RouterInput>;
196
+ run(data: RouteRunData<ExtractRouteParams<Path> | AdditionalParams, ContextTo, Query, Body>): Promise<ResultOutput> | ResultOutput;
146
197
  };
147
- type AnyRoute = Route<any, any, any, any, any, any>;
198
+ type AnyRoute = Route<any, any, any, unknown, any, any, any, any, any>;
148
199
 
149
- type PrefixRoutesPathInner<R extends AnyRoute, Prefix extends `/${string}`> = R extends Route<infer ContextTo, infer Result, infer Path, infer Method, infer Query, infer BodyOutput> ? Route<ContextTo, Result, `${Prefix}${Path}`, Method, Query, BodyOutput> : never;
150
- type PrefixRoutesPath<Prefix extends `/${string}`, R extends AnyRoute> = R extends R ? PrefixRoutesPathInner<R, Prefix> : never;
151
- type RouterState<ContextFrom, ContextTo, Routes extends AnyRoute> = {
152
- routes: Set<Routes>;
153
- through: (context: unknown) => Promise<ContextTo>;
154
- config: KaitoConfig<ContextFrom>;
155
- };
200
+ /**
201
+ * A helper to check if the environment is Node.js-like and the `NODE_ENV` environment variable is set to `'development'`
202
+ */
203
+ declare const isNodeLikeDev: boolean;
204
+ type MaybePromise<T> = T | Promise<T>;
205
+ type KaitoMethod = 'GET' | 'POST' | 'PUT' | 'PATCH' | 'DELETE' | 'HEAD' | 'OPTIONS';
206
+ type ExtractRouteParams<T extends string> = string extends T ? string : T extends `${string}:${infer Param}/${infer Rest}` ? Param | ExtractRouteParams<Rest> : T extends `${string}:${infer Param}` ? Param : never;
156
207
  /**
157
208
  * Accepts a router instance, and returns a union of all the routes in the router
158
209
  *
159
210
  * @example
160
211
  * ```ts
161
- * const app = router().get('/', () => 'Hello, world!');
212
+ * const app = router.get('/', () => 'Hello, world!');
162
213
  *
163
214
  * type Routes = InferRoutes<typeof app>;
164
215
  * ```
165
216
  */
166
- type InferRoutes<R extends Router<any, any, any>> = R extends Router<any, any, infer R extends AnyRoute> ? R : never;
167
- declare class Router<ContextFrom, ContextTo, R extends AnyRoute> {
168
- private readonly state;
169
- static create: <Context>(config: KaitoConfig<Context>) => Router<Context, Context, never>;
170
- constructor(state: RouterState<ContextFrom, ContextTo, R>);
171
- get routes(): Set<R>;
172
- private add;
173
- readonly merge: <PathPrefix extends `/${string}`, OtherRoutes extends AnyRoute>(pathPrefix: PathPrefix, other: Router<ContextFrom, unknown, OtherRoutes>) => Router<ContextFrom, ContextTo, Extract<R | PrefixRoutesPath<PathPrefix, Extract<OtherRoutes, AnyRoute>>, AnyRoute>>;
174
- protected static getFindRoute: <R_1>(routes: Map<KaitoMethod, Map<string, R_1>>) => (method: KaitoMethod, path: string) => {
175
- route?: never;
176
- params?: never;
177
- } | {
178
- route: R_1;
179
- params: Record<string, string>;
180
- };
181
- private static buildQuerySchema;
182
- serve: () => (request: Request) => Promise<Response>;
183
- openapi: (highLevelSpec: {
184
- info: {
185
- version: string;
186
- title: string;
187
- description?: string;
188
- };
189
- servers?: Partial<Record<(`https://` | `http://`) | ({} & string), string>>;
190
- }) => Router<ContextFrom, ContextTo, R | Route<ContextTo, Response, "/openapi.json", "GET", AnyQuery, unknown>>;
191
- private readonly method;
192
- get: <Result, Path extends string, Query extends AnyQuery = {}, Body = never>(path: Path, route: ((data: RouteRunData<Path, ContextTo, Query, Body>) => Result | Promise<Result>) | Omit<Route<ContextTo, Result, Path, "GET", Query, Body>, "body" | "path" | "method" | "through">) => Router<ContextFrom, ContextTo, R | Route<ContextTo, Result, Path, "GET", Query, Body>>;
193
- post: <Result, Path extends string, Query extends AnyQuery = {}, Body = never>(path: Path, route: ((data: RouteRunData<Path, ContextTo, Query, Body>) => Result | Promise<Result>) | Omit<Route<ContextTo, Result, Path, "POST", Query, Body>, "path" | "method" | "through">) => Router<ContextFrom, ContextTo, R | Route<ContextTo, Result, Path, "POST", Query, Body>>;
194
- put: <Result, Path extends string, Query extends AnyQuery = {}, Body = never>(path: Path, route: ((data: RouteRunData<Path, ContextTo, Query, Body>) => Result | Promise<Result>) | Omit<Route<ContextTo, Result, Path, "PUT", Query, Body>, "path" | "method" | "through">) => Router<ContextFrom, ContextTo, R | Route<ContextTo, Result, Path, "PUT", Query, Body>>;
195
- patch: <Result, Path extends string, Query extends AnyQuery = {}, Body = never>(path: Path, route: ((data: RouteRunData<Path, ContextTo, Query, Body>) => Result | Promise<Result>) | Omit<Route<ContextTo, Result, Path, "PATCH", Query, Body>, "path" | "method" | "through">) => Router<ContextFrom, ContextTo, R | Route<ContextTo, Result, Path, "PATCH", Query, Body>>;
196
- delete: <Result, Path extends string, Query extends AnyQuery = {}, Body = never>(path: Path, route: ((data: RouteRunData<Path, ContextTo, Query, Body>) => Result | Promise<Result>) | Omit<Route<ContextTo, Result, Path, "DELETE", Query, Body>, "path" | "method" | "through">) => Router<ContextFrom, ContextTo, R | Route<ContextTo, Result, Path, "DELETE", Query, Body>>;
197
- head: <Result, Path extends string, Query extends AnyQuery = {}, Body = never>(path: Path, route: ((data: RouteRunData<Path, ContextTo, Query, Body>) => Result | Promise<Result>) | Omit<Route<ContextTo, Result, Path, "HEAD", Query, Body>, "path" | "method" | "through">) => Router<ContextFrom, ContextTo, R | Route<ContextTo, Result, Path, "HEAD", Query, Body>>;
198
- options: <Result, Path extends string, Query extends AnyQuery = {}, Body = never>(path: Path, route: ((data: RouteRunData<Path, ContextTo, Query, Body>) => Result | Promise<Result>) | Omit<Route<ContextTo, Result, Path, "OPTIONS", Query, Body>, "path" | "method" | "through">) => Router<ContextFrom, ContextTo, R | Route<ContextTo, Result, Path, "OPTIONS", Query, Body>>;
199
- through: <NextContext>(through: (context: ContextTo) => MaybePromise<NextContext>) => Router<ContextFrom, NextContext, R>;
200
- }
217
+ type InferRoutes<R extends Router<never, never, never, never, never>> = R extends Router<any, any, any, infer R extends AnyRoute, any> ? R : never;
218
+ /**
219
+ * A function that is called to get the context for a request.
220
+ *
221
+ * This is useful for things like authentication, to pass in a database connection, etc.
222
+ *
223
+ * It's fine for this function to throw; if it does, the error is passed to the `onError` function.
224
+ *
225
+ * @param req - The kaito request object, which contains the request method, url, headers, etc
226
+ * @param head - The kaito head object, which contains getters and setters for headers and status
227
+ * @returns The context for your routes
228
+ */
229
+ type GetContext<Result, Input extends readonly unknown[]> = (req: KaitoRequest, head: KaitoHead, ...args: Input) => MaybePromise<Result>;
201
230
 
202
- type KaitoConfig<ContextFrom> = {
231
+ interface KaitoConfig<ContextFrom, Input extends readonly unknown[]> {
203
232
  /**
204
233
  * A function that is called to get the context for a request.
205
234
  *
@@ -207,7 +236,7 @@ type KaitoConfig<ContextFrom> = {
207
236
  *
208
237
  * It's fine for this function to throw; if it does, the error is passed to the `onError` function.
209
238
  */
210
- getContext?: GetContext<ContextFrom>;
239
+ getContext?: GetContext<ContextFrom, Input>;
211
240
  /**
212
241
  * A function that is called when an error occurs inside a route handler.
213
242
  *
@@ -256,15 +285,16 @@ type KaitoConfig<ContextFrom> = {
256
285
  * ```
257
286
  */
258
287
  transform?: (req: Request, res: Response) => MaybePromise<Response | void | undefined>;
259
- };
288
+ }
289
+
260
290
  /**
261
- * Create a helper function for instantiating a Kaito router
291
+ * Helper function for instantiating a Kaito router
262
292
  *
263
293
  * This is the starting point for any Kaito application
264
294
  *
265
295
  * @param config - The configuration for the router
266
296
  * @returns A new Kaito router
267
297
  */
268
- declare function create<Context = null>(config?: KaitoConfig<Context>): () => Router<Context, Context, never>;
298
+ declare const create: <Context = null, Input extends readonly unknown[] = []>(config?: KaitoConfig<Context, Input>) => Router<Context, Context, never, never, Input>;
269
299
 
270
- export { type APIResponse, type AnyQuery, type AnyResponse, type AnyRoute, type ErroredAPIResponse, type ExtractRouteParams, type GetContext, type InferRoutes, type JSONOutputSpec, type KaitoConfig, KaitoError, type KaitoMethod, KaitoRequest, type MakeOptional, type MaybePromise, type OutputSpec, type Route, type RouteRunData, Router, type RouterState, type SSEOutputSpec, type SuccessfulAPIResponse, type Through, WrappedError, create, isNodeLikeDev };
300
+ export { type AnyQuery, type AnyRoute, AnySchemaFor, BaseSchema, type ExtractRouteParams, type GetContext, type InferRoutes, type JSONOutputSpec, JSONValue, type KaitoConfig, KaitoError, KaitoHead, type KaitoMethod, KaitoRequest, type MaybePromise, type OpenAPISpec, type OpenAPISpecFor, type OutputSpec, type ResponseOutputSpec, type Route, type RouteRunData, Router, type RouterState, type SSEOutputSpec, type SSEOutputSpecWithSchema, type SSEOutputSpecWithoutSchema, type Through, WrappedError, create, isNodeLikeDev };