@tahminator/sapling 2.0.5-beta.c70dc62b → 2.0.5-beta.e0403942

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,24 +768,157 @@ 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 {
864
777
  private controllers;
865
- private config;
866
- setConfig(config: OpenAPIConfig): void;
867
778
  registerController(controllerClass: Function, prefix: string): void;
779
+ private get metadata();
868
780
  generateSpec(): OpenAPIV3.Document;
869
781
  private toJsonSchema;
870
782
  }
871
783
  declare const openApiGenerator: OpenAPIGenerator;
872
- declare function _registerControllerClass(controllerClass: Function, prefix: string): void;
873
- declare function setOpenApiConfig(config: OpenAPIConfig): void;
784
+ declare function _registerController(controllerClass: Function, prefix: string): void;
874
785
  declare function generateOpenApiSpec(): OpenAPIV3.Document;
875
786
  //#endregion
787
+ //#region src/helper/sapling.d.ts
788
+ type Settings = {
789
+ serialize: (value: any) => string;
790
+ deserialize: (value: string) => any;
791
+ doc: {
792
+ openApiPath: string;
793
+ swaggerPath: string;
794
+ metadata: OpenAPIMetadata;
795
+ };
796
+ };
797
+ declare const _settings: Settings;
798
+ /**
799
+ * Collection of utility functions which are essential for Sapling to function.
800
+ */
801
+ declare class Sapling {
802
+ /**
803
+ * If you would prefer to manually resolve your controllers instead, call resolve
804
+ * on the controller class.
805
+ *
806
+ * @example```ts
807
+ * import { Sapling } from "@tahminator/sapling";
808
+ * import TestController from "./path/to/test.controller";
809
+ *
810
+ * const app = express();
811
+ *
812
+ * const router = Sapling.resolve(TestController);
813
+ * app.use(router);
814
+ * ```
815
+ */
816
+ static resolve<TClass>(this: void, clazz: Class<TClass>): Router | ErrorRequestHandler;
817
+ /**
818
+ * Register this function as a middleware in order to utilize Sapling's `deserialize` function.
819
+ *
820
+ * @example```ts
821
+ * import { Sapling } from "@tahminator/sapling";
822
+ * import express from "express";
823
+ *
824
+ * const app = express();
825
+ *
826
+ * app.use(Sapling.json());
827
+ * ```
828
+ */
829
+ static json(this: void): ExpressMiddlewareFn;
830
+ /**
831
+ * Register your application with all the necessary middlewares and logics for Sapling to function.
832
+ *
833
+ * @example```ts
834
+ * import { Sapling } from "@tahminator/sapling";
835
+ * import express from "express";
836
+ *
837
+ * const app = express();
838
+ *
839
+ * app.registerApp(app);
840
+ * ```
841
+ */
842
+ static registerApp(app: e.Express): void;
843
+ /**
844
+ * Serialize a value into a JSON string.
845
+ *
846
+ * This function is used in {@link ResponseEntity} to serialize the `body`.
847
+ *
848
+ * Use `setSerializeFn` to override underlying implementation.
849
+ *
850
+ * @defaultValue `JSON.stringify`
851
+ */
852
+ static serialize(this: void, value: any): string;
853
+ /**
854
+ * Replace the function used for `serialize`.
855
+ */
856
+ static setSerializeFn(this: void, fn: (value: any) => string): void;
857
+ /**
858
+ * De-serialize a JSON string back to a JavaScript object.
859
+ *
860
+ * This function is used to de-serialize a string into a `body`.
861
+ *
862
+ * Use `setDeserializeFn` to override underlying implementation.
863
+ *
864
+ * @defaultValue `JSON.parse`
865
+ */
866
+ static deserialize<T = any>(this: void, value: string): T;
867
+ /**
868
+ * Replace the function used for `deserialize`
869
+ */
870
+ static setDeserializeFn(this: void, fn: (value: string) => any): void;
871
+ /**
872
+ * Modify extra settings
873
+ */
874
+ static Extras: {
875
+ /**
876
+ * Modify default settings applied to OpenAPI & Swagger
877
+ */
878
+ swaggerAndOpenApi: {
879
+ /**
880
+ * Set base OpenAPI metadata values.
881
+ *
882
+ * @default { title: "API", version: "1.0.0" }
883
+ */
884
+ setMetadata(metadata: OpenAPIMetadata): void;
885
+ /**
886
+ * change default endpoint that will serve OpenAPI spec.
887
+ * Swagger will also load this endpoint on load.
888
+ *
889
+ * @default `/openapi.json`
890
+ */
891
+ setOpenApiPath(this: void, path: string): void;
892
+ /**
893
+ * change Swagger endpoint.
894
+ *
895
+ * @default `/swagger.html`
896
+ */
897
+ setSwaggerPath(this: void, path: string): void;
898
+ };
899
+ };
900
+ /**
901
+ * This method can be used in a `@MiddlewareClass` to register any libraries
902
+ * that expect you to register multiple registers at once. An example is `swagger-ui-express`
903
+ *
904
+ * @example
905
+ * ```ts
906
+ * ⠀@MiddlewareClass()
907
+ * class Serve {
908
+ * // `swagger.serve` returns multiple Express handlers for all the assets and routes
909
+ * // that will be served
910
+ * private readonly handlers: RequestHandler[] = swagger.serve;
911
+ *
912
+ * ⠀@Middleware(_settings.doc.swaggerPath)
913
+ * handle(request: Request, response: Response, next: NextFunction) {
914
+ * return Sapling.chainHandlers(this.handlers, request, response, next);
915
+ * }
916
+ * }
917
+ * ```
918
+ */
919
+ static chainHandlers(this: void, handlers: RequestHandler[], request: Request, response: Response$1, next: NextFunction, index?: number): void;
920
+ }
921
+ //#endregion
876
922
  //#region src/annotation/validator.d.ts
877
923
  type ValidatorSchema = {
878
924
  requestBody?: StandardSchemaV1 & StandardJSONSchemaV1;
@@ -885,15 +931,45 @@ declare function RequestBody(schema: StandardSchemaV1 & StandardJSONSchemaV1): M
885
931
  declare function RequestParam(schema: StandardSchemaV1 & StandardJSONSchemaV1): MethodDecorator;
886
932
  declare function RequestQuery(schema: StandardSchemaV1 & StandardJSONSchemaV1): MethodDecorator;
887
933
  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>>;
934
+ declare function _parseOrThrow<TSchema extends StandardSchemaV1>(schema: TSchema, input: unknown, location: ParserErrorLocation, fnName: string): Promise<StandardSchemaV1.InferOutput<TSchema>>;
935
+ declare function _saveValidatorSchema(def: ValidatorSchema, key: keyof ValidatorSchema, schema: StandardSchemaV1 & StandardJSONSchemaV1, fnName: string): void;
889
936
  declare function _getValidatorSchema(ctor: Function, fnName: string): ValidatorSchema | undefined;
890
- declare function _setOnce(def: ValidatorSchema, key: keyof ValidatorSchema, schema: StandardSchemaV1 & StandardJSONSchemaV1, fnName: string): void;
937
+ //#endregion
938
+ //#region src/annotation/schema.d.ts
939
+ type ResponseSchema = {
940
+ statusCode: HttpStatus;
941
+ description?: string;
942
+ schema?: StandardSchemaV1 & StandardJSONSchemaV1;
943
+ };
944
+ type RouteSchemaDefinition = {
945
+ summary?: string;
946
+ description?: string;
947
+ responses?: ResponseSchema[];
948
+ };
949
+ type ControllerSchemaDefinition = {
950
+ title?: string;
951
+ description?: string;
952
+ };
953
+ declare function ControllerSchema(options: {
954
+ title?: string;
955
+ description?: string;
956
+ }): ClassDecorator;
957
+ declare function RouteSchema(options: {
958
+ summary?: string;
959
+ description?: string;
960
+ responses?: ResponseSchema[];
961
+ }): MethodDecorator;
962
+ declare function _setRouteSchema(ctor: Function, fnName: string, options: RouteSchemaDefinition): void;
963
+ declare function _setControllerSchema(ctor: Function, options: ControllerSchemaDefinition): void;
964
+ declare function _getRouteSchema(ctor: Function, fnName: string): RouteSchemaDefinition | undefined;
965
+ declare function _getControllerSchema(ctor: Function): ControllerSchemaDefinition | undefined;
891
966
  //#endregion
892
967
  //#region src/middleware/default/error/base.d.ts
893
968
  /**
894
969
  * This should be registered last in the middleware chain.
895
970
  *
896
971
  * All exception messages are hidden from the request by default.
972
+ * If the default is not suitable, you may also easily write your own.
897
973
  */
898
974
  declare class DefaultBaseErrorMiddleware {
899
975
  handle(err: unknown, _request: Request, _response: Response$1, _next: NextFunction): ResponseEntity<{
@@ -902,6 +978,10 @@ declare class DefaultBaseErrorMiddleware {
902
978
  }
903
979
  //#endregion
904
980
  //#region src/middleware/default/error/parse.d.ts
981
+ /**
982
+ * Default error middleware that handles `ParserError`.
983
+ * If the default is not suitable, you may also easily write your own.
984
+ */
905
985
  declare class DefaultParserErrorMiddleware {
906
986
  handle(err: unknown, _request: Request, _response: Response$1, next: NextFunction): ResponseEntity<{
907
987
  message: string;
@@ -909,6 +989,10 @@ declare class DefaultParserErrorMiddleware {
909
989
  }
910
990
  //#endregion
911
991
  //#region src/middleware/default/error/responsestatus.d.ts
992
+ /**
993
+ * Default error middleware that handles `ResponseStatusError`.
994
+ * If the default is not suitable, you may also easily write your own.
995
+ */
912
996
  declare class DefaultResponseStatusErrorMiddleware {
913
997
  handle(err: unknown, _request: Request, _response: Response$1, next: NextFunction): ResponseEntity<{
914
998
  message: string;
@@ -916,23 +1000,87 @@ declare class DefaultResponseStatusErrorMiddleware {
916
1000
  }
917
1001
  //#endregion
918
1002
  //#region src/middleware/default/openapi/index.d.ts
1003
+ /**
1004
+ * Enable the serving of the OpenAPI spec autogenerated by Sapling.
1005
+ *
1006
+ * Configure any middleware-specific settings with `Sapling.Extras.swaggerAndOpenApi`
1007
+ *
1008
+ * You must register `DefaultSwaggerMiddleware.Serve` & `DefaultSwaggerMiddleware.Setup` after `DefaultOpenApiMiddleware`
1009
+ *
1010
+ * ```ts
1011
+ * const middlewares = [
1012
+ * DefaultOpenApiMiddleware,
1013
+ * DefaultSwaggerMiddleware.Serve,
1014
+ * DefaultSwaggerMiddleware.Setup,
1015
+ * ];
1016
+ * middlewares.map(Sapling.resolve).forEach((r) => app.use(r));
1017
+ * ```
1018
+ */
919
1019
  declare class DefaultOpenApiMiddleware {
920
1020
  handle(_request: Request, _response: Response$1, _next: NextFunction): ResponseEntity<OpenAPIV3.Document<{}>>;
921
1021
  }
922
1022
  //#endregion
923
1023
  //#region src/middleware/default/swagger/index.d.ts
1024
+ /**
1025
+ * Enable the serving of the Swagger endpoint used to serve the OpenAPI spec generated by Sapling.
1026
+ *
1027
+ * Configure any middleware-specific settings with `Sapling.Extras.swaggerAndOpenApi`
1028
+ *
1029
+ * You must register `DefaultSwaggerMiddleware.Serve` & `DefaultSwaggerMiddleware.Setup` after `DefaultOpenApiMiddleware`
1030
+ *
1031
+ * ```ts
1032
+ * const middlewares = [
1033
+ * DefaultOpenApiMiddleware,
1034
+ * DefaultSwaggerMiddleware.Serve,
1035
+ * DefaultSwaggerMiddleware.Setup,
1036
+ * ];
1037
+ * middlewares.map(Sapling.resolve).forEach((r) => app.use(r));
1038
+ * ```
1039
+ */
924
1040
  declare class Serve {
925
1041
  private readonly handlers;
926
1042
  handle(request: Request, response: Response$1, next: NextFunction): void;
927
1043
  }
1044
+ /**
1045
+ * Enable the serving of the Swagger endpoint used to serve the OpenAPI spec generated by Sapling.
1046
+ *
1047
+ * Configure any middleware-specific settings with `Sapling.Extras.swaggerAndOpenApi`
1048
+ *
1049
+ * You must register `DefaultSwaggerMiddleware.Serve` & `DefaultSwaggerMiddleware.Setup` after `DefaultOpenApiMiddleware`
1050
+ *
1051
+ * ```ts
1052
+ * const middlewares = [
1053
+ * DefaultOpenApiMiddleware,
1054
+ * DefaultSwaggerMiddleware.Serve,
1055
+ * DefaultSwaggerMiddleware.Setup,
1056
+ * ];
1057
+ * middlewares.map(Sapling.resolve).forEach((r) => app.use(r));
1058
+ * ```
1059
+ */
928
1060
  declare class Setup {
929
1061
  private readonly handler;
930
1062
  constructor();
931
1063
  handle(request: Request, response: Response$1, next: NextFunction): unknown;
932
1064
  }
1065
+ /**
1066
+ * Enable the serving of the Swagger endpoint used to serve the OpenAPI spec generated by Sapling.
1067
+ *
1068
+ * Configure any middleware-specific settings with `Sapling.Extras.swaggerAndOpenApi`
1069
+ *
1070
+ * You must register `DefaultSwaggerMiddleware.Serve` & `DefaultSwaggerMiddleware.Setup` after `DefaultOpenApiMiddleware`
1071
+ *
1072
+ * ```ts
1073
+ * const middlewares = [
1074
+ * DefaultOpenApiMiddleware,
1075
+ * DefaultSwaggerMiddleware.Serve,
1076
+ * DefaultSwaggerMiddleware.Setup,
1077
+ * ];
1078
+ * middlewares.map(Sapling.resolve).forEach((r) => app.use(r));
1079
+ * ```
1080
+ */
933
1081
  declare const DefaultSwaggerMiddleware: {
934
1082
  Serve: typeof Serve;
935
1083
  Setup: typeof Setup;
936
1084
  };
937
1085
  //#endregion
938
- export { Class, Controller, 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, ResponseStatusError, RouteDefinition, Sapling, ValidatorSchema, _ControllerRegistry, _InjectableDeps, _InjectableRegistry, _Route, _getOrCreateSchemaDefinition, _getRoutes, _getValidatorSchema, _parseOrThrow, _registerControllerClass, _resolve, _setOnce, _settings, generateOpenApiSpec, methodResolve, openApiGenerator, setOpenApiConfig };
1086
+ 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, _getControllerSchema, _getOrCreateSchemaDefinition, _getRouteSchema, _getRoutes, _getValidatorSchema, _parseOrThrow, _registerController, _resolve, _saveValidatorSchema, _setControllerSchema, _setRouteSchema, _settings, generateOpenApiSpec, methodResolve, openApiGenerator };