@nestia/sdk 1.4.12 → 1.4.13-dev.20230726-2

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 (43) hide show
  1. package/lib/INestiaConfig.d.ts +6 -15
  2. package/lib/NestiaSdkApplication.js +28 -0
  3. package/lib/NestiaSdkApplication.js.map +1 -1
  4. package/lib/analyses/ControllerAnalyzer.js +21 -2
  5. package/lib/analyses/ControllerAnalyzer.js.map +1 -1
  6. package/lib/analyses/ReflectAnalyzer.d.ts +1 -0
  7. package/lib/analyses/ReflectAnalyzer.js +8 -0
  8. package/lib/analyses/ReflectAnalyzer.js.map +1 -1
  9. package/lib/analyses/SecurityAnalyzer.d.ts +3 -0
  10. package/lib/analyses/SecurityAnalyzer.js +25 -0
  11. package/lib/analyses/SecurityAnalyzer.js.map +1 -0
  12. package/lib/executable/internal/NestiaSdkConfig.js +67 -67
  13. package/lib/executable/internal/NestiaSdkConfig.js.map +1 -1
  14. package/lib/generates/SdkGenerator.js +0 -28
  15. package/lib/generates/SdkGenerator.js.map +1 -1
  16. package/lib/generates/SwaggerGenerator.js +42 -0
  17. package/lib/generates/SwaggerGenerator.js.map +1 -1
  18. package/lib/structures/IController.d.ts +2 -0
  19. package/lib/structures/IRoute.d.ts +1 -0
  20. package/lib/structures/ISwaggerComponents.d.ts +26 -0
  21. package/lib/structures/ISwaggerComponents.js +3 -0
  22. package/lib/structures/ISwaggerComponents.js.map +1 -0
  23. package/lib/structures/ISwaggerDocument.d.ts +70 -86
  24. package/lib/structures/ISwaggerRoute.d.ts +43 -0
  25. package/lib/structures/ISwaggerRoute.js +3 -0
  26. package/lib/structures/ISwaggerRoute.js.map +1 -0
  27. package/lib/structures/ISwaggerSecurityScheme.d.ts +50 -0
  28. package/lib/structures/ISwaggerSecurityScheme.js +3 -0
  29. package/lib/structures/ISwaggerSecurityScheme.js.map +1 -0
  30. package/package.json +4 -3
  31. package/src/INestiaConfig.ts +6 -22
  32. package/src/NestiaSdkApplication.ts +29 -0
  33. package/src/analyses/ControllerAnalyzer.ts +24 -1
  34. package/src/analyses/ReflectAnalyzer.ts +10 -2
  35. package/src/analyses/SecurityAnalyzer.ts +20 -0
  36. package/src/generates/SdkGenerator.ts +0 -29
  37. package/src/generates/SwaggerGenerator.ts +90 -10
  38. package/src/structures/IController.ts +2 -0
  39. package/src/structures/IRoute.ts +1 -0
  40. package/src/structures/ISwaggerComponents.ts +29 -0
  41. package/src/structures/ISwaggerDocument.ts +77 -105
  42. package/src/structures/ISwaggerRoute.ts +47 -0
  43. package/src/structures/ISwaggerSecurityScheme.ts +57 -0
@@ -1,102 +1,86 @@
1
- import { IJsonComponents, IJsonSchema } from "typia";
2
- import { IJsDocTagInfo } from "typia/lib/metadata/IJsDocTagInfo";
1
+ import { ISwaggerComponents } from "./ISwaggerComponents";
2
+ import { ISwaggerRoute } from "./ISwaggerRoute";
3
+ /**
4
+ * Swagger Document.
5
+ *
6
+ * `ISwaggerDocument` is a data structure representing content of `swagger.json` file
7
+ * generated by Nestia. Note that, this is not an universal structure, but a dedicated
8
+ * structure only for Nestia.
9
+ *
10
+ * @author Jeongho Nam - https://github.com/samchon
11
+ */
3
12
  export interface ISwaggerDocument {
4
- openapi: "3.0.1";
13
+ /**
14
+ * The version of the OpenAPI document.
15
+ *
16
+ * Nestia always generate OpenAPI 3.0.x document.
17
+ */
18
+ openapi: `3.0.${number}`;
19
+ /**
20
+ * List of servers that provide the API.
21
+ */
5
22
  servers: ISwaggerDocument.IServer[];
23
+ /**
24
+ * Information about the API.
25
+ */
6
26
  info: ISwaggerDocument.IInfo;
7
- components: ISwaggerDocument.IComponents;
27
+ /**
28
+ * The available paths and operations for the API.
29
+ *
30
+ * The 1st key is the path, and the 2nd key is the HTTP method.
31
+ */
32
+ paths: Record<string, Record<string, ISwaggerRoute>>;
33
+ /**
34
+ * An object to hold reusable data structures.
35
+ *
36
+ * It stores both DTO schemas and security schemes.
37
+ *
38
+ * For reference, `nestia` defines every object and alias types as reusable DTO
39
+ * schemas. The alias type means that defined by `type` keyword in TypeScript.
40
+ */
41
+ components: ISwaggerComponents;
42
+ /**
43
+ * A declaration of which security mechanisms can be used across the API.
44
+ *
45
+ * When this property be configured, it would be overwritten in every API routes.
46
+ *
47
+ * For reference, key means the name of security scheme and value means the `scopes`.
48
+ * The `scopes` can be used only when target security scheme is `oauth2` type,
49
+ * especially for {@link ISwaggerSecurityScheme.IOAuth2.IFlow.scopes} property.
50
+ */
8
51
  security?: Record<string, string[]>[];
9
- paths: Record<string, ISwaggerDocument.IPath>;
10
52
  }
11
53
  export declare namespace ISwaggerDocument {
54
+ /**
55
+ * Remote server definition.
56
+ */
12
57
  interface IServer {
58
+ /**
59
+ * A URL to the target host.
60
+ *
61
+ * @format url
62
+ */
13
63
  url: string;
64
+ /**
65
+ * An optional string describing the target server.
66
+ */
14
67
  description?: string;
15
68
  }
69
+ /**
70
+ * General information about the API.
71
+ */
16
72
  interface IInfo {
73
+ /**
74
+ * Version of the API.
75
+ */
17
76
  version: string;
77
+ /**
78
+ * The title of the API.
79
+ */
18
80
  title: string;
81
+ /**
82
+ * A short description of the API.
83
+ */
19
84
  description?: string;
20
85
  }
21
- interface IComponents extends IJsonComponents {
22
- securitySchemes?: Record<string, ISecurityScheme>;
23
- }
24
- type ISecurityScheme = ISecurityScheme.IHttpBasic | ISecurityScheme.IHttpBearer | ISecurityScheme.IApiKey | ISecurityScheme.IOpenId | ISecurityScheme.IOAuth2;
25
- namespace ISecurityScheme {
26
- interface IHttpBasic {
27
- type: "http";
28
- schema: "basic";
29
- }
30
- interface IHttpBearer {
31
- type: "http";
32
- scheme: "bearer";
33
- bearerFormat?: string;
34
- }
35
- interface IApiKey {
36
- type: "apiKey";
37
- in: "header" | "query" | "cookie";
38
- name: string;
39
- }
40
- interface IOpenId {
41
- type: "openIdConnect";
42
- openIdConnectUrl: string;
43
- }
44
- interface IOAuth2 {
45
- type: "oauth2";
46
- flows: IOAuth2.IFlowSet;
47
- description?: string;
48
- }
49
- namespace IOAuth2 {
50
- interface IFlowSet {
51
- authorizationCode?: IFlow;
52
- implicit?: Omit<IFlow, "tokenUrl">;
53
- password?: Omit<IFlow, "authorizationUrl">;
54
- clientCredentials?: Omit<IFlow, "authorizationUrl">;
55
- }
56
- interface IFlow {
57
- authorizationUrl: string;
58
- tokenUrl: string;
59
- refreshUrl: string;
60
- scopes?: Record<string, string>;
61
- }
62
- }
63
- }
64
- type IPath = Record<string, IRoute>;
65
- interface IRoute {
66
- deprecated?: boolean;
67
- tags: string[];
68
- parameters: IParameter[];
69
- requestBody?: IRequestBody;
70
- responses: IResponseBody;
71
- summary?: string;
72
- description?: string;
73
- "x-nestia-namespace": string;
74
- "x-nestia-jsDocTags": IJsDocTagInfo[];
75
- }
76
- interface IParameter {
77
- name: string;
78
- in: string;
79
- schema: IJsonSchema;
80
- required: boolean;
81
- description: string;
82
- }
83
- interface IRequestBody {
84
- description: string;
85
- content: IJsonContent;
86
- required: true;
87
- "x-nestia-encrypted": boolean;
88
- }
89
- type IResponseBody = Record<string, {
90
- description: string;
91
- content?: IJsonContent;
92
- "x-nestia-encrypted"?: boolean;
93
- }>;
94
- interface IJsonContent {
95
- "application/json"?: {
96
- schema: IJsonSchema;
97
- };
98
- "text/plain"?: {
99
- schema: IJsonSchema;
100
- };
101
- }
102
86
  }
@@ -0,0 +1,43 @@
1
+ import { IJsonSchema } from "typia";
2
+ import { IJsDocTagInfo } from "typia/lib/metadata/IJsDocTagInfo";
3
+ export interface ISwaggerRoute {
4
+ deprecated?: boolean;
5
+ security?: Record<string, string[]>[];
6
+ tags: string[];
7
+ parameters: ISwaggerRoute.IParameter[];
8
+ requestBody?: ISwaggerRoute.IRequestBody;
9
+ responses: ISwaggerRoute.IResponseBody;
10
+ summary?: string;
11
+ description?: string;
12
+ "x-nestia-method": string;
13
+ "x-nestia-namespace": string;
14
+ "x-nestia-jsDocTags": IJsDocTagInfo[];
15
+ }
16
+ export declare namespace ISwaggerRoute {
17
+ interface IParameter {
18
+ name: string;
19
+ in: string;
20
+ schema: IJsonSchema;
21
+ required: boolean;
22
+ description: string;
23
+ }
24
+ interface IRequestBody {
25
+ description: string;
26
+ content: IContent;
27
+ required: true;
28
+ "x-nestia-encrypted": boolean;
29
+ }
30
+ type IResponseBody = Record<string, {
31
+ description: string;
32
+ content?: IContent;
33
+ "x-nestia-encrypted"?: boolean;
34
+ }>;
35
+ interface IContent {
36
+ "application/json"?: {
37
+ schema: IJsonSchema;
38
+ };
39
+ "text/plain"?: {
40
+ schema: IJsonSchema;
41
+ };
42
+ }
43
+ }
@@ -0,0 +1,3 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ //# sourceMappingURL=ISwaggerRoute.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"ISwaggerRoute.js","sourceRoot":"","sources":["../../src/structures/ISwaggerRoute.ts"],"names":[],"mappings":""}
@@ -0,0 +1,50 @@
1
+ /**
2
+ * Security schema 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 = ISwaggerSecurityScheme.IHttpBasic | ISwaggerSecurityScheme.IHttpBearer | ISwaggerSecurityScheme.IApiKey | ISwaggerSecurityScheme.IOpenId | ISwaggerSecurityScheme.IOAuth2;
12
+ export declare namespace ISwaggerSecurityScheme {
13
+ interface IHttpBasic {
14
+ type: "http";
15
+ schema: "basic";
16
+ }
17
+ interface IHttpBearer {
18
+ type: "http";
19
+ scheme: "bearer";
20
+ bearerFormat?: string;
21
+ }
22
+ interface IApiKey {
23
+ type: "apiKey";
24
+ in?: "header" | "query" | "cookie";
25
+ name?: string;
26
+ }
27
+ interface IOpenId {
28
+ type: "openIdConnect";
29
+ openIdConnectUrl: string;
30
+ }
31
+ interface IOAuth2 {
32
+ type: "oauth2";
33
+ flows: IOAuth2.IFlowSet;
34
+ description?: string;
35
+ }
36
+ namespace IOAuth2 {
37
+ interface IFlowSet {
38
+ authorizationCode?: IFlow;
39
+ implicit?: Omit<IFlow, "tokenUrl">;
40
+ password?: Omit<IFlow, "authorizationUrl">;
41
+ clientCredentials?: Omit<IFlow, "authorizationUrl">;
42
+ }
43
+ interface IFlow {
44
+ authorizationUrl: string;
45
+ tokenUrl: string;
46
+ refreshUrl: string;
47
+ scopes?: Record<string, string>;
48
+ }
49
+ }
50
+ }
@@ -0,0 +1,3 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ //# sourceMappingURL=ISwaggerSecurityScheme.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"ISwaggerSecurityScheme.js","sourceRoot":"","sources":["../../src/structures/ISwaggerSecurityScheme.ts"],"names":[],"mappings":""}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nestia/sdk",
3
- "version": "1.4.12",
3
+ "version": "1.4.13-dev.20230726-2",
4
4
  "description": "Nestia SDK and Swagger generator",
5
5
  "main": "lib/index.js",
6
6
  "typings": "lib/index.d.ts",
@@ -35,15 +35,16 @@
35
35
  },
36
36
  "homepage": "https://nestia.io",
37
37
  "dependencies": {
38
- "@nestia/fetcher": "^1.4.0",
38
+ "@nestia/fetcher": ">= 1.4.0",
39
39
  "cli": "^1.0.1",
40
40
  "glob": "^7.2.0",
41
41
  "path-to-regexp": "^6.2.1",
42
+ "reflect-metadata": ">= 0.1.12",
42
43
  "tgrid": "^0.8.7",
43
44
  "tsconfck": "^2.0.1",
44
45
  "tsconfig-paths": "^4.1.1",
45
46
  "tstl": "^2.5.13",
46
- "typia": "^4.1.6"
47
+ "typia": "^4.1.8"
47
48
  },
48
49
  "peerDependencies": {
49
50
  "@nestia/fetcher": ">= 1.4.0",
@@ -1,6 +1,7 @@
1
1
  import type ts from "typescript";
2
2
 
3
3
  import type { ISwaggerDocument } from "./structures/ISwaggerDocument";
4
+ import { ISwaggerSecurityScheme } from "./structures/ISwaggerSecurityScheme";
4
5
  import type { StripEnums } from "./utils/StripEnums";
5
6
 
6
7
  /**
@@ -178,28 +179,11 @@ export namespace INestiaConfig {
178
179
 
179
180
  /**
180
181
  * Security schemes.
182
+ *
183
+ * When generating `swagger.json` file through `nestia`, if your controllers or
184
+ * theirs methods have a security key which is not enrolled in here property,
185
+ * it would be an error.
181
186
  */
182
- security?: Record<string, ISwaggerConfig.ISecurityScheme>;
183
- }
184
- export namespace ISwaggerConfig {
185
- export type ISecurityScheme =
186
- | IApiKey
187
- | Exclude<
188
- ISwaggerDocument.ISecurityScheme,
189
- ISwaggerDocument.ISecurityScheme.IApiKey
190
- >;
191
- export interface IApiKey {
192
- type: "apiKey";
193
-
194
- /**
195
- * @default header
196
- */
197
- in?: "header" | "query" | "cookie";
198
-
199
- /**
200
- * @default Authorization
201
- */
202
- name?: string;
203
- }
187
+ security?: Record<string, ISwaggerSecurityScheme>;
204
188
  }
205
189
  }
@@ -210,6 +210,18 @@ export class NestiaSdkApplication {
210
210
  routeList.push(...ControllerAnalyzer.analyze(checker, file, c));
211
211
  }
212
212
 
213
+ // FIND IMPLICIT TYPES
214
+ const implicit: IRoute[] = routeList.filter(is_implicit_return_typed);
215
+ if (implicit.length > 0)
216
+ throw new Error(
217
+ `NestiaApplication.${method}(): implicit return type is not allowed.\n` +
218
+ "\n" +
219
+ "List of implicit return typed routes:\n" +
220
+ implicit
221
+ .map((it) => ` - ${it.symbol} at "${it.location}"`)
222
+ .join("\n"),
223
+ );
224
+
213
225
  // DO GENERATE
214
226
  AccessorAnalyzer.analyze(routeList);
215
227
  await archiver(checker)(config(this.config_))(routeList);
@@ -273,3 +285,20 @@ const title = (str: string): void => {
273
285
  console.log(` ${str}`);
274
286
  console.log("-----------------------------------------------------------");
275
287
  };
288
+
289
+ const is_implicit_return_typed = (route: IRoute): boolean => {
290
+ const name: string = route.output.name;
291
+ if (name === "void") return false;
292
+ else if (name.indexOf("readonly [") !== -1) return true;
293
+
294
+ const pos: number = name.indexOf("__object");
295
+ if (pos === -1) return false;
296
+
297
+ const before: number = pos - 1;
298
+ const after: number = pos + "__object".length;
299
+ for (const i of [before, after])
300
+ if (name[i] === undefined) continue;
301
+ else if (VARIABLE.test(name[i])) return false;
302
+ return true;
303
+ };
304
+ const VARIABLE = /[a-zA-Z_$0-9]/;
@@ -10,6 +10,7 @@ import { PathUtil } from "../utils/PathUtil";
10
10
  import { GenericAnalyzer } from "./GenericAnalyzer";
11
11
  import { ImportAnalyzer } from "./ImportAnalyzer";
12
12
  import { PathAnalyzer } from "./PathAnalyzer";
13
+ import { SecurityAnalyzer } from "./SecurityAnalyzer";
13
14
 
14
15
  export namespace ControllerAnalyzer {
15
16
  export function analyze(
@@ -134,8 +135,29 @@ export namespace ControllerAnalyzer {
134
135
  .toJSON()
135
136
  .map((pair) => [pair.first, pair.second.toJSON()]);
136
137
 
137
- // CONSTRUCT COMMON DATA
138
+ // PARSE COMMENT TAGS
138
139
  const tags = signature.getJsDocTags();
140
+ const security: Record<string, string[]>[] = SecurityAnalyzer.merge(
141
+ ...controller.security,
142
+ ...func.security,
143
+ ...tags
144
+ .filter((tag) => tag.name === "security")
145
+ .map((tag) =>
146
+ (tag.text ?? []).map((text) => {
147
+ const line: string[] = text.text
148
+ .split(" ")
149
+ .filter((s) => s.trim())
150
+ .filter((s) => !!s.length);
151
+ if (line.length === 0) return {};
152
+ return {
153
+ [line[0]]: line.slice(1),
154
+ };
155
+ }),
156
+ )
157
+ .flat(),
158
+ );
159
+
160
+ // CONSTRUCT COMMON DATA
139
161
  const common: Omit<IRoute, "path" | "accessors"> = {
140
162
  ...func,
141
163
  parameters,
@@ -168,6 +190,7 @@ export namespace ControllerAnalyzer {
168
190
  source: t.text![0].text,
169
191
  },
170
192
  ),
193
+ security,
171
194
  };
172
195
 
173
196
  // CONFIGURE PATHS
@@ -1,12 +1,12 @@
1
1
  import * as Constants from "@nestjs/common/constants";
2
+ import "reflect-metadata";
2
3
  import { equal } from "tstl/ranges/module";
3
4
 
4
5
  import { IController } from "../structures/IController";
5
6
  import { ParamCategory } from "../structures/ParamCategory";
6
7
  import { ArrayUtil } from "../utils/ArrayUtil";
7
8
  import { PathAnalyzer } from "./PathAnalyzer";
8
-
9
- declare const Reflect: any;
9
+ import { SecurityAnalyzer } from "./SecurityAnalyzer";
10
10
 
11
11
  type IModule = {
12
12
  [key: string]: any;
@@ -89,6 +89,7 @@ export namespace ReflectAnalyzer {
89
89
  name,
90
90
  paths,
91
91
  functions: [],
92
+ security: _Get_security(creator),
92
93
  };
93
94
 
94
95
  // PARSE CHILDREN DATA
@@ -125,6 +126,12 @@ export namespace ReflectAnalyzer {
125
126
  else return value;
126
127
  }
127
128
 
129
+ function _Get_security(value: any): Record<string, string[]>[] {
130
+ const entire: Record<string, string[]>[] | undefined =
131
+ Reflect.getMetadata("swagger/apiSecurity", value);
132
+ return entire ? SecurityAnalyzer.merge(...entire) : [];
133
+ }
134
+
128
135
  /* ---------------------------------------------------------
129
136
  FUNCTION
130
137
  --------------------------------------------------------- */
@@ -173,6 +180,7 @@ export namespace ReflectAnalyzer {
173
180
  typeof h?.value === "string" &&
174
181
  h.name.toLowerCase() === "content-type",
175
182
  )?.value ?? "application/json",
183
+ security: _Get_security(proto),
176
184
  };
177
185
 
178
186
  // PARSE CHILDREN DATA
@@ -0,0 +1,20 @@
1
+ import { MapUtil } from "../utils/MapUtil";
2
+
3
+ export namespace SecurityAnalyzer {
4
+ export const merge = (...entire: Record<string, string[]>[]) => {
5
+ const dict: Map<string, Set<string>> = new Map();
6
+ for (const obj of entire)
7
+ for (const [key, value] of Object.entries(obj)) {
8
+ const set = MapUtil.take(dict, key, () => new Set());
9
+ for (const val of value) set.add(val);
10
+ }
11
+ const output: Record<string, string[]>[] = [];
12
+ for (const [key, set] of dict) {
13
+ const obj = {
14
+ [key]: [...set],
15
+ };
16
+ output.push(obj);
17
+ }
18
+ return output;
19
+ };
20
+ }
@@ -13,18 +13,6 @@ export namespace SdkGenerator {
13
13
  async (routes: IRoute[]): Promise<void> => {
14
14
  console.log("Generating SDK Library");
15
15
 
16
- // FIND IMPLICIT TYPES
17
- const implicit: IRoute[] = routes.filter(is_implicit_return_typed);
18
- if (implicit.length > 0)
19
- throw new Error(
20
- "NestiaApplication.sdk(): implicit return type is not allowed.\n" +
21
- "\n" +
22
- "List of implicit return typed routes:\n" +
23
- implicit
24
- .map((it) => ` - ${it.symbol} at "${it.location}"`)
25
- .join("\n"),
26
- );
27
-
28
16
  // PREPARE NEW DIRECTORIES
29
17
  try {
30
18
  await fs.promises.mkdir(config.output!);
@@ -85,23 +73,6 @@ export namespace SdkGenerator {
85
73
  );
86
74
  }
87
75
 
88
- const is_implicit_return_typed = (route: IRoute): boolean => {
89
- const name: string = route.output.name;
90
- if (name === "void") return false;
91
- else if (name.indexOf("readonly [") !== -1) return true;
92
-
93
- const pos: number = name.indexOf("__object");
94
- if (pos === -1) return false;
95
-
96
- const before: number = pos - 1;
97
- const after: number = pos + "__object".length;
98
- for (const i of [before, after])
99
- if (name[i] === undefined) continue;
100
- else if (VARIABLE.test(name[i])) return false;
101
- return true;
102
- };
103
- const VARIABLE = /[a-zA-Z_$0-9]/;
104
-
105
76
  const BUNDLE_CHANGES: Record<string, [string, string][]> = {
106
77
  "IConnection.ts": [
107
78
  [