swagger-typescript-api 13.0.9 → 13.0.11

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 (69) hide show
  1. package/README.md +3 -3
  2. package/dist/chunk-3OGOYA5P.cjs +104 -0
  3. package/dist/chunk-3OGOYA5P.cjs.map +1 -0
  4. package/dist/chunk-R4CYI34W.js +65 -0
  5. package/dist/chunk-R4CYI34W.js.map +1 -0
  6. package/dist/cli.cjs +43 -0
  7. package/dist/cli.cjs.map +1 -0
  8. package/dist/cli.js +36 -0
  9. package/dist/cli.js.map +1 -0
  10. package/dist/lib.cjs +21 -0
  11. package/dist/lib.cjs.map +1 -0
  12. package/dist/lib.js +4 -0
  13. package/dist/lib.js.map +1 -0
  14. package/index.d.ts +1 -1
  15. package/package.json +25 -14
  16. package/templates/base/http-clients/axios-http-client.ejs +3 -0
  17. package/templates/base/http-clients/fetch-http-client.ejs +3 -3
  18. package/cli/constants.js +0 -6
  19. package/cli/execute.js +0 -180
  20. package/cli/index.js +0 -94
  21. package/cli/operations/display-help.js +0 -177
  22. package/cli/operations/display-version.js +0 -5
  23. package/cli/parse-args.js +0 -24
  24. package/cli/process-option.js +0 -75
  25. package/index.js +0 -344
  26. package/src/code-formatter.js +0 -114
  27. package/src/code-gen-process.js +0 -569
  28. package/src/commands/generate-templates/configuration.js +0 -31
  29. package/src/commands/generate-templates/index.js +0 -10
  30. package/src/commands/generate-templates/templates-gen-process.js +0 -205
  31. package/src/component-type-name-resolver.js +0 -42
  32. package/src/configuration.js +0 -445
  33. package/src/constants.js +0 -77
  34. package/src/index.js +0 -16
  35. package/src/schema-components-map.js +0 -76
  36. package/src/schema-parser/base-schema-parsers/array.js +0 -41
  37. package/src/schema-parser/base-schema-parsers/complex.js +0 -49
  38. package/src/schema-parser/base-schema-parsers/discriminator.js +0 -305
  39. package/src/schema-parser/base-schema-parsers/enum.js +0 -156
  40. package/src/schema-parser/base-schema-parsers/object.js +0 -103
  41. package/src/schema-parser/base-schema-parsers/primitive.js +0 -61
  42. package/src/schema-parser/complex-schema-parsers/all-of.js +0 -26
  43. package/src/schema-parser/complex-schema-parsers/any-of.js +0 -27
  44. package/src/schema-parser/complex-schema-parsers/not.js +0 -9
  45. package/src/schema-parser/complex-schema-parsers/one-of.js +0 -27
  46. package/src/schema-parser/mono-schema-parser.js +0 -46
  47. package/src/schema-parser/schema-formatters.js +0 -164
  48. package/src/schema-parser/schema-parser-fabric.js +0 -130
  49. package/src/schema-parser/schema-parser.js +0 -295
  50. package/src/schema-parser/schema-utils.js +0 -319
  51. package/src/schema-parser/util/enum-key-resolver.js +0 -24
  52. package/src/schema-routes/schema-routes.js +0 -1208
  53. package/src/schema-routes/util/specific-arg-name-resolver.js +0 -24
  54. package/src/schema-walker.js +0 -91
  55. package/src/swagger-schema-resolver.js +0 -195
  56. package/src/templates-worker.js +0 -243
  57. package/src/translators/javascript.js +0 -81
  58. package/src/translators/translator.js +0 -33
  59. package/src/type-name-formatter.js +0 -111
  60. package/src/util/file-system.js +0 -95
  61. package/src/util/id.js +0 -7
  62. package/src/util/internal-case.js +0 -7
  63. package/src/util/logger.js +0 -142
  64. package/src/util/name-resolver.js +0 -103
  65. package/src/util/object-assign.js +0 -17
  66. package/src/util/pascal-case.js +0 -7
  67. package/src/util/random.js +0 -11
  68. package/src/util/request.js +0 -63
  69. package/src/util/sort-by-property.js +0 -15
@@ -1,569 +0,0 @@
1
- import _ from "lodash";
2
- import ts from "typescript";
3
- import { CodeFormatter } from "./code-formatter.js";
4
- import { CodeGenConfig } from "./configuration.js";
5
- import { SchemaComponentsMap } from "./schema-components-map.js";
6
- import { SchemaParserFabric } from "./schema-parser/schema-parser-fabric.js";
7
- import { SchemaRoutes } from "./schema-routes/schema-routes.js";
8
- import { SchemaWalker } from "./schema-walker.js";
9
- import { SwaggerSchemaResolver } from "./swagger-schema-resolver.js";
10
- import { TemplatesWorker } from "./templates-worker.js";
11
- import { JavascriptTranslator } from "./translators/javascript.js";
12
- import { TypeNameFormatter } from "./type-name-formatter.js";
13
- import { FileSystem } from "./util/file-system.js";
14
- import { internalCase } from "./util/internal-case.js";
15
- import { Logger } from "./util/logger.js";
16
- import { NameResolver } from "./util/name-resolver.js";
17
- import { pascalCase } from "./util/pascal-case.js";
18
- import { sortByProperty } from "./util/sort-by-property.js";
19
-
20
- const PATCHABLE_INSTANCES = [
21
- "schemaWalker",
22
- "swaggerSchemaResolver",
23
- "schemaComponentsMap",
24
- "typeNameFormatter",
25
- "templatesWorker",
26
- "codeFormatter",
27
- "schemaParserFabric",
28
- "schemaRoutes",
29
- "javascriptTranslator",
30
- ];
31
-
32
- class CodeGenProcess {
33
- /** @type {CodeGenConfig} */
34
- config;
35
- /** @type {SwaggerSchemaResolver} */
36
- swaggerSchemaResolver;
37
- /** @type {SchemaComponentsMap} */
38
- schemaComponentsMap;
39
- /** @type {Logger} */
40
- logger;
41
- /** @type {TypeNameFormatter} */
42
- typeNameFormatter;
43
- /** @type {SchemaParserFabric} */
44
- schemaParserFabric;
45
- /** @type {SchemaRoutes} */
46
- schemaRoutes;
47
- /** @type {FileSystem} */
48
- fileSystem;
49
- /** @type {CodeFormatter} */
50
- codeFormatter;
51
- /** type {TemplatesWorker} */
52
- templatesWorker;
53
- /** @type {SchemaWalker} */
54
- schemaWalker;
55
- /** @type {JavascriptTranslator} */
56
- javascriptTranslator;
57
-
58
- /**
59
- *
60
- * @param config {Partial<import("../index.d.ts").GenerateApiConfiguration['config']>}
61
- */
62
- constructor(config) {
63
- this.config = new CodeGenConfig(config);
64
- this.logger = new Logger(this);
65
- this.fileSystem = new FileSystem(this);
66
- this.schemaWalker = new SchemaWalker(this);
67
- this.swaggerSchemaResolver = new SwaggerSchemaResolver(this);
68
- this.schemaComponentsMap = new SchemaComponentsMap(this);
69
- this.typeNameFormatter = new TypeNameFormatter(this);
70
- this.templatesWorker = new TemplatesWorker(this);
71
- this.codeFormatter = new CodeFormatter(this);
72
- this.schemaParserFabric = new SchemaParserFabric(this);
73
- this.schemaRoutes = new SchemaRoutes(this);
74
- this.javascriptTranslator = new JavascriptTranslator(this);
75
- this.config.componentTypeNameResolver.logger = this.logger;
76
- }
77
-
78
- async start() {
79
- this.config.update({
80
- templatePaths: this.templatesWorker.getTemplatePaths(this.config),
81
- });
82
- this.config.update({
83
- templatesToRender: this.templatesWorker.getTemplates(this.config),
84
- });
85
-
86
- const swagger = await this.swaggerSchemaResolver.create();
87
-
88
- this.swaggerSchemaResolver.fixSwaggerSchema(swagger);
89
-
90
- this.config.update({
91
- swaggerSchema: swagger.usageSchema,
92
- originalSchema: swagger.originalSchema,
93
- });
94
-
95
- this.schemaWalker.addSchema("$usage", swagger.usageSchema);
96
- this.schemaWalker.addSchema("$original", swagger.originalSchema);
97
-
98
- this.logger.event("start generating your typescript api");
99
-
100
- this.config.update(
101
- this.config.hooks.onInit(this.config, this) || this.config,
102
- );
103
-
104
- this.schemaComponentsMap.clear();
105
-
106
- _.each(swagger.usageSchema.components, (component, componentName) =>
107
- _.each(component, (rawTypeData, typeName) => {
108
- this.schemaComponentsMap.createComponent(
109
- this.schemaComponentsMap.createRef([
110
- "components",
111
- componentName,
112
- typeName,
113
- ]),
114
- rawTypeData,
115
- );
116
- }),
117
- );
118
-
119
- /**
120
- * @type {SchemaComponent[]}
121
- */
122
- const componentsToParse = this.schemaComponentsMap.filter(
123
- _.compact(["schemas", this.config.extractResponses && "responses"]),
124
- );
125
-
126
- const parsedSchemas = componentsToParse.map((schemaComponent) => {
127
- const parsed = this.schemaParserFabric.parseSchema(
128
- schemaComponent.rawTypeData,
129
- schemaComponent.typeName,
130
- );
131
- schemaComponent.typeData = parsed;
132
- return parsed;
133
- });
134
-
135
- this.schemaRoutes.attachSchema({
136
- usageSchema: swagger.usageSchema,
137
- parsedSchemas,
138
- });
139
-
140
- const rawConfiguration = {
141
- apiConfig: this.createApiConfig(swagger.usageSchema),
142
- config: this.config,
143
- modelTypes: this.collectModelTypes(),
144
- hasSecurityRoutes: this.schemaRoutes.hasSecurityRoutes,
145
- hasQueryRoutes: this.schemaRoutes.hasQueryRoutes,
146
- hasFormDataRoutes: this.schemaRoutes.hasFormDataRoutes,
147
- generateResponses: this.config.generateResponses,
148
- routes: this.schemaRoutes.getGroupedRoutes(),
149
- extraTemplates: this.config.extraTemplates,
150
- fileName: this.config.fileName,
151
- translateToJavaScript: this.config.toJS,
152
- customTranslator: this.config.customTranslator
153
- ? new this.config.customTranslator(this)
154
- : null,
155
- utils: this.getRenderTemplateData().utils,
156
- };
157
-
158
- const configuration =
159
- this.config.hooks.onPrepareConfig(rawConfiguration) || rawConfiguration;
160
-
161
- if (this.fileSystem.pathIsExist(this.config.output)) {
162
- if (this.config.cleanOutput) {
163
- this.logger.debug(`cleaning dir ${this.config.output}`);
164
- this.fileSystem.cleanDir(this.config.output);
165
- }
166
- } else {
167
- this.logger.debug(
168
- `path ${this.config.output} is not exist. creating dir by this path`,
169
- );
170
- this.fileSystem.createDir(this.config.output);
171
- }
172
-
173
- const files = await this.generateOutputFiles({
174
- configuration: configuration,
175
- });
176
-
177
- const isDirPath = this.fileSystem.pathIsDir(this.config.output);
178
-
179
- if (isDirPath) {
180
- files.forEach((file) => {
181
- this.fileSystem.createFile({
182
- path: this.config.output,
183
- fileName: `${file.fileName}${file.fileExtension}`,
184
- content: file.fileContent,
185
- withPrefix: true,
186
- });
187
-
188
- this.logger.success(
189
- "api file",
190
- `"${file.fileName}${file.fileExtension}"`,
191
- `created in ${this.config.output}`,
192
- );
193
- });
194
- }
195
-
196
- return {
197
- files,
198
- configuration,
199
- getTemplate: this.templatesWorker.getTemplate,
200
- renderTemplate: this.templatesWorker.renderTemplate,
201
- createFile: this.fileSystem.createFile,
202
- formatTSContent: this.codeFormatter.formatCode,
203
- };
204
- }
205
-
206
- getRenderTemplateData = () => {
207
- return {
208
- utils: {
209
- Ts: this.config.Ts,
210
- formatDescription:
211
- this.schemaParserFabric.schemaFormatters.formatDescription,
212
- internalCase: internalCase,
213
- classNameCase: pascalCase,
214
- pascalCase: pascalCase,
215
- getInlineParseContent: this.schemaParserFabric.getInlineParseContent,
216
- getParseContent: this.schemaParserFabric.getParseContent,
217
- getComponentByRef: this.schemaComponentsMap.get,
218
- parseSchema: this.schemaParserFabric.parseSchema,
219
- checkAndAddNull: this.schemaParserFabric.schemaUtils.safeAddNullToType,
220
- safeAddNullToType:
221
- this.schemaParserFabric.schemaUtils.safeAddNullToType,
222
- isNeedToAddNull:
223
- this.schemaParserFabric.schemaUtils.isNullMissingInType,
224
- inlineExtraFormatters: this.schemaParserFabric.schemaFormatters.inline,
225
- formatters: this.schemaParserFabric.schemaFormatters.base,
226
- formatModelName: this.typeNameFormatter.format,
227
- fmtToJSDocLine: function fmtToJSDocLine(line, { eol = true }) {
228
- return ` * ${line}${eol ? "\n" : ""}`;
229
- },
230
- NameResolver: NameResolver,
231
- _,
232
- require: this.templatesWorker.requireFnFromTemplate,
233
- },
234
- config: this.config,
235
- };
236
- };
237
-
238
- collectModelTypes = () => {
239
- const components = this.schemaComponentsMap.getComponents();
240
- let modelTypes = [];
241
-
242
- const modelTypeComponents = _.compact([
243
- "schemas",
244
- this.config.extractResponses && "responses",
245
- ]);
246
-
247
- const getSchemaComponentsCount = () =>
248
- this.schemaComponentsMap.filter(...modelTypeComponents).length;
249
-
250
- let schemaComponentsCount = getSchemaComponentsCount();
251
- let processedCount = 0;
252
-
253
- while (processedCount < schemaComponentsCount) {
254
- modelTypes = [];
255
- processedCount = 0;
256
- for (const component of components) {
257
- if (modelTypeComponents.includes(component.componentName)) {
258
- const modelType = this.prepareModelType(component);
259
- if (modelType) {
260
- modelTypes.push(modelType);
261
- }
262
- processedCount++;
263
- }
264
- }
265
- schemaComponentsCount = getSchemaComponentsCount();
266
- }
267
-
268
- if (this.config.sortTypes) {
269
- return modelTypes.sort(sortByProperty("name"));
270
- }
271
-
272
- return modelTypes;
273
- };
274
-
275
- prepareModelType = (typeInfo) => {
276
- if (typeInfo.$prepared) return typeInfo.$prepared;
277
-
278
- if (!typeInfo.typeData) {
279
- typeInfo.typeData = this.schemaParserFabric.parseSchema(
280
- typeInfo.rawTypeData,
281
- typeInfo.typeName,
282
- );
283
- }
284
- const rawTypeData = typeInfo.typeData;
285
- const typeData = this.schemaParserFabric.schemaFormatters.base[
286
- rawTypeData.type
287
- ]
288
- ? this.schemaParserFabric.schemaFormatters.base[rawTypeData.type](
289
- rawTypeData,
290
- )
291
- : rawTypeData;
292
- let { typeIdentifier, name: originalName, content, description } = typeData;
293
- const name = this.typeNameFormatter.format(originalName);
294
-
295
- if (name === null) return null;
296
-
297
- const preparedModelType = {
298
- ...typeData,
299
- typeIdentifier,
300
- name,
301
- description,
302
- $content: rawTypeData.content,
303
- rawContent: rawTypeData.content,
304
- content: content,
305
- typeData,
306
- };
307
-
308
- typeInfo.$prepared = preparedModelType;
309
-
310
- return preparedModelType;
311
- };
312
-
313
- /**
314
- *
315
- * @param configuration
316
- * @returns {Promise<TranslatorIO[]>}
317
- */
318
- generateOutputFiles = async ({ configuration }) => {
319
- const { modular, templatesToRender } = this.config;
320
-
321
- const output = modular
322
- ? await this.createMultipleFileInfos(templatesToRender, configuration)
323
- : await this.createSingleFileInfo(templatesToRender, configuration);
324
-
325
- if (!_.isEmpty(configuration.extraTemplates)) {
326
- for (const extraTemplate of configuration.extraTemplates) {
327
- const content = this.templatesWorker.renderTemplate(
328
- this.fileSystem.getFileContent(extraTemplate.path),
329
- configuration,
330
- );
331
- output.push(
332
- ...(await this.createOutputFileInfo(
333
- configuration,
334
- extraTemplate.name,
335
- content,
336
- )),
337
- );
338
- }
339
- }
340
-
341
- return output.filter((fileInfo) => !!fileInfo && !!fileInfo.fileContent);
342
- };
343
-
344
- /**
345
- * @param templatesToRender
346
- * @param configuration
347
- * @returns {Promise<TranslatorIO[]>}
348
- */
349
- createMultipleFileInfos = async (templatesToRender, configuration) => {
350
- const { routes } = configuration;
351
- const { fileNames, generateRouteTypes, generateClient } =
352
- configuration.config;
353
- /**
354
- * @type {TranslatorIO[]}
355
- */
356
- const modularApiFileInfos = [];
357
-
358
- if (routes.$outOfModule) {
359
- if (generateRouteTypes) {
360
- const outOfModuleRouteContent = this.templatesWorker.renderTemplate(
361
- templatesToRender.routeTypes,
362
- {
363
- ...configuration,
364
- route: configuration.routes.$outOfModule,
365
- },
366
- );
367
-
368
- modularApiFileInfos.push(
369
- ...(await this.createOutputFileInfo(
370
- configuration,
371
- fileNames.outOfModuleApi,
372
- outOfModuleRouteContent,
373
- )),
374
- );
375
- }
376
- if (generateClient) {
377
- const outOfModuleApiContent = this.templatesWorker.renderTemplate(
378
- templatesToRender.api,
379
- {
380
- ...configuration,
381
- route: configuration.routes.$outOfModule,
382
- },
383
- );
384
-
385
- modularApiFileInfos.push(
386
- ...(await this.createOutputFileInfo(
387
- configuration,
388
- fileNames.outOfModuleApi,
389
- outOfModuleApiContent,
390
- )),
391
- );
392
- }
393
- }
394
-
395
- if (routes.combined) {
396
- for (const route of routes.combined) {
397
- if (generateRouteTypes) {
398
- const routeModuleContent = this.templatesWorker.renderTemplate(
399
- templatesToRender.routeTypes,
400
- {
401
- ...configuration,
402
- route,
403
- },
404
- );
405
-
406
- modularApiFileInfos.push(
407
- ...(await this.createOutputFileInfo(
408
- configuration,
409
- pascalCase(`${route.moduleName}_Route`),
410
- routeModuleContent,
411
- )),
412
- );
413
- }
414
-
415
- if (generateClient) {
416
- const apiModuleContent = this.templatesWorker.renderTemplate(
417
- templatesToRender.api,
418
- {
419
- ...configuration,
420
- route,
421
- },
422
- );
423
-
424
- modularApiFileInfos.push(
425
- ...(await this.createOutputFileInfo(
426
- configuration,
427
- pascalCase(route.moduleName),
428
- apiModuleContent,
429
- )),
430
- );
431
- }
432
- }
433
- }
434
-
435
- return [
436
- ...(await this.createOutputFileInfo(
437
- configuration,
438
- fileNames.dataContracts,
439
- this.templatesWorker.renderTemplate(
440
- templatesToRender.dataContracts,
441
- configuration,
442
- ),
443
- )),
444
- ...(generateClient
445
- ? await this.createOutputFileInfo(
446
- configuration,
447
- fileNames.httpClient,
448
- this.templatesWorker.renderTemplate(
449
- templatesToRender.httpClient,
450
- configuration,
451
- ),
452
- )
453
- : []),
454
- ...modularApiFileInfos,
455
- ];
456
- };
457
-
458
- /**
459
- *
460
- * @param templatesToRender
461
- * @param configuration
462
- * @returns {Promise<TranslatorIO[]>}
463
- */
464
- createSingleFileInfo = async (templatesToRender, configuration) => {
465
- const { generateRouteTypes, generateClient } = configuration.config;
466
-
467
- return await this.createOutputFileInfo(
468
- configuration,
469
- configuration.fileName,
470
- _.compact([
471
- this.templatesWorker.renderTemplate(
472
- templatesToRender.dataContracts,
473
- configuration,
474
- ),
475
- generateRouteTypes &&
476
- this.templatesWorker.renderTemplate(
477
- templatesToRender.routeTypes,
478
- configuration,
479
- ),
480
- generateClient &&
481
- this.templatesWorker.renderTemplate(
482
- templatesToRender.httpClient,
483
- configuration,
484
- ),
485
- generateClient &&
486
- this.templatesWorker.renderTemplate(
487
- templatesToRender.api,
488
- configuration,
489
- ),
490
- ]).join("\n"),
491
- );
492
- };
493
-
494
- /**
495
- *
496
- * @param configuration
497
- * @param fileNameFull
498
- * @param content
499
- * @returns {Promise<TranslatorIO[]>}
500
- */
501
- createOutputFileInfo = async (configuration, fileNameFull, content) => {
502
- const fileName = this.fileSystem.cropExtension(fileNameFull);
503
- const fileExtension = ts.Extension.Ts;
504
-
505
- if (configuration.translateToJavaScript) {
506
- this.logger.debug("using js translator for", fileName);
507
- return await this.javascriptTranslator.translate({
508
- fileName: fileName,
509
- fileExtension: fileExtension,
510
- fileContent: content,
511
- });
512
- }
513
-
514
- if (configuration.customTranslator) {
515
- this.logger.debug("using custom translator for", fileName);
516
- return await configuration.customTranslator.translate({
517
- fileName: fileName,
518
- fileExtension: fileExtension,
519
- fileContent: content,
520
- });
521
- }
522
-
523
- this.logger.debug("generating output for", `${fileName}${fileExtension}`);
524
-
525
- return [
526
- {
527
- fileName,
528
- fileExtension: fileExtension,
529
- fileContent: await this.codeFormatter.formatCode(content),
530
- },
531
- ];
532
- };
533
-
534
- createApiConfig = (swaggerSchema) => {
535
- const { info, servers, host, basePath, externalDocs, tags } = swaggerSchema;
536
- const server = servers?.[0] || { url: "" };
537
- const { title = "No title", version } = info || {};
538
- const { url: serverUrl } = server;
539
-
540
- return {
541
- info: info || {},
542
- servers: servers || [],
543
- basePath,
544
- host,
545
- externalDocs: _.merge(
546
- {
547
- url: "",
548
- description: "",
549
- },
550
- externalDocs,
551
- ),
552
- tags: _.compact(tags),
553
- baseUrl: serverUrl,
554
- title,
555
- version,
556
- };
557
- };
558
-
559
- injectClassInstance = (key, value) => {
560
- this[key] = value;
561
- PATCHABLE_INSTANCES.forEach((instanceKey) => {
562
- if (instanceKey !== key && key in this[instanceKey]) {
563
- this[instanceKey][key] = value;
564
- }
565
- });
566
- };
567
- }
568
-
569
- export { CodeGenProcess };
@@ -1,31 +0,0 @@
1
- import { HTTP_CLIENT, PROJECT_VERSION } from "../../constants.js";
2
- import { objectAssign } from "../../util/object-assign.js";
3
-
4
- /**
5
- * @type {GenerateTemplatesParams}}
6
- */
7
- class TemplatesGenConfig {
8
- cleanOutput = false;
9
- output = undefined;
10
- httpClientType = HTTP_CLIENT.FETCH;
11
- modular = false;
12
- silent = false;
13
- version = PROJECT_VERSION;
14
- rewrite = false;
15
-
16
- /**
17
- * @param config {GenerateTemplatesParams}
18
- */
19
- constructor(config) {
20
- this.update(config);
21
- }
22
-
23
- /**
24
- * @param update {Partial<GenerateTemplatesParams>}
25
- */
26
- update = (update) => {
27
- objectAssign(this, update);
28
- };
29
- }
30
-
31
- export { TemplatesGenConfig };
@@ -1,10 +0,0 @@
1
- #!/usr/bin/env node
2
-
3
- import { TemplatesGenProcess } from "./templates-gen-process.js";
4
-
5
- async function generateTemplates(config) {
6
- const codeGenProcess = new TemplatesGenProcess(config);
7
- return await codeGenProcess.start();
8
- }
9
-
10
- export { generateTemplates };