@kaito-http/core 4.0.0-beta.31 → 4.0.0-beta.33
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.cjs +17 -16
- package/dist/index.d.cts +30 -19
- package/dist/index.d.ts +30 -19
- package/dist/index.js +17 -16
- package/package.json +1 -1
package/dist/index.cjs
CHANGED
|
@@ -1170,7 +1170,7 @@ var isNodeLikeDev = typeof process !== "undefined" && typeof process.env !== "un
|
|
|
1170
1170
|
// src/router/router.ts
|
|
1171
1171
|
var Router = class _Router {
|
|
1172
1172
|
#state;
|
|
1173
|
-
static create = (config = {}) => new _Router({
|
|
1173
|
+
static create = (config = {}) => new _Router({ pipe: (context) => context, routes: /* @__PURE__ */ new Set(), config });
|
|
1174
1174
|
constructor(state) {
|
|
1175
1175
|
this.#state = state;
|
|
1176
1176
|
}
|
|
@@ -1252,9 +1252,11 @@ var Router = class _Router {
|
|
|
1252
1252
|
try {
|
|
1253
1253
|
const body = route.body ? await route.body.parse(await req.json()) : void 0;
|
|
1254
1254
|
const query = route.fastQuerySchema ? route.fastQuerySchema.parse(url.searchParams) : {};
|
|
1255
|
-
const ctx = await route.router.#state.
|
|
1255
|
+
const ctx = await route.router.#state.pipe(
|
|
1256
1256
|
await this.#state.config.getContext?.(request, head, ...args) ?? null,
|
|
1257
|
-
rawParams
|
|
1257
|
+
rawParams,
|
|
1258
|
+
request,
|
|
1259
|
+
head
|
|
1258
1260
|
);
|
|
1259
1261
|
const result = await route.run({
|
|
1260
1262
|
ctx,
|
|
@@ -1263,8 +1265,7 @@ var Router = class _Router {
|
|
|
1263
1265
|
params: rawParams
|
|
1264
1266
|
});
|
|
1265
1267
|
if (result instanceof KaitoSSEResponse) {
|
|
1266
|
-
const
|
|
1267
|
-
const schema = body2 && "schema" in body2 ? body2.schema : void 0;
|
|
1268
|
+
const schema = route.openapi && "schema" in route.openapi ? route.openapi.schema : void 0;
|
|
1268
1269
|
const stringStream = result.events.pipeThrough(
|
|
1269
1270
|
new TransformStream({
|
|
1270
1271
|
transform(event, controller) {
|
|
@@ -1296,8 +1297,8 @@ var Router = class _Router {
|
|
|
1296
1297
|
}
|
|
1297
1298
|
return result;
|
|
1298
1299
|
}
|
|
1299
|
-
if (route.openapi && "schema" in route.openapi
|
|
1300
|
-
const parsed = route.openapi.
|
|
1300
|
+
if (route.openapi && "schema" in route.openapi && route.openapi.schema) {
|
|
1301
|
+
const parsed = route.openapi.schema.serialize(result);
|
|
1301
1302
|
return head.toResponse(parsed);
|
|
1302
1303
|
}
|
|
1303
1304
|
if (result === void 0) {
|
|
@@ -1417,7 +1418,7 @@ var Router = class _Router {
|
|
|
1417
1418
|
paths[pathWithColonParamsReplaceWithCurlyBraces] = {};
|
|
1418
1419
|
}
|
|
1419
1420
|
let contentType;
|
|
1420
|
-
const type = route.openapi.
|
|
1421
|
+
const type = route.openapi.type;
|
|
1421
1422
|
switch (type) {
|
|
1422
1423
|
case "json":
|
|
1423
1424
|
contentType = "application/json";
|
|
@@ -1431,15 +1432,15 @@ var Router = class _Router {
|
|
|
1431
1432
|
default:
|
|
1432
1433
|
throw new Error(`Unknown output type in route ${route.method} ${route.path}: ${type}`);
|
|
1433
1434
|
}
|
|
1434
|
-
if ("schema" in route.openapi
|
|
1435
|
+
if ("schema" in route.openapi && route.openapi.schema) visit(route.openapi.schema);
|
|
1435
1436
|
if (route.body) visit(route.body);
|
|
1436
|
-
const responseSchema = "schema" in route.openapi
|
|
1437
|
+
const responseSchema = "schema" in route.openapi && route.openapi.schema ? route.openapi.schema.toOpenAPI() : { type: "string" };
|
|
1437
1438
|
const item = {
|
|
1438
1439
|
...route.openapi.summary ? { summary: route.openapi.summary } : {},
|
|
1439
1440
|
description: route.openapi?.description ?? "Successful response",
|
|
1440
1441
|
responses: {
|
|
1441
1442
|
200: {
|
|
1442
|
-
description: route.openapi.
|
|
1443
|
+
description: route.openapi.description ?? "Successful response",
|
|
1443
1444
|
content: {
|
|
1444
1445
|
[contentType]: {
|
|
1445
1446
|
schema: responseSchema
|
|
@@ -1482,15 +1483,15 @@ var Router = class _Router {
|
|
|
1482
1483
|
delete = this.method("DELETE");
|
|
1483
1484
|
head = this.method("HEAD");
|
|
1484
1485
|
options = this.method("OPTIONS");
|
|
1485
|
-
|
|
1486
|
+
pipe = (pipe) => {
|
|
1486
1487
|
return new _Router({
|
|
1487
1488
|
...this.#state,
|
|
1488
|
-
|
|
1489
|
-
const next = this.#state.
|
|
1489
|
+
pipe: (context, params, request, head) => {
|
|
1490
|
+
const next = this.#state.pipe(context, params, request, head);
|
|
1490
1491
|
if (next instanceof Promise) {
|
|
1491
|
-
return next.then((next2) =>
|
|
1492
|
+
return next.then((next2) => pipe(next2, params, request, head));
|
|
1492
1493
|
}
|
|
1493
|
-
return
|
|
1494
|
+
return pipe(next, params, request, head);
|
|
1494
1495
|
}
|
|
1495
1496
|
});
|
|
1496
1497
|
};
|
package/dist/index.d.cts
CHANGED
|
@@ -74,13 +74,13 @@ declare class KaitoHead {
|
|
|
74
74
|
get touched(): boolean;
|
|
75
75
|
}
|
|
76
76
|
|
|
77
|
-
type
|
|
78
|
-
type PrefixRoutesPath<Prefix extends `/${string}`, R extends AnyRoute> = R extends R ? PrefixRoutesPathInner<R, Prefix> : never;
|
|
77
|
+
type PrefixRoutesPath<Prefix extends `/${string}`, R extends AnyRoute> = R extends R ? R extends Route<infer ContextFrom, infer ContextTo, infer RouterInput, infer ResultOutput, infer Path, infer AdditionalParams, infer Method, infer Query, infer BodyInput, infer BodyOutput> ? Route<ContextFrom, ContextTo, RouterInput, ResultOutput, `${Prefix}${Path extends '/' ? '' : Path}`, AdditionalParams, Method, Query, BodyInput, BodyOutput> : never : never;
|
|
79
78
|
type RouterState<ContextFrom, ContextTo, RequiredParams extends string, Routes extends AnyRoute, Input extends readonly unknown[]> = {
|
|
80
79
|
routes: Set<Routes>;
|
|
81
|
-
|
|
80
|
+
pipe: (context: ContextFrom, params: Record<RequiredParams, string>, request: KaitoRequest, head: KaitoHead) => Promise<ContextTo> | ContextTo;
|
|
82
81
|
config: KaitoConfig<ContextFrom, Input>;
|
|
83
82
|
};
|
|
83
|
+
type Params<P extends string = string> = Record<P, string>;
|
|
84
84
|
declare class Router<ContextFrom, ContextTo, RequiredParams extends string, Routes extends AnyRoute, Input extends readonly unknown[]> {
|
|
85
85
|
#private;
|
|
86
86
|
static create: <Context = null, Input_1 extends readonly unknown[] = []>(config?: KaitoConfig<Context, Input_1>) => Router<Context, Context, never, never, Input_1>;
|
|
@@ -144,7 +144,7 @@ declare class Router<ContextFrom, ContextTo, RequiredParams extends string, Rout
|
|
|
144
144
|
readonly options: <Path extends string, ResultOutput = never, Query extends AnyQuery = {}, BodyInput extends JSONValue = never, BodyOutput = never>(path: Path, route: ((data: RouteRunData<RequiredParams | ExtractRouteParams<Path>, ContextTo, Query, BodyOutput>) => ResultOutput | Promise<ResultOutput>) | (Omit<Route<ContextFrom, ContextTo, Input, ResultOutput, Path, RequiredParams, "OPTIONS", Query, BodyInput, BodyOutput>, "path" | "method" | "router" | "openapi"> & {
|
|
145
145
|
openapi?: OpenAPISpecFor<ResultOutput>;
|
|
146
146
|
})) => Router<ContextFrom, ContextTo, RequiredParams, Routes | Route<ContextFrom, ContextTo, Input, ResultOutput, Path, RequiredParams, "OPTIONS", Query, BodyInput, BodyOutput>, Input>;
|
|
147
|
-
|
|
147
|
+
pipe: <NextContext>(pipe: (context: ContextTo, params: Record<RequiredParams, string>, request: KaitoRequest, head: KaitoHead) => MaybePromise<NextContext>) => Router<ContextFrom, NextContext, RequiredParams, Routes, Input>;
|
|
148
148
|
}
|
|
149
149
|
|
|
150
150
|
type RouteRunData<Params extends string, Context, QueryOutput, BodyOutput> = {
|
|
@@ -156,34 +156,45 @@ type RouteRunData<Params extends string, Context, QueryOutput, BodyOutput> = {
|
|
|
156
156
|
type AnyQuery = {
|
|
157
157
|
[key in string]: any;
|
|
158
158
|
};
|
|
159
|
-
|
|
160
|
-
|
|
159
|
+
/**
|
|
160
|
+
* Wraps BaseSchema to prevent the schema from participating in inference for `Output`.
|
|
161
|
+
*
|
|
162
|
+
* BaseSchema's `_output` is covariant (readonly), which means when both `run()` and the schema
|
|
163
|
+
* compete to infer `ResultOutput`, TypeScript widens to the less specific type (e.g. `string`
|
|
164
|
+
* instead of `"us-east-1"`). `NoInfer` ensures `Output` is only inferred from
|
|
165
|
+
* `run()`, and the schema only checks against it via the contravariant `serialize` property.
|
|
166
|
+
*
|
|
167
|
+
* @see https://github.com/microsoft/TypeScript/issues/51756
|
|
168
|
+
*/
|
|
169
|
+
type OutputSchema<Output> = BaseSchema<any, any, any> & {
|
|
170
|
+
serialize: (value: NoInfer<Output>) => JSONValue;
|
|
171
|
+
};
|
|
172
|
+
type SSEOutputSpecWithSchema<Output> = {
|
|
161
173
|
type: 'sse';
|
|
162
|
-
schema:
|
|
174
|
+
schema: OutputSchema<Output>;
|
|
175
|
+
summary?: string | undefined;
|
|
163
176
|
description?: string | undefined;
|
|
164
177
|
};
|
|
165
178
|
type SSEOutputSpecWithoutSchema = {
|
|
166
179
|
type: 'sse';
|
|
167
180
|
schema?: undefined;
|
|
181
|
+
summary?: string | undefined;
|
|
168
182
|
description?: string | undefined;
|
|
169
183
|
};
|
|
170
|
-
type SSEOutputSpec = SSEOutputSpecWithSchema | SSEOutputSpecWithoutSchema;
|
|
171
|
-
type JSONOutputSpec = {
|
|
184
|
+
type SSEOutputSpec<Output> = SSEOutputSpecWithSchema<Output> | SSEOutputSpecWithoutSchema;
|
|
185
|
+
type JSONOutputSpec<Output> = {
|
|
172
186
|
type: 'json';
|
|
173
|
-
schema:
|
|
187
|
+
schema: OutputSchema<Output>;
|
|
188
|
+
summary?: string | undefined;
|
|
174
189
|
description?: string | undefined;
|
|
175
190
|
};
|
|
176
191
|
type ResponseOutputSpec = {
|
|
177
192
|
type: 'response';
|
|
193
|
+
summary?: string | undefined;
|
|
178
194
|
description?: string | undefined;
|
|
179
195
|
};
|
|
180
|
-
type
|
|
181
|
-
type
|
|
182
|
-
summary?: string;
|
|
183
|
-
description?: string;
|
|
184
|
-
body: Body;
|
|
185
|
-
};
|
|
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>;
|
|
196
|
+
type AnyOutputSpec = SSEOutputSpec<any> | JSONOutputSpec<any> | ResponseOutputSpec;
|
|
197
|
+
type OpenAPISpecFor<ResultOutput> = [ResultOutput] extends [never] ? AnyOutputSpec : [ResultOutput] extends [KaitoSSEResponse<infer Event>] ? Event extends SSEEvent<infer U, any> ? [U] extends [JSONValue] ? SSEOutputSpec<Event> : SSEOutputSpecWithSchema<Event> : SSEOutputSpec<any> : [ResultOutput] extends [Response] ? ResponseOutputSpec : JSONOutputSpec<ResultOutput>;
|
|
187
198
|
type Route<ContextFrom, ContextTo, RouterInput extends readonly unknown[], ResultOutput, Path extends string, AdditionalParams extends string, Method extends KaitoMethod, Query extends Record<string, JSONValue>, BodyInput extends JSONValue, BodyOutput> = {
|
|
188
199
|
body?: BaseSchema<BodyInput, BodyOutput, BaseSchemaDef<BodyInput>>;
|
|
189
200
|
query?: {
|
|
@@ -191,7 +202,7 @@ type Route<ContextFrom, ContextTo, RouterInput extends readonly unknown[], Resul
|
|
|
191
202
|
};
|
|
192
203
|
path: Path;
|
|
193
204
|
method: Method;
|
|
194
|
-
openapi?:
|
|
205
|
+
openapi?: AnyOutputSpec;
|
|
195
206
|
router: Router<ContextFrom, ContextTo, AdditionalParams, AnyRoute, RouterInput>;
|
|
196
207
|
run(data: RouteRunData<ExtractRouteParams<Path> | AdditionalParams, ContextTo, Query, BodyOutput>): Promise<ResultOutput> | ResultOutput;
|
|
197
208
|
};
|
|
@@ -297,4 +308,4 @@ interface KaitoConfig<ContextFrom, Input extends readonly unknown[]> {
|
|
|
297
308
|
*/
|
|
298
309
|
declare const create: <Context = null, Input extends readonly unknown[] = []>(config?: KaitoConfig<Context, Input>) => Router<Context, Context, never, never, Input>;
|
|
299
310
|
|
|
300
|
-
export { type AnyQuery, type AnyRoute, AnySchemaFor, BaseSchema, BaseSchemaDef, type ExtractRouteParams, type GetContext, type InferRoutes, type JSONOutputSpec, JSONValue, type KaitoConfig, KaitoError, KaitoHead, type KaitoMethod, KaitoRequest, type MaybePromise, type
|
|
311
|
+
export { type AnyOutputSpec, type AnyQuery, type AnyRoute, AnySchemaFor, BaseSchema, BaseSchemaDef, type ExtractRouteParams, type GetContext, type InferRoutes, type JSONOutputSpec, JSONValue, type KaitoConfig, KaitoError, KaitoHead, type KaitoMethod, KaitoRequest, type MaybePromise, type OpenAPISpecFor, type Params, type ResponseOutputSpec, type Route, type RouteRunData, Router, type RouterState, type SSEOutputSpec, type SSEOutputSpecWithSchema, type SSEOutputSpecWithoutSchema, WrappedError, create, isNodeLikeDev };
|
package/dist/index.d.ts
CHANGED
|
@@ -74,13 +74,13 @@ declare class KaitoHead {
|
|
|
74
74
|
get touched(): boolean;
|
|
75
75
|
}
|
|
76
76
|
|
|
77
|
-
type
|
|
78
|
-
type PrefixRoutesPath<Prefix extends `/${string}`, R extends AnyRoute> = R extends R ? PrefixRoutesPathInner<R, Prefix> : never;
|
|
77
|
+
type PrefixRoutesPath<Prefix extends `/${string}`, R extends AnyRoute> = R extends R ? R extends Route<infer ContextFrom, infer ContextTo, infer RouterInput, infer ResultOutput, infer Path, infer AdditionalParams, infer Method, infer Query, infer BodyInput, infer BodyOutput> ? Route<ContextFrom, ContextTo, RouterInput, ResultOutput, `${Prefix}${Path extends '/' ? '' : Path}`, AdditionalParams, Method, Query, BodyInput, BodyOutput> : never : never;
|
|
79
78
|
type RouterState<ContextFrom, ContextTo, RequiredParams extends string, Routes extends AnyRoute, Input extends readonly unknown[]> = {
|
|
80
79
|
routes: Set<Routes>;
|
|
81
|
-
|
|
80
|
+
pipe: (context: ContextFrom, params: Record<RequiredParams, string>, request: KaitoRequest, head: KaitoHead) => Promise<ContextTo> | ContextTo;
|
|
82
81
|
config: KaitoConfig<ContextFrom, Input>;
|
|
83
82
|
};
|
|
83
|
+
type Params<P extends string = string> = Record<P, string>;
|
|
84
84
|
declare class Router<ContextFrom, ContextTo, RequiredParams extends string, Routes extends AnyRoute, Input extends readonly unknown[]> {
|
|
85
85
|
#private;
|
|
86
86
|
static create: <Context = null, Input_1 extends readonly unknown[] = []>(config?: KaitoConfig<Context, Input_1>) => Router<Context, Context, never, never, Input_1>;
|
|
@@ -144,7 +144,7 @@ declare class Router<ContextFrom, ContextTo, RequiredParams extends string, Rout
|
|
|
144
144
|
readonly options: <Path extends string, ResultOutput = never, Query extends AnyQuery = {}, BodyInput extends JSONValue = never, BodyOutput = never>(path: Path, route: ((data: RouteRunData<RequiredParams | ExtractRouteParams<Path>, ContextTo, Query, BodyOutput>) => ResultOutput | Promise<ResultOutput>) | (Omit<Route<ContextFrom, ContextTo, Input, ResultOutput, Path, RequiredParams, "OPTIONS", Query, BodyInput, BodyOutput>, "path" | "method" | "router" | "openapi"> & {
|
|
145
145
|
openapi?: OpenAPISpecFor<ResultOutput>;
|
|
146
146
|
})) => Router<ContextFrom, ContextTo, RequiredParams, Routes | Route<ContextFrom, ContextTo, Input, ResultOutput, Path, RequiredParams, "OPTIONS", Query, BodyInput, BodyOutput>, Input>;
|
|
147
|
-
|
|
147
|
+
pipe: <NextContext>(pipe: (context: ContextTo, params: Record<RequiredParams, string>, request: KaitoRequest, head: KaitoHead) => MaybePromise<NextContext>) => Router<ContextFrom, NextContext, RequiredParams, Routes, Input>;
|
|
148
148
|
}
|
|
149
149
|
|
|
150
150
|
type RouteRunData<Params extends string, Context, QueryOutput, BodyOutput> = {
|
|
@@ -156,34 +156,45 @@ type RouteRunData<Params extends string, Context, QueryOutput, BodyOutput> = {
|
|
|
156
156
|
type AnyQuery = {
|
|
157
157
|
[key in string]: any;
|
|
158
158
|
};
|
|
159
|
-
|
|
160
|
-
|
|
159
|
+
/**
|
|
160
|
+
* Wraps BaseSchema to prevent the schema from participating in inference for `Output`.
|
|
161
|
+
*
|
|
162
|
+
* BaseSchema's `_output` is covariant (readonly), which means when both `run()` and the schema
|
|
163
|
+
* compete to infer `ResultOutput`, TypeScript widens to the less specific type (e.g. `string`
|
|
164
|
+
* instead of `"us-east-1"`). `NoInfer` ensures `Output` is only inferred from
|
|
165
|
+
* `run()`, and the schema only checks against it via the contravariant `serialize` property.
|
|
166
|
+
*
|
|
167
|
+
* @see https://github.com/microsoft/TypeScript/issues/51756
|
|
168
|
+
*/
|
|
169
|
+
type OutputSchema<Output> = BaseSchema<any, any, any> & {
|
|
170
|
+
serialize: (value: NoInfer<Output>) => JSONValue;
|
|
171
|
+
};
|
|
172
|
+
type SSEOutputSpecWithSchema<Output> = {
|
|
161
173
|
type: 'sse';
|
|
162
|
-
schema:
|
|
174
|
+
schema: OutputSchema<Output>;
|
|
175
|
+
summary?: string | undefined;
|
|
163
176
|
description?: string | undefined;
|
|
164
177
|
};
|
|
165
178
|
type SSEOutputSpecWithoutSchema = {
|
|
166
179
|
type: 'sse';
|
|
167
180
|
schema?: undefined;
|
|
181
|
+
summary?: string | undefined;
|
|
168
182
|
description?: string | undefined;
|
|
169
183
|
};
|
|
170
|
-
type SSEOutputSpec = SSEOutputSpecWithSchema | SSEOutputSpecWithoutSchema;
|
|
171
|
-
type JSONOutputSpec = {
|
|
184
|
+
type SSEOutputSpec<Output> = SSEOutputSpecWithSchema<Output> | SSEOutputSpecWithoutSchema;
|
|
185
|
+
type JSONOutputSpec<Output> = {
|
|
172
186
|
type: 'json';
|
|
173
|
-
schema:
|
|
187
|
+
schema: OutputSchema<Output>;
|
|
188
|
+
summary?: string | undefined;
|
|
174
189
|
description?: string | undefined;
|
|
175
190
|
};
|
|
176
191
|
type ResponseOutputSpec = {
|
|
177
192
|
type: 'response';
|
|
193
|
+
summary?: string | undefined;
|
|
178
194
|
description?: string | undefined;
|
|
179
195
|
};
|
|
180
|
-
type
|
|
181
|
-
type
|
|
182
|
-
summary?: string;
|
|
183
|
-
description?: string;
|
|
184
|
-
body: Body;
|
|
185
|
-
};
|
|
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>;
|
|
196
|
+
type AnyOutputSpec = SSEOutputSpec<any> | JSONOutputSpec<any> | ResponseOutputSpec;
|
|
197
|
+
type OpenAPISpecFor<ResultOutput> = [ResultOutput] extends [never] ? AnyOutputSpec : [ResultOutput] extends [KaitoSSEResponse<infer Event>] ? Event extends SSEEvent<infer U, any> ? [U] extends [JSONValue] ? SSEOutputSpec<Event> : SSEOutputSpecWithSchema<Event> : SSEOutputSpec<any> : [ResultOutput] extends [Response] ? ResponseOutputSpec : JSONOutputSpec<ResultOutput>;
|
|
187
198
|
type Route<ContextFrom, ContextTo, RouterInput extends readonly unknown[], ResultOutput, Path extends string, AdditionalParams extends string, Method extends KaitoMethod, Query extends Record<string, JSONValue>, BodyInput extends JSONValue, BodyOutput> = {
|
|
188
199
|
body?: BaseSchema<BodyInput, BodyOutput, BaseSchemaDef<BodyInput>>;
|
|
189
200
|
query?: {
|
|
@@ -191,7 +202,7 @@ type Route<ContextFrom, ContextTo, RouterInput extends readonly unknown[], Resul
|
|
|
191
202
|
};
|
|
192
203
|
path: Path;
|
|
193
204
|
method: Method;
|
|
194
|
-
openapi?:
|
|
205
|
+
openapi?: AnyOutputSpec;
|
|
195
206
|
router: Router<ContextFrom, ContextTo, AdditionalParams, AnyRoute, RouterInput>;
|
|
196
207
|
run(data: RouteRunData<ExtractRouteParams<Path> | AdditionalParams, ContextTo, Query, BodyOutput>): Promise<ResultOutput> | ResultOutput;
|
|
197
208
|
};
|
|
@@ -297,4 +308,4 @@ interface KaitoConfig<ContextFrom, Input extends readonly unknown[]> {
|
|
|
297
308
|
*/
|
|
298
309
|
declare const create: <Context = null, Input extends readonly unknown[] = []>(config?: KaitoConfig<Context, Input>) => Router<Context, Context, never, never, Input>;
|
|
299
310
|
|
|
300
|
-
export { type AnyQuery, type AnyRoute, AnySchemaFor, BaseSchema, BaseSchemaDef, type ExtractRouteParams, type GetContext, type InferRoutes, type JSONOutputSpec, JSONValue, type KaitoConfig, KaitoError, KaitoHead, type KaitoMethod, KaitoRequest, type MaybePromise, type
|
|
311
|
+
export { type AnyOutputSpec, type AnyQuery, type AnyRoute, AnySchemaFor, BaseSchema, BaseSchemaDef, type ExtractRouteParams, type GetContext, type InferRoutes, type JSONOutputSpec, JSONValue, type KaitoConfig, KaitoError, KaitoHead, type KaitoMethod, KaitoRequest, type MaybePromise, type OpenAPISpecFor, type Params, type ResponseOutputSpec, type Route, type RouteRunData, Router, type RouterState, type SSEOutputSpec, type SSEOutputSpecWithSchema, type SSEOutputSpecWithoutSchema, WrappedError, create, isNodeLikeDev };
|
package/dist/index.js
CHANGED
|
@@ -140,7 +140,7 @@ var isNodeLikeDev = typeof process !== "undefined" && typeof process.env !== "un
|
|
|
140
140
|
// src/router/router.ts
|
|
141
141
|
var Router = class _Router {
|
|
142
142
|
#state;
|
|
143
|
-
static create = (config = {}) => new _Router({
|
|
143
|
+
static create = (config = {}) => new _Router({ pipe: (context) => context, routes: /* @__PURE__ */ new Set(), config });
|
|
144
144
|
constructor(state) {
|
|
145
145
|
this.#state = state;
|
|
146
146
|
}
|
|
@@ -222,9 +222,11 @@ var Router = class _Router {
|
|
|
222
222
|
try {
|
|
223
223
|
const body = route.body ? await route.body.parse(await req.json()) : void 0;
|
|
224
224
|
const query = route.fastQuerySchema ? route.fastQuerySchema.parse(url.searchParams) : {};
|
|
225
|
-
const ctx = await route.router.#state.
|
|
225
|
+
const ctx = await route.router.#state.pipe(
|
|
226
226
|
await this.#state.config.getContext?.(request, head, ...args) ?? null,
|
|
227
|
-
rawParams
|
|
227
|
+
rawParams,
|
|
228
|
+
request,
|
|
229
|
+
head
|
|
228
230
|
);
|
|
229
231
|
const result = await route.run({
|
|
230
232
|
ctx,
|
|
@@ -233,8 +235,7 @@ var Router = class _Router {
|
|
|
233
235
|
params: rawParams
|
|
234
236
|
});
|
|
235
237
|
if (result instanceof KaitoSSEResponse) {
|
|
236
|
-
const
|
|
237
|
-
const schema = body2 && "schema" in body2 ? body2.schema : void 0;
|
|
238
|
+
const schema = route.openapi && "schema" in route.openapi ? route.openapi.schema : void 0;
|
|
238
239
|
const stringStream = result.events.pipeThrough(
|
|
239
240
|
new TransformStream({
|
|
240
241
|
transform(event, controller) {
|
|
@@ -266,8 +267,8 @@ var Router = class _Router {
|
|
|
266
267
|
}
|
|
267
268
|
return result;
|
|
268
269
|
}
|
|
269
|
-
if (route.openapi && "schema" in route.openapi
|
|
270
|
-
const parsed = route.openapi.
|
|
270
|
+
if (route.openapi && "schema" in route.openapi && route.openapi.schema) {
|
|
271
|
+
const parsed = route.openapi.schema.serialize(result);
|
|
271
272
|
return head.toResponse(parsed);
|
|
272
273
|
}
|
|
273
274
|
if (result === void 0) {
|
|
@@ -387,7 +388,7 @@ var Router = class _Router {
|
|
|
387
388
|
paths[pathWithColonParamsReplaceWithCurlyBraces] = {};
|
|
388
389
|
}
|
|
389
390
|
let contentType;
|
|
390
|
-
const type = route.openapi.
|
|
391
|
+
const type = route.openapi.type;
|
|
391
392
|
switch (type) {
|
|
392
393
|
case "json":
|
|
393
394
|
contentType = "application/json";
|
|
@@ -401,15 +402,15 @@ var Router = class _Router {
|
|
|
401
402
|
default:
|
|
402
403
|
throw new Error(`Unknown output type in route ${route.method} ${route.path}: ${type}`);
|
|
403
404
|
}
|
|
404
|
-
if ("schema" in route.openapi
|
|
405
|
+
if ("schema" in route.openapi && route.openapi.schema) visit(route.openapi.schema);
|
|
405
406
|
if (route.body) visit(route.body);
|
|
406
|
-
const responseSchema = "schema" in route.openapi
|
|
407
|
+
const responseSchema = "schema" in route.openapi && route.openapi.schema ? route.openapi.schema.toOpenAPI() : { type: "string" };
|
|
407
408
|
const item = {
|
|
408
409
|
...route.openapi.summary ? { summary: route.openapi.summary } : {},
|
|
409
410
|
description: route.openapi?.description ?? "Successful response",
|
|
410
411
|
responses: {
|
|
411
412
|
200: {
|
|
412
|
-
description: route.openapi.
|
|
413
|
+
description: route.openapi.description ?? "Successful response",
|
|
413
414
|
content: {
|
|
414
415
|
[contentType]: {
|
|
415
416
|
schema: responseSchema
|
|
@@ -452,15 +453,15 @@ var Router = class _Router {
|
|
|
452
453
|
delete = this.method("DELETE");
|
|
453
454
|
head = this.method("HEAD");
|
|
454
455
|
options = this.method("OPTIONS");
|
|
455
|
-
|
|
456
|
+
pipe = (pipe) => {
|
|
456
457
|
return new _Router({
|
|
457
458
|
...this.#state,
|
|
458
|
-
|
|
459
|
-
const next = this.#state.
|
|
459
|
+
pipe: (context, params, request, head) => {
|
|
460
|
+
const next = this.#state.pipe(context, params, request, head);
|
|
460
461
|
if (next instanceof Promise) {
|
|
461
|
-
return next.then((next2) =>
|
|
462
|
+
return next.then((next2) => pipe(next2, params, request, head));
|
|
462
463
|
}
|
|
463
|
-
return
|
|
464
|
+
return pipe(next, params, request, head);
|
|
464
465
|
}
|
|
465
466
|
});
|
|
466
467
|
};
|