@kaito-http/core 4.0.0-beta.11 → 4.0.0-beta.14
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-THVWVAMX.js +797 -0
- package/dist/index.cjs +874 -90
- package/dist/index.d.cts +90 -109
- package/dist/index.d.ts +90 -109
- package/dist/index.js +98 -91
- package/dist/schema/schema.cjs +837 -0
- package/dist/schema/schema.d.cts +326 -0
- package/dist/schema/schema.d.ts +326 -0
- package/dist/schema/schema.js +38 -0
- package/package.json +6 -9
package/dist/index.js
CHANGED
|
@@ -1,9 +1,25 @@
|
|
|
1
|
-
// src/router/router.ts
|
|
2
|
-
import { z } from "zod";
|
|
3
1
|
import {
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
2
|
+
BaseSchema,
|
|
3
|
+
KArray,
|
|
4
|
+
KBoolean,
|
|
5
|
+
KLiteral,
|
|
6
|
+
KNull,
|
|
7
|
+
KNumber,
|
|
8
|
+
KObject,
|
|
9
|
+
KObjectFromURLSearchParams,
|
|
10
|
+
KRef,
|
|
11
|
+
KScalar,
|
|
12
|
+
KString,
|
|
13
|
+
KUnion,
|
|
14
|
+
ParseContext,
|
|
15
|
+
STRING_FORMAT_REGEXES,
|
|
16
|
+
SchemaError,
|
|
17
|
+
isPrimitiveJSONValue,
|
|
18
|
+
k
|
|
19
|
+
} from "./chunk-THVWVAMX.js";
|
|
20
|
+
|
|
21
|
+
// src/router/router.ts
|
|
22
|
+
import "openapi3-ts/oas31";
|
|
7
23
|
|
|
8
24
|
// src/error.ts
|
|
9
25
|
var WrappedError = class _WrappedError extends Error {
|
|
@@ -116,20 +132,19 @@ var isNodeLikeDev = typeof process !== "undefined" && typeof process.env !== "un
|
|
|
116
132
|
|
|
117
133
|
// src/router/router.ts
|
|
118
134
|
var Router = class _Router {
|
|
119
|
-
state;
|
|
120
|
-
static create = (config) => {
|
|
135
|
+
#state;
|
|
136
|
+
static create = (config = {}) => {
|
|
121
137
|
return new _Router({
|
|
122
|
-
through:
|
|
138
|
+
through: (context) => context,
|
|
123
139
|
routes: /* @__PURE__ */ new Set(),
|
|
124
|
-
config
|
|
125
|
-
paramsSchema: null
|
|
140
|
+
config
|
|
126
141
|
});
|
|
127
142
|
};
|
|
128
143
|
constructor(state) {
|
|
129
|
-
this
|
|
144
|
+
this.#state = state;
|
|
130
145
|
}
|
|
131
146
|
get routes() {
|
|
132
|
-
return this
|
|
147
|
+
return this.#state.routes;
|
|
133
148
|
}
|
|
134
149
|
add = (method, path, route) => {
|
|
135
150
|
const merged = {
|
|
@@ -139,16 +154,13 @@ var Router = class _Router {
|
|
|
139
154
|
router: this
|
|
140
155
|
};
|
|
141
156
|
return new _Router({
|
|
142
|
-
...this
|
|
143
|
-
routes: /* @__PURE__ */ new Set([...this
|
|
157
|
+
...this.#state,
|
|
158
|
+
routes: /* @__PURE__ */ new Set([...this.#state.routes, merged])
|
|
144
159
|
});
|
|
145
160
|
};
|
|
146
|
-
params = (
|
|
147
|
-
...this.state,
|
|
148
|
-
paramsSchema: z.object(spec)
|
|
149
|
-
});
|
|
161
|
+
params = () => this;
|
|
150
162
|
merge = (pathPrefix, other) => {
|
|
151
|
-
const newRoutes = [...other
|
|
163
|
+
const newRoutes = [...other.#state.routes].map((route) => ({
|
|
152
164
|
...route,
|
|
153
165
|
// handle pathPrefix = / & route.path = / case causing //
|
|
154
166
|
// we intentionally are replacing on the joining path and not the pathPrefix, in case of
|
|
@@ -156,8 +168,8 @@ var Router = class _Router {
|
|
|
156
168
|
path: `${pathPrefix}${route.path === "/" ? "" : route.path}`
|
|
157
169
|
}));
|
|
158
170
|
return new _Router({
|
|
159
|
-
...this
|
|
160
|
-
routes: /* @__PURE__ */ new Set([...this
|
|
171
|
+
...this.#state,
|
|
172
|
+
routes: /* @__PURE__ */ new Set([...this.#state.routes, ...newRoutes])
|
|
161
173
|
});
|
|
162
174
|
};
|
|
163
175
|
static getFindRoute = (routes) => (method, path) => {
|
|
@@ -185,25 +197,15 @@ var Router = class _Router {
|
|
|
185
197
|
}
|
|
186
198
|
return {};
|
|
187
199
|
};
|
|
188
|
-
static buildQuerySchema = (schema) => {
|
|
189
|
-
const keys = Object.keys(schema);
|
|
190
|
-
return z.instanceof(URLSearchParams).transform((params) => {
|
|
191
|
-
const result = {};
|
|
192
|
-
for (const key of keys) {
|
|
193
|
-
result[key] = params.get(key);
|
|
194
|
-
}
|
|
195
|
-
return result;
|
|
196
|
-
}).pipe(z.object(schema));
|
|
197
|
-
};
|
|
198
200
|
serve = () => {
|
|
199
201
|
const methodToRoutesMap = /* @__PURE__ */ new Map();
|
|
200
|
-
for (const route of this
|
|
202
|
+
for (const route of this.#state.routes) {
|
|
201
203
|
if (!methodToRoutesMap.has(route.method)) {
|
|
202
204
|
methodToRoutesMap.set(route.method, /* @__PURE__ */ new Map());
|
|
203
205
|
}
|
|
204
206
|
methodToRoutesMap.get(route.method).set(route.path, {
|
|
205
207
|
...route,
|
|
206
|
-
fastQuerySchema: route.query ?
|
|
208
|
+
fastQuerySchema: route.query ? k.objectFromURLSearchParams(route.query) : void 0
|
|
207
209
|
});
|
|
208
210
|
}
|
|
209
211
|
const findRoute = _Router.getFindRoute(methodToRoutesMap);
|
|
@@ -222,18 +224,17 @@ var Router = class _Router {
|
|
|
222
224
|
const request = new KaitoRequest(url, req);
|
|
223
225
|
const head = new KaitoHead();
|
|
224
226
|
try {
|
|
225
|
-
const body = route.body ? await route.body.
|
|
226
|
-
const query = route.fastQuerySchema ?
|
|
227
|
-
const
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
params
|
|
227
|
+
const body = route.body ? await route.body.parse(await req.json()) : void 0;
|
|
228
|
+
const query = route.fastQuerySchema ? route.fastQuerySchema.parse(url.searchParams) : {};
|
|
229
|
+
const ctx = await route.router.#state.through(
|
|
230
|
+
await this.#state.config.getContext?.(request, head, ...args) ?? null,
|
|
231
|
+
rawParams
|
|
231
232
|
);
|
|
232
233
|
const result = await route.run({
|
|
233
234
|
ctx,
|
|
234
235
|
body,
|
|
235
236
|
query,
|
|
236
|
-
params
|
|
237
|
+
params: rawParams
|
|
237
238
|
});
|
|
238
239
|
if (result instanceof Response) {
|
|
239
240
|
if (isNodeLikeDev) {
|
|
@@ -262,7 +263,7 @@ var Router = class _Router {
|
|
|
262
263
|
message: error.message
|
|
263
264
|
});
|
|
264
265
|
}
|
|
265
|
-
if (!this
|
|
266
|
+
if (!this.#state.config.onError) {
|
|
266
267
|
return head.status(500).toResponse({
|
|
267
268
|
success: false,
|
|
268
269
|
data: null,
|
|
@@ -270,14 +271,14 @@ var Router = class _Router {
|
|
|
270
271
|
});
|
|
271
272
|
}
|
|
272
273
|
try {
|
|
273
|
-
const { status, message } = await this
|
|
274
|
+
const { status, message } = await this.#state.config.onError(error, request);
|
|
274
275
|
return head.status(status).toResponse({
|
|
275
276
|
success: false,
|
|
276
277
|
data: null,
|
|
277
278
|
message
|
|
278
279
|
});
|
|
279
280
|
} catch (e2) {
|
|
280
|
-
console.error("
|
|
281
|
+
console.error("[Kaito] Failed to handle error inside `.onError()`, returning 500 and Internal Server Error");
|
|
281
282
|
console.error(e2);
|
|
282
283
|
return head.status(500).toResponse({
|
|
283
284
|
success: false,
|
|
@@ -288,11 +289,11 @@ var Router = class _Router {
|
|
|
288
289
|
}
|
|
289
290
|
};
|
|
290
291
|
return async (request, ...args) => {
|
|
291
|
-
if (this
|
|
292
|
-
const result = await this
|
|
292
|
+
if (this.#state.config.before) {
|
|
293
|
+
const result = await this.#state.config.before(request);
|
|
293
294
|
if (result instanceof Response) {
|
|
294
|
-
if (this
|
|
295
|
-
const transformed = await this
|
|
295
|
+
if (this.#state.config.transform) {
|
|
296
|
+
const transformed = await this.#state.config.transform(request, result);
|
|
296
297
|
if (transformed instanceof Response) {
|
|
297
298
|
return result;
|
|
298
299
|
}
|
|
@@ -301,8 +302,8 @@ var Router = class _Router {
|
|
|
301
302
|
}
|
|
302
303
|
}
|
|
303
304
|
const response = await handle(request, ...args);
|
|
304
|
-
if (this
|
|
305
|
-
const transformed = await this
|
|
305
|
+
if (this.#state.config.transform) {
|
|
306
|
+
const transformed = await this.#state.config.transform(request, response);
|
|
306
307
|
if (transformed instanceof Response) {
|
|
307
308
|
return transformed;
|
|
308
309
|
}
|
|
@@ -310,10 +311,13 @@ var Router = class _Router {
|
|
|
310
311
|
return response;
|
|
311
312
|
};
|
|
312
313
|
};
|
|
313
|
-
openapi = (
|
|
314
|
-
|
|
314
|
+
openapi = ({
|
|
315
|
+
info,
|
|
316
|
+
servers
|
|
317
|
+
}) => {
|
|
318
|
+
const OPENAPI_VERSION = "3.1.0";
|
|
315
319
|
const paths = {};
|
|
316
|
-
for (const route of this
|
|
320
|
+
for (const route of this.#state.routes) {
|
|
317
321
|
const path = route.path;
|
|
318
322
|
if (!route.openapi) {
|
|
319
323
|
continue;
|
|
@@ -324,18 +328,18 @@ var Router = class _Router {
|
|
|
324
328
|
}
|
|
325
329
|
const content = route.openapi.body.type === "json" ? {
|
|
326
330
|
"application/json": {
|
|
327
|
-
schema: z.object({
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
331
|
+
// schema: z.object({
|
|
332
|
+
// success: z.literal(true).openapi({
|
|
333
|
+
// type: 'boolean',
|
|
334
|
+
// enum: [true], // Need this as zod-openapi doesn't properly work with literals
|
|
335
|
+
// }),
|
|
336
|
+
// data: route.openapi.body.schema,
|
|
337
|
+
// }),
|
|
338
|
+
schema: route.openapi.body.schema.toOpenAPI()
|
|
335
339
|
}
|
|
336
340
|
} : {
|
|
337
341
|
"text/event-stream": {
|
|
338
|
-
schema: route.openapi.body.schema
|
|
342
|
+
schema: route.openapi.body.schema.toOpenAPI()
|
|
339
343
|
}
|
|
340
344
|
};
|
|
341
345
|
const item = {
|
|
@@ -350,37 +354,24 @@ var Router = class _Router {
|
|
|
350
354
|
if (route.body) {
|
|
351
355
|
item.requestBody = {
|
|
352
356
|
content: {
|
|
353
|
-
"application/json": { schema: route.body }
|
|
357
|
+
"application/json": { schema: route.body.toOpenAPI() }
|
|
354
358
|
}
|
|
355
359
|
};
|
|
356
360
|
}
|
|
357
|
-
const params = {};
|
|
358
|
-
if (route.query) {
|
|
359
|
-
params.query = z.object(route.query);
|
|
360
|
-
}
|
|
361
|
-
const urlParams = path.match(/:(\w+)/g);
|
|
362
|
-
if (urlParams) {
|
|
363
|
-
const pathParams = {};
|
|
364
|
-
for (const param of urlParams) {
|
|
365
|
-
pathParams[param.slice(1)] = z.string();
|
|
366
|
-
}
|
|
367
|
-
params.path = z.object(pathParams);
|
|
368
|
-
}
|
|
369
|
-
item.requestParams = params;
|
|
370
361
|
paths[pathWithColonParamsReplaceWithCurlyBraces][route.method.toLowerCase()] = item;
|
|
371
362
|
}
|
|
372
|
-
const doc =
|
|
363
|
+
const doc = {
|
|
373
364
|
openapi: OPENAPI_VERSION,
|
|
365
|
+
info,
|
|
374
366
|
paths,
|
|
375
|
-
|
|
376
|
-
servers: Object.entries(highLevelSpec.servers ?? {}).map((entry) => {
|
|
367
|
+
servers: Object.entries(servers ?? {}).map((entry) => {
|
|
377
368
|
const [url, description] = entry;
|
|
378
369
|
return {
|
|
379
370
|
url,
|
|
380
371
|
description
|
|
381
372
|
};
|
|
382
373
|
})
|
|
383
|
-
}
|
|
374
|
+
};
|
|
384
375
|
return this.get("/openapi.json", () => Response.json(doc));
|
|
385
376
|
};
|
|
386
377
|
method = (method) => {
|
|
@@ -395,27 +386,43 @@ var Router = class _Router {
|
|
|
395
386
|
options = this.method("OPTIONS");
|
|
396
387
|
through = (through) => {
|
|
397
388
|
return new _Router({
|
|
398
|
-
...this
|
|
399
|
-
through:
|
|
389
|
+
...this.#state,
|
|
390
|
+
through: (context, params) => {
|
|
391
|
+
const next = this.#state.through(context, params);
|
|
392
|
+
if (next instanceof Promise) {
|
|
393
|
+
return next.then((next2) => through(next2, params));
|
|
394
|
+
}
|
|
395
|
+
return through(next, params);
|
|
396
|
+
}
|
|
400
397
|
});
|
|
401
398
|
};
|
|
402
399
|
};
|
|
403
400
|
|
|
404
|
-
// src/
|
|
405
|
-
|
|
406
|
-
return Router.create(config);
|
|
407
|
-
}
|
|
408
|
-
create.withInput = () => {
|
|
409
|
-
return {
|
|
410
|
-
create: (config = {}) => Router.create(config)
|
|
411
|
-
};
|
|
412
|
-
};
|
|
401
|
+
// src/index.ts
|
|
402
|
+
var create = Router.create;
|
|
413
403
|
export {
|
|
404
|
+
BaseSchema,
|
|
405
|
+
KArray,
|
|
406
|
+
KBoolean,
|
|
407
|
+
KLiteral,
|
|
408
|
+
KNull,
|
|
409
|
+
KNumber,
|
|
410
|
+
KObject,
|
|
411
|
+
KObjectFromURLSearchParams,
|
|
412
|
+
KRef,
|
|
413
|
+
KScalar,
|
|
414
|
+
KString,
|
|
415
|
+
KUnion,
|
|
414
416
|
KaitoError,
|
|
415
417
|
KaitoHead,
|
|
416
418
|
KaitoRequest,
|
|
419
|
+
ParseContext,
|
|
417
420
|
Router,
|
|
421
|
+
STRING_FORMAT_REGEXES,
|
|
422
|
+
SchemaError,
|
|
418
423
|
WrappedError,
|
|
419
424
|
create,
|
|
420
|
-
isNodeLikeDev
|
|
425
|
+
isNodeLikeDev,
|
|
426
|
+
isPrimitiveJSONValue,
|
|
427
|
+
k
|
|
421
428
|
};
|