swaggular 0.2.2 → 0.2.4

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.
package/dist/index.js CHANGED
@@ -16,6 +16,7 @@ async function main() {
16
16
  try {
17
17
  const parsed = (0, args_1.getParseArgs)(process.argv.slice(2));
18
18
  const variables = (0, variables_1.toVariables)(parsed);
19
+ console.log('Generating interfaces and services using Swaggular@' + process.env.npm_package_version);
19
20
  swagger_parser_1.SwaggerParser.parse(variables.input, {
20
21
  mode: variables.groupingMode,
21
22
  segmentsToIgnore: variables.segmentsToIgnore,
@@ -154,7 +154,11 @@ function generateComponentsSchemas() {
154
154
  const interData = {
155
155
  name: key,
156
156
  type: 'interface',
157
- imports: properties.filter((p) => !(0, type_guard_1.isNativeType)(p.type)).map((p) => p.type),
157
+ imports: properties
158
+ .filter((p) => !(0, type_guard_1.isNativeType)(p.type))
159
+ .map((p) => {
160
+ return p.type.replaceAll('[]', '');
161
+ }),
158
162
  properties,
159
163
  };
160
164
  const generic = (0, generic_types_1.isGenericType)(interData);
@@ -175,7 +179,7 @@ function generateInterfacesFiles(locations) {
175
179
  const filesContent = [];
176
180
  for (const [key, value] of Object.entries(interfacesData)) {
177
181
  const location = [value.type === 'enum' ? 'enums' : 'dtos', ...(locations?.[key] ?? [])];
178
- const content = generateContent(value, location.length);
182
+ const content = generateContent(value, location.length + 1);
179
183
  const extraName = value.type === 'enum' ? 'enum' : 'dto';
180
184
  filesContent.push({
181
185
  location: location,
@@ -188,9 +192,8 @@ function generateInterfacesFiles(locations) {
188
192
  }
189
193
  function generateContent(interfaceData, deep = 0) {
190
194
  const imports = [...interfaceData.imports];
191
- const importsTemplate = imports.length > 0
192
- ? `import { ${imports.join(', ')} } from "${'../'.repeat(deep)}models";`
193
- : '';
195
+ const path = '../'.repeat(deep);
196
+ const importsTemplate = imports.length > 0 ? `import { ${imports.join(', ')} } from "${path}models";` : '';
194
197
  if (interfaceData.type === 'interface') {
195
198
  const content = `${importsTemplate}\n\nexport interface ${interfaceData.name} ${interfaceData.extendsFrom && interfaceData.extendsFrom.length > 0 ? `extends ${interfaceData.extendsFrom.join(', ')}` : ''} {
196
199
  ${interfaceData.properties
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "swaggular",
3
- "version": "0.2.2",
3
+ "version": "0.2.4",
4
4
  "main": "dist/index.js",
5
5
  "types": "dist/index.d.ts",
6
6
  "bin": {
@@ -1,48 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.getParseArgs = getParseArgs;
4
- function getParseArgs(argv) {
5
- const args = {};
6
- const positional = [];
7
- for (let i = 0; i < argv.length; i++) {
8
- const arg = argv[i];
9
- if (arg.startsWith("--") && arg.includes("=")) {
10
- const [k, v] = arg.slice(2).split("=");
11
- args[k] = v;
12
- continue;
13
- }
14
- if (arg.startsWith("--")) {
15
- const k = arg.slice(2);
16
- const next = argv[i + 1];
17
- if (!next || next.startsWith("-")) {
18
- args[k] = true;
19
- }
20
- else {
21
- args[k] = next;
22
- i++;
23
- }
24
- continue;
25
- }
26
- if (arg.startsWith("-") && arg.length > 2) {
27
- arg
28
- .slice(1)
29
- .split("")
30
- .forEach((k) => (args[k] = true));
31
- continue;
32
- }
33
- if (arg.startsWith("-")) {
34
- const k = arg.slice(1);
35
- const next = argv[i + 1];
36
- if (!next || next.startsWith("-")) {
37
- args[k] = true;
38
- }
39
- else {
40
- args[k] = next;
41
- i++;
42
- }
43
- continue;
44
- }
45
- positional.push(arg);
46
- }
47
- return { args, positional };
48
- }
@@ -1,24 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.toVariables = toVariables;
4
- function toVariables(parsed) {
5
- const variables = {};
6
- if (parsed.args.mode) {
7
- variables.groupingMode = parsed.args.mode;
8
- }
9
- if (parsed.args.segmentsToIgnore) {
10
- if (typeof parsed.args.segmentsToIgnore === "string") {
11
- variables.segmentsToIgnore = parsed.args.segmentsToIgnore.split(",");
12
- }
13
- }
14
- if (parsed.args.ignoreVariables) {
15
- variables.ignoreVariables =
16
- parsed.args.ignoreVariables === "true" ||
17
- parsed.args.ignoreVariables === true;
18
- }
19
- return {
20
- groupingMode: variables.groupingMode || "path",
21
- segmentsToIgnore: variables.segmentsToIgnore || ["api"],
22
- ignoreVariables: variables.ignoreVariables || true,
23
- };
24
- }
@@ -1,25 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.buildComments = buildComments;
4
- function buildComments(param) {
5
- const comments = [];
6
- if (param.format !== undefined) {
7
- comments.push(`format: ${param.format}`);
8
- }
9
- if (param.minLength !== undefined) {
10
- comments.push(`minLength: ${param.minLength}`);
11
- }
12
- if (param.maxLength !== undefined) {
13
- comments.push(`maxLength: ${param.maxLength}`);
14
- }
15
- if (param.minimum !== undefined) {
16
- comments.push(`minimum: ${param.minimum}`);
17
- }
18
- if (param.maximum !== undefined) {
19
- comments.push(`maximum: ${param.maximum}`);
20
- }
21
- if (comments.length > 0) {
22
- return ` /**\n * ${comments.join("\n * ")}\n */`;
23
- }
24
- return "";
25
- }
@@ -1 +0,0 @@
1
- "use strict";
@@ -1,40 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.generateInterfaceComments = generateInterfaceComments;
4
- exports.generateServiceComments = generateServiceComments;
5
- function generateInterfaceComments(param) {
6
- const comments = [];
7
- if (param.format !== undefined) {
8
- comments.push(`format: ${param.format}`);
9
- }
10
- if (param.minLength !== undefined) {
11
- comments.push(`minLength: ${param.minLength}`);
12
- }
13
- if (param.maxLength !== undefined) {
14
- comments.push(`maxLength: ${param.maxLength}`);
15
- }
16
- if (param.minimum !== undefined) {
17
- comments.push(`minimum: ${param.minimum}`);
18
- }
19
- if (param.maximum !== undefined) {
20
- comments.push(`maximum: ${param.maximum}`);
21
- }
22
- if (comments.length > 0) {
23
- return ` /**\n * ${comments.join("\n * ")}\n */`;
24
- }
25
- return "";
26
- }
27
- function generateServiceComments(summary, responseType, parameters) {
28
- const summarySplited = [];
29
- for (let i = 0; i < summary.length; i += 100) {
30
- summarySplited.push(summary.slice(i, i + 100));
31
- }
32
- const comments = summarySplited;
33
- if (parameters && parameters.length > 0) {
34
- comments.push(`${parameters.map((p) => `\t\t @param ${p.name} : ${p.type}`).join("\n")}`);
35
- }
36
- if (responseType) {
37
- comments.push(`\t\t @returns Observable<${responseType}>`);
38
- }
39
- return `/**\n${comments.map((c) => `\t\t* ${c}\n`).join("")}\n */`;
40
- }
@@ -1,219 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.parametersToIProperties = parametersToIProperties;
4
- exports.propertiesToIProperties = propertiesToIProperties;
5
- exports.generateInterfaces = generateInterfaces;
6
- exports.generateWithParameters = generateWithParameters;
7
- exports.computeParametersName = computeParametersName;
8
- exports.generateComponentsSchemas = generateComponentsSchemas;
9
- exports.generateInterfacesFiles = generateInterfacesFiles;
10
- exports.generateContent = generateContent;
11
- const interface_data_store_1 = require("../stores/interface-data-store");
12
- const swagger_store_1 = require("../stores/swagger-store");
13
- const extends_from_types_1 = require("../templates/types/extends-from-types");
14
- const generic_types_1 = require("../templates/types/generic-types");
15
- const build_types_1 = require("../utils/build-types");
16
- const object_utils_1 = require("../utils/object-utils");
17
- const path_utils_1 = require("../utils/path-utils");
18
- const string_utils_1 = require("../utils/string-utils");
19
- const type_guard_1 = require("../utils/type-guard");
20
- const generate_comments_1 = require("./generate-comments");
21
- function parametersToIProperties(parameters) {
22
- const properties = [];
23
- for (const param of parameters) {
24
- if ((0, type_guard_1.isReference)(param)) {
25
- const ref = param.$ref.split("/").pop();
26
- properties.push({
27
- name: (0, string_utils_1.lowerFirst)(ref),
28
- type: ref,
29
- optional: false,
30
- comments: "",
31
- });
32
- continue;
33
- }
34
- if (param.in !== "query")
35
- continue;
36
- const tsType = (0, build_types_1.switchTypeJson)(param.schema);
37
- properties.push({
38
- name: (0, string_utils_1.lowerFirst)(param.name),
39
- type: tsType,
40
- optional: !param.required,
41
- comments: (0, generate_comments_1.generateInterfaceComments)(param.schema),
42
- });
43
- }
44
- return properties;
45
- }
46
- function propertiesToIProperties(properties) {
47
- const interfaceDataProperties = [];
48
- for (const [name, property] of Object.entries(properties)) {
49
- if ((0, type_guard_1.isReference)(property)) {
50
- const ref = property.$ref.split("/").pop();
51
- interfaceDataProperties.push({
52
- name,
53
- type: ref,
54
- optional: false,
55
- comments: "",
56
- });
57
- continue;
58
- }
59
- interfaceDataProperties.push({
60
- name,
61
- type: (0, build_types_1.switchTypeJson)(property),
62
- optional: property.nullable ?? false,
63
- comments: (0, generate_comments_1.generateInterfaceComments)(property),
64
- });
65
- }
66
- return interfaceDataProperties;
67
- }
68
- function generateInterfaces() {
69
- generateComponentsSchemas();
70
- generateWithParameters();
71
- }
72
- function generateWithParameters() {
73
- const paths = swagger_store_1.swaggerStore.getPaths();
74
- const groupedPaths = swagger_store_1.swaggerStore.getPathsGroupedByScope();
75
- if (!paths || !groupedPaths)
76
- return;
77
- const interfacesData = [];
78
- for (const groupedPath of Object.values(groupedPaths)) {
79
- for (const innerPath of groupedPath.paths) {
80
- const pathInfo = paths[innerPath];
81
- if (!pathInfo)
82
- continue;
83
- const methods = (0, object_utils_1.removeFalsyValues)({
84
- get: pathInfo.get,
85
- post: pathInfo.post,
86
- put: pathInfo.put,
87
- delete: pathInfo.delete,
88
- patch: pathInfo.patch,
89
- });
90
- for (const [httpMethod, pathData] of Object.entries(methods)) {
91
- if (!pathData?.parameters)
92
- continue;
93
- const parameters = pathData.parameters;
94
- const properties = parametersToIProperties(parameters);
95
- if (properties.length === 0)
96
- continue;
97
- const interfaceName = computeParametersName(httpMethod, innerPath, groupedPath);
98
- const interData = {
99
- name: interfaceName,
100
- type: "interface",
101
- imports: properties
102
- .filter((p) => !(0, type_guard_1.isNativeType)(p.type))
103
- .map((p) => p.type),
104
- properties,
105
- };
106
- const generic = (0, generic_types_1.isGenericType)(interData);
107
- if (generic)
108
- continue;
109
- const interDataWithExtendsFrom = (0, extends_from_types_1.computeExtendsFromType)(interData);
110
- interfacesData.push(interDataWithExtendsFrom);
111
- }
112
- }
113
- }
114
- interface_data_store_1.interfaceDataStore.addInterfaces(interfacesData);
115
- }
116
- function computeParametersName(method, innerPath, groupedPath) {
117
- const dict = {
118
- get: "",
119
- post: "Create",
120
- put: "Update",
121
- delete: "Delete",
122
- patch: "Patch",
123
- };
124
- const name = dict[method];
125
- const extra = (0, string_utils_1.kebabToPascalCase)((0, path_utils_1.removeVariablesFromPath)((0, path_utils_1.getExtraSegments)(innerPath, (0, path_utils_1.createBaseUrl)(groupedPath.baseSegments)).join("")));
126
- return name + groupedPath.groupName + (0, string_utils_1.upFirst)(extra) + "Params";
127
- }
128
- function generateComponentsSchemas() {
129
- const schemas = swagger_store_1.swaggerStore.getSchemas();
130
- if (!schemas)
131
- return;
132
- const interfaces = [];
133
- for (const [key, value] of Object.entries(schemas)) {
134
- if ((0, type_guard_1.isReference)(value))
135
- continue;
136
- if (!value.properties) {
137
- if (value.enum) {
138
- const interData = {
139
- name: key,
140
- type: "enum",
141
- imports: [],
142
- properties: value.enum.map((e) => {
143
- return {
144
- name: e,
145
- type: e,
146
- optional: false,
147
- comments: "",
148
- };
149
- }),
150
- };
151
- interfaces.push(interData);
152
- }
153
- continue;
154
- }
155
- const properties = propertiesToIProperties(value.properties);
156
- const interData = {
157
- name: key,
158
- type: "interface",
159
- imports: properties
160
- .filter((p) => !(0, type_guard_1.isNativeType)(p.type))
161
- .map((p) => p.type),
162
- properties,
163
- };
164
- const generic = (0, generic_types_1.isGenericType)(interData);
165
- if (generic) {
166
- continue;
167
- }
168
- const interDataWithExtendsFrom = (0, extends_from_types_1.computeExtendsFromType)(interData);
169
- interfaces.push(interDataWithExtendsFrom);
170
- }
171
- interface_data_store_1.interfaceDataStore.addInterfaces(interfaces);
172
- }
173
- function generateInterfacesFiles(locations) {
174
- const interfacesData = [
175
- ...generic_types_1.genericTypesData,
176
- ...extends_from_types_1.extendsFromTypes,
177
- ...Array.from(Object.values(interface_data_store_1.interfaceDataStore.generatedInterfaces)),
178
- ];
179
- const filesContent = [];
180
- for (const [key, value] of Object.entries(interfacesData)) {
181
- const location = [
182
- value.type === "enum" ? "enums" : "dtos",
183
- ...(locations?.[key] ?? []),
184
- ];
185
- const content = generateContent(value, location.length);
186
- const extraName = value.type === "enum" ? "enum" : "dto";
187
- filesContent.push({
188
- location: location,
189
- content,
190
- extension: "ts",
191
- name: (0, string_utils_1.toKebabCase)(value.name) + `.${extraName}`,
192
- });
193
- }
194
- return filesContent;
195
- }
196
- generateContent;
197
- function generateContent(interfaceData, deep = 0) {
198
- const imports = [...interfaceData.imports];
199
- const importsTemplate = imports.length > 0
200
- ? `import { ${imports.join(", ")} } from "${"../".repeat(deep)}models";`
201
- : "";
202
- if (interfaceData.type === "interface") {
203
- const content = `${importsTemplate}\n\nexport interface ${interfaceData.name} ${interfaceData.extendsFrom && interfaceData.extendsFrom.length > 0 ? `extends ${interfaceData.extendsFrom.join(", ")}` : ""} {
204
- ${interfaceData.properties
205
- .map((p) => `${p.comments}\n\t${(0, string_utils_1.lowerFirst)(p.name)}${p.optional ? "?" : ""}: ${p.type};`)
206
- .join("\n")}
207
- }`;
208
- return content;
209
- }
210
- if (interfaceData.type === "enum") {
211
- const content = `export enum ${interfaceData.name} {
212
- ${interfaceData.properties
213
- .map((p) => `${p.comments}\n\t${p.name} = '${p.type}',`)
214
- .join("\n")}
215
- }`;
216
- return content;
217
- }
218
- return "";
219
- }