swagger-typescript-api 10.0.0 → 10.0.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 (56) hide show
  1. package/LICENSE +21 -21
  2. package/README.md +284 -260
  3. package/index.d.ts +7 -1
  4. package/index.js +115 -115
  5. package/package.json +116 -113
  6. package/src/apiConfig.js +30 -30
  7. package/src/common.js +28 -28
  8. package/src/components.js +3 -5
  9. package/src/config.js +4 -0
  10. package/src/constants.js +7 -0
  11. package/src/filePrefix.js +14 -14
  12. package/src/files.js +6 -6
  13. package/src/formatFileContent.js +13 -6
  14. package/src/index.js +271 -270
  15. package/src/logger.js +59 -59
  16. package/src/modelNames.js +78 -78
  17. package/src/modelTypes.js +31 -30
  18. package/src/output.js +165 -166
  19. package/src/prettierOptions.js +23 -23
  20. package/src/render/utils/fmtToJSDocLine.js +10 -10
  21. package/src/render/utils/index.js +31 -23
  22. package/src/render/utils/templateRequire.js +17 -17
  23. package/src/routeNames.js +46 -46
  24. package/src/routes.js +4 -1
  25. package/src/schema.js +87 -64
  26. package/src/swagger.js +4 -1
  27. package/src/templates.js +155 -132
  28. package/src/translators/JavaScript.js +60 -60
  29. package/src/typeFormatters.js +121 -75
  30. package/src/utils/id.js +9 -9
  31. package/src/utils/random.js +14 -14
  32. package/src/utils/resolveName.js +97 -97
  33. package/templates/README.md +17 -13
  34. package/templates/base/README.md +7 -7
  35. package/templates/base/data-contract-jsdoc.ejs +32 -0
  36. package/templates/base/data-contracts.ejs +28 -0
  37. package/templates/base/enum-data-contract.ejs +15 -0
  38. package/templates/base/{http-client.eta → http-client.ejs} +2 -2
  39. package/templates/base/http-clients/{axios-http-client.eta → axios-http-client.ejs} +133 -145
  40. package/templates/base/http-clients/{fetch-http-client.eta → fetch-http-client.ejs} +222 -222
  41. package/templates/base/interface-data-contract.ejs +10 -0
  42. package/templates/base/object-field-jsdoc.ejs +28 -0
  43. package/templates/base/{route-docs.eta → route-docs.ejs} +31 -31
  44. package/templates/base/{route-name.eta → route-name.ejs} +42 -42
  45. package/templates/base/{route-type.eta → route-type.ejs} +21 -21
  46. package/templates/base/type-data-contract.ejs +15 -0
  47. package/templates/default/README.md +6 -6
  48. package/templates/default/{api.eta → api.ejs} +65 -65
  49. package/templates/default/{procedure-call.eta → procedure-call.ejs} +98 -98
  50. package/templates/default/{route-types.eta → route-types.ejs} +28 -28
  51. package/templates/modular/README.md +6 -6
  52. package/templates/modular/{api.eta → api.ejs} +28 -28
  53. package/templates/modular/{procedure-call.eta → procedure-call.ejs} +98 -98
  54. package/templates/modular/{route-types.eta → route-types.ejs} +18 -18
  55. package/CHANGELOG.md +0 -866
  56. package/templates/base/data-contracts.eta +0 -45
package/src/templates.js CHANGED
@@ -1,132 +1,155 @@
1
- const _ = require("lodash");
2
- const Eta = require("eta");
3
- const { getFileContent, pathIsExist } = require("./files");
4
- const { config } = require("./config");
5
- const { resolve } = require("path");
6
- const { logger } = require("./logger");
7
-
8
- /**
9
- * name - project template name,
10
- * fileName - template file name,
11
- */
12
- const TEMPLATE_INFOS = [
13
- { name: "api", fileName: "api.eta" },
14
- { name: "dataContracts", fileName: "data-contracts.eta" },
15
- { name: "httpClient", fileName: "http-client.eta" },
16
- { name: "routeTypes", fileName: "route-types.eta" },
17
- { name: "routeName", fileName: "route-name.eta" },
18
- ];
19
-
20
- const getTemplatePaths = ({ templates, modular }) => {
21
- const baseTemplatesPath = resolve(__dirname, "../templates/base");
22
- const defaultTemplatesPath = resolve(__dirname, "../templates/default");
23
- const modularTemplatesPath = resolve(__dirname, "../templates/modular");
24
- const originalTemplatesPath = modular ? modularTemplatesPath : defaultTemplatesPath;
25
- const customTemplatesPath = templates ? resolve(process.cwd(), templates) : originalTemplatesPath;
26
-
27
- return {
28
- /** `templates/base` */
29
- base: baseTemplatesPath,
30
- /** `templates/default` */
31
- default: defaultTemplatesPath,
32
- /** `templates/modular` */
33
- modular: modularTemplatesPath,
34
- /** usage path if `--templates` option is not set */
35
- original: originalTemplatesPath,
36
- /** custom path to templates (`--templates`) */
37
- custom: customTemplatesPath,
38
- };
39
- };
40
-
41
- const getTemplate = ({ fileName, name, path }) => {
42
- const { templatePaths } = config;
43
-
44
- if (path) {
45
- return getFileContent(path);
46
- }
47
-
48
- if (!fileName) return "";
49
-
50
- const customFullPath = resolve(templatePaths.custom, "./", fileName);
51
- let fileContent = pathIsExist(customFullPath) && getFileContent(customFullPath);
52
-
53
- if (!fileContent) {
54
- const baseFullPath = resolve(templatePaths.base, "./", fileName);
55
- const originalFullPath = resolve(templatePaths.original, "./", fileName);
56
-
57
- if (pathIsExist(baseFullPath)) {
58
- fileContent = getFileContent(baseFullPath);
59
- } else {
60
- logger.warn(
61
- `${_.lowerCase(name)} template not found in ${customFullPath}`,
62
- `\nCode generator will use the default template`,
63
- );
64
- }
65
-
66
- if (pathIsExist(originalFullPath)) {
67
- fileContent = getFileContent(originalFullPath);
68
- }
69
- }
70
-
71
- return fileContent;
72
- };
73
-
74
- const getTemplates = ({ templatePaths }) => {
75
- logger.log(`try to read templates from directory "${templatePaths.custom}"`);
76
-
77
- const templatesMap = _.reduce(
78
- TEMPLATE_INFOS,
79
- (acc, { fileName, name }) => ({
80
- ...acc,
81
- [name]: getTemplate({ fileName, name }),
82
- }),
83
- {},
84
- );
85
-
86
- return templatesMap;
87
- };
88
-
89
- const getTemplateContent = (path) => {
90
- let fixedPath = _.endsWith(path, ".eta") ? path : `${path}.eta`;
91
-
92
- _.keys(config.templatePaths).forEach((key) => {
93
- if (_.startsWith(fixedPath, `@${key}`)) {
94
- fixedPath = resolve(_.replace(fixedPath, `@${key}`, config.templatePaths[key]));
95
- }
96
- });
97
-
98
- if (pathIsExist(fixedPath)) {
99
- return getFileContent(fixedPath);
100
- }
101
-
102
- const customPath = resolve(config.templatePaths.custom, fixedPath);
103
-
104
- if (pathIsExist(customPath)) {
105
- return getFileContent(customPath);
106
- }
107
-
108
- const originalPath = resolve(config.templatePaths.original, fixedPath);
109
-
110
- if (pathIsExist(originalPath)) {
111
- return getFileContent(originalPath);
112
- }
113
-
114
- return "";
115
- };
116
-
117
- const renderTemplate = (template, configuration, options) => {
118
- if (!template) return "";
119
-
120
- return Eta.render(template, configuration, {
121
- async: false,
122
- ...(options || {}),
123
- includeFile: (path, payload) => renderTemplate(getTemplateContent(path), payload),
124
- });
125
- };
126
-
127
- module.exports = {
128
- getTemplate,
129
- getTemplates,
130
- getTemplatePaths,
131
- renderTemplate,
132
- };
1
+ const _ = require("lodash");
2
+ const Eta = require("eta");
3
+ const { getFileContent, pathIsExist } = require("./files");
4
+ const { config } = require("./config");
5
+ const { resolve } = require("path");
6
+ const { logger } = require("./logger");
7
+
8
+ const TEMPLATE_EXTENSIONS = [".eta", ".ejs"];
9
+
10
+ /**
11
+ * name - project template name,
12
+ * fileName - template file name,
13
+ */
14
+ const TEMPLATE_INFOS = [
15
+ { name: "api", fileName: "api" },
16
+ { name: "dataContracts", fileName: "data-contracts" },
17
+ { name: "dataContractJsDoc", fileName: "data-contract-jsdoc" },
18
+ { name: "interfaceDataContract", fileName: "interface-data-contract" },
19
+ { name: "typeDataContract", fileName: "type-data-contract" },
20
+ { name: "enumDataContract", fileName: "enum-data-contract" },
21
+ { name: "objectFieldJsDoc", fileName: "object-field-jsdoc" },
22
+ { name: "httpClient", fileName: "http-client" },
23
+ { name: "routeTypes", fileName: "route-types" },
24
+ { name: "routeName", fileName: "route-name" },
25
+ ];
26
+
27
+ const getTemplatePaths = ({ templates, modular }) => {
28
+ const baseTemplatesPath = resolve(__dirname, "../templates/base");
29
+ const defaultTemplatesPath = resolve(__dirname, "../templates/default");
30
+ const modularTemplatesPath = resolve(__dirname, "../templates/modular");
31
+ const originalTemplatesPath = modular ? modularTemplatesPath : defaultTemplatesPath;
32
+ const customTemplatesPath = templates ? resolve(process.cwd(), templates) : originalTemplatesPath;
33
+
34
+ return {
35
+ /** `templates/base` */
36
+ base: baseTemplatesPath,
37
+ /** `templates/default` */
38
+ default: defaultTemplatesPath,
39
+ /** `templates/modular` */
40
+ modular: modularTemplatesPath,
41
+ /** usage path if `--templates` option is not set */
42
+ original: originalTemplatesPath,
43
+ /** custom path to templates (`--templates`) */
44
+ custom: customTemplatesPath,
45
+ };
46
+ };
47
+
48
+ const cropExtension = (path) =>
49
+ TEMPLATE_EXTENSIONS.reduce((path, ext) => (_.endsWith(path, ext) ? path.replace(ext, "") : path), path);
50
+
51
+ const getTemplateFullPath = (path, fileName) => {
52
+ const raw = resolve(path, "./", cropExtension(fileName));
53
+ const pathVariants = TEMPLATE_EXTENSIONS.map((extension) => `${raw}${extension}`);
54
+
55
+ return pathVariants.find((variant) => !!pathIsExist(variant));
56
+ };
57
+
58
+ const getTemplate = ({ fileName, name, path }) => {
59
+ const { templatePaths } = config;
60
+
61
+ if (path) {
62
+ return getFileContent(path);
63
+ }
64
+
65
+ if (!fileName) return "";
66
+
67
+ const customFullPath = getTemplateFullPath(templatePaths.custom, fileName);
68
+ let fileContent = customFullPath && getFileContent(customFullPath);
69
+
70
+ if (!fileContent) {
71
+ const baseFullPath = getTemplateFullPath(templatePaths.base, fileName);
72
+
73
+ if (baseFullPath) {
74
+ fileContent = getFileContent(baseFullPath);
75
+ } else {
76
+ logger.warn(
77
+ `${_.lowerCase(name)} template not found in ${customFullPath}`,
78
+ `\nCode generator will use the default template`,
79
+ );
80
+ }
81
+
82
+ const originalFullPath = getTemplateFullPath(templatePaths.original, fileName);
83
+
84
+ if (originalFullPath) {
85
+ fileContent = getFileContent(originalFullPath);
86
+ }
87
+ }
88
+
89
+ return fileContent;
90
+ };
91
+
92
+ const getTemplates = ({ templatePaths }) => {
93
+ logger.log(`try to read templates from directory "${templatePaths.custom}"`);
94
+
95
+ const templatesMap = _.reduce(
96
+ TEMPLATE_INFOS,
97
+ (acc, { fileName, name }) => ({
98
+ ...acc,
99
+ [name]: getTemplate({ fileName, name }),
100
+ }),
101
+ {},
102
+ );
103
+
104
+ return templatesMap;
105
+ };
106
+
107
+ const getTemplateContent = (path) => {
108
+ const foundTemplatePathKey = _.keys(config.templatePaths).find((key) => _.startsWith(path, `@${key}`));
109
+
110
+ const findPathWithExt = (path) => {
111
+ const raw = cropExtension(path);
112
+ const pathVariants = TEMPLATE_EXTENSIONS.map((extension) => `${raw}${extension}`);
113
+ return pathVariants.find((variant) => pathIsExist(variant));
114
+ };
115
+
116
+ const rawPath = resolve(_.replace(path, `@${foundTemplatePathKey}`, config.templatePaths[foundTemplatePathKey]));
117
+ const fixedPath = findPathWithExt(rawPath);
118
+
119
+ if (fixedPath) {
120
+ return getFileContent(fixedPath);
121
+ }
122
+
123
+ const customPath = findPathWithExt(resolve(config.templatePaths.custom, path));
124
+
125
+ if (customPath) {
126
+ return getFileContent(customPath);
127
+ }
128
+
129
+ const originalPath = findPathWithExt(resolve(config.templatePaths.original, path));
130
+
131
+ if (originalPath) {
132
+ return getFileContent(originalPath);
133
+ }
134
+
135
+ return "";
136
+ };
137
+
138
+ const renderTemplate = (template, configuration, options) => {
139
+ if (!template) return "";
140
+
141
+ return Eta.render(template, configuration, {
142
+ async: false,
143
+ ...(options || {}),
144
+ includeFile: (path, payload, options) => {
145
+ return renderTemplate(getTemplateContent(path), payload, options);
146
+ },
147
+ });
148
+ };
149
+
150
+ module.exports = {
151
+ getTemplate,
152
+ getTemplates,
153
+ getTemplatePaths,
154
+ renderTemplate,
155
+ };
@@ -1,60 +1,60 @@
1
- const ts = require("typescript");
2
-
3
- function translate(fileName, content, options) {
4
- const output = {};
5
- const host = ts.createCompilerHost(options, true);
6
- const fileNames = [fileName];
7
- const originalSourceFileGet = host.getSourceFile.bind(host);
8
- host.getSourceFile = (sourceFileName, languageVersion, onError, shouldCreateNewSourceFile) => {
9
- if (sourceFileName !== fileName)
10
- return originalSourceFileGet(
11
- sourceFileName,
12
- languageVersion,
13
- onError,
14
- shouldCreateNewSourceFile,
15
- );
16
-
17
- return ts.createSourceFile(
18
- sourceFileName,
19
- content,
20
- languageVersion,
21
- true,
22
- ts.ScriptKind.External,
23
- );
24
- };
25
-
26
- host.writeFile = (fileName, contents) => {
27
- output[fileName] = contents;
28
- };
29
-
30
- ts.createProgram(fileNames, options, host).emit();
31
-
32
- return output;
33
- }
34
-
35
- module.exports = {
36
- translate: (fileName, sourceTypeScript) => {
37
- const translated = translate(fileName, sourceTypeScript, {
38
- module: "ESNext",
39
- noImplicitReturns: true,
40
- alwaysStrict: true,
41
- target: ts.ScriptTarget.ESNext,
42
- declaration: true,
43
- noImplicitAny: false,
44
- sourceMap: false,
45
- removeComments: false,
46
- disableSizeLimit: true,
47
- esModuleInterop: true,
48
- emitDecoratorMetadata: true,
49
- skipLibCheck: true,
50
- });
51
-
52
- const sourceFileName = fileName.replace(ts.Extension.Ts, ts.Extension.Js);
53
- const declarationFileName = fileName.replace(ts.Extension.Ts, ts.Extension.Dts);
54
-
55
- return {
56
- sourceContent: translated[sourceFileName],
57
- declarationContent: translated[declarationFileName],
58
- };
59
- },
60
- };
1
+ const ts = require("typescript");
2
+
3
+ function translate(fileName, content, options) {
4
+ const output = {};
5
+ const host = ts.createCompilerHost(options, true);
6
+ const fileNames = [fileName];
7
+ const originalSourceFileGet = host.getSourceFile.bind(host);
8
+ host.getSourceFile = (sourceFileName, languageVersion, onError, shouldCreateNewSourceFile) => {
9
+ if (sourceFileName !== fileName)
10
+ return originalSourceFileGet(
11
+ sourceFileName,
12
+ languageVersion,
13
+ onError,
14
+ shouldCreateNewSourceFile,
15
+ );
16
+
17
+ return ts.createSourceFile(
18
+ sourceFileName,
19
+ content,
20
+ languageVersion,
21
+ true,
22
+ ts.ScriptKind.External,
23
+ );
24
+ };
25
+
26
+ host.writeFile = (fileName, contents) => {
27
+ output[fileName] = contents;
28
+ };
29
+
30
+ ts.createProgram(fileNames, options, host).emit();
31
+
32
+ return output;
33
+ }
34
+
35
+ module.exports = {
36
+ translate: (fileName, sourceTypeScript) => {
37
+ const translated = translate(fileName, sourceTypeScript, {
38
+ module: "ESNext",
39
+ noImplicitReturns: true,
40
+ alwaysStrict: true,
41
+ target: ts.ScriptTarget.ESNext,
42
+ declaration: true,
43
+ noImplicitAny: false,
44
+ sourceMap: false,
45
+ removeComments: false,
46
+ disableSizeLimit: true,
47
+ esModuleInterop: true,
48
+ emitDecoratorMetadata: true,
49
+ skipLibCheck: true,
50
+ });
51
+
52
+ const sourceFileName = fileName.replace(ts.Extension.Ts, ts.Extension.Js);
53
+ const declarationFileName = fileName.replace(ts.Extension.Ts, ts.Extension.Dts);
54
+
55
+ return {
56
+ sourceContent: translated[sourceFileName],
57
+ declarationContent: translated[declarationFileName],
58
+ };
59
+ },
60
+ };
@@ -1,75 +1,121 @@
1
- const _ = require("lodash");
2
- const { config } = require("./config");
3
- const { TS_KEYWORDS, SCHEMA_TYPES } = require("./constants");
4
-
5
- const formatters = {
6
- [SCHEMA_TYPES.ENUM]: (content) => {
7
- const isNumberEnum = _.some(content, (content) => typeof content.key === "number");
8
- const formatAsUnionType = !!(isNumberEnum || config.generateUnionEnums);
9
-
10
- if (formatAsUnionType) {
11
- return _.map(content, ({ value }) => value).join(" | ");
12
- }
13
-
14
- return _.map(content, ({ key, value }) => ` ${key} = ${value}`).join(",\n");
15
- },
16
- [SCHEMA_TYPES.OBJECT]: (content) =>
17
- _.map(content, (part) => {
18
- const extraSpace = " ";
19
- const result = `${extraSpace}${part.field};\n`;
20
-
21
- const comments = _.uniq(_.compact([part.title, part.description]).reduce(
22
- (acc, comment) => [...acc, ...comment.split(/\n/g)],
23
- [],
24
- ));
25
-
26
- const commonText = comments.length
27
- ? [
28
- "",
29
- ...(comments.length === 1
30
- ? [`/** ${comments[0]} */`]
31
- : ["/**", ...comments.map((commentPart) => ` * ${commentPart}`), " */"]),
32
- ]
33
- .map((part) => `${extraSpace}${part}\n`)
34
- .join("")
35
- : "";
36
-
37
- return `${commonText}${result}`;
38
- }).join(""),
39
- [SCHEMA_TYPES.PRIMITIVE]: (content) => {
40
- return content;
41
- },
42
- };
43
-
44
- /** transform content of parsed schema to string or compact size */
45
- const inlineExtraFormatters = {
46
- [SCHEMA_TYPES.OBJECT]: (parsedSchema) => {
47
- return {
48
- ...parsedSchema,
49
- typeIdentifier: TS_KEYWORDS.TYPE,
50
- content: _.isString(parsedSchema.content)
51
- ? parsedSchema.content
52
- : parsedSchema.content.length
53
- ? `{ ${parsedSchema.content.map((part) => part.field).join(", ")} }`
54
- : TS_KEYWORDS.OBJECT,
55
- };
56
- },
57
- [SCHEMA_TYPES.ENUM]: (parsedSchema) => {
58
- return {
59
- ...parsedSchema,
60
- content: parsedSchema.$ref
61
- ? parsedSchema.typeName
62
- : _.uniq(
63
- _.compact([
64
- ..._.map(parsedSchema.content, ({ value }) => `${value}`),
65
- parsedSchema.nullable && TS_KEYWORDS.NULL,
66
- ]),
67
- ).join(" | "),
68
- };
69
- },
70
- };
71
-
72
- module.exports = {
73
- formatters,
74
- inlineExtraFormatters,
75
- };
1
+ const _ = require("lodash");
2
+ const { config } = require("./config");
3
+ const { TS_KEYWORDS, SCHEMA_TYPES, TS_EXTERNAL } = require("./constants");
4
+
5
+ const checkAndAddNull = (schema, value) => {
6
+ const { nullable, type } = schema || {};
7
+ return (nullable || !!_.get(schema, "x-nullable") || type === TS_KEYWORDS.NULL) &&
8
+ _.isString(value) &&
9
+ !value.includes(` ${TS_KEYWORDS.NULL}`) &&
10
+ !value.includes(`${TS_KEYWORDS.NULL} `)
11
+ ? `${value} | ${TS_KEYWORDS.NULL}`
12
+ : value;
13
+ };
14
+
15
+ const formatters = {
16
+ [SCHEMA_TYPES.ENUM]: (parsedSchema) => {
17
+ const isNumberEnum = _.some(parsedSchema.content, (content) => typeof content.key === "number");
18
+ const formatAsUnionType = !!(isNumberEnum || config.generateUnionEnums);
19
+
20
+ if (formatAsUnionType) {
21
+ return {
22
+ ...parsedSchema,
23
+ $content: parsedSchema.content,
24
+ content: _.map(parsedSchema.content, ({ value }) => value).join(" | "),
25
+ };
26
+ }
27
+
28
+ return {
29
+ ...parsedSchema,
30
+ $content: parsedSchema.content,
31
+ content: _.map(parsedSchema.content, ({ key, value }) => ` ${key} = ${value}`).join(",\n"),
32
+ };
33
+ },
34
+ [SCHEMA_TYPES.OBJECT]: (parsedSchema) => {
35
+ if (parsedSchema.nullable) return inlineExtraFormatters[SCHEMA_TYPES.OBJECT](parsedSchema);
36
+ return {
37
+ ...parsedSchema,
38
+ $content: parsedSchema.content,
39
+ content: formatObjectContent(parsedSchema.content),
40
+ };
41
+ },
42
+ [SCHEMA_TYPES.PRIMITIVE]: (parsedSchema) => {
43
+ return {
44
+ ...parsedSchema,
45
+ $content: parsedSchema.content,
46
+ };
47
+ },
48
+ };
49
+
50
+ /** transform content of parsed schema to string or compact size */
51
+ const inlineExtraFormatters = {
52
+ [SCHEMA_TYPES.OBJECT]: (parsedSchema) => {
53
+ if (_.isString(parsedSchema.content)) {
54
+ return {
55
+ ...parsedSchema,
56
+ typeIdentifier: TS_KEYWORDS.TYPE,
57
+ content: checkAndAddNull(parsedSchema.content),
58
+ };
59
+ }
60
+
61
+ return {
62
+ ...parsedSchema,
63
+ typeIdentifier: TS_KEYWORDS.TYPE,
64
+ content: checkAndAddNull(
65
+ parsedSchema,
66
+ parsedSchema.content.length ? `{\n${formatObjectContent(parsedSchema.content)}\n}` : TS_EXTERNAL.RECORD,
67
+ ),
68
+ };
69
+ },
70
+ [SCHEMA_TYPES.ENUM]: (parsedSchema) => {
71
+ return {
72
+ ...parsedSchema,
73
+ content: parsedSchema.$ref
74
+ ? parsedSchema.typeName
75
+ : _.uniq(
76
+ _.compact([
77
+ ..._.map(parsedSchema.content, ({ value }) => `${value}`),
78
+ parsedSchema.nullable && TS_KEYWORDS.NULL,
79
+ ]),
80
+ ).join(" | "),
81
+ };
82
+ },
83
+ };
84
+
85
+ const formatObjectContent = (content) => {
86
+ return _.map(content, (part) => {
87
+ const extraSpace = " ";
88
+ const result = `${extraSpace}${part.field},\n`;
89
+
90
+ const comments = _.uniq(
91
+ _.compact([
92
+ part.title,
93
+ part.description,
94
+ part.deprecated && ` * @deprecated`,
95
+ !_.isUndefined(part.format) && `@format ${part.format}`,
96
+ !_.isUndefined(part.minimum) && `@min ${part.minimum}`,
97
+ !_.isUndefined(part.maximum) && `@max ${part.maximum}`,
98
+ !_.isUndefined(part.pattern) && `@pattern ${part.pattern}`,
99
+ !_.isUndefined(part.example) &&
100
+ `@example ${_.isObject(part.example) ? JSON.stringify(part.example) : part.example}`,
101
+ ]).reduce((acc, comment) => [...acc, ...comment.split(/\n/g)], []),
102
+ );
103
+
104
+ const commonText = comments.length
105
+ ? [
106
+ ...(comments.length === 1
107
+ ? [`/** ${comments[0]} */`]
108
+ : ["/**", ...comments.map((commentPart) => ` * ${commentPart}`), " */"]),
109
+ ]
110
+ .map((part) => `${extraSpace}${part}\n`)
111
+ .join("")
112
+ : "";
113
+
114
+ return `${commonText}${result}`;
115
+ }).join("");
116
+ };
117
+
118
+ module.exports = {
119
+ formatters,
120
+ inlineExtraFormatters,
121
+ };
package/src/utils/id.js CHANGED
@@ -1,9 +1,9 @@
1
- const { customAlphabet } = require("nanoid");
2
-
3
- const ALPHABET = "abcdefghijklmnopqrstuvwxyz0123456789";
4
-
5
- const generateId = customAlphabet(ALPHABET, 12);
6
-
7
- module.exports = {
8
- generateId,
9
- };
1
+ const { customAlphabet } = require("nanoid");
2
+
3
+ const ALPHABET = "abcdefghijklmnopqrstuvwxyz0123456789";
4
+
5
+ const generateId = customAlphabet(ALPHABET, 12);
6
+
7
+ module.exports = {
8
+ generateId,
9
+ };
@@ -1,14 +1,14 @@
1
- const getRandomFloat = (min = 0, max = 1) => {
2
- return Math.random() * (max - min) + min;
3
- };
4
-
5
- const getRandomInt = (min = 0, max = 1) => {
6
- if (min === max) return min;
7
-
8
- return Math.round(getRandomFloat(min, max));
9
- };
10
-
11
- module.exports = {
12
- getRandomFloat,
13
- getRandomInt,
14
- };
1
+ const getRandomFloat = (min = 0, max = 1) => {
2
+ return Math.random() * (max - min) + min;
3
+ };
4
+
5
+ const getRandomInt = (min = 0, max = 1) => {
6
+ if (min === max) return min;
7
+
8
+ return Math.round(getRandomFloat(min, max));
9
+ };
10
+
11
+ module.exports = {
12
+ getRandomFloat,
13
+ getRandomInt,
14
+ };