@tahminator/sapling 2.0.4 → 2.0.5-beta.18ab8bae
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/README.md +94 -4
- package/dist/index.cjs +364 -188
- package/dist/index.d.cts +464 -91
- package/dist/index.d.mts +464 -91
- package/dist/index.mjs +336 -188
- package/package.json +8 -2
package/dist/index.cjs
CHANGED
|
@@ -23,6 +23,8 @@ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__ge
|
|
|
23
23
|
//#endregion
|
|
24
24
|
let express = require("express");
|
|
25
25
|
express = __toESM(express);
|
|
26
|
+
let swagger_ui_express = require("swagger-ui-express");
|
|
27
|
+
swagger_ui_express = __toESM(swagger_ui_express);
|
|
26
28
|
//#region src/html/404.ts
|
|
27
29
|
/**
|
|
28
30
|
* Default Express.js 404 error page, as a string.
|
|
@@ -256,15 +258,20 @@ var ParserError = class ParserError extends ResponseStatusError {
|
|
|
256
258
|
case "reqbody": return "request body";
|
|
257
259
|
case "reqparams": return "request params";
|
|
258
260
|
case "reqquery": return "request query";
|
|
261
|
+
case "resbody": return "response body";
|
|
259
262
|
}
|
|
260
263
|
})()}: ${formatted}`;
|
|
261
264
|
}
|
|
262
265
|
};
|
|
263
266
|
//#endregion
|
|
264
267
|
//#region src/helper/sapling.ts
|
|
265
|
-
const
|
|
268
|
+
const _settings = {
|
|
266
269
|
serialize: JSON.stringify,
|
|
267
|
-
deserialize: JSON.parse
|
|
270
|
+
deserialize: JSON.parse,
|
|
271
|
+
doc: {
|
|
272
|
+
openApiPath: "/openapi.json",
|
|
273
|
+
swaggerPath: "/swagger.html"
|
|
274
|
+
}
|
|
268
275
|
};
|
|
269
276
|
/**
|
|
270
277
|
* Collection of utility functions which are essential for Sapling to function.
|
|
@@ -343,13 +350,13 @@ var Sapling = class Sapling {
|
|
|
343
350
|
* @defaultValue `JSON.stringify`
|
|
344
351
|
*/
|
|
345
352
|
static serialize(value) {
|
|
346
|
-
return
|
|
353
|
+
return _settings.serialize(value);
|
|
347
354
|
}
|
|
348
355
|
/**
|
|
349
356
|
* Replace the function used for `serialize`.
|
|
350
357
|
*/
|
|
351
358
|
static setSerializeFn(fn) {
|
|
352
|
-
|
|
359
|
+
_settings.serialize = fn;
|
|
353
360
|
}
|
|
354
361
|
/**
|
|
355
362
|
* De-serialize a JSON string back to a JavaScript object.
|
|
@@ -361,15 +368,264 @@ var Sapling = class Sapling {
|
|
|
361
368
|
* @defaultValue `JSON.parse`
|
|
362
369
|
*/
|
|
363
370
|
static deserialize(value) {
|
|
364
|
-
return
|
|
371
|
+
return _settings.deserialize(value);
|
|
365
372
|
}
|
|
366
373
|
/**
|
|
367
374
|
* Replace the function used for `deserialize`
|
|
368
375
|
*/
|
|
369
376
|
static setDeserializeFn(fn) {
|
|
370
|
-
|
|
377
|
+
_settings.deserialize = fn;
|
|
378
|
+
}
|
|
379
|
+
static setOpenApiPath(path) {
|
|
380
|
+
_settings.doc.openApiPath = path;
|
|
381
|
+
}
|
|
382
|
+
static setSwaggerPath(path) {
|
|
383
|
+
_settings.doc.swaggerPath = path;
|
|
384
|
+
}
|
|
385
|
+
static chainHandlers(handlers, request, response, next, index = 0) {
|
|
386
|
+
if (index >= handlers.length) {
|
|
387
|
+
next();
|
|
388
|
+
return;
|
|
389
|
+
}
|
|
390
|
+
handlers[index]?.(request, response, (err) => {
|
|
391
|
+
if (err) {
|
|
392
|
+
next(err);
|
|
393
|
+
return;
|
|
394
|
+
}
|
|
395
|
+
Sapling.chainHandlers(handlers, request, response, next, index + 1);
|
|
396
|
+
});
|
|
397
|
+
}
|
|
398
|
+
};
|
|
399
|
+
//#endregion
|
|
400
|
+
//#region src/annotation/route.ts
|
|
401
|
+
const _routeStore = /* @__PURE__ */ new WeakMap();
|
|
402
|
+
/**
|
|
403
|
+
* Custom annotation that will store all routes inside of a map,
|
|
404
|
+
* which can then be used to initialize all the routes to the router.
|
|
405
|
+
*/
|
|
406
|
+
function _Route({ method, path = "" }) {
|
|
407
|
+
return (target, propertyKey) => {
|
|
408
|
+
const ctor = target.constructor;
|
|
409
|
+
const list = _routeStore.get(ctor) ?? [];
|
|
410
|
+
list.push({
|
|
411
|
+
method,
|
|
412
|
+
path: path ?? "",
|
|
413
|
+
fnName: String(propertyKey)
|
|
414
|
+
});
|
|
415
|
+
_routeStore.set(ctor, list);
|
|
416
|
+
};
|
|
417
|
+
}
|
|
418
|
+
/**
|
|
419
|
+
* Register GET route on the given path (default "") for the given controller.
|
|
420
|
+
*/
|
|
421
|
+
const GET = (path = "") => _Route({
|
|
422
|
+
method: "GET",
|
|
423
|
+
path
|
|
424
|
+
});
|
|
425
|
+
/**
|
|
426
|
+
* Register POST route on the given path (default "") for the given controller.
|
|
427
|
+
*/
|
|
428
|
+
const POST = (path = "") => _Route({
|
|
429
|
+
method: "POST",
|
|
430
|
+
path
|
|
431
|
+
});
|
|
432
|
+
/**
|
|
433
|
+
* Register PUT route on the given path (default "") for the given controller.
|
|
434
|
+
*/
|
|
435
|
+
const PUT = (path = "") => _Route({
|
|
436
|
+
method: "PUT",
|
|
437
|
+
path
|
|
438
|
+
});
|
|
439
|
+
/**
|
|
440
|
+
* Register DELETE route on the given path (default "") for the given controller.
|
|
441
|
+
*/
|
|
442
|
+
const DELETE = (path = "") => _Route({
|
|
443
|
+
method: "DELETE",
|
|
444
|
+
path
|
|
445
|
+
});
|
|
446
|
+
/**
|
|
447
|
+
* Register OPTIONS route on the given path (default "") for the given controller.
|
|
448
|
+
*/
|
|
449
|
+
const OPTIONS = (path = "") => _Route({
|
|
450
|
+
method: "OPTIONS",
|
|
451
|
+
path
|
|
452
|
+
});
|
|
453
|
+
/**
|
|
454
|
+
* Register PATCH route on the given path (default "") for the given controller.
|
|
455
|
+
*/
|
|
456
|
+
const PATCH = (path = "") => _Route({
|
|
457
|
+
method: "PATCH",
|
|
458
|
+
path
|
|
459
|
+
});
|
|
460
|
+
/**
|
|
461
|
+
* Register HEAD route on the given path (default "") for the given controller.
|
|
462
|
+
*/
|
|
463
|
+
const HEAD = (path = "") => _Route({
|
|
464
|
+
method: "HEAD",
|
|
465
|
+
path
|
|
466
|
+
});
|
|
467
|
+
/**
|
|
468
|
+
* Register a middleware route on the given path (default "") for the given controller.
|
|
469
|
+
*/
|
|
470
|
+
const Middleware = (path = "") => _Route({
|
|
471
|
+
method: "USE",
|
|
472
|
+
path
|
|
473
|
+
});
|
|
474
|
+
/**
|
|
475
|
+
* Given a class constructor, fetch all the routes attached.
|
|
476
|
+
*/
|
|
477
|
+
function _getRoutes(ctor) {
|
|
478
|
+
return _routeStore.get(ctor) ?? [];
|
|
479
|
+
}
|
|
480
|
+
//#endregion
|
|
481
|
+
//#region src/utils.ts
|
|
482
|
+
function _getOrCreateMap(store, ctor) {
|
|
483
|
+
const existing = store.get(ctor);
|
|
484
|
+
if (existing) return existing;
|
|
485
|
+
const created = /* @__PURE__ */ new Map();
|
|
486
|
+
store.set(ctor, created);
|
|
487
|
+
return created;
|
|
488
|
+
}
|
|
489
|
+
//#endregion
|
|
490
|
+
//#region src/annotation/schema.ts
|
|
491
|
+
const _routeSchemaStore = /* @__PURE__ */ new WeakMap();
|
|
492
|
+
const _controllerSchemaStore = /* @__PURE__ */ new WeakMap();
|
|
493
|
+
function ControllerSchema(options) {
|
|
494
|
+
return (target) => {
|
|
495
|
+
_setControllerSchema(target, options);
|
|
496
|
+
};
|
|
497
|
+
}
|
|
498
|
+
function RouteSchema(options) {
|
|
499
|
+
return (target, propertyKey) => {
|
|
500
|
+
const ctor = target.constructor;
|
|
501
|
+
_setRouteSchema(ctor, String(propertyKey), options);
|
|
502
|
+
};
|
|
503
|
+
}
|
|
504
|
+
function _setRouteSchema(ctor, fnName, options) {
|
|
505
|
+
_getOrCreateMap(_routeSchemaStore, ctor).set(fnName, options);
|
|
506
|
+
}
|
|
507
|
+
function _setControllerSchema(ctor, options) {
|
|
508
|
+
_controllerSchemaStore.set(ctor, options);
|
|
509
|
+
}
|
|
510
|
+
function _getRouteSchema(ctor, fnName) {
|
|
511
|
+
return _routeSchemaStore.get(ctor)?.get(fnName);
|
|
512
|
+
}
|
|
513
|
+
function _getControllerSchema(ctor) {
|
|
514
|
+
return _controllerSchemaStore.get(ctor);
|
|
515
|
+
}
|
|
516
|
+
//#endregion
|
|
517
|
+
//#region src/helper/openapi.ts
|
|
518
|
+
var OpenAPIGenerator = class {
|
|
519
|
+
constructor() {
|
|
520
|
+
this.controllers = /* @__PURE__ */ new Set();
|
|
521
|
+
this.config = {
|
|
522
|
+
title: "API",
|
|
523
|
+
version: "1.0.0"
|
|
524
|
+
};
|
|
525
|
+
}
|
|
526
|
+
setConfig(config) {
|
|
527
|
+
this.config = config;
|
|
528
|
+
}
|
|
529
|
+
registerController(controllerClass, prefix) {
|
|
530
|
+
this.controllers.add({
|
|
531
|
+
class: controllerClass,
|
|
532
|
+
prefix
|
|
533
|
+
});
|
|
534
|
+
}
|
|
535
|
+
generateSpec() {
|
|
536
|
+
const config = this.config;
|
|
537
|
+
const paths = {};
|
|
538
|
+
const tags = [];
|
|
539
|
+
for (const { class: controllerClass, prefix } of this.controllers) {
|
|
540
|
+
const routes = _getRoutes(controllerClass);
|
|
541
|
+
const controllerSchema = _getControllerSchema(controllerClass);
|
|
542
|
+
if (controllerSchema?.title) tags.push({
|
|
543
|
+
name: controllerSchema.title,
|
|
544
|
+
description: controllerSchema.description
|
|
545
|
+
});
|
|
546
|
+
for (const route of routes) {
|
|
547
|
+
if (route.method === "USE") continue;
|
|
548
|
+
const schemas = _getValidatorSchema(controllerClass, route.fnName);
|
|
549
|
+
const routeSchema = _getRouteSchema(controllerClass, route.fnName);
|
|
550
|
+
if (route.path instanceof RegExp) throw new Error(`You have a route with a regex path of ${route.path.source}. This is not compatible with OpenAPI.`);
|
|
551
|
+
const openApiPath = prefix + route.path;
|
|
552
|
+
if (!paths[openApiPath]) paths[openApiPath] = {};
|
|
553
|
+
const responses = {};
|
|
554
|
+
if (schemas?.responseBody) responses["200"] = {
|
|
555
|
+
description: "Successful response",
|
|
556
|
+
content: { "application/json": { schema: this.toJsonSchema(schemas.responseBody) } }
|
|
557
|
+
};
|
|
558
|
+
else responses["200"] = { description: "Successful response" };
|
|
559
|
+
if (routeSchema?.responses) for (const resp of routeSchema.responses) responses[String(resp.statusCode)] = {
|
|
560
|
+
description: `Response ${resp.statusCode}`,
|
|
561
|
+
content: { "application/json": { schema: this.toJsonSchema(resp.schema) } }
|
|
562
|
+
};
|
|
563
|
+
const operation = {
|
|
564
|
+
responses,
|
|
565
|
+
description: routeSchema?.description,
|
|
566
|
+
tags: controllerSchema?.title ? [controllerSchema.title] : void 0
|
|
567
|
+
};
|
|
568
|
+
const parameters = [];
|
|
569
|
+
if (schemas?.requestParam) {
|
|
570
|
+
const paramSchema = this.toJsonSchema(schemas.requestParam);
|
|
571
|
+
if (paramSchema.type === "object" && paramSchema.properties) for (const [name, schema] of Object.entries(paramSchema.properties)) parameters.push({
|
|
572
|
+
name,
|
|
573
|
+
in: "path",
|
|
574
|
+
required: true,
|
|
575
|
+
schema
|
|
576
|
+
});
|
|
577
|
+
}
|
|
578
|
+
if (schemas?.requestQuery) {
|
|
579
|
+
const querySchema = this.toJsonSchema(schemas.requestQuery);
|
|
580
|
+
if (querySchema.type === "object" && querySchema.properties) for (const [name, schema] of Object.entries(querySchema.properties)) {
|
|
581
|
+
const isRequired = Array.isArray(querySchema.required) && querySchema.required.includes(name);
|
|
582
|
+
parameters.push({
|
|
583
|
+
name,
|
|
584
|
+
in: "query",
|
|
585
|
+
required: isRequired,
|
|
586
|
+
schema
|
|
587
|
+
});
|
|
588
|
+
}
|
|
589
|
+
}
|
|
590
|
+
if (parameters.length > 0) operation.parameters = parameters;
|
|
591
|
+
if (schemas?.requestBody) operation.requestBody = {
|
|
592
|
+
required: true,
|
|
593
|
+
content: { "application/json": { schema: this.toJsonSchema(schemas.requestBody) } }
|
|
594
|
+
};
|
|
595
|
+
const method = route.method.toLowerCase();
|
|
596
|
+
paths[openApiPath][method] = operation;
|
|
597
|
+
}
|
|
598
|
+
}
|
|
599
|
+
return {
|
|
600
|
+
openapi: "3.0.0",
|
|
601
|
+
info: {
|
|
602
|
+
title: config.title,
|
|
603
|
+
version: config.version,
|
|
604
|
+
description: config.description
|
|
605
|
+
},
|
|
606
|
+
tags: tags.length > 0 ? tags : void 0,
|
|
607
|
+
paths
|
|
608
|
+
};
|
|
609
|
+
}
|
|
610
|
+
toJsonSchema(schema) {
|
|
611
|
+
try {
|
|
612
|
+
return schema["~standard"].jsonSchema.output({ target: "openapi-3.0" });
|
|
613
|
+
} catch (e) {
|
|
614
|
+
if (e instanceof Error && e.message.includes("Transforms cannot be represented in JSON Schema")) throw new Error(`${e.message}.\nIt appears that you are using z.transform() - it is highly recommended that you use z.codec instead - https://zod.dev/codecs`);
|
|
615
|
+
throw e;
|
|
616
|
+
}
|
|
371
617
|
}
|
|
372
618
|
};
|
|
619
|
+
const openApiGenerator = new OpenAPIGenerator();
|
|
620
|
+
function _registerControllerClass(controllerClass, prefix) {
|
|
621
|
+
openApiGenerator.registerController(controllerClass, prefix);
|
|
622
|
+
}
|
|
623
|
+
function setOpenApiConfig(config) {
|
|
624
|
+
openApiGenerator.setConfig(config);
|
|
625
|
+
}
|
|
626
|
+
function generateOpenApiSpec() {
|
|
627
|
+
return openApiGenerator.generateSpec();
|
|
628
|
+
}
|
|
373
629
|
//#endregion
|
|
374
630
|
//#region src/types.ts
|
|
375
631
|
const methodResolve = {
|
|
@@ -503,124 +759,44 @@ function _resolve(ctor) {
|
|
|
503
759
|
return _InjectableRegistry.get(ctor);
|
|
504
760
|
}
|
|
505
761
|
//#endregion
|
|
506
|
-
//#region src/annotation/
|
|
507
|
-
const
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
|
|
512
|
-
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
* ```ts
|
|
516
|
-
* const CREATE_BOOK_REQUEST_BODY_SCHEMA = z.object({
|
|
517
|
-
* name: z.string(),
|
|
518
|
-
* description: z.string().optional(),
|
|
519
|
-
* });
|
|
520
|
-
*
|
|
521
|
-
* ⠀@Controller({ prefix: "/api/book" })
|
|
522
|
-
* class BookController {
|
|
523
|
-
* ⠀@RequestBody(CREATE_BOOK_REQUEST_BODY_SCHEMA)
|
|
524
|
-
* ⠀@POST()
|
|
525
|
-
* public createBook(request: e.Request) {
|
|
526
|
-
* const { name, description } = request.body as unknown as z.infer<
|
|
527
|
-
* typeof CREATE_BOOK_REQUEST_BODY_SCHEMA
|
|
528
|
-
* >;
|
|
529
|
-
* }
|
|
530
|
-
* }
|
|
531
|
-
* ```
|
|
532
|
-
*/
|
|
762
|
+
//#region src/annotation/validator.ts
|
|
763
|
+
const _validatorSchemaStore = /* @__PURE__ */ new WeakMap();
|
|
764
|
+
function ResponseBody(schema) {
|
|
765
|
+
return (target, propertyKey) => {
|
|
766
|
+
const ctor = target.constructor;
|
|
767
|
+
const fnName = String(propertyKey);
|
|
768
|
+
_setOnce(_getOrCreateSchemaDefinition(ctor, fnName), "responseBody", schema, fnName);
|
|
769
|
+
};
|
|
770
|
+
}
|
|
533
771
|
function RequestBody(schema) {
|
|
534
772
|
return (target, propertyKey) => {
|
|
535
773
|
const ctor = target.constructor;
|
|
536
774
|
const fnName = String(propertyKey);
|
|
537
|
-
_setOnce(
|
|
775
|
+
_setOnce(_getOrCreateSchemaDefinition(ctor, fnName), "requestBody", schema, fnName);
|
|
538
776
|
};
|
|
539
777
|
}
|
|
540
|
-
/**
|
|
541
|
-
* Apply to a route method to have `request.param` be parsed by `schema`.
|
|
542
|
-
*
|
|
543
|
-
* This annotation will parse `request.param` & then override `request.param`.
|
|
544
|
-
* You can then just simply cast `request.param` for your use
|
|
545
|
-
*
|
|
546
|
-
* @example
|
|
547
|
-
* ```ts
|
|
548
|
-
* const GET_BOOK_REQUEST_PARAM_SCHEMA = z.object({
|
|
549
|
-
* bookId: z.string(),
|
|
550
|
-
* });
|
|
551
|
-
*
|
|
552
|
-
* ⠀@Controller({ prefix: "/api/book" })
|
|
553
|
-
* class BookController {
|
|
554
|
-
* ⠀@RequestParam(GET_BOOK_REQUEST_PARAM_SCHEMA)
|
|
555
|
-
* ⠀@GET("/:bookId")
|
|
556
|
-
* public getBook(request: e.Request) {
|
|
557
|
-
* const { bookId } = request.param as unknown as z.infer<
|
|
558
|
-
* typeof GET_BOOK_REQUEST_PARAM_SCHEMA
|
|
559
|
-
* >;
|
|
560
|
-
* }
|
|
561
|
-
* }
|
|
562
|
-
* ```
|
|
563
|
-
*/
|
|
564
778
|
function RequestParam(schema) {
|
|
565
779
|
return (target, propertyKey) => {
|
|
566
780
|
const ctor = target.constructor;
|
|
567
781
|
const fnName = String(propertyKey);
|
|
568
|
-
_setOnce(
|
|
782
|
+
_setOnce(_getOrCreateSchemaDefinition(ctor, fnName), "requestParam", schema, fnName);
|
|
569
783
|
};
|
|
570
784
|
}
|
|
571
|
-
/**
|
|
572
|
-
* Apply to a route method to have `request.query` be parsed by `schema`.
|
|
573
|
-
*
|
|
574
|
-
* This annotation will parse `request.query` & then override `request.query`.
|
|
575
|
-
* You can then just simply cast `request.query` for your use
|
|
576
|
-
*
|
|
577
|
-
* @example
|
|
578
|
-
* ```ts
|
|
579
|
-
* const LIST_BOOKS_REQUEST_QUERY_SCHEMA = z.object({
|
|
580
|
-
* sort: z.enum(["name", "createdAt"]).optional(),
|
|
581
|
-
* q: z.string().optional(),
|
|
582
|
-
* });
|
|
583
|
-
*
|
|
584
|
-
* ⠀@Controller({ prefix: "/api/book" })
|
|
585
|
-
* class BookController {
|
|
586
|
-
* ⠀@RequestQuery(LIST_BOOKS_REQUEST_QUERY_SCHEMA)
|
|
587
|
-
* ⠀@GET()
|
|
588
|
-
* public listBooks(request: e.Request) {
|
|
589
|
-
* const { sort, q } = request.query as unknown as z.infer<
|
|
590
|
-
* typeof LIST_BOOKS_REQUEST_QUERY_SCHEMA
|
|
591
|
-
* >;
|
|
592
|
-
* }
|
|
593
|
-
* }
|
|
594
|
-
* ```
|
|
595
|
-
*/
|
|
596
785
|
function RequestQuery(schema) {
|
|
597
786
|
return (target, propertyKey) => {
|
|
598
787
|
const ctor = target.constructor;
|
|
599
788
|
const fnName = String(propertyKey);
|
|
600
|
-
_setOnce(
|
|
789
|
+
_setOnce(_getOrCreateSchemaDefinition(ctor, fnName), "requestQuery", schema, fnName);
|
|
601
790
|
};
|
|
602
791
|
}
|
|
603
|
-
function
|
|
604
|
-
const byFn = (
|
|
605
|
-
const fn = _requestSchemaStore.get(ctor);
|
|
606
|
-
if (fn) return fn;
|
|
607
|
-
const newFn = /* @__PURE__ */ new Map();
|
|
608
|
-
_requestSchemaStore.set(ctor, newFn);
|
|
609
|
-
return newFn;
|
|
610
|
-
})();
|
|
792
|
+
function _getOrCreateSchemaDefinition(ctor, fnName) {
|
|
793
|
+
const byFn = _getOrCreateMap(_validatorSchemaStore, ctor);
|
|
611
794
|
const existing = byFn.get(fnName);
|
|
612
795
|
if (existing) return existing;
|
|
613
796
|
const created = {};
|
|
614
797
|
byFn.set(fnName, created);
|
|
615
798
|
return created;
|
|
616
799
|
}
|
|
617
|
-
function _setOnce(def, key, schema, fnName) {
|
|
618
|
-
if (def[key]) throw new Error(`Duplicate request schema for "${String(key)}" on method "${fnName}"`);
|
|
619
|
-
def[key] = schema;
|
|
620
|
-
}
|
|
621
|
-
function _getRequestSchemas(ctor, fnName) {
|
|
622
|
-
return _requestSchemaStore.get(ctor)?.get(fnName);
|
|
623
|
-
}
|
|
624
800
|
async function _parseOrThrow(schema, input, kind) {
|
|
625
801
|
const result = await schema["~standard"].validate(input);
|
|
626
802
|
if (result.issues) {
|
|
@@ -629,86 +805,12 @@ async function _parseOrThrow(schema, input, kind) {
|
|
|
629
805
|
}
|
|
630
806
|
return result.value;
|
|
631
807
|
}
|
|
632
|
-
|
|
633
|
-
|
|
634
|
-
const _routeStore = /* @__PURE__ */ new WeakMap();
|
|
635
|
-
/**
|
|
636
|
-
* Custom annotation that will store all routes inside of a map,
|
|
637
|
-
* which can then be used to initialize all the routes to the router.
|
|
638
|
-
*/
|
|
639
|
-
function _Route({ method, path = "" }) {
|
|
640
|
-
return (target, propertyKey) => {
|
|
641
|
-
const ctor = target.constructor;
|
|
642
|
-
const list = _routeStore.get(ctor) ?? [];
|
|
643
|
-
list.push({
|
|
644
|
-
method,
|
|
645
|
-
path: path ?? "",
|
|
646
|
-
fnName: String(propertyKey)
|
|
647
|
-
});
|
|
648
|
-
_routeStore.set(ctor, list);
|
|
649
|
-
};
|
|
808
|
+
function _getValidatorSchema(ctor, fnName) {
|
|
809
|
+
return _validatorSchemaStore.get(ctor)?.get(fnName);
|
|
650
810
|
}
|
|
651
|
-
|
|
652
|
-
|
|
653
|
-
|
|
654
|
-
const GET = (path = "") => _Route({
|
|
655
|
-
method: "GET",
|
|
656
|
-
path
|
|
657
|
-
});
|
|
658
|
-
/**
|
|
659
|
-
* Register POST route on the given path (default "") for the given controller.
|
|
660
|
-
*/
|
|
661
|
-
const POST = (path = "") => _Route({
|
|
662
|
-
method: "POST",
|
|
663
|
-
path
|
|
664
|
-
});
|
|
665
|
-
/**
|
|
666
|
-
* Register PUT route on the given path (default "") for the given controller.
|
|
667
|
-
*/
|
|
668
|
-
const PUT = (path = "") => _Route({
|
|
669
|
-
method: "PUT",
|
|
670
|
-
path
|
|
671
|
-
});
|
|
672
|
-
/**
|
|
673
|
-
* Register DELETE route on the given path (default "") for the given controller.
|
|
674
|
-
*/
|
|
675
|
-
const DELETE = (path = "") => _Route({
|
|
676
|
-
method: "DELETE",
|
|
677
|
-
path
|
|
678
|
-
});
|
|
679
|
-
/**
|
|
680
|
-
* Register OPTIONS route on the given path (default "") for the given controller.
|
|
681
|
-
*/
|
|
682
|
-
const OPTIONS = (path = "") => _Route({
|
|
683
|
-
method: "OPTIONS",
|
|
684
|
-
path
|
|
685
|
-
});
|
|
686
|
-
/**
|
|
687
|
-
* Register PATCH route on the given path (default "") for the given controller.
|
|
688
|
-
*/
|
|
689
|
-
const PATCH = (path = "") => _Route({
|
|
690
|
-
method: "PATCH",
|
|
691
|
-
path
|
|
692
|
-
});
|
|
693
|
-
/**
|
|
694
|
-
* Register HEAD route on the given path (default "") for the given controller.
|
|
695
|
-
*/
|
|
696
|
-
const HEAD = (path = "") => _Route({
|
|
697
|
-
method: "HEAD",
|
|
698
|
-
path
|
|
699
|
-
});
|
|
700
|
-
/**
|
|
701
|
-
* Register a middleware route on the given path (default "") for the given controller.
|
|
702
|
-
*/
|
|
703
|
-
const Middleware = (path = "") => _Route({
|
|
704
|
-
method: "USE",
|
|
705
|
-
path
|
|
706
|
-
});
|
|
707
|
-
/**
|
|
708
|
-
* Given a class constructor, fetch all the routes attached.
|
|
709
|
-
*/
|
|
710
|
-
function _getRoutes(ctor) {
|
|
711
|
-
return _routeStore.get(ctor) ?? [];
|
|
811
|
+
function _setOnce(def, key, schema, fnName) {
|
|
812
|
+
if (def[key]) throw new Error(`Duplicate schema for "${String(key)}" on method "${fnName}"`);
|
|
813
|
+
def[key] = schema;
|
|
712
814
|
}
|
|
713
815
|
//#endregion
|
|
714
816
|
//#region src/annotation/controller.ts
|
|
@@ -722,6 +824,7 @@ const _ControllerRegistry = /* @__PURE__ */ new WeakMap();
|
|
|
722
824
|
function Controller({ prefix = "", deps = [] } = {}) {
|
|
723
825
|
return (target) => {
|
|
724
826
|
const targetClass = target;
|
|
827
|
+
_registerControllerClass(target, prefix);
|
|
725
828
|
const router = (0, express.Router)();
|
|
726
829
|
const routes = _getRoutes(target);
|
|
727
830
|
const usedRoutes = /* @__PURE__ */ new Set();
|
|
@@ -764,12 +867,12 @@ Split these into separate @MiddlewareClass classes, or merge the logic into a si
|
|
|
764
867
|
return;
|
|
765
868
|
}
|
|
766
869
|
router[methodName](fp, async (request, response, next) => {
|
|
767
|
-
const schemas =
|
|
870
|
+
const schemas = _getValidatorSchema(target, fnName);
|
|
768
871
|
if (schemas) {
|
|
769
|
-
if (schemas.
|
|
770
|
-
if (schemas.
|
|
771
|
-
if (schemas.
|
|
772
|
-
const parsedQuery = await _parseOrThrow(schemas.
|
|
872
|
+
if (schemas.requestBody) request.body = await _parseOrThrow(schemas.requestBody, request.body, "reqbody");
|
|
873
|
+
if (schemas.requestParam) request.params = await _parseOrThrow(schemas.requestParam, request.params, "reqparams");
|
|
874
|
+
if (schemas.requestQuery) {
|
|
875
|
+
const parsedQuery = await _parseOrThrow(schemas.requestQuery, request.query, "reqquery");
|
|
773
876
|
Object.defineProperty(request, "query", {
|
|
774
877
|
value: parsedQuery,
|
|
775
878
|
writable: true,
|
|
@@ -779,7 +882,8 @@ Split these into separate @MiddlewareClass classes, or merge the logic into a si
|
|
|
779
882
|
}
|
|
780
883
|
const result = await fn.bind(controllerInstance)(request, response, next);
|
|
781
884
|
if (result instanceof ResponseEntity) {
|
|
782
|
-
|
|
885
|
+
const body = schemas && schemas.responseBody ? await _parseOrThrow(schemas.responseBody, result.getBody(), "resbody") : result.getBody();
|
|
886
|
+
response.contentType("application/json").status(result.getStatusCode()).set(result.getHeaders()).send(Sapling.serialize(body));
|
|
783
887
|
return;
|
|
784
888
|
}
|
|
785
889
|
if (result instanceof RedirectView) {
|
|
@@ -813,7 +917,7 @@ function __decorate(decorators, target, key, desc) {
|
|
|
813
917
|
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
814
918
|
}
|
|
815
919
|
//#endregion
|
|
816
|
-
//#region src/middleware/default/base.ts
|
|
920
|
+
//#region src/middleware/default/error/base.ts
|
|
817
921
|
let DefaultBaseErrorMiddleware = class DefaultBaseErrorMiddleware {
|
|
818
922
|
handle(err, _request, _response, _next) {
|
|
819
923
|
console.error("[Error]", err);
|
|
@@ -823,7 +927,17 @@ let DefaultBaseErrorMiddleware = class DefaultBaseErrorMiddleware {
|
|
|
823
927
|
__decorate([Middleware()], DefaultBaseErrorMiddleware.prototype, "handle", null);
|
|
824
928
|
DefaultBaseErrorMiddleware = __decorate([MiddlewareClass()], DefaultBaseErrorMiddleware);
|
|
825
929
|
//#endregion
|
|
826
|
-
//#region src/middleware/default/
|
|
930
|
+
//#region src/middleware/default/error/parse.ts
|
|
931
|
+
let DefaultParserErrorMiddleware = class DefaultParserErrorMiddleware {
|
|
932
|
+
handle(err, _request, _response, next) {
|
|
933
|
+
if (err instanceof ParserError) return ResponseEntity.status(err.status).body({ message: err.message });
|
|
934
|
+
next(err);
|
|
935
|
+
}
|
|
936
|
+
};
|
|
937
|
+
__decorate([Middleware()], DefaultParserErrorMiddleware.prototype, "handle", null);
|
|
938
|
+
DefaultParserErrorMiddleware = __decorate([MiddlewareClass()], DefaultParserErrorMiddleware);
|
|
939
|
+
//#endregion
|
|
940
|
+
//#region src/middleware/default/error/responsestatus.ts
|
|
827
941
|
let DefaultResponseStatusErrorMiddleware = class DefaultResponseStatusErrorMiddleware {
|
|
828
942
|
handle(err, _request, _response, next) {
|
|
829
943
|
if (err instanceof ResponseStatusError) return ResponseEntity.status(err.status).body({ message: err.message });
|
|
@@ -833,7 +947,43 @@ let DefaultResponseStatusErrorMiddleware = class DefaultResponseStatusErrorMiddl
|
|
|
833
947
|
__decorate([Middleware()], DefaultResponseStatusErrorMiddleware.prototype, "handle", null);
|
|
834
948
|
DefaultResponseStatusErrorMiddleware = __decorate([MiddlewareClass()], DefaultResponseStatusErrorMiddleware);
|
|
835
949
|
//#endregion
|
|
950
|
+
//#region src/middleware/default/openapi/index.ts
|
|
951
|
+
let DefaultOpenApiMiddleware = class DefaultOpenApiMiddleware {
|
|
952
|
+
handle(_request, _response, _next) {
|
|
953
|
+
return ResponseEntity.ok().body(generateOpenApiSpec());
|
|
954
|
+
}
|
|
955
|
+
};
|
|
956
|
+
__decorate([GET(_settings.doc.openApiPath)], DefaultOpenApiMiddleware.prototype, "handle", null);
|
|
957
|
+
DefaultOpenApiMiddleware = __decorate([MiddlewareClass()], DefaultOpenApiMiddleware);
|
|
958
|
+
//#endregion
|
|
959
|
+
//#region src/middleware/default/swagger/index.ts
|
|
960
|
+
let Serve = class Serve {
|
|
961
|
+
constructor() {
|
|
962
|
+
this.handlers = swagger_ui_express.default.serve;
|
|
963
|
+
}
|
|
964
|
+
handle(request, response, next) {
|
|
965
|
+
return Sapling.chainHandlers(this.handlers, request, response, next);
|
|
966
|
+
}
|
|
967
|
+
};
|
|
968
|
+
__decorate([Middleware(_settings.doc.swaggerPath)], Serve.prototype, "handle", null);
|
|
969
|
+
Serve = __decorate([MiddlewareClass()], Serve);
|
|
970
|
+
let Setup = class Setup {
|
|
971
|
+
constructor() {
|
|
972
|
+
this.handler = swagger_ui_express.default.setup(null, { swaggerOptions: { url: _settings.doc.openApiPath } });
|
|
973
|
+
}
|
|
974
|
+
handle(request, response, next) {
|
|
975
|
+
return this.handler(request, response, next);
|
|
976
|
+
}
|
|
977
|
+
};
|
|
978
|
+
__decorate([Middleware(_settings.doc.swaggerPath)], Setup.prototype, "handle", null);
|
|
979
|
+
Setup = __decorate([MiddlewareClass()], Setup);
|
|
980
|
+
const DefaultSwaggerMiddleware = {
|
|
981
|
+
Serve,
|
|
982
|
+
Setup
|
|
983
|
+
};
|
|
984
|
+
//#endregion
|
|
836
985
|
exports.Controller = Controller;
|
|
986
|
+
exports.ControllerSchema = ControllerSchema;
|
|
837
987
|
exports.DELETE = DELETE;
|
|
838
988
|
Object.defineProperty(exports, "DefaultBaseErrorMiddleware", {
|
|
839
989
|
enumerable: true,
|
|
@@ -841,12 +991,25 @@ Object.defineProperty(exports, "DefaultBaseErrorMiddleware", {
|
|
|
841
991
|
return DefaultBaseErrorMiddleware;
|
|
842
992
|
}
|
|
843
993
|
});
|
|
994
|
+
Object.defineProperty(exports, "DefaultOpenApiMiddleware", {
|
|
995
|
+
enumerable: true,
|
|
996
|
+
get: function() {
|
|
997
|
+
return DefaultOpenApiMiddleware;
|
|
998
|
+
}
|
|
999
|
+
});
|
|
1000
|
+
Object.defineProperty(exports, "DefaultParserErrorMiddleware", {
|
|
1001
|
+
enumerable: true,
|
|
1002
|
+
get: function() {
|
|
1003
|
+
return DefaultParserErrorMiddleware;
|
|
1004
|
+
}
|
|
1005
|
+
});
|
|
844
1006
|
Object.defineProperty(exports, "DefaultResponseStatusErrorMiddleware", {
|
|
845
1007
|
enumerable: true,
|
|
846
1008
|
get: function() {
|
|
847
1009
|
return DefaultResponseStatusErrorMiddleware;
|
|
848
1010
|
}
|
|
849
1011
|
});
|
|
1012
|
+
exports.DefaultSwaggerMiddleware = DefaultSwaggerMiddleware;
|
|
850
1013
|
exports.GET = GET;
|
|
851
1014
|
exports.HEAD = HEAD;
|
|
852
1015
|
exports.Html404ErrorPage = Html404ErrorPage;
|
|
@@ -863,16 +1026,29 @@ exports.RedirectView = RedirectView;
|
|
|
863
1026
|
exports.RequestBody = RequestBody;
|
|
864
1027
|
exports.RequestParam = RequestParam;
|
|
865
1028
|
exports.RequestQuery = RequestQuery;
|
|
1029
|
+
exports.ResponseBody = ResponseBody;
|
|
866
1030
|
exports.ResponseEntity = ResponseEntity;
|
|
867
1031
|
exports.ResponseEntityBuilder = ResponseEntityBuilder;
|
|
868
1032
|
exports.ResponseStatusError = ResponseStatusError;
|
|
1033
|
+
exports.RouteSchema = RouteSchema;
|
|
869
1034
|
exports.Sapling = Sapling;
|
|
870
1035
|
exports._ControllerRegistry = _ControllerRegistry;
|
|
871
1036
|
exports._InjectableDeps = _InjectableDeps;
|
|
872
1037
|
exports._InjectableRegistry = _InjectableRegistry;
|
|
873
1038
|
exports._Route = _Route;
|
|
874
|
-
exports.
|
|
1039
|
+
exports._getControllerSchema = _getControllerSchema;
|
|
1040
|
+
exports._getOrCreateSchemaDefinition = _getOrCreateSchemaDefinition;
|
|
1041
|
+
exports._getRouteSchema = _getRouteSchema;
|
|
875
1042
|
exports._getRoutes = _getRoutes;
|
|
1043
|
+
exports._getValidatorSchema = _getValidatorSchema;
|
|
876
1044
|
exports._parseOrThrow = _parseOrThrow;
|
|
1045
|
+
exports._registerControllerClass = _registerControllerClass;
|
|
877
1046
|
exports._resolve = _resolve;
|
|
1047
|
+
exports._setControllerSchema = _setControllerSchema;
|
|
1048
|
+
exports._setOnce = _setOnce;
|
|
1049
|
+
exports._setRouteSchema = _setRouteSchema;
|
|
1050
|
+
exports._settings = _settings;
|
|
1051
|
+
exports.generateOpenApiSpec = generateOpenApiSpec;
|
|
878
1052
|
exports.methodResolve = methodResolve;
|
|
1053
|
+
exports.openApiGenerator = openApiGenerator;
|
|
1054
|
+
exports.setOpenApiConfig = setOpenApiConfig;
|