swagger-typescript-api 13.0.0-experimental-1 → 13.0.0

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 (81) hide show
  1. package/LICENSE +21 -21
  2. package/README.md +22 -12
  3. package/cli/constants.js +3 -3
  4. package/cli/execute.js +52 -31
  5. package/cli/index.d.ts +1 -2
  6. package/cli/index.js +18 -17
  7. package/cli/operations/display-help.js +51 -29
  8. package/cli/parse-args.js +3 -3
  9. package/cli/process-option.js +28 -20
  10. package/index.d.ts +113 -8
  11. package/index.js +158 -135
  12. package/package.json +35 -30
  13. package/src/code-formatter.js +28 -13
  14. package/src/code-gen-process.js +357 -259
  15. package/src/commands/generate-templates/configuration.js +2 -2
  16. package/src/commands/generate-templates/index.js +1 -2
  17. package/src/commands/generate-templates/templates-gen-process.js +62 -35
  18. package/src/component-type-name-resolver.js +44 -0
  19. package/src/configuration.js +167 -95
  20. package/src/constants.js +28 -22
  21. package/src/index.js +3 -4
  22. package/src/schema-components-map.js +39 -23
  23. package/src/schema-parser/base-schema-parsers/array.js +43 -0
  24. package/src/schema-parser/base-schema-parsers/complex.js +51 -0
  25. package/src/schema-parser/base-schema-parsers/discriminator.js +301 -0
  26. package/src/schema-parser/base-schema-parsers/enum.js +158 -0
  27. package/src/schema-parser/base-schema-parsers/object.js +105 -0
  28. package/src/schema-parser/base-schema-parsers/primitive.js +63 -0
  29. package/src/schema-parser/complex-schema-parsers/all-of.js +26 -0
  30. package/src/schema-parser/complex-schema-parsers/any-of.js +34 -0
  31. package/src/schema-parser/complex-schema-parsers/not.js +9 -0
  32. package/src/schema-parser/complex-schema-parsers/one-of.js +27 -0
  33. package/src/schema-parser/mono-schema-parser.js +48 -0
  34. package/src/schema-parser/schema-formatters.js +69 -60
  35. package/src/schema-parser/schema-parser-fabric.js +131 -0
  36. package/src/schema-parser/schema-parser.js +208 -427
  37. package/src/schema-parser/schema-utils.js +123 -58
  38. package/src/schema-parser/util/enum-key-resolver.js +26 -0
  39. package/src/schema-routes/schema-routes.js +1225 -0
  40. package/src/schema-routes/util/specific-arg-name-resolver.js +26 -0
  41. package/src/schema-walker.js +93 -0
  42. package/src/swagger-schema-resolver.js +61 -28
  43. package/src/templates-worker.js +240 -0
  44. package/src/translators/javascript.js +83 -0
  45. package/src/translators/translator.js +35 -0
  46. package/src/type-name-formatter.js +33 -18
  47. package/src/util/file-system.js +30 -14
  48. package/src/util/id.js +2 -2
  49. package/src/util/internal-case.js +1 -1
  50. package/src/util/logger.js +46 -20
  51. package/src/util/name-resolver.js +52 -60
  52. package/src/util/object-assign.js +7 -3
  53. package/src/util/pascal-case.js +1 -1
  54. package/src/util/request.js +5 -5
  55. package/src/util/sort-by-property.js +17 -0
  56. package/templates/README.md +17 -17
  57. package/templates/base/README.md +7 -7
  58. package/templates/base/data-contract-jsdoc.ejs +37 -37
  59. package/templates/base/data-contracts.ejs +40 -27
  60. package/templates/base/enum-data-contract.ejs +12 -12
  61. package/templates/base/http-client.ejs +3 -3
  62. package/templates/base/http-clients/axios-http-client.ejs +139 -138
  63. package/templates/base/http-clients/fetch-http-client.ejs +224 -224
  64. package/templates/base/interface-data-contract.ejs +10 -10
  65. package/templates/base/object-field-jsdoc.ejs +28 -28
  66. package/templates/base/route-docs.ejs +30 -30
  67. package/templates/base/route-name.ejs +42 -42
  68. package/templates/base/route-type.ejs +22 -21
  69. package/templates/base/type-data-contract.ejs +15 -15
  70. package/templates/default/README.md +6 -6
  71. package/templates/default/api.ejs +69 -68
  72. package/templates/default/procedure-call.ejs +100 -100
  73. package/templates/default/route-types.ejs +32 -32
  74. package/templates/modular/README.md +6 -6
  75. package/templates/modular/api.ejs +28 -28
  76. package/templates/modular/procedure-call.ejs +100 -100
  77. package/templates/modular/route-types.ejs +18 -18
  78. package/src/schema-parser/schema-processor.js +0 -79
  79. package/src/schema-parser/schema-routes.js +0 -950
  80. package/src/templates.js +0 -182
  81. package/src/translators/JavaScript.js +0 -60
package/index.d.ts CHANGED
@@ -1,3 +1,5 @@
1
+ import {MonoSchemaParser} from "./src/schema-parser/mono-schema-parser";
2
+
1
3
  type HttpClientType = "axios" | "fetch";
2
4
 
3
5
  interface GenerateApiParamsBase {
@@ -7,7 +9,7 @@ interface GenerateApiParamsBase {
7
9
  name?: string;
8
10
 
9
11
  /**
10
- * path to folder where will been located the created api module.
12
+ * path to folder where will be located the created api module.
11
13
  *
12
14
  * may set to `false` to skip writing content to disk. in this case,
13
15
  * you may access the `files` on the return value.
@@ -54,6 +56,16 @@ interface GenerateApiParamsBase {
54
56
  */
55
57
  unwrapResponseData?: boolean;
56
58
 
59
+ /**
60
+ * sort data contracts in alphabetical order
61
+ */
62
+ sortTypes?: boolean;
63
+
64
+ /**
65
+ * sort routes in alphabetical order
66
+ */
67
+ sortRoutes?: boolean;
68
+
57
69
  /**
58
70
  * generate js api module with declaration file (default: false)
59
71
  */
@@ -139,22 +151,79 @@ interface GenerateApiParamsBase {
139
151
 
140
152
  /** extract all enums from nested types\interfaces to `enum` construction */
141
153
  extractEnums?: boolean;
154
+
142
155
  /** prefix string value needed to fix invalid type names (default: 'Type') */
143
156
  fixInvalidTypeNamePrefix?: string;
157
+
144
158
  /** prefix string value needed to fix invalid enum keys (default: 'Value') */
145
159
  fixInvalidEnumKeyPrefix?: string;
160
+
146
161
  /** prefix string value for enum keys */
147
162
  enumKeyPrefix?: string;
163
+
148
164
  /** suffix string value for enum keys */
149
165
  enumKeySuffix?: string;
166
+
150
167
  /** prefix string value for type names */
151
168
  typePrefix?: string;
169
+
152
170
  /** suffix string value for type names */
153
171
  typeSuffix?: string;
172
+
154
173
  /** extra configuration for extracting type names operations */
155
174
  extractingOptions?: Partial<ExtractingOptions>;
175
+
156
176
  /** configuration for fetching swagger schema requests */
157
177
  requestOptions?: null | Partial<import("node-fetch").RequestInit>;
178
+
179
+ /** ts compiler configuration object (for --to-js option) */
180
+ compilerTsConfig?: Record<string, any>;
181
+
182
+ /**
183
+ * custom ts->* translator
184
+ * do not use constructor args, it can break functionality of this property, just send class reference
185
+ *
186
+ * @example
187
+ * ```ts
188
+ * const { Translator } = require("swagger-typescript-api/src/translators/translator");
189
+ *
190
+ * class MyTranslator extends Translator {
191
+ *
192
+ * translate({ fileName, fileExtension, fileContent }) {
193
+ * this.codeFormatter.format()
194
+ * this.config.
195
+ * this.logger.
196
+ *
197
+ * return [
198
+ * {
199
+ * fileName,
200
+ * fileExtension,
201
+ * fileContent,
202
+ * }
203
+ * ]
204
+ * }
205
+ * }
206
+ * ```
207
+ */
208
+ customTranslator?: new () => typeof import("./src/translators/translator").Translator;
209
+ /** fallback name for enum key resolver */
210
+ enumKeyResolverName?: string;
211
+ /** fallback name for type name resolver */
212
+ typeNameResolverName?: string;
213
+ /** fallback name for specific arg name resolver */
214
+ specificArgNameResolverName?: string;
215
+ schemaParsers?: {
216
+ complexOneOf?:MonoSchemaParser;
217
+ complexAllOf?:MonoSchemaParser;
218
+ complexAnyOf?:MonoSchemaParser;
219
+ complexNot?:MonoSchemaParser;
220
+ enum?:MonoSchemaParser;
221
+ object?:MonoSchemaParser;
222
+ complex?:MonoSchemaParser;
223
+ primitive?:MonoSchemaParser;
224
+ discriminator?:MonoSchemaParser;
225
+ array?: MonoSchemaParser;
226
+ }
158
227
  }
159
228
 
160
229
  type CodeGenConstruct = {
@@ -201,7 +270,7 @@ type CodeGenConstruct = {
201
270
 
202
271
  type PrimitiveTypeStructValue =
203
272
  | string
204
- | ((schema: Record<string, any>, parser: import("./src/schema-parser/schema-processor").SchemaProcessor) => string);
273
+ | ((schema: Record<string, any>, parser: import("./src/schema-parser/schema-parser").SchemaParser) => string);
205
274
 
206
275
  type PrimitiveTypeStruct = Record<
207
276
  "integer" | "number" | "boolean" | "object" | "file" | "string" | "array",
@@ -269,7 +338,10 @@ export interface Hooks {
269
338
  /** calls after parse route (return type: customized route (ParsedRoute), nothing change (void), false (ignore this route)) */
270
339
  onCreateRoute: (routeData: ParsedRoute) => ParsedRoute | void | false;
271
340
  /** Start point of work this tool (after fetching schema) */
272
- onInit?: <C extends GenerateApiConfiguration["config"]>(configuration: C) => C | void;
341
+ onInit?: <C extends GenerateApiConfiguration["config"]>(
342
+ configuration: C,
343
+ codeGenProcess: import("./src/code-gen-process").CodeGenProcess,
344
+ ) => C | void;
273
345
  /** customize configuration object before sending it to ETA templates */
274
346
  onPrepareConfig?: <C extends GenerateApiConfiguration>(currentConfiguration: C) => C | void;
275
347
  /** customize route name as you need */
@@ -354,7 +426,7 @@ export interface SchemaComponent {
354
426
  };
355
427
  $parsed: ParsedSchema<SchemaTypeObjectContent | SchemaTypeEnumContent | SchemaTypePrimitiveContent>;
356
428
  };
357
- componentName: string;
429
+ componentName: "schemas" | "paths";
358
430
  typeData: ParsedSchema<SchemaTypeObjectContent | SchemaTypeEnumContent | SchemaTypePrimitiveContent> | null;
359
431
  }
360
432
 
@@ -430,10 +502,16 @@ type ExtractingOptions = {
430
502
  responseBodySuffix: string[];
431
503
  responseErrorSuffix: string[];
432
504
  requestParamsSuffix: string[];
505
+ enumSuffix: string[];
506
+ discriminatorMappingSuffix: string[];
507
+ discriminatorAbstractPrefix: string[];
433
508
  requestBodyNameResolver: (name: string, reservedNames: string) => string | undefined;
434
509
  responseBodyNameResolver: (name: string, reservedNames: string) => string | undefined;
435
510
  responseErrorNameResolver: (name: string, reservedNames: string) => string | undefined;
436
511
  requestParamsNameResolver: (name: string, reservedNames: string) => string | undefined;
512
+ enumNameResolver: (name: string, reservedNames: string) => string | undefined;
513
+ discriminatorMappingNameResolver: (name: string, reservedNames: string) => string | undefined;
514
+ discriminatorAbstractResolver: (name: string, reservedNames: string) => string | undefined;
437
515
  };
438
516
 
439
517
  export interface GenerateApiConfiguration {
@@ -450,6 +528,18 @@ export interface GenerateApiConfiguration {
450
528
  url: string;
451
529
  spec: any;
452
530
  fileName: string;
531
+ templatePaths: {
532
+ /** `templates/base` */
533
+ base: string;
534
+ /** `templates/default` */
535
+ default: string;
536
+ /** `templates/modular` */
537
+ modular: string;
538
+ /** usage path if `--templates` option is not set */
539
+ original: string;
540
+ /** custom path to templates (`--templates`) */
541
+ custom: string | null;
542
+ };
453
543
  authorizationToken?: string;
454
544
  generateResponses: boolean;
455
545
  defaultResponseAsSuccess: boolean;
@@ -468,6 +558,7 @@ export interface GenerateApiConfiguration {
468
558
  extractRequestParams: boolean;
469
559
  unwrapResponseData: boolean;
470
560
  sortTypes: boolean;
561
+ sortRoutes: boolean;
471
562
  singleHttpClient: boolean;
472
563
  typePrefix: string;
473
564
  typeSuffix: string;
@@ -492,10 +583,16 @@ export interface GenerateApiConfiguration {
492
583
  hooks: Hooks;
493
584
  enumNamesAsValues: boolean;
494
585
  version: string;
586
+ compilerTsConfig: Record<string, any>;
587
+ enumKeyResolverName: string;
588
+ typeNameResolverName: string;
589
+ specificArgNameResolverName: string;
590
+ /** do not use constructor args, it can break functionality of this property, just send class reference */
591
+ customTranslator?: new (...args: never[]) => typeof import("./src/translators/translator").Translator;
495
592
  internalTemplateOptions: {
496
593
  addUtilRequiredKeysType: boolean;
497
594
  };
498
- componentTypeNameResolver: typeof import("./src/util/name-resolver").ComponentTypeNameResolver;
595
+ componentTypeNameResolver: typeof import("./src/component-type-name-resolver").ComponentTypeNameResolver;
499
596
  fileNames: {
500
597
  dataContracts: string;
501
598
  routeTypes: string;
@@ -520,7 +617,6 @@ export interface GenerateApiConfiguration {
520
617
  extractingOptions: ExtractingOptions;
521
618
  };
522
619
  modelTypes: ModelType[];
523
- rawModelTypes: SchemaComponent[];
524
620
  hasFormDataRoutes: boolean;
525
621
  hasSecurityRoutes: boolean;
526
622
  hasQueryRoutes: boolean;
@@ -556,9 +652,18 @@ export interface GenerateApiConfiguration {
556
652
  };
557
653
  }
558
654
 
655
+ type FileInfo = {
656
+ /** @example myFilename */
657
+ fileName: string;
658
+ /** @example .d.ts */
659
+ fileExtension: string;
660
+ /** content of the file */
661
+ fileContent: string;
662
+ };
663
+
559
664
  export interface GenerateApiOutput {
560
665
  configuration: GenerateApiConfiguration;
561
- files: { name: string; content: string; declaration: { name: string; content: string } | null }[];
666
+ files: FileInfo[];
562
667
  createFile: (params: { path: string; fileName: string; content: string; withPrefix?: boolean }) => void;
563
668
  renderTemplate: (
564
669
  templateContent: string,
@@ -566,7 +671,7 @@ export interface GenerateApiOutput {
566
671
  etaOptions?: import("eta/dist/types/config").PartialConfig,
567
672
  ) => string;
568
673
  getTemplate: (params: { fileName?: string; name?: string; path?: string }) => string;
569
- formatTSContent: (content: string) => string;
674
+ formatTSContent: (content: string) => Promise<string>;
570
675
  }
571
676
 
572
677
  export declare function generateApi(params: GenerateApiParams): Promise<GenerateApiOutput>;