@tahminator/sapling 2.0.5-beta.0b3bd061 → 2.0.5-beta.1843d6fa

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.mts CHANGED
@@ -439,99 +439,12 @@ declare class ResponseStatusError extends Error {
439
439
  //#region src/helper/error/parse.d.ts
440
440
  type ParserErrorLocation = "reqbody" | "reqparams" | "reqquery" | "resbody";
441
441
  /**
442
- * This error should be thrown when some data cannot be parsed by a given schema.
442
+ * This error should be thrown when some data cannot be parsed by a given Standard Schema compatible schema.
443
443
  */
444
444
  declare class ParserError extends ResponseStatusError {
445
- constructor(location: ParserErrorLocation, issues: readonly StandardSchemaV1.Issue[], vendor: string);
445
+ constructor(location: ParserErrorLocation, issues: readonly StandardSchemaV1.Issue[], vendor: string, functionName: string);
446
446
  private static formatMessage;
447
- }
448
- //#endregion
449
- //#region src/helper/sapling.d.ts
450
- type Settings = {
451
- serialize: (value: any) => string;
452
- deserialize: (value: string) => any;
453
- doc: {
454
- openApiPath: string;
455
- swaggerPath: string;
456
- };
457
- };
458
- declare const _settings: Settings;
459
- /**
460
- * Collection of utility functions which are essential for Sapling to function.
461
- */
462
- declare class Sapling {
463
- /**
464
- * If you would prefer to manually resolve your controllers instead, call resolve
465
- * on the controller class.
466
- *
467
- * @example```ts
468
- * import { Sapling } from "@tahminator/sapling";
469
- * import TestController from "./path/to/test.controller";
470
- *
471
- * const app = express();
472
- *
473
- * const router = Sapling.resolve(TestController);
474
- * app.use(router);
475
- * ```
476
- */
477
- static resolve<TClass>(this: void, clazz: Class<TClass>): Router | ErrorRequestHandler;
478
- /**
479
- * Register this function as a middleware in order to utilize Sapling's `deserialize` function.
480
- *
481
- * @example```ts
482
- * import { Sapling } from "@tahminator/sapling";
483
- * import express from "express";
484
- *
485
- * const app = express();
486
- *
487
- * app.use(Sapling.json());
488
- * ```
489
- */
490
- static json(this: void): ExpressMiddlewareFn;
491
- /**
492
- * Register your application with all the necessary middlewares and logics for Sapling to function.
493
- *
494
- * @example```ts
495
- * import { Sapling } from "@tahminator/sapling";
496
- * import express from "express";
497
- *
498
- * const app = express();
499
- *
500
- * app.registerApp(app);
501
- * ```
502
- */
503
- static registerApp(app: e.Express): void;
504
- /**
505
- * Serialize a value into a JSON string.
506
- *
507
- * This function is used in {@link ResponseEntity} to serialize the `body`.
508
- *
509
- * Use `setSerializeFn` to override underlying implementation.
510
- *
511
- * @defaultValue `JSON.stringify`
512
- */
513
- static serialize(this: void, value: any): string;
514
- /**
515
- * Replace the function used for `serialize`.
516
- */
517
- static setSerializeFn(this: void, fn: (value: any) => string): void;
518
- /**
519
- * De-serialize a JSON string back to a JavaScript object.
520
- *
521
- * This function is used to de-serialize a string into a `body`.
522
- *
523
- * Use `setDeserializeFn` to override underlying implementation.
524
- *
525
- * @defaultValue `JSON.parse`
526
- */
527
- static deserialize<T = any>(this: void, value: string): T;
528
- /**
529
- * Replace the function used for `deserialize`
530
- */
531
- static setDeserializeFn(this: void, fn: (value: string) => any): void;
532
- static setOpenApiPath(this: void, path: string): void;
533
- static setSwaggerPath(this: void, path: string): void;
534
- static chainHandlers(this: void, handlers: RequestHandler[], request: Request, response: Response$1, next: NextFunction, index?: number): void;
447
+ static getPrettyLocationString(location: ParserErrorLocation): "request body" | "request params" | "request query" | "response body";
535
448
  }
536
449
  //#endregion
537
450
  //#region node_modules/.pnpm/openapi-types@12.1.3/node_modules/openapi-types/dist/index.d.ts
@@ -855,23 +768,162 @@ declare namespace OpenAPIV3 {
855
768
  }
856
769
  //#endregion
857
770
  //#region src/helper/openapi.d.ts
858
- type OpenAPIConfig = {
771
+ type OpenAPIMetadata = {
859
772
  title: string;
860
773
  version: string;
861
774
  description?: string;
862
775
  };
863
776
  declare class OpenAPIGenerator {
777
+ private OPENAPI_VERSION;
864
778
  private controllers;
865
- private config;
866
- setConfig(config: OpenAPIConfig): void;
867
779
  registerController(controllerClass: Function, prefix: string): void;
780
+ /**
781
+ * visible for testing
782
+ */
783
+ _clearControllers(): void;
784
+ private get metadata();
868
785
  generateSpec(): OpenAPIV3.Document;
869
786
  private toJsonSchema;
870
787
  }
871
788
  declare const openApiGenerator: OpenAPIGenerator;
872
- declare function _registerControllerClass(controllerClass: Function, prefix: string): void;
873
- declare function setOpenApiConfig(config: OpenAPIConfig): void;
789
+ declare function _registerController(controllerClass: Function, prefix: string): void;
874
790
  declare function generateOpenApiSpec(): OpenAPIV3.Document;
791
+ declare function _clearOpenApiRegistry(): void;
792
+ //#endregion
793
+ //#region src/helper/sapling.d.ts
794
+ type Settings = {
795
+ serialize: (value: any) => string;
796
+ deserialize: (value: string) => any;
797
+ doc: {
798
+ openApiPath: string;
799
+ swaggerPath: string;
800
+ metadata: OpenAPIMetadata;
801
+ };
802
+ };
803
+ declare const _settings: Settings;
804
+ /**
805
+ * Collection of utility functions which are essential for Sapling to function.
806
+ */
807
+ declare class Sapling {
808
+ /**
809
+ * If you would prefer to manually resolve your controllers instead, call resolve
810
+ * on the controller class.
811
+ *
812
+ * @example```ts
813
+ * import { Sapling } from "@tahminator/sapling";
814
+ * import TestController from "./path/to/test.controller";
815
+ *
816
+ * const app = express();
817
+ *
818
+ * const router = Sapling.resolve(TestController);
819
+ * app.use(router);
820
+ * ```
821
+ */
822
+ static resolve<TClass>(this: void, clazz: Class<TClass>): Router | ErrorRequestHandler;
823
+ /**
824
+ * Register this function as a middleware in order to utilize Sapling's `deserialize` function.
825
+ *
826
+ * @example```ts
827
+ * import { Sapling } from "@tahminator/sapling";
828
+ * import express from "express";
829
+ *
830
+ * const app = express();
831
+ *
832
+ * app.use(Sapling.json());
833
+ * ```
834
+ */
835
+ static json(this: void): ExpressMiddlewareFn;
836
+ /**
837
+ * Register your application with all the necessary middlewares and logics for Sapling to function.
838
+ *
839
+ * @example```ts
840
+ * import { Sapling } from "@tahminator/sapling";
841
+ * import express from "express";
842
+ *
843
+ * const app = express();
844
+ *
845
+ * app.registerApp(app);
846
+ * ```
847
+ */
848
+ static registerApp(app: e.Express): void;
849
+ /**
850
+ * Serialize a value into a JSON string.
851
+ *
852
+ * This function is used in {@link ResponseEntity} to serialize the `body`.
853
+ *
854
+ * Use `setSerializeFn` to override underlying implementation.
855
+ *
856
+ * @defaultValue `JSON.stringify`
857
+ */
858
+ static serialize(this: void, value: any): string;
859
+ /**
860
+ * Replace the function used for `serialize`.
861
+ */
862
+ static setSerializeFn(this: void, fn: (value: any) => string): void;
863
+ /**
864
+ * De-serialize a JSON string back to a JavaScript object.
865
+ *
866
+ * This function is used to de-serialize a string into a `body`.
867
+ *
868
+ * Use `setDeserializeFn` to override underlying implementation.
869
+ *
870
+ * @defaultValue `JSON.parse`
871
+ */
872
+ static deserialize<T = any>(this: void, value: string): T;
873
+ /**
874
+ * Replace the function used for `deserialize`
875
+ */
876
+ static setDeserializeFn(this: void, fn: (value: string) => any): void;
877
+ /**
878
+ * Modify extra settings
879
+ */
880
+ static Extras: {
881
+ /**
882
+ * Modify default settings applied to OpenAPI & Swagger
883
+ */
884
+ swaggerAndOpenApi: {
885
+ /**
886
+ * Set base OpenAPI metadata values.
887
+ *
888
+ * @default { title: "API", version: "1.0.0" }
889
+ */
890
+ setMetadata(metadata: OpenAPIMetadata): void;
891
+ /**
892
+ * change default endpoint that will serve OpenAPI spec.
893
+ * Swagger will also load this endpoint on load.
894
+ *
895
+ * @default `/openapi.json`
896
+ */
897
+ setOpenApiPath(this: void, path: string): void;
898
+ /**
899
+ * change Swagger endpoint.
900
+ *
901
+ * @default `/swagger.html`
902
+ */
903
+ setSwaggerPath(this: void, path: string): void;
904
+ };
905
+ };
906
+ /**
907
+ * This method can be used in a `@MiddlewareClass` to register any libraries
908
+ * that expect you to register multiple registers at once. An example is `swagger-ui-express`
909
+ *
910
+ * @example
911
+ * ```ts
912
+ * ⠀@MiddlewareClass()
913
+ * class Serve {
914
+ * // `swagger.serve` returns multiple Express handlers for all the assets and routes
915
+ * // that will be served
916
+ * private readonly handlers: RequestHandler[] = swagger.serve;
917
+ *
918
+ * ⠀@Middleware(_settings.doc.swaggerPath)
919
+ * handle(request: Request, response: Response, next: NextFunction) {
920
+ * return Sapling.chainHandlers(this.handlers, request, response, next);
921
+ * }
922
+ * }
923
+ * ```
924
+ */
925
+ static chainHandlers(this: void, handlers: RequestHandler[], request: Request, response: Response$1, next: NextFunction, index?: number): void;
926
+ }
875
927
  //#endregion
876
928
  //#region src/annotation/validator.d.ts
877
929
  type ValidatorSchema = {
@@ -885,9 +937,9 @@ declare function RequestBody(schema: StandardSchemaV1 & StandardJSONSchemaV1): M
885
937
  declare function RequestParam(schema: StandardSchemaV1 & StandardJSONSchemaV1): MethodDecorator;
886
938
  declare function RequestQuery(schema: StandardSchemaV1 & StandardJSONSchemaV1): MethodDecorator;
887
939
  declare function _getOrCreateSchemaDefinition(ctor: Function, fnName: string): ValidatorSchema;
888
- declare function _parseOrThrow<TSchema extends StandardSchemaV1>(schema: TSchema, input: unknown, kind: ParserErrorLocation): Promise<StandardSchemaV1.InferOutput<TSchema>>;
940
+ declare function _parseOrThrow<TSchema extends StandardSchemaV1>(schema: TSchema, input: unknown, location: ParserErrorLocation, fnName: string): Promise<StandardSchemaV1.InferOutput<TSchema>>;
941
+ declare function _saveValidatorSchema(def: ValidatorSchema, key: keyof ValidatorSchema, schema: StandardSchemaV1 & StandardJSONSchemaV1, fnName: string): void;
889
942
  declare function _getValidatorSchema(ctor: Function, fnName: string): ValidatorSchema | undefined;
890
- declare function _setOnce(def: ValidatorSchema, key: keyof ValidatorSchema, schema: StandardSchemaV1 & StandardJSONSchemaV1, fnName: string): void;
891
943
  //#endregion
892
944
  //#region src/annotation/schema.d.ts
893
945
  type ResponseSchema = {
@@ -923,6 +975,7 @@ declare function _getControllerSchema(ctor: Function): ControllerSchemaDefinitio
923
975
  * This should be registered last in the middleware chain.
924
976
  *
925
977
  * All exception messages are hidden from the request by default.
978
+ * If the default is not suitable, you may also easily write your own.
926
979
  */
927
980
  declare class DefaultBaseErrorMiddleware {
928
981
  handle(err: unknown, _request: Request, _response: Response$1, _next: NextFunction): ResponseEntity<{
@@ -931,6 +984,10 @@ declare class DefaultBaseErrorMiddleware {
931
984
  }
932
985
  //#endregion
933
986
  //#region src/middleware/default/error/parse.d.ts
987
+ /**
988
+ * Default error middleware that handles `ParserError`.
989
+ * If the default is not suitable, you may also easily write your own.
990
+ */
934
991
  declare class DefaultParserErrorMiddleware {
935
992
  handle(err: unknown, _request: Request, _response: Response$1, next: NextFunction): ResponseEntity<{
936
993
  message: string;
@@ -938,6 +995,10 @@ declare class DefaultParserErrorMiddleware {
938
995
  }
939
996
  //#endregion
940
997
  //#region src/middleware/default/error/responsestatus.d.ts
998
+ /**
999
+ * Default error middleware that handles `ResponseStatusError`.
1000
+ * If the default is not suitable, you may also easily write your own.
1001
+ */
941
1002
  declare class DefaultResponseStatusErrorMiddleware {
942
1003
  handle(err: unknown, _request: Request, _response: Response$1, next: NextFunction): ResponseEntity<{
943
1004
  message: string;
@@ -945,23 +1006,87 @@ declare class DefaultResponseStatusErrorMiddleware {
945
1006
  }
946
1007
  //#endregion
947
1008
  //#region src/middleware/default/openapi/index.d.ts
1009
+ /**
1010
+ * Enable the serving of the OpenAPI spec autogenerated by Sapling.
1011
+ *
1012
+ * Configure any middleware-specific settings with `Sapling.Extras.swaggerAndOpenApi`
1013
+ *
1014
+ * You must register `DefaultSwaggerMiddleware.Serve` & `DefaultSwaggerMiddleware.Setup` after `DefaultOpenApiMiddleware`
1015
+ *
1016
+ * ```ts
1017
+ * const middlewares = [
1018
+ * DefaultOpenApiMiddleware,
1019
+ * DefaultSwaggerMiddleware.Serve,
1020
+ * DefaultSwaggerMiddleware.Setup,
1021
+ * ];
1022
+ * middlewares.map(Sapling.resolve).forEach((r) => app.use(r));
1023
+ * ```
1024
+ */
948
1025
  declare class DefaultOpenApiMiddleware {
949
1026
  handle(_request: Request, _response: Response$1, _next: NextFunction): ResponseEntity<OpenAPIV3.Document<{}>>;
950
1027
  }
951
1028
  //#endregion
952
1029
  //#region src/middleware/default/swagger/index.d.ts
1030
+ /**
1031
+ * Enable the serving of the Swagger endpoint used to serve the OpenAPI spec generated by Sapling.
1032
+ *
1033
+ * Configure any middleware-specific settings with `Sapling.Extras.swaggerAndOpenApi`
1034
+ *
1035
+ * You must register `DefaultSwaggerMiddleware.Serve` & `DefaultSwaggerMiddleware.Setup` after `DefaultOpenApiMiddleware`
1036
+ *
1037
+ * ```ts
1038
+ * const middlewares = [
1039
+ * DefaultOpenApiMiddleware,
1040
+ * DefaultSwaggerMiddleware.Serve,
1041
+ * DefaultSwaggerMiddleware.Setup,
1042
+ * ];
1043
+ * middlewares.map(Sapling.resolve).forEach((r) => app.use(r));
1044
+ * ```
1045
+ */
953
1046
  declare class Serve {
954
1047
  private readonly handlers;
955
1048
  handle(request: Request, response: Response$1, next: NextFunction): void;
956
1049
  }
1050
+ /**
1051
+ * Enable the serving of the Swagger endpoint used to serve the OpenAPI spec generated by Sapling.
1052
+ *
1053
+ * Configure any middleware-specific settings with `Sapling.Extras.swaggerAndOpenApi`
1054
+ *
1055
+ * You must register `DefaultSwaggerMiddleware.Serve` & `DefaultSwaggerMiddleware.Setup` after `DefaultOpenApiMiddleware`
1056
+ *
1057
+ * ```ts
1058
+ * const middlewares = [
1059
+ * DefaultOpenApiMiddleware,
1060
+ * DefaultSwaggerMiddleware.Serve,
1061
+ * DefaultSwaggerMiddleware.Setup,
1062
+ * ];
1063
+ * middlewares.map(Sapling.resolve).forEach((r) => app.use(r));
1064
+ * ```
1065
+ */
957
1066
  declare class Setup {
958
1067
  private readonly handler;
959
1068
  constructor();
960
1069
  handle(request: Request, response: Response$1, next: NextFunction): unknown;
961
1070
  }
1071
+ /**
1072
+ * Enable the serving of the Swagger endpoint used to serve the OpenAPI spec generated by Sapling.
1073
+ *
1074
+ * Configure any middleware-specific settings with `Sapling.Extras.swaggerAndOpenApi`
1075
+ *
1076
+ * You must register `DefaultSwaggerMiddleware.Serve` & `DefaultSwaggerMiddleware.Setup` after `DefaultOpenApiMiddleware`
1077
+ *
1078
+ * ```ts
1079
+ * const middlewares = [
1080
+ * DefaultOpenApiMiddleware,
1081
+ * DefaultSwaggerMiddleware.Serve,
1082
+ * DefaultSwaggerMiddleware.Setup,
1083
+ * ];
1084
+ * middlewares.map(Sapling.resolve).forEach((r) => app.use(r));
1085
+ * ```
1086
+ */
962
1087
  declare const DefaultSwaggerMiddleware: {
963
1088
  Serve: typeof Serve;
964
1089
  Setup: typeof Setup;
965
1090
  };
966
1091
  //#endregion
967
- export { Class, Controller, ControllerSchema, ControllerSchemaDefinition, DELETE, DefaultBaseErrorMiddleware, DefaultOpenApiMiddleware, DefaultParserErrorMiddleware, DefaultResponseStatusErrorMiddleware, DefaultSwaggerMiddleware, ExpressMiddlewareFn, ExpressRouterMethodKey, ExpressRouterMethods, GET, HEAD, Html404ErrorPage, HttpHeaders, HttpStatus, Injectable, Middleware, MiddlewareClass, OPTIONS, PATCH, POST, PUT, ParserError, ParserErrorLocation, RedirectView, RequestBody, RequestParam, RequestQuery, ResponseBody, ResponseEntity, ResponseEntityBuilder, ResponseSchema, ResponseStatusError, RouteDefinition, RouteSchema, RouteSchemaDefinition, Sapling, ValidatorSchema, _ControllerRegistry, _InjectableDeps, _InjectableRegistry, _Route, _getControllerSchema, _getOrCreateSchemaDefinition, _getRouteSchema, _getRoutes, _getValidatorSchema, _parseOrThrow, _registerControllerClass, _resolve, _setControllerSchema, _setOnce, _setRouteSchema, _settings, generateOpenApiSpec, methodResolve, openApiGenerator, setOpenApiConfig };
1092
+ export { Class, Controller, ControllerSchema, ControllerSchemaDefinition, DELETE, DefaultBaseErrorMiddleware, DefaultOpenApiMiddleware, DefaultParserErrorMiddleware, DefaultResponseStatusErrorMiddleware, DefaultSwaggerMiddleware, ExpressMiddlewareFn, ExpressRouterMethodKey, ExpressRouterMethods, GET, HEAD, Html404ErrorPage, HttpHeaders, HttpStatus, Injectable, Middleware, MiddlewareClass, OPTIONS, OpenAPIMetadata, PATCH, POST, PUT, ParserError, ParserErrorLocation, RedirectView, RequestBody, RequestParam, RequestQuery, ResponseBody, ResponseEntity, ResponseEntityBuilder, ResponseSchema, ResponseStatusError, RouteDefinition, RouteSchema, RouteSchemaDefinition, Sapling, ValidatorSchema, _ControllerRegistry, _InjectableDeps, _InjectableRegistry, _Route, _clearOpenApiRegistry, _getControllerSchema, _getOrCreateSchemaDefinition, _getRouteSchema, _getRoutes, _getValidatorSchema, _parseOrThrow, _registerController, _resolve, _saveValidatorSchema, _setControllerSchema, _setRouteSchema, _settings, generateOpenApiSpec, methodResolve, openApiGenerator };