@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/chunk-DRJX3OJG.js +1003 -0
- package/dist/chunk-HJ5HIYGW.js +92 -0
- package/dist/cors/cors.cjs +85 -17
- package/dist/cors/cors.d.cts +122 -26
- package/dist/cors/cors.d.ts +122 -26
- package/dist/cors/cors.js +85 -17
- package/dist/index.cjs +1218 -106
- package/dist/index.d.cts +137 -107
- package/dist/index.d.ts +137 -107
- package/dist/index.js +218 -105
- package/dist/schema/schema.cjs +1046 -0
- package/dist/schema/schema.d.cts +391 -0
- package/dist/schema/schema.d.ts +391 -0
- package/dist/schema/schema.js +44 -0
- package/dist/stream/stream.cjs +11 -23
- package/dist/stream/stream.d.cts +14 -11
- package/dist/stream/stream.d.ts +14 -11
- package/dist/stream/stream.js +7 -96
- package/package.json +6 -9
package/dist/index.d.cts
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
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>(
|
|
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
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
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
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
[
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
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<
|
|
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
|
-
|
|
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
|
|
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:
|
|
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
|
|
170
|
+
type SSEOutputSpec = SSEOutputSpecWithSchema | SSEOutputSpecWithoutSchema;
|
|
171
|
+
type JSONOutputSpec = {
|
|
128
172
|
type: 'json';
|
|
129
|
-
schema:
|
|
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
|
|
180
|
+
type OutputSpec = SSEOutputSpec | JSONOutputSpec | ResponseOutputSpec;
|
|
181
|
+
type OpenAPISpec<Body extends OutputSpec = OutputSpec> = {
|
|
182
|
+
summary?: string;
|
|
133
183
|
description?: string;
|
|
134
|
-
body:
|
|
184
|
+
body: Body;
|
|
135
185
|
};
|
|
136
|
-
type
|
|
137
|
-
|
|
138
|
-
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]:
|
|
190
|
+
[Key in keyof Query]: AnySchemaFor<Query[Key]>;
|
|
141
191
|
};
|
|
142
192
|
path: Path;
|
|
143
193
|
method: Method;
|
|
144
|
-
openapi?:
|
|
145
|
-
|
|
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
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
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
|
|
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<
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
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
|
-
|
|
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
|
-
*
|
|
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
|
|
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
|
|
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 };
|