swagger-typescript-api 10.0.2 → 10.0.3

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.
@@ -1,121 +1,121 @@
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
- };
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
+ };
@@ -1,97 +1,94 @@
1
- const _ = require("lodash");
2
- const { getRandomInt } = require("./random");
3
-
4
- class NameResolver {
5
- reservedNames = [];
6
- getDefaultName = null;
7
-
8
- /**
9
- *
10
- * @param {string[]} reservedNames
11
- */
12
- constructor(reservedNames, getDefaultName) {
13
- this.getDefaultName = getDefaultName;
14
- this.reserve(reservedNames);
15
- }
16
-
17
- /**
18
- *
19
- * @param {string[]} names
20
- */
21
- reserve(names) {
22
- this.reservedNames.push(..._.uniq(_.compact(names)));
23
- }
24
-
25
- unreserve(names) {
26
- this.reservedNames.filter((reservedName) => !names.some((name) => name === reservedName));
27
- }
28
-
29
- isReserved(name) {
30
- return _.some(this.reservedNames, (reservedName) => reservedName === name);
31
- }
32
-
33
- /**
34
- *
35
- * @param {string[]} variants
36
- * @param {((variant: string) => string) | undefined} onSelectMutation
37
- * @returns {string | null}
38
- */
39
- resolve(variants, onSelectMutation) {
40
- let usageName = null;
41
- const uniqVariants = _.uniq(_.compact(variants));
42
-
43
- _.forEach(uniqVariants, (variant) => {
44
- const mutatedVariant = onSelectMutation ? onSelectMutation(variant) : variant;
45
- if (!usageName && !this.isReserved(mutatedVariant)) {
46
- usageName = mutatedVariant;
47
- }
48
- });
49
-
50
- if (usageName) {
51
- this.reserve([usageName]);
52
- return usageName;
53
- }
54
-
55
- const defaultName = this.getDefaultName && this.getDefaultName(variants);
56
-
57
- if (defaultName) {
58
- this.reserve([onSelectMutation ? onSelectMutation(defaultName) : defaultName]);
59
- return defaultName;
60
- }
61
-
62
- return null;
63
- }
64
- }
65
-
66
- class SpecificArgNameResolver extends NameResolver {
67
- /**
68
- *
69
- * @param {string[]} reservedNames
70
- */
71
- constructor(reservedNames) {
72
- super(reservedNames, (variants) => {
73
- return (variants[0] && `${variants[0]}${getRandomInt(1, 10)}`) || `arg${getRandomInt(1, 10)}`;
74
- });
75
- }
76
- }
77
-
78
- class ComponentTypeNameResolver extends NameResolver {
79
- /**
80
- *
81
- * @param {string[]} reservedNames
82
- */
83
- constructor(reservedNames) {
84
- super(reservedNames, (variants) => {
85
- return (
86
- (variants[0] && `${variants[0]}${getRandomInt(1, 10)}`) ||
87
- `ComponentType${getRandomInt(1, 10)}`
88
- );
89
- });
90
- }
91
- }
92
-
93
- module.exports = {
94
- NameResolver,
95
- SpecificArgNameResolver,
96
- ComponentTypeNameResolver,
97
- };
1
+ const _ = require("lodash");
2
+ const { getRandomInt } = require("./random");
3
+
4
+ class NameResolver {
5
+ reservedNames = [];
6
+ getDefaultName = null;
7
+
8
+ /**
9
+ *
10
+ * @param {string[]} reservedNames
11
+ */
12
+ constructor(reservedNames, getDefaultName) {
13
+ this.getDefaultName = getDefaultName;
14
+ this.reserve(reservedNames);
15
+ }
16
+
17
+ /**
18
+ *
19
+ * @param {string[]} names
20
+ */
21
+ reserve(names) {
22
+ this.reservedNames.push(..._.uniq(_.compact(names)));
23
+ }
24
+
25
+ unreserve(names) {
26
+ this.reservedNames.filter((reservedName) => !names.some((name) => name === reservedName));
27
+ }
28
+
29
+ isReserved(name) {
30
+ return _.some(this.reservedNames, (reservedName) => reservedName === name);
31
+ }
32
+
33
+ /**
34
+ *
35
+ * @param {string[]} variants
36
+ * @param {((variant: string) => string) | undefined} onSelectMutation
37
+ * @returns {string | null}
38
+ */
39
+ resolve(variants, onSelectMutation) {
40
+ let usageName = null;
41
+ const uniqVariants = _.uniq(_.compact(variants));
42
+
43
+ _.forEach(uniqVariants, (variant) => {
44
+ const mutatedVariant = onSelectMutation ? onSelectMutation(variant) : variant;
45
+ if (!usageName && !this.isReserved(mutatedVariant)) {
46
+ usageName = mutatedVariant;
47
+ }
48
+ });
49
+
50
+ if (usageName) {
51
+ this.reserve([usageName]);
52
+ return usageName;
53
+ }
54
+
55
+ const defaultName = this.getDefaultName && this.getDefaultName(variants);
56
+
57
+ if (defaultName) {
58
+ this.reserve([onSelectMutation ? onSelectMutation(defaultName) : defaultName]);
59
+ return defaultName;
60
+ }
61
+
62
+ return null;
63
+ }
64
+ }
65
+
66
+ class SpecificArgNameResolver extends NameResolver {
67
+ /**
68
+ *
69
+ * @param {string[]} reservedNames
70
+ */
71
+ constructor(reservedNames) {
72
+ super(reservedNames, (variants) => {
73
+ return (variants[0] && `${variants[0]}${getRandomInt(1, 10)}`) || `arg${getRandomInt(1, 10)}`;
74
+ });
75
+ }
76
+ }
77
+
78
+ class ComponentTypeNameResolver extends NameResolver {
79
+ /**
80
+ *
81
+ * @param {string[]} reservedNames
82
+ */
83
+ constructor(reservedNames) {
84
+ super(reservedNames, (variants) => {
85
+ return (variants[0] && `${variants[0]}${getRandomInt(1, 10)}`) || `ComponentType${getRandomInt(1, 10)}`;
86
+ });
87
+ }
88
+ }
89
+
90
+ module.exports = {
91
+ NameResolver,
92
+ SpecificArgNameResolver,
93
+ ComponentTypeNameResolver,
94
+ };
@@ -21,8 +21,7 @@ const jsDocLines = _.compact([
21
21
  ` * @response \`${status}\` \`${_.replace(_.replace(type, /\/\*/g, "\\*"), /\*\//g, "*\\")}\` ${description}`,
22
22
  )
23
23
  : []),
24
- ]).join("\n");
25
-
24
+ ]).map(str => str.trimEnd()).join("\n");
26
25
 
27
26
  return {
28
27
  description: jsDocDescription,
@@ -81,7 +81,7 @@ const describeReturnType = () => {
81
81
  /**
82
82
  <%~ routeDocs.description %>
83
83
 
84
- * <% /* Here you can add some other JSDoc tags */ %>
84
+ *<% /* Here you can add some other JSDoc tags */ %>
85
85
 
86
86
  <%~ routeDocs.lines %>
87
87
 
@@ -96,4 +96,4 @@ const describeReturnType = () => {
96
96
  <%~ bodyContentKindTmpl ? `type: ${bodyContentKindTmpl},` : '' %>
97
97
  <%~ responseFormatTmpl ? `format: ${responseFormatTmpl},` : '' %>
98
98
  ...<%~ _.get(requestConfigParam, "name") %>,
99
- })<%~ route.namespace ? ',' : '' %>
99
+ })<%~ route.namespace ? ',' : '' %>
@@ -81,7 +81,7 @@ const describeReturnType = () => {
81
81
  /**
82
82
  <%~ routeDocs.description %>
83
83
 
84
- * <% /* Here you can add some other JSDoc tags */ %>
84
+ *<% /* Here you can add some other JSDoc tags */ %>
85
85
 
86
86
  <%~ routeDocs.lines %>
87
87
 
@@ -96,4 +96,4 @@ const describeReturnType = () => {
96
96
  <%~ bodyContentKindTmpl ? `type: ${bodyContentKindTmpl},` : '' %>
97
97
  <%~ responseFormatTmpl ? `format: ${responseFormatTmpl},` : '' %>
98
98
  ...<%~ _.get(requestConfigParam, "name") %>,
99
- })
99
+ })