@nestia/core 2.5.8 → 2.5.9

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.
Files changed (45) hide show
  1. package/README.md +9 -6
  2. package/lib/decorators/SwaggerCustomizer.d.ts +64 -0
  3. package/lib/decorators/SwaggerCustomizer.js +25 -0
  4. package/lib/decorators/SwaggerCustomizer.js.map +1 -0
  5. package/lib/module.d.ts +1 -0
  6. package/lib/module.js +1 -0
  7. package/lib/module.js.map +1 -1
  8. package/lib/structures/ISwagger.d.ts +72 -0
  9. package/lib/structures/ISwagger.js +3 -0
  10. package/lib/structures/ISwagger.js.map +1 -0
  11. package/lib/structures/ISwaggerComponents.d.ts +26 -0
  12. package/lib/structures/ISwaggerComponents.js +3 -0
  13. package/lib/structures/ISwaggerComponents.js.map +1 -0
  14. package/lib/structures/ISwaggerInfo.d.ts +71 -0
  15. package/lib/structures/ISwaggerInfo.js +3 -0
  16. package/lib/structures/ISwaggerInfo.js.map +1 -0
  17. package/lib/structures/ISwaggerRoute.d.ts +46 -0
  18. package/lib/structures/ISwaggerRoute.js +3 -0
  19. package/lib/structures/ISwaggerRoute.js.map +1 -0
  20. package/lib/structures/ISwaggerSecurityScheme.d.ts +56 -0
  21. package/lib/structures/ISwaggerSecurityScheme.js +3 -0
  22. package/lib/structures/ISwaggerSecurityScheme.js.map +1 -0
  23. package/package.json +3 -3
  24. package/src/decorators/EncryptedBody.ts +105 -105
  25. package/src/decorators/EncryptedModule.ts +96 -96
  26. package/src/decorators/PlainBody.ts +75 -75
  27. package/src/decorators/SwaggerCustomizer.ts +87 -0
  28. package/src/decorators/TypedBody.ts +62 -62
  29. package/src/decorators/TypedException.ts +90 -90
  30. package/src/decorators/TypedRoute.ts +144 -144
  31. package/src/decorators/internal/get_path_and_querify.ts +106 -106
  32. package/src/decorators/internal/load_controller.ts +51 -51
  33. package/src/decorators/internal/validate_request_body.ts +72 -72
  34. package/src/decorators/internal/validate_request_headers.ts +83 -83
  35. package/src/decorators/internal/validate_request_query.ts +71 -71
  36. package/src/module.ts +1 -0
  37. package/src/structures/ISwagger.ts +91 -0
  38. package/src/structures/ISwaggerComponents.ts +29 -0
  39. package/src/structures/ISwaggerInfo.ts +80 -0
  40. package/src/structures/ISwaggerRoute.ts +50 -0
  41. package/src/structures/ISwaggerSecurityScheme.ts +65 -0
  42. package/src/transformers/NodeTransformer.ts +16 -16
  43. package/src/transformers/TypedExceptionTransformer.ts +48 -48
  44. package/src/transformers/TypedRouteTransformer.ts +88 -88
  45. package/src/utils/Singleton.ts +20 -20
@@ -1,71 +1,71 @@
1
- import { BadRequestException } from "@nestjs/common";
2
- import typia, { IValidation, TypeGuardError } from "typia";
3
-
4
- import { IRequestQueryValidator } from "../../options/IRequestQueryValidator";
5
- import { NoTransformConfigureError } from "./NoTransformConfigureError";
6
-
7
- /**
8
- * @internal
9
- */
10
- export const validate_request_query = <T>(
11
- validator?: IRequestQueryValidator<T>,
12
- ) => {
13
- if (!validator) return () => NoTransformConfigureError("TypedQuery");
14
- else if (validator.type === "assert") return assert(validator.assert);
15
- else if (validator.type === "is") return is(validator.is);
16
- else if (validator.type === "validate") return validate(validator.validate);
17
- return () =>
18
- new Error(`Error on nestia.core.TypedQuery(): invalid typed validator.`);
19
- };
20
-
21
- /**
22
- * @internal
23
- */
24
- const assert =
25
- <T>(closure: (input: URLSearchParams) => T) =>
26
- (input: URLSearchParams): T | BadRequestException => {
27
- try {
28
- return closure(input);
29
- } catch (exp) {
30
- if (typia.is<TypeGuardError>(exp)) {
31
- return new BadRequestException({
32
- path: exp.path,
33
- reason: exp.message,
34
- expected: exp.expected,
35
- value: exp.value,
36
- message: MESSAGE,
37
- });
38
- }
39
- throw exp;
40
- }
41
- };
42
-
43
- /**
44
- * @internal
45
- */
46
- const is =
47
- <T>(closure: (input: URLSearchParams) => T | null) =>
48
- (input: URLSearchParams): T | BadRequestException => {
49
- const result: T | null = closure(input);
50
- return result !== null ? result : new BadRequestException(MESSAGE);
51
- };
52
-
53
- /**
54
- * @internal
55
- */
56
- const validate =
57
- <T>(closure: (input: URLSearchParams) => IValidation<T>) =>
58
- (input: URLSearchParams): T | BadRequestException => {
59
- const result: IValidation<T> = closure(input);
60
- return result.success
61
- ? result.data
62
- : new BadRequestException({
63
- errors: result.errors,
64
- message: MESSAGE,
65
- });
66
- };
67
-
68
- /**
69
- * @internal
70
- */
71
- const MESSAGE = "Request query data is not following the promised type.";
1
+ import { BadRequestException } from "@nestjs/common";
2
+ import typia, { IValidation, TypeGuardError } from "typia";
3
+
4
+ import { IRequestQueryValidator } from "../../options/IRequestQueryValidator";
5
+ import { NoTransformConfigureError } from "./NoTransformConfigureError";
6
+
7
+ /**
8
+ * @internal
9
+ */
10
+ export const validate_request_query = <T>(
11
+ validator?: IRequestQueryValidator<T>,
12
+ ) => {
13
+ if (!validator) return () => NoTransformConfigureError("TypedQuery");
14
+ else if (validator.type === "assert") return assert(validator.assert);
15
+ else if (validator.type === "is") return is(validator.is);
16
+ else if (validator.type === "validate") return validate(validator.validate);
17
+ return () =>
18
+ new Error(`Error on nestia.core.TypedQuery(): invalid typed validator.`);
19
+ };
20
+
21
+ /**
22
+ * @internal
23
+ */
24
+ const assert =
25
+ <T>(closure: (input: URLSearchParams) => T) =>
26
+ (input: URLSearchParams): T | BadRequestException => {
27
+ try {
28
+ return closure(input);
29
+ } catch (exp) {
30
+ if (typia.is<TypeGuardError>(exp)) {
31
+ return new BadRequestException({
32
+ path: exp.path,
33
+ reason: exp.message,
34
+ expected: exp.expected,
35
+ value: exp.value,
36
+ message: MESSAGE,
37
+ });
38
+ }
39
+ throw exp;
40
+ }
41
+ };
42
+
43
+ /**
44
+ * @internal
45
+ */
46
+ const is =
47
+ <T>(closure: (input: URLSearchParams) => T | null) =>
48
+ (input: URLSearchParams): T | BadRequestException => {
49
+ const result: T | null = closure(input);
50
+ return result !== null ? result : new BadRequestException(MESSAGE);
51
+ };
52
+
53
+ /**
54
+ * @internal
55
+ */
56
+ const validate =
57
+ <T>(closure: (input: URLSearchParams) => IValidation<T>) =>
58
+ (input: URLSearchParams): T | BadRequestException => {
59
+ const result: IValidation<T> = closure(input);
60
+ return result.success
61
+ ? result.data
62
+ : new BadRequestException({
63
+ errors: result.errors,
64
+ message: MESSAGE,
65
+ });
66
+ };
67
+
68
+ /**
69
+ * @internal
70
+ */
71
+ const MESSAGE = "Request query data is not following the promised type.";
package/src/module.ts CHANGED
@@ -5,6 +5,7 @@ export * from "./decorators/EncryptedModule";
5
5
  export * from "./decorators/EncryptedRoute";
6
6
  export * from "./utils/ExceptionManager";
7
7
  export * from "./decorators/PlainBody";
8
+ export * from "./decorators/SwaggerCustomizer";
8
9
  export * from "./decorators/TypedBody";
9
10
  export * from "./decorators/TypedException";
10
11
  export * from "./decorators/TypedHeaders";
@@ -0,0 +1,91 @@
1
+ import { ISwaggerComponents } from "./ISwaggerComponents";
2
+ import { ISwaggerInfo } from "./ISwaggerInfo";
3
+ import { ISwaggerRoute } from "./ISwaggerRoute";
4
+
5
+ /**
6
+ * Swagger Document.
7
+ *
8
+ * `ISwagger` is a data structure representing content of `swagger.json` file
9
+ * generated by Nestia. Note that, this is not an universal structure, but a dedicated
10
+ * structure only for Nestia.
11
+ *
12
+ * @author Jeongho Nam - https://github.com/samchon
13
+ */
14
+ export interface ISwagger {
15
+ /**
16
+ * The version of the OpenAPI document.
17
+ *
18
+ * Nestia always generate OpenAPI 3.0.x document.
19
+ */
20
+ openapi: `3.0.${number}`;
21
+
22
+ /**
23
+ * List of servers that provide the API.
24
+ */
25
+ servers: ISwagger.IServer[];
26
+
27
+ /**
28
+ * Information about the API.
29
+ */
30
+ info: ISwaggerInfo;
31
+
32
+ /**
33
+ * The available paths and operations for the API.
34
+ *
35
+ * The 1st key is the path, and the 2nd key is the HTTP method.
36
+ */
37
+ paths: Record<string, Record<string, ISwaggerRoute>>;
38
+
39
+ /**
40
+ * An object to hold reusable data structures.
41
+ *
42
+ * It stores both DTO schemas and security schemes.
43
+ *
44
+ * For reference, `nestia` defines every object and alias types as reusable DTO
45
+ * schemas. The alias type means that defined by `type` keyword in TypeScript.
46
+ */
47
+ components: ISwaggerComponents;
48
+
49
+ // /**
50
+ // * A declaration of which security mechanisms can be used across the API.
51
+ // *
52
+ // * When this property be configured, it would be overwritten in every API routes.
53
+ // *
54
+ // * For reference, key means the name of security scheme and value means the `scopes`.
55
+ // * The `scopes` can be used only when target security scheme is `oauth2` type,
56
+ // * especially for {@link ISwaggerSecurityScheme.IOAuth2.IFlow.scopes} property.
57
+ // */
58
+ // security?: Record<string, string[]>[];
59
+ }
60
+ export namespace ISwagger {
61
+ /**
62
+ * Remote server definition.
63
+ */
64
+ export interface IServer {
65
+ /**
66
+ * A URL to the target host.
67
+ *
68
+ * @format uri
69
+ */
70
+ url: string;
71
+
72
+ /**
73
+ * An optional string describing the target server.
74
+ */
75
+ description?: string;
76
+ }
77
+
78
+ export interface IExternalDocs {
79
+ /**
80
+ * The URL for target documentation.
81
+ *
82
+ * @format uri
83
+ */
84
+ url: string;
85
+
86
+ /**
87
+ * A short description of the target documentation.
88
+ */
89
+ description?: string;
90
+ }
91
+ }
@@ -0,0 +1,29 @@
1
+ import { IJsonComponents } from "typia";
2
+
3
+ import { ISwaggerSecurityScheme } from "./ISwaggerSecurityScheme";
4
+
5
+ /**
6
+ * Reusable components in Swagger.
7
+ *
8
+ * `ISwaggerComponents` is a data structure representing content of `components` object
9
+ * in `swagger.json` file generated by Nestia. Note that, this is not an universal
10
+ * structure, but a dedicated structure only for Nestia.
11
+ *
12
+ * @author Jeongho Nam - https://github.com/samchon
13
+ */
14
+ export interface ISwaggerComponents {
15
+ /**
16
+ * An object to hold reusable DTO schemas.
17
+ *
18
+ * For reference, `nestia` stores every object and alias types as reusable DTO
19
+ * schemas. The alias type means that defined by `type` keyword in TypeScript.
20
+ */
21
+ schemas?: Record<string, IJsonComponents.IAlias>;
22
+
23
+ /**
24
+ * An object to hold reusable security schemes.
25
+ *
26
+ * This property be configured by user in `nestia.config.ts` file.
27
+ */
28
+ securitySchemes?: Record<string, ISwaggerSecurityScheme>;
29
+ }
@@ -0,0 +1,80 @@
1
+ /**
2
+ * Information about the API.
3
+ *
4
+ * @author Samchon
5
+ */
6
+ export interface ISwaggerInfo {
7
+ /**
8
+ * The title of the API.
9
+ */
10
+ title: string;
11
+
12
+ /**
13
+ * A short description of the API.
14
+ */
15
+ description?: string;
16
+
17
+ /**
18
+ * A URL to the Terms of Service for the API.
19
+ *
20
+ * @format uri
21
+ */
22
+ termsOfService?: string;
23
+
24
+ /**
25
+ * The contact information for the exposed API.
26
+ */
27
+ contact?: ISwaggerInfo.IContact;
28
+
29
+ /**
30
+ * The license information for the exposed API.
31
+ */
32
+ license?: ISwaggerInfo.ILicense;
33
+
34
+ /**
35
+ * Version of the API.
36
+ */
37
+ version: string;
38
+ }
39
+ export namespace ISwaggerInfo {
40
+ /**
41
+ * Contact information for the exposed API.
42
+ */
43
+ export interface IContact {
44
+ /**
45
+ * The identifying name of the contact person/organization.
46
+ */
47
+ name?: string;
48
+
49
+ /**
50
+ * The URL pointing to the contact information.
51
+ *
52
+ * @format uri
53
+ */
54
+ url?: string;
55
+
56
+ /**
57
+ * The email address of the contact person/organization.
58
+ *
59
+ * @format email
60
+ */
61
+ email?: string;
62
+ }
63
+
64
+ /**
65
+ * License information for the exposed API.
66
+ */
67
+ export interface ILicense {
68
+ /**
69
+ * The license name used for the API.
70
+ */
71
+ name: string;
72
+
73
+ /**
74
+ * A URL to the license used for the API.
75
+ *
76
+ * @format uri
77
+ */
78
+ url?: string;
79
+ }
80
+ }
@@ -0,0 +1,50 @@
1
+ import { IJsonSchema } from "typia";
2
+
3
+ export interface ISwaggerRoute {
4
+ deprecated?: boolean;
5
+ security?: Record<string, string[]>[];
6
+ operationId?: string;
7
+ tags: string[];
8
+ parameters: ISwaggerRoute.IParameter[];
9
+ requestBody?: ISwaggerRoute.IRequestBody;
10
+ responses: ISwaggerRoute.IResponseBody;
11
+ summary?: string;
12
+ description?: string;
13
+ }
14
+ export namespace ISwaggerRoute {
15
+ export interface IParameter {
16
+ name: string;
17
+ in: string;
18
+ schema: IJsonSchema;
19
+ required: boolean;
20
+ description?: string;
21
+ }
22
+ export interface IRequestBody {
23
+ description?: string;
24
+ content: IContent;
25
+ required: true;
26
+ "x-nestia-encrypted"?: boolean;
27
+ }
28
+ export type IResponseBody = Record<
29
+ string,
30
+ {
31
+ description: string;
32
+ content?: IContent;
33
+ "x-nestia-encrypted"?: boolean;
34
+ }
35
+ >;
36
+ export interface IContent {
37
+ "application/x-www-form-urlencoded"?: {
38
+ schema: IJsonSchema;
39
+ };
40
+ "application/json"?: {
41
+ schema: IJsonSchema;
42
+ };
43
+ "text/plain"?: {
44
+ schema: IJsonSchema;
45
+ };
46
+ "multipart/form-data"?: {
47
+ schema: IJsonSchema;
48
+ };
49
+ }
50
+ }
@@ -0,0 +1,65 @@
1
+ /**
2
+ * Security scheme of Swagger Documents.
3
+ *
4
+ * `ISwaggerSecurityScheme` is a data structure representing content of
5
+ * `securitySchemes` in `swagger.json` file. It is composed with 5 types of security
6
+ * schemes as an union type like below.
7
+ *
8
+ * @reference https://swagger.io/specification/#security-scheme-object
9
+ * @author Jeongho Nam - https://github.com/samchon
10
+ */
11
+ export type ISwaggerSecurityScheme =
12
+ | ISwaggerSecurityScheme.IHttpBasic
13
+ | ISwaggerSecurityScheme.IHttpBearer
14
+ | ISwaggerSecurityScheme.IApiKey
15
+ | ISwaggerSecurityScheme.IOpenId
16
+ | ISwaggerSecurityScheme.IOAuth2;
17
+ export namespace ISwaggerSecurityScheme {
18
+ export interface IHttpBasic {
19
+ type: "http";
20
+ scheme: "basic";
21
+ }
22
+ export interface IHttpBearer {
23
+ type: "http";
24
+ scheme: "bearer";
25
+ bearerFormat?: string;
26
+ }
27
+ export interface IApiKey {
28
+ type: "apiKey";
29
+
30
+ /**
31
+ * @default header
32
+ */
33
+ in?: "header" | "query" | "cookie";
34
+
35
+ /**
36
+ * @default Authorization
37
+ */
38
+ name?: string;
39
+ }
40
+
41
+ export interface IOpenId {
42
+ type: "openIdConnect";
43
+ openIdConnectUrl: string;
44
+ }
45
+
46
+ export interface IOAuth2 {
47
+ type: "oauth2";
48
+ flows: IOAuth2.IFlowSet;
49
+ description?: string;
50
+ }
51
+ export namespace IOAuth2 {
52
+ export interface IFlowSet {
53
+ authorizationCode?: IFlow;
54
+ implicit?: Omit<IFlow, "tokenUrl">;
55
+ password?: Omit<IFlow, "authorizationUrl">;
56
+ clientCredentials?: Omit<IFlow, "authorizationUrl">;
57
+ }
58
+ export interface IFlow {
59
+ authorizationUrl: string;
60
+ tokenUrl: string;
61
+ refreshUrl: string;
62
+ scopes?: Record<string, string>;
63
+ }
64
+ }
65
+ }
@@ -1,16 +1,16 @@
1
- import ts from "typescript";
2
-
3
- import { INestiaTransformProject } from "../options/INestiaTransformProject";
4
- import { MethodTransformer } from "./MethodTransformer";
5
- import { ParameterTransformer } from "./ParameterTransformer";
6
-
7
- export namespace NodeTransformer {
8
- export const transform =
9
- (project: INestiaTransformProject) =>
10
- (node: ts.Node): ts.Node =>
11
- ts.isMethodDeclaration(node)
12
- ? MethodTransformer.transform(project)(node)
13
- : ts.isParameter(node)
14
- ? ParameterTransformer.transform(project)(node)
15
- : node;
16
- }
1
+ import ts from "typescript";
2
+
3
+ import { INestiaTransformProject } from "../options/INestiaTransformProject";
4
+ import { MethodTransformer } from "./MethodTransformer";
5
+ import { ParameterTransformer } from "./ParameterTransformer";
6
+
7
+ export namespace NodeTransformer {
8
+ export const transform =
9
+ (project: INestiaTransformProject) =>
10
+ (node: ts.Node): ts.Node =>
11
+ ts.isMethodDeclaration(node)
12
+ ? MethodTransformer.transform(project)(node)
13
+ : ts.isParameter(node)
14
+ ? ParameterTransformer.transform(project)(node)
15
+ : node;
16
+ }
@@ -1,48 +1,48 @@
1
- import path from "path";
2
- import ts from "typescript";
3
-
4
- import { INestiaTransformProject } from "../options/INestiaTransformProject";
5
- import { TypedExceptionProgrammer } from "../programmers/TypedExceptionProgrammer";
6
-
7
- export namespace TypedExceptionTransformer {
8
- export const transform =
9
- (project: INestiaTransformProject) =>
10
- (decorator: ts.Decorator): ts.Decorator => {
11
- if (!ts.isCallExpression(decorator.expression)) return decorator;
12
-
13
- // CHECK SIGNATURE
14
- const signature: ts.Signature | undefined =
15
- project.checker.getResolvedSignature(decorator.expression);
16
- if (!signature || !signature.declaration) return decorator;
17
-
18
- // CHECK TO BE TRANSFORMED
19
- const done: boolean = (() => {
20
- // CHECK FILENAME
21
- const location: string = path.resolve(
22
- signature.declaration.getSourceFile().fileName,
23
- );
24
- if (location.indexOf(LIB_PATH) === -1 && location !== SRC_PATH)
25
- return false;
26
-
27
- // CHECK DUPLICATED
28
- return decorator.expression.arguments.length !== 3;
29
- })();
30
- if (done === false) return decorator;
31
-
32
- // DO TRANSFORM
33
- return ts.factory.createDecorator(
34
- TypedExceptionProgrammer.generate(project)(decorator.expression),
35
- );
36
- };
37
-
38
- const LIB_PATH = path.join(
39
- "@nestia",
40
- "core",
41
- "lib",
42
- "decorators",
43
- `TypedException.d.ts`,
44
- );
45
- const SRC_PATH = path.resolve(
46
- path.join(__dirname, "..", "decorators", `TypedException.ts`),
47
- );
48
- }
1
+ import path from "path";
2
+ import ts from "typescript";
3
+
4
+ import { INestiaTransformProject } from "../options/INestiaTransformProject";
5
+ import { TypedExceptionProgrammer } from "../programmers/TypedExceptionProgrammer";
6
+
7
+ export namespace TypedExceptionTransformer {
8
+ export const transform =
9
+ (project: INestiaTransformProject) =>
10
+ (decorator: ts.Decorator): ts.Decorator => {
11
+ if (!ts.isCallExpression(decorator.expression)) return decorator;
12
+
13
+ // CHECK SIGNATURE
14
+ const signature: ts.Signature | undefined =
15
+ project.checker.getResolvedSignature(decorator.expression);
16
+ if (!signature || !signature.declaration) return decorator;
17
+
18
+ // CHECK TO BE TRANSFORMED
19
+ const done: boolean = (() => {
20
+ // CHECK FILENAME
21
+ const location: string = path.resolve(
22
+ signature.declaration.getSourceFile().fileName,
23
+ );
24
+ if (location.indexOf(LIB_PATH) === -1 && location !== SRC_PATH)
25
+ return false;
26
+
27
+ // CHECK DUPLICATED
28
+ return decorator.expression.arguments.length !== 3;
29
+ })();
30
+ if (done === false) return decorator;
31
+
32
+ // DO TRANSFORM
33
+ return ts.factory.createDecorator(
34
+ TypedExceptionProgrammer.generate(project)(decorator.expression),
35
+ );
36
+ };
37
+
38
+ const LIB_PATH = path.join(
39
+ "@nestia",
40
+ "core",
41
+ "lib",
42
+ "decorators",
43
+ `TypedException.d.ts`,
44
+ );
45
+ const SRC_PATH = path.resolve(
46
+ path.join(__dirname, "..", "decorators", `TypedException.ts`),
47
+ );
48
+ }