@necessarylion/adonis-autoswagger 0.0.4 → 0.0.6

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 (40) hide show
  1. package/dist/adonis_helpers.d.ts +21 -0
  2. package/dist/{adonishelpers.js → adonis_helpers.js} +17 -2
  3. package/dist/auto_swagger.d.ts +28 -0
  4. package/dist/{autoswagger.js → auto_swagger.js} +43 -394
  5. package/dist/example.d.ts +47 -0
  6. package/dist/example.js +48 -1
  7. package/dist/file.d.ts +44 -0
  8. package/dist/file.js +93 -0
  9. package/dist/helpers.d.ts +11 -2
  10. package/dist/helpers.js +11 -25
  11. package/dist/index.d.ts +1 -1
  12. package/dist/index.js +2 -2
  13. package/dist/parsers/comment_parser.d.ts +10 -0
  14. package/dist/parsers/comment_parser.js +417 -0
  15. package/dist/parsers/enum_parser.d.ts +5 -0
  16. package/dist/parsers/enum_parser.js +51 -0
  17. package/dist/parsers/helpers.d.ts +13 -0
  18. package/dist/parsers/helpers.js +38 -0
  19. package/dist/parsers/index.d.ts +6 -0
  20. package/dist/parsers/index.js +22 -0
  21. package/dist/parsers/interface_parser.d.ts +13 -0
  22. package/dist/parsers/interface_parser.js +254 -0
  23. package/dist/parsers/model_parser.d.ts +11 -0
  24. package/dist/parsers/model_parser.js +234 -0
  25. package/dist/parsers/route_parser.d.ts +10 -0
  26. package/dist/parsers/route_parser.js +46 -0
  27. package/dist/parsers/validator_parser.d.ts +7 -0
  28. package/dist/parsers/validator_parser.js +192 -0
  29. package/dist/schema.d.ts +46 -0
  30. package/dist/schema.js +266 -0
  31. package/dist/types.d.ts +16 -17
  32. package/dist/ui.d.ts +31 -0
  33. package/dist/ui.js +157 -0
  34. package/package.json +2 -2
  35. package/dist/adonishelpers.d.ts +0 -6
  36. package/dist/autoswagger.d.ts +0 -30
  37. package/dist/parsers.d.ts +0 -83
  38. package/dist/parsers.js +0 -1142
  39. /package/dist/{scalarCustomCss.d.ts → scalar_custom_css.d.ts} +0 -0
  40. /package/dist/{scalarCustomCss.js → scalar_custom_css.js} +0 -0
package/dist/example.d.ts CHANGED
@@ -1,18 +1,65 @@
1
1
  export default class ExampleGenerator {
2
2
  schemas: {};
3
3
  constructor(schemas: any);
4
+ /**
5
+ * Convert json to ref
6
+ * @param json any
7
+ * @returns
8
+ */
4
9
  jsonToRef(json: any): {};
10
+ /**
11
+ * Parse ref
12
+ * @param line string
13
+ * @param exampleOnly boolean
14
+ * @returns
15
+ */
5
16
  parseRef(line: string, exampleOnly?: boolean): {};
17
+ /**
18
+ * Example by validator rule
19
+ * @param rule string
20
+ * @returns
21
+ */
6
22
  exampleByValidatorRule(rule: string): "user@example.com" | "Some string";
23
+ /**
24
+ * Get schema example based on annotation
25
+ * @param schema string
26
+ * @param inc string
27
+ * @param exc string
28
+ * @param onl string
29
+ * @param first string
30
+ * @param parent string
31
+ * @param deepRels string[]
32
+ * @returns
33
+ */
7
34
  getSchemaExampleBasedOnAnnotation(schema: string, inc?: string, exc?: string, onl?: string, first?: string, parent?: string, deepRels?: string[]): any;
35
+ /**
36
+ * Example by type
37
+ * @param type any
38
+ * @returns
39
+ */
8
40
  exampleByType(type: any): any;
41
+ /**
42
+ * Example by field
43
+ * @param field any
44
+ * @param type string
45
+ * @returns
46
+ */
9
47
  exampleByField(field: any, type?: string): any;
48
+ /**
49
+ * Get paginated data
50
+ * @param line string
51
+ * @returns
52
+ */
10
53
  getPaginatedData(line: string): {
11
54
  dataName: string;
12
55
  metaName: string;
13
56
  };
14
57
  }
15
58
  export declare abstract class ExampleInterfaces {
59
+ /**
60
+ * Pagination interface
61
+ * @returns
62
+ */
16
63
  static paginationInterface(): {
17
64
  PaginationMeta: {
18
65
  type: string;
package/dist/example.js CHANGED
@@ -2,12 +2,17 @@
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.ExampleInterfaces = void 0;
4
4
  const lodash_1 = require("lodash");
5
- const helpers_1 = require("./helpers");
5
+ const helpers_1 = require("./parsers/helpers");
6
6
  class ExampleGenerator {
7
7
  schemas = {};
8
8
  constructor(schemas) {
9
9
  this.schemas = schemas;
10
10
  }
11
+ /**
12
+ * Convert json to ref
13
+ * @param json any
14
+ * @returns
15
+ */
11
16
  jsonToRef(json) {
12
17
  const jsonObjectIsArray = Array.isArray(json);
13
18
  let out = {};
@@ -30,6 +35,12 @@ class ExampleGenerator {
30
35
  }
31
36
  return outArr.length > 0 ? outArr.flat() : out;
32
37
  }
38
+ /**
39
+ * Parse ref
40
+ * @param line string
41
+ * @param exampleOnly boolean
42
+ * @returns
43
+ */
33
44
  parseRef(line, exampleOnly = false) {
34
45
  let rawRef = line.substring(line.indexOf("<") + 1, line.lastIndexOf(">"));
35
46
  if (rawRef === "") {
@@ -133,6 +144,11 @@ class ExampleGenerator {
133
144
  },
134
145
  };
135
146
  }
147
+ /**
148
+ * Example by validator rule
149
+ * @param rule string
150
+ * @returns
151
+ */
136
152
  exampleByValidatorRule(rule) {
137
153
  switch (rule) {
138
154
  case "email":
@@ -141,6 +157,17 @@ class ExampleGenerator {
141
157
  return "Some string";
142
158
  }
143
159
  }
160
+ /**
161
+ * Get schema example based on annotation
162
+ * @param schema string
163
+ * @param inc string
164
+ * @param exc string
165
+ * @param onl string
166
+ * @param first string
167
+ * @param parent string
168
+ * @param deepRels string[]
169
+ * @returns
170
+ */
144
171
  getSchemaExampleBasedOnAnnotation(schema, inc = "", exc = "", onl = "", first = "", parent = "", deepRels = [""]) {
145
172
  let props = {};
146
173
  if (!this.schemas[schema]) {
@@ -244,6 +271,11 @@ class ExampleGenerator {
244
271
  }
245
272
  return props;
246
273
  }
274
+ /**
275
+ * Example by type
276
+ * @param type any
277
+ * @returns
278
+ */
247
279
  exampleByType(type) {
248
280
  switch (type) {
249
281
  case "string":
@@ -268,6 +300,12 @@ class ExampleGenerator {
268
300
  return null;
269
301
  }
270
302
  }
303
+ /**
304
+ * Example by field
305
+ * @param field any
306
+ * @param type string
307
+ * @returns
308
+ */
271
309
  exampleByField(field, type = "") {
272
310
  const ex = {
273
311
  datetime: "2021-03-23T16:13:08.489+01:00",
@@ -304,6 +342,11 @@ class ExampleGenerator {
304
342
  }
305
343
  return null;
306
344
  }
345
+ /**
346
+ * Get paginated data
347
+ * @param line string
348
+ * @returns
349
+ */
307
350
  getPaginatedData(line) {
308
351
  const match = line.match(/<.*>\.paginated\((.*)\)/);
309
352
  if (!match) {
@@ -317,6 +360,10 @@ class ExampleGenerator {
317
360
  }
318
361
  exports.default = ExampleGenerator;
319
362
  class ExampleInterfaces {
363
+ /**
364
+ * Pagination interface
365
+ * @returns
366
+ */
320
367
  static paginationInterface() {
321
368
  return {
322
369
  PaginationMeta: {
package/dist/file.d.ts ADDED
@@ -0,0 +1,44 @@
1
+ import type { options } from "./types";
2
+ import { AdonisRoutes } from "./types";
3
+ export declare class FileService {
4
+ /**
5
+ * Convert json to yaml
6
+ * @param json any
7
+ * @returns
8
+ */
9
+ jsonToYaml(json: any): any;
10
+ /**
11
+ * Generate json output
12
+ * @param routes AdonisRoutes
13
+ * @param options options
14
+ * @returns
15
+ */
16
+ json(routes: AdonisRoutes, options: options, generate: (routes: AdonisRoutes, options: options) => Promise<any>): Promise<any>;
17
+ /**
18
+ * Write swagger file
19
+ * @param routes AdonisRoutes
20
+ * @param options options
21
+ */
22
+ writeFile(routes: AdonisRoutes, options: options, generate: (routes: AdonisRoutes, options: options) => Promise<any>): Promise<void>;
23
+ /**
24
+ * Read swagger file
25
+ * @param rootPath string
26
+ * @param type string
27
+ * @returns
28
+ */
29
+ readFile(rootPath: string, type?: string): Promise<string | undefined>;
30
+ /**
31
+ * Generate docs
32
+ * @param routes AdonisRoutes
33
+ * @param options options
34
+ * @returns
35
+ */
36
+ docs(routes: AdonisRoutes, options: options, generate: (routes: AdonisRoutes, options: options) => Promise<any>): Promise<string>;
37
+ /**
38
+ * Get files in directory
39
+ * @param dir string
40
+ * @param files_ string[]
41
+ * @returns
42
+ */
43
+ getFiles(dir: string, files_?: string[]): Promise<string[]>;
44
+ }
package/dist/file.js ADDED
@@ -0,0 +1,93 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.FileService = void 0;
7
+ const json_to_pretty_yaml_1 = __importDefault(require("json-to-pretty-yaml"));
8
+ const fs_1 = __importDefault(require("fs"));
9
+ class FileService {
10
+ /**
11
+ * Convert json to yaml
12
+ * @param json any
13
+ * @returns
14
+ */
15
+ jsonToYaml(json) {
16
+ return json_to_pretty_yaml_1.default.stringify(json);
17
+ }
18
+ /**
19
+ * Generate json output
20
+ * @param routes AdonisRoutes
21
+ * @param options options
22
+ * @returns
23
+ */
24
+ async json(routes, options, generate) {
25
+ if (process.env.NODE_ENV === (options.productionEnv || "production")) {
26
+ const str = await this.readFile(options.path, "json");
27
+ return JSON.parse(str);
28
+ }
29
+ return generate(routes, options);
30
+ }
31
+ /**
32
+ * Write swagger file
33
+ * @param routes AdonisRoutes
34
+ * @param options options
35
+ */
36
+ async writeFile(routes, options, generate) {
37
+ const json = await generate(routes, options);
38
+ const contents = this.jsonToYaml(json);
39
+ const filePath = options.path + "swagger.yml";
40
+ const filePathJson = options.path + "swagger.json";
41
+ fs_1.default.writeFileSync(filePath, contents);
42
+ fs_1.default.writeFileSync(filePathJson, JSON.stringify(json, null, 2));
43
+ }
44
+ /**
45
+ * Read swagger file
46
+ * @param rootPath string
47
+ * @param type string
48
+ * @returns
49
+ */
50
+ async readFile(rootPath, type = "yml") {
51
+ const filePath = rootPath + "swagger." + type;
52
+ const data = fs_1.default.readFileSync(filePath, "utf-8");
53
+ if (!data) {
54
+ console.error("Error reading file");
55
+ return;
56
+ }
57
+ return data;
58
+ }
59
+ /**
60
+ * Generate docs
61
+ * @param routes AdonisRoutes
62
+ * @param options options
63
+ * @returns
64
+ */
65
+ async docs(routes, options, generate) {
66
+ if (process.env.NODE_ENV === (options.productionEnv || "production")) {
67
+ return this.readFile(options.path);
68
+ }
69
+ return this.jsonToYaml(await generate(routes, options));
70
+ }
71
+ /**
72
+ * Get files in directory
73
+ * @param dir string
74
+ * @param files_ string[]
75
+ * @returns
76
+ */
77
+ async getFiles(dir, files_) {
78
+ const fs = require("fs");
79
+ files_ = files_ || [];
80
+ var files = await fs.readdirSync(dir);
81
+ for (let i in files) {
82
+ var name = dir + "/" + files[i];
83
+ if (fs.statSync(name).isDirectory()) {
84
+ await this.getFiles(name, files_);
85
+ }
86
+ else {
87
+ files_.push(name);
88
+ }
89
+ }
90
+ return files_;
91
+ }
92
+ }
93
+ exports.FileService = FileService;
package/dist/helpers.d.ts CHANGED
@@ -1,7 +1,16 @@
1
- export declare function isJSONString(str: string): boolean;
2
- export declare function getBetweenBrackets(value: string, start: string): string;
1
+ /**
2
+ * Merge params
3
+ * @param initial any
4
+ * @param custom any
5
+ * @returns
6
+ */
3
7
  export declare function mergeParams(initial: any, custom: any): any[];
4
8
  /**
5
9
  * Helpers
6
10
  */
11
+ /**
12
+ * Format operation id
13
+ * @param inputString string
14
+ * @returns
15
+ */
7
16
  export declare function formatOperationId(inputString: string): string;
package/dist/helpers.js CHANGED
@@ -1,36 +1,17 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.isJSONString = isJSONString;
4
- exports.getBetweenBrackets = getBetweenBrackets;
5
3
  exports.mergeParams = mergeParams;
6
4
  exports.formatOperationId = formatOperationId;
7
5
  /**
8
6
  * Check if a string is a valid JSON
9
7
  */
10
8
  const lodash_1 = require("lodash");
11
- function isJSONString(str) {
12
- try {
13
- JSON.parse(str);
14
- return true;
15
- }
16
- catch (error) {
17
- return false;
18
- }
19
- }
20
- function getBetweenBrackets(value, start) {
21
- let match = value.match(new RegExp(start + "\\(([^()]*)\\)", "g"));
22
- if (match !== null) {
23
- let m = match[0].replace(start + "(", "").replace(")", "");
24
- if (start !== "example") {
25
- m = m.replace(/ /g, "");
26
- }
27
- if (start === "paginated") {
28
- return "true";
29
- }
30
- return m;
31
- }
32
- return "";
33
- }
9
+ /**
10
+ * Merge params
11
+ * @param initial any
12
+ * @param custom any
13
+ * @returns
14
+ */
34
15
  function mergeParams(initial, custom) {
35
16
  let merge = Object.assign(initial, custom);
36
17
  let params = [];
@@ -42,6 +23,11 @@ function mergeParams(initial, custom) {
42
23
  /**
43
24
  * Helpers
44
25
  */
26
+ /**
27
+ * Format operation id
28
+ * @param inputString string
29
+ * @returns
30
+ */
45
31
  function formatOperationId(inputString) {
46
32
  // Remove non-alphanumeric characters and split the string into words
47
33
  const cleanedWords = inputString.replace(/[^a-zA-Z0-9]/g, " ").split(" ");
package/dist/index.d.ts CHANGED
@@ -1,3 +1,3 @@
1
- import { AutoSwagger } from "./autoswagger";
1
+ import { AutoSwagger } from "./auto_swagger";
2
2
  declare const _default: AutoSwagger;
3
3
  export default _default;
package/dist/index.js CHANGED
@@ -1,4 +1,4 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- const autoswagger_1 = require("./autoswagger");
4
- exports.default = new autoswagger_1.AutoSwagger();
3
+ const auto_swagger_1 = require("./auto_swagger");
4
+ exports.default = new auto_swagger_1.AutoSwagger();
@@ -0,0 +1,10 @@
1
+ import ExampleGenerator from "../example";
2
+ import type { options } from "../types";
3
+ export declare class CommentParser {
4
+ #private;
5
+ private parsedFiles;
6
+ exampleGenerator: ExampleGenerator;
7
+ options: options;
8
+ constructor(options: options);
9
+ getAnnotations(file: string, action: string): Promise<Record<string, any>>;
10
+ }