@devlearning/swagger-generator 1.1.21 → 1.1.22

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 (55) hide show
  1. package/.vscode/launch.json +28 -28
  2. package/README-OLD.md +209 -209
  3. package/README.md +277 -277
  4. package/dist/api.constants.js +22 -22
  5. package/dist/generators-writers/angular/api-angular-writer.js +38 -38
  6. package/dist/generators-writers/angular/constants.js +24 -24
  7. package/dist/generators-writers/angular/model-angular-writer.js +6 -6
  8. package/dist/generators-writers/dart/model-dart-writer.js +11 -4
  9. package/dist/generators-writers/dart/templates/api.mustache +143 -143
  10. package/dist/generators-writers/dart/templates/enum.mustache +14 -14
  11. package/dist/generators-writers/dart/templates/model.mustache +23 -23
  12. package/dist/generators-writers/nextjs/api-nextjs-writer.js +12 -12
  13. package/dist/generators-writers/nextjs/constants.js +4 -4
  14. package/dist/generators-writers/nextjs/model-nextjs-writer.js +6 -6
  15. package/dist/generators-writers/utils.d.ts +1 -0
  16. package/dist/generators-writers/utils.js +16 -4
  17. package/dist/templates/api.mustache +29 -0
  18. package/dist/templates/model.mustache +18 -0
  19. package/package.json +49 -49
  20. package/src/api.constants.ts +26 -26
  21. package/src/generator-old.ts +449 -449
  22. package/src/generator.ts +752 -752
  23. package/src/generators-writers/angular/api-angular-writer.ts +187 -187
  24. package/src/generators-writers/angular/constants.ts +36 -36
  25. package/src/generators-writers/angular/model-angular-writer.ts +65 -65
  26. package/src/generators-writers/angular/normalizator.ts +41 -41
  27. package/src/generators-writers/dart/api-dart-writer.ts +303 -303
  28. package/src/generators-writers/dart/model-dart-writer.ts +226 -219
  29. package/src/generators-writers/dart/models/import-definition-dart.ts +5 -5
  30. package/src/generators-writers/dart/normalizator.ts +72 -72
  31. package/src/generators-writers/dart/templates/api.mustache +143 -143
  32. package/src/generators-writers/dart/templates/enum.mustache +14 -14
  33. package/src/generators-writers/dart/templates/model.mustache +23 -23
  34. package/src/generators-writers/nextjs/api-nextjs-writer.ts +157 -157
  35. package/src/generators-writers/nextjs/constants.ts +5 -5
  36. package/src/generators-writers/nextjs/model-nextjs-writer.ts +61 -61
  37. package/src/generators-writers/utils.ts +111 -93
  38. package/src/index.ts +103 -103
  39. package/src/models/api-dto.ts +17 -17
  40. package/src/models/enum-value-dto.ts +3 -3
  41. package/src/models/model-dto.ts +9 -9
  42. package/src/models/parameter-dto.ts +7 -7
  43. package/src/models/property-dto.ts +4 -4
  44. package/src/models/swagger/swagger-component-property.ts +11 -11
  45. package/src/models/swagger/swagger-component.ts +17 -17
  46. package/src/models/swagger/swagger-content.ts +4 -4
  47. package/src/models/swagger/swagger-info.ts +3 -3
  48. package/src/models/swagger/swagger-method.ts +7 -7
  49. package/src/models/swagger/swagger-schema.ts +21 -21
  50. package/src/models/swagger/swagger.ts +38 -38
  51. package/src/models/type-dto.ts +7 -7
  52. package/src/swagger-downloader.ts +46 -46
  53. package/src/utils/logger.ts +73 -73
  54. package/src/utils/swagger-validator.ts +89 -89
  55. package/tsconfig.json +33 -33
package/src/index.ts CHANGED
@@ -1,104 +1,104 @@
1
- #!/usr/bin/env node
2
-
3
- import yargs from 'yargs';
4
- import { hideBin } from 'yargs/helpers';
5
- import { Generator } from './generator.js';
6
- import { SwaggerDownloader } from './swagger-downloader.js';
7
-
8
- export enum DateTimeLibrary {
9
- Moment = 'moment',
10
- DateFns = 'date-fn',
11
- }
12
-
13
- export enum TargetGeneration {
14
- Angular = 'angular',
15
- Next = 'next',
16
- Flutter = 'flutter',
17
- }
18
-
19
- export interface CommandLineArgs {
20
- swaggerJsonUrl: string;
21
- outputDirectory: string;
22
- target: TargetGeneration;
23
- dateTimeLibrary?: DateTimeLibrary;
24
- package?: string;
25
- apiClientName?: string;
26
- }
27
-
28
- const argv = yargs(hideBin(process.argv))
29
- .scriptName("swagger-gen")
30
- .usage('Usage: $0 --url <swaggerJsonUrl> --output <outputDirectory> --target <target> [--dateTimeLibrary <dateTimeLibrary>] [--package <name>] [--api-client-name <className>]')
31
- .option('url', {
32
- alias: 'u',
33
- type: 'string',
34
- demandOption: true,
35
- describe: 'URL of swagger.json file',
36
- })
37
- .option('output', {
38
- alias: 'o',
39
- type: 'string',
40
- demandOption: true,
41
- describe: 'Output directory for generated code',
42
- })
43
- .option('target', {
44
- alias: 't',
45
- type: 'string',
46
- choices: ['angular', 'next', 'flutter'],
47
- demandOption: true,
48
- describe: 'Target format for the generated code',
49
- })
50
- .option('dateTimeLibrary', {
51
- alias: 'd',
52
- type: 'string',
53
- choices: ['moment', 'date-fns'],
54
- default: 'date-fns',
55
- describe: 'Date library to use for date handling',
56
- })
57
- .option('package', {
58
- alias: 'p',
59
- type: 'string',
60
- describe: 'For Dart: package name for Dart project',
61
- })
62
- .option('api-client-name', {
63
- alias: 'a',
64
- type: 'string',
65
- describe: 'Name for the unified API client class (all services in one class)',
66
- })
67
- .help()
68
- .parseSync();
69
-
70
- const commandLineArgs: CommandLineArgs = {
71
- swaggerJsonUrl: argv.url,
72
- outputDirectory: argv.output,
73
- target: parseTargetGeneration(argv.target),
74
- dateTimeLibrary: parseDateTimeLibrary(argv.dateTimeLibrary),
75
- package: argv.package ?? '',
76
- apiClientName: argv['api-client-name'],
77
- };
78
-
79
- const swaggerDownloader = new SwaggerDownloader();
80
-
81
- swaggerDownloader.download(new URL(commandLineArgs.swaggerJsonUrl))
82
- .then(swaggerDoc => {
83
- return new Generator(swaggerDoc, commandLineArgs);
84
- })
85
- .then(generator => { generator.generate(); });
86
-
87
-
88
- // require('./index.js')({swaggerDownloader});
89
-
90
- function parseDateTimeLibrary(value: string): DateTimeLibrary | undefined {
91
- const values = Object.values(DateTimeLibrary);
92
- if (values.includes(value as DateTimeLibrary)) {
93
- return value as DateTimeLibrary;
94
- }
95
- return undefined;
96
- }
97
-
98
- function parseTargetGeneration(value: string): TargetGeneration {
99
- const values = Object.values(TargetGeneration);
100
- if (values.includes(value as TargetGeneration)) {
101
- return value as TargetGeneration;
102
- }
103
- throw new Error(`Invalid OutputFormat: ${value}`);
1
+ #!/usr/bin/env node
2
+
3
+ import yargs from 'yargs';
4
+ import { hideBin } from 'yargs/helpers';
5
+ import { Generator } from './generator.js';
6
+ import { SwaggerDownloader } from './swagger-downloader.js';
7
+
8
+ export enum DateTimeLibrary {
9
+ Moment = 'moment',
10
+ DateFns = 'date-fn',
11
+ }
12
+
13
+ export enum TargetGeneration {
14
+ Angular = 'angular',
15
+ Next = 'next',
16
+ Flutter = 'flutter',
17
+ }
18
+
19
+ export interface CommandLineArgs {
20
+ swaggerJsonUrl: string;
21
+ outputDirectory: string;
22
+ target: TargetGeneration;
23
+ dateTimeLibrary?: DateTimeLibrary;
24
+ package?: string;
25
+ apiClientName?: string;
26
+ }
27
+
28
+ const argv = yargs(hideBin(process.argv))
29
+ .scriptName("swagger-gen")
30
+ .usage('Usage: $0 --url <swaggerJsonUrl> --output <outputDirectory> --target <target> [--dateTimeLibrary <dateTimeLibrary>] [--package <name>] [--api-client-name <className>]')
31
+ .option('url', {
32
+ alias: 'u',
33
+ type: 'string',
34
+ demandOption: true,
35
+ describe: 'URL of swagger.json file',
36
+ })
37
+ .option('output', {
38
+ alias: 'o',
39
+ type: 'string',
40
+ demandOption: true,
41
+ describe: 'Output directory for generated code',
42
+ })
43
+ .option('target', {
44
+ alias: 't',
45
+ type: 'string',
46
+ choices: ['angular', 'next', 'flutter'],
47
+ demandOption: true,
48
+ describe: 'Target format for the generated code',
49
+ })
50
+ .option('dateTimeLibrary', {
51
+ alias: 'd',
52
+ type: 'string',
53
+ choices: ['moment', 'date-fns'],
54
+ default: 'date-fns',
55
+ describe: 'Date library to use for date handling',
56
+ })
57
+ .option('package', {
58
+ alias: 'p',
59
+ type: 'string',
60
+ describe: 'For Dart: package name for Dart project',
61
+ })
62
+ .option('api-client-name', {
63
+ alias: 'a',
64
+ type: 'string',
65
+ describe: 'Name for the unified API client class (all services in one class)',
66
+ })
67
+ .help()
68
+ .parseSync();
69
+
70
+ const commandLineArgs: CommandLineArgs = {
71
+ swaggerJsonUrl: argv.url,
72
+ outputDirectory: argv.output,
73
+ target: parseTargetGeneration(argv.target),
74
+ dateTimeLibrary: parseDateTimeLibrary(argv.dateTimeLibrary),
75
+ package: argv.package ?? '',
76
+ apiClientName: argv['api-client-name'],
77
+ };
78
+
79
+ const swaggerDownloader = new SwaggerDownloader();
80
+
81
+ swaggerDownloader.download(new URL(commandLineArgs.swaggerJsonUrl))
82
+ .then(swaggerDoc => {
83
+ return new Generator(swaggerDoc, commandLineArgs);
84
+ })
85
+ .then(generator => { generator.generate(); });
86
+
87
+
88
+ // require('./index.js')({swaggerDownloader});
89
+
90
+ function parseDateTimeLibrary(value: string): DateTimeLibrary | undefined {
91
+ const values = Object.values(DateTimeLibrary);
92
+ if (values.includes(value as DateTimeLibrary)) {
93
+ return value as DateTimeLibrary;
94
+ }
95
+ return undefined;
96
+ }
97
+
98
+ function parseTargetGeneration(value: string): TargetGeneration {
99
+ const values = Object.values(TargetGeneration);
100
+ if (values.includes(value as TargetGeneration)) {
101
+ return value as TargetGeneration;
102
+ }
103
+ throw new Error(`Invalid OutputFormat: ${value}`);
104
104
  }
@@ -1,18 +1,18 @@
1
- import { ParameterDto } from "./parameter-dto.js";
2
- import { SwaggerMethod } from "./swagger/swagger-method.js";
3
- import { TypeDto } from "./type-dto.js";
4
-
5
- export interface ApiDto {
6
- name: string;
7
- method: string;
8
- url: string;
9
- description?: string;
10
- parameters: ParameterDto[];
11
- returnType: TypeDto | undefined;
12
- haveRequest: boolean;
13
- isMultiPart: boolean;
14
- tag: string;
15
-
16
- swaggerMethodKey: { [key: string]: SwaggerMethod; };
17
- swaggerMethod: SwaggerMethod;
1
+ import { ParameterDto } from "./parameter-dto.js";
2
+ import { SwaggerMethod } from "./swagger/swagger-method.js";
3
+ import { TypeDto } from "./type-dto.js";
4
+
5
+ export interface ApiDto {
6
+ name: string;
7
+ method: string;
8
+ url: string;
9
+ description?: string;
10
+ parameters: ParameterDto[];
11
+ returnType: TypeDto | undefined;
12
+ haveRequest: boolean;
13
+ isMultiPart: boolean;
14
+ tag: string;
15
+
16
+ swaggerMethodKey: { [key: string]: SwaggerMethod; };
17
+ swaggerMethod: SwaggerMethod;
18
18
  }
@@ -1,4 +1,4 @@
1
- export interface EnumValueDto {
2
- name: string;
3
- value: string;
1
+ export interface EnumValueDto {
2
+ name: string;
3
+ value: string;
4
4
  }
@@ -1,10 +1,10 @@
1
- import { EnumValueDto } from "./enum-value-dto.js";
2
- import { PropertyDto } from "./property-dto.js";
3
-
4
- export interface ModelDto {
5
- typeName: string;
6
- modelType: 'enum' | 'interface' | 'class';
7
- name: string;
8
- properties: PropertyDto[];
9
- enumValues: EnumValueDto[];
1
+ import { EnumValueDto } from "./enum-value-dto.js";
2
+ import { PropertyDto } from "./property-dto.js";
3
+
4
+ export interface ModelDto {
5
+ typeName: string;
6
+ modelType: 'enum' | 'interface' | 'class';
7
+ name: string;
8
+ properties: PropertyDto[];
9
+ enumValues: EnumValueDto[];
10
10
  }
@@ -1,8 +1,8 @@
1
- import { PropertyDto } from "./property-dto.js";
2
- import { SwaggerParameter } from "./swagger/swagger.js";
3
-
4
- export interface ParameterDto extends PropertyDto {
5
- isQuery: boolean;
6
- isEnum: boolean;
7
- swaggerParameter?: SwaggerParameter;
1
+ import { PropertyDto } from "./property-dto.js";
2
+ import { SwaggerParameter } from "./swagger/swagger.js";
3
+
4
+ export interface ParameterDto extends PropertyDto {
5
+ isQuery: boolean;
6
+ isEnum: boolean;
7
+ swaggerParameter?: SwaggerParameter;
8
8
  }
@@ -1,5 +1,5 @@
1
- import { TypeDto } from "./type-dto.js";
2
-
3
- export interface PropertyDto extends TypeDto {
4
- name: string;
1
+ import { TypeDto } from "./type-dto.js";
2
+
3
+ export interface PropertyDto extends TypeDto {
4
+ name: string;
5
5
  }
@@ -1,12 +1,12 @@
1
- import { SwaggerSchema } from "./swagger-schema.js";
2
-
3
- // export interface SwaggerComponentProperty {
4
- // type: string;
5
- // $ref: string;
6
- // allOf: SwaggerComponentProperty[];
7
- // format: string;
8
- // items: SwaggerSchema;
9
- // properties: { [key: string]: SwaggerComponentProperty; };
10
- // nullable: boolean;
11
- // minLength: number;
1
+ import { SwaggerSchema } from "./swagger-schema.js";
2
+
3
+ // export interface SwaggerComponentProperty {
4
+ // type: string;
5
+ // $ref: string;
6
+ // allOf: SwaggerComponentProperty[];
7
+ // format: string;
8
+ // items: SwaggerSchema;
9
+ // properties: { [key: string]: SwaggerComponentProperty; };
10
+ // nullable: boolean;
11
+ // minLength: number;
12
12
  // }
@@ -1,18 +1,18 @@
1
- // import { SwaggerComponentProperty } from "./swagger-component-property.js";
2
-
3
- import { SwaggerSchema } from "./swagger-schema.js";
4
-
5
- // export interface SwaggerComponent {
6
- // type: string;
7
- // properties: { [key: string]: SwaggerComponentProperty; };
8
- // additionalProperties: boolean;
9
- // enum: string[];
10
- // }
11
-
12
- export interface SwaggerComponent {
13
- type: string;
14
- properties: { [key: string]: SwaggerSchema; };
15
- additionalProperties: boolean;
16
- enum?: Array<string | number>;
17
- required?: string[];
1
+ // import { SwaggerComponentProperty } from "./swagger-component-property.js";
2
+
3
+ import { SwaggerSchema } from "./swagger-schema.js";
4
+
5
+ // export interface SwaggerComponent {
6
+ // type: string;
7
+ // properties: { [key: string]: SwaggerComponentProperty; };
8
+ // additionalProperties: boolean;
9
+ // enum: string[];
10
+ // }
11
+
12
+ export interface SwaggerComponent {
13
+ type: string;
14
+ properties: { [key: string]: SwaggerSchema; };
15
+ additionalProperties: boolean;
16
+ enum?: Array<string | number>;
17
+ required?: string[];
18
18
  }
@@ -1,5 +1,5 @@
1
- import { SwaggerSchema } from "./swagger-schema.js";
2
-
3
- export interface SwaggerContent {
4
- schema: SwaggerSchema;
1
+ import { SwaggerSchema } from "./swagger-schema.js";
2
+
3
+ export interface SwaggerContent {
4
+ schema: SwaggerSchema;
5
5
  }
@@ -1,4 +1,4 @@
1
- export interface SwaggerInfo {
2
- title: string;
3
- version: string;
1
+ export interface SwaggerInfo {
2
+ title: string;
3
+ version: string;
4
4
  }
@@ -1,8 +1,8 @@
1
- import { SwaggerParameter, SwaggerRequestBody, SwaggerResponses } from "./swagger.js";
2
-
3
- export interface SwaggerMethod {
4
- tags: string[];
5
- parameters: SwaggerParameter[];
6
- requestBody: SwaggerRequestBody;
7
- responses: { [key: string]: SwaggerResponses; };
1
+ import { SwaggerParameter, SwaggerRequestBody, SwaggerResponses } from "./swagger.js";
2
+
3
+ export interface SwaggerMethod {
4
+ tags: string[];
5
+ parameters: SwaggerParameter[];
6
+ requestBody: SwaggerRequestBody;
7
+ responses: { [key: string]: SwaggerResponses; };
8
8
  }
@@ -1,22 +1,22 @@
1
- // import { SwaggerComponentProperty } from "./swagger-component-property.js";
2
-
3
- // export interface SwaggerSchema {
4
- // type: string;
5
- // $ref: string;
6
- // allOf: SwaggerSchema[];
7
- // format: string;
8
- // items: SwaggerSchema;
9
- // properties: { [key: string]: SwaggerComponentProperty; };
10
- // }
11
-
12
- export interface SwaggerSchema {
13
- type?: string;
14
- $ref?: string;
15
- allOf?: SwaggerSchema[];
16
- format?: string;
17
- items?: SwaggerSchema;
18
- properties?: { [key: string]: SwaggerSchema };
19
- enum?: Array<string | number>;
20
- nullable?: boolean;
21
- minLength?: number;
1
+ // import { SwaggerComponentProperty } from "./swagger-component-property.js";
2
+
3
+ // export interface SwaggerSchema {
4
+ // type: string;
5
+ // $ref: string;
6
+ // allOf: SwaggerSchema[];
7
+ // format: string;
8
+ // items: SwaggerSchema;
9
+ // properties: { [key: string]: SwaggerComponentProperty; };
10
+ // }
11
+
12
+ export interface SwaggerSchema {
13
+ type?: string;
14
+ $ref?: string;
15
+ allOf?: SwaggerSchema[];
16
+ format?: string;
17
+ items?: SwaggerSchema;
18
+ properties?: { [key: string]: SwaggerSchema };
19
+ enum?: Array<string | number>;
20
+ nullable?: boolean;
21
+ minLength?: number;
22
22
  }
@@ -1,39 +1,39 @@
1
- import { SwaggerComponent } from "./swagger-component.js";
2
- import { SwaggerContent } from "./swagger-content.js";
3
- import { SwaggerInfo } from "./swagger-info.js";
4
- import { SwaggerMethod } from "./swagger-method.js";
5
- import { SwaggerSchema } from "./swagger-schema.js";
6
-
7
- export interface Swagger {
8
- openApi: string;
9
- info: SwaggerInfo;
10
- paths: { [key: string]: { [key: string]: SwaggerMethod; }; };
11
- components: SwaggerComponents;
12
- }
13
-
14
- export interface SwaggerParameter {
15
- name: string;
16
- in: string;
17
- required: boolean;
18
- schema: SwaggerSchema;
19
- }
20
-
21
- export interface SwaggerRequestBody {
22
- content: { [key: string]: SwaggerContent; };
23
- encoding : SwaggerEncoding;
24
- required?: boolean;
25
- }
26
-
27
- export interface SwaggerResponses {
28
- description: string;
29
- content: { [key: string]: SwaggerContent; };
30
- }
31
-
32
- export interface SwaggerComponents {
33
- schemas: { [key: string]: SwaggerComponent; };
34
- }
35
-
36
- export interface SwaggerEncoding{
37
- File: {style: 'form'},
38
- Id: {style: 'form'},
1
+ import { SwaggerComponent } from "./swagger-component.js";
2
+ import { SwaggerContent } from "./swagger-content.js";
3
+ import { SwaggerInfo } from "./swagger-info.js";
4
+ import { SwaggerMethod } from "./swagger-method.js";
5
+ import { SwaggerSchema } from "./swagger-schema.js";
6
+
7
+ export interface Swagger {
8
+ openApi: string;
9
+ info: SwaggerInfo;
10
+ paths: { [key: string]: { [key: string]: SwaggerMethod; }; };
11
+ components: SwaggerComponents;
12
+ }
13
+
14
+ export interface SwaggerParameter {
15
+ name: string;
16
+ in: string;
17
+ required: boolean;
18
+ schema: SwaggerSchema;
19
+ }
20
+
21
+ export interface SwaggerRequestBody {
22
+ content: { [key: string]: SwaggerContent; };
23
+ encoding : SwaggerEncoding;
24
+ required?: boolean;
25
+ }
26
+
27
+ export interface SwaggerResponses {
28
+ description: string;
29
+ content: { [key: string]: SwaggerContent; };
30
+ }
31
+
32
+ export interface SwaggerComponents {
33
+ schemas: { [key: string]: SwaggerComponent; };
34
+ }
35
+
36
+ export interface SwaggerEncoding{
37
+ File: {style: 'form'},
38
+ Id: {style: 'form'},
39
39
  }
@@ -1,8 +1,8 @@
1
- export interface TypeDto {
2
- typeName: string;
3
- nullable: boolean;
4
- isNativeType: boolean;
5
- isTypeReference: boolean;
6
- isVoid: boolean;
7
- isArray: boolean;
1
+ export interface TypeDto {
2
+ typeName: string;
3
+ nullable: boolean;
4
+ isNativeType: boolean;
5
+ isTypeReference: boolean;
6
+ isVoid: boolean;
7
+ isArray: boolean;
8
8
  }
@@ -1,46 +1,46 @@
1
- import fetch, { RequestInit } from 'node-fetch';
2
- import { Swagger } from "./models/swagger/swagger.js";
3
- import { logger } from './utils/logger.js';
4
- import { SwaggerValidator } from './utils/swagger-validator.js';
5
-
6
- const settings: RequestInit = { method: "Get" };
7
-
8
- export class SwaggerDownloader {
9
- async download(swaggerJsonUrl: URL): Promise<Swagger> {
10
- try {
11
- logger.progress(`Downloading Swagger from: ${swaggerJsonUrl}`);
12
-
13
- const response = await fetch(swaggerJsonUrl, settings);
14
-
15
- if (!response.ok) {
16
- throw new Error(`HTTP ${response.status}: ${response.statusText}`);
17
- }
18
-
19
- const json = await response.json();
20
-
21
- // Validate the downloaded swagger document
22
- if (!SwaggerValidator.isValidSwaggerDocument(json)) {
23
- throw new Error('Downloaded document is not a valid Swagger/OpenAPI specification');
24
- }
25
-
26
- // Normalize openapi field to openApi (camelCase) if needed
27
- const jsonAny = json as any;
28
- if (jsonAny.openapi && !jsonAny.openApi) {
29
- jsonAny.openApi = jsonAny.openapi;
30
- }
31
- if (jsonAny.swagger && !jsonAny.openApi) {
32
- jsonAny.openApi = jsonAny.swagger;
33
- }
34
-
35
- const swagger = jsonAny as Swagger;
36
- SwaggerValidator.validate(swagger);
37
-
38
- logger.success(`Swagger document downloaded successfully`);
39
- return swagger;
40
-
41
- } catch (error) {
42
- logger.error(`Failed to download Swagger from ${swaggerJsonUrl}`, error);
43
- throw error;
44
- }
45
- }
46
- }
1
+ import fetch, { RequestInit } from 'node-fetch';
2
+ import { Swagger } from "./models/swagger/swagger.js";
3
+ import { logger } from './utils/logger.js';
4
+ import { SwaggerValidator } from './utils/swagger-validator.js';
5
+
6
+ const settings: RequestInit = { method: "Get" };
7
+
8
+ export class SwaggerDownloader {
9
+ async download(swaggerJsonUrl: URL): Promise<Swagger> {
10
+ try {
11
+ logger.progress(`Downloading Swagger from: ${swaggerJsonUrl}`);
12
+
13
+ const response = await fetch(swaggerJsonUrl, settings);
14
+
15
+ if (!response.ok) {
16
+ throw new Error(`HTTP ${response.status}: ${response.statusText}`);
17
+ }
18
+
19
+ const json = await response.json();
20
+
21
+ // Validate the downloaded swagger document
22
+ if (!SwaggerValidator.isValidSwaggerDocument(json)) {
23
+ throw new Error('Downloaded document is not a valid Swagger/OpenAPI specification');
24
+ }
25
+
26
+ // Normalize openapi field to openApi (camelCase) if needed
27
+ const jsonAny = json as any;
28
+ if (jsonAny.openapi && !jsonAny.openApi) {
29
+ jsonAny.openApi = jsonAny.openapi;
30
+ }
31
+ if (jsonAny.swagger && !jsonAny.openApi) {
32
+ jsonAny.openApi = jsonAny.swagger;
33
+ }
34
+
35
+ const swagger = jsonAny as Swagger;
36
+ SwaggerValidator.validate(swagger);
37
+
38
+ logger.success(`Swagger document downloaded successfully`);
39
+ return swagger;
40
+
41
+ } catch (error) {
42
+ logger.error(`Failed to download Swagger from ${swaggerJsonUrl}`, error);
43
+ throw error;
44
+ }
45
+ }
46
+ }