@devlearning/swagger-generator 1.1.15 → 1.1.17
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/.vscode/launch.json +28 -28
- package/README-OLD.md +209 -209
- package/README.md +277 -277
- package/dist/api.constants.js +22 -22
- package/dist/generator.d.ts +4 -0
- package/dist/generator.js +118 -4
- package/dist/generators-writers/angular/api-angular-writer.js +38 -38
- package/dist/generators-writers/angular/constants.js +24 -24
- package/dist/generators-writers/angular/model-angular-writer.js +6 -6
- package/dist/generators-writers/dart/model-dart-writer.d.ts +1 -0
- package/dist/generators-writers/dart/model-dart-writer.js +13 -0
- package/dist/generators-writers/dart/templates/api.mustache +143 -143
- package/dist/generators-writers/dart/templates/enum.mustache +14 -14
- package/dist/generators-writers/dart/templates/model.mustache +20 -20
- package/dist/generators-writers/nextjs/api-nextjs-writer.js +12 -12
- package/dist/generators-writers/nextjs/constants.js +4 -4
- package/dist/generators-writers/nextjs/model-nextjs-writer.js +6 -6
- package/dist/models/swagger/swagger-component.d.ts +2 -2
- package/dist/models/swagger/swagger-schema.d.ts +1 -0
- package/package.json +49 -49
- package/src/api.constants.ts +26 -26
- package/src/generator-old.ts +449 -449
- package/src/generator.ts +752 -625
- package/src/generators-writers/angular/api-angular-writer.ts +187 -187
- package/src/generators-writers/angular/constants.ts +36 -36
- package/src/generators-writers/angular/model-angular-writer.ts +65 -65
- package/src/generators-writers/angular/normalizator.ts +41 -41
- package/src/generators-writers/dart/api-dart-writer.ts +303 -303
- package/src/generators-writers/dart/model-dart-writer.ts +212 -195
- package/src/generators-writers/dart/models/import-definition-dart.ts +5 -5
- package/src/generators-writers/dart/normalizator.ts +72 -72
- package/src/generators-writers/dart/templates/api.mustache +143 -143
- package/src/generators-writers/dart/templates/enum.mustache +14 -14
- package/src/generators-writers/dart/templates/model.mustache +20 -20
- package/src/generators-writers/nextjs/api-nextjs-writer.ts +157 -157
- package/src/generators-writers/nextjs/constants.ts +5 -5
- package/src/generators-writers/nextjs/model-nextjs-writer.ts +61 -61
- package/src/generators-writers/utils.ts +93 -93
- package/src/index.ts +103 -103
- package/src/models/api-dto.ts +17 -17
- package/src/models/enum-value-dto.ts +3 -3
- package/src/models/model-dto.ts +9 -9
- package/src/models/parameter-dto.ts +7 -7
- package/src/models/property-dto.ts +4 -4
- package/src/models/swagger/swagger-component-property.ts +11 -11
- package/src/models/swagger/swagger-component.ts +17 -17
- package/src/models/swagger/swagger-content.ts +4 -4
- package/src/models/swagger/swagger-info.ts +3 -3
- package/src/models/swagger/swagger-method.ts +7 -7
- package/src/models/swagger/swagger-schema.ts +21 -20
- package/src/models/swagger/swagger.ts +38 -38
- package/src/models/type-dto.ts +7 -7
- package/src/swagger-downloader.ts +46 -46
- package/src/utils/logger.ts +73 -73
- package/src/utils/swagger-validator.ts +89 -89
- package/tsconfig.json +33 -33
- package/dist/templates/api.mustache +0 -29
- package/dist/templates/model.mustache +0 -18
package/.vscode/launch.json
CHANGED
|
@@ -1,29 +1,29 @@
|
|
|
1
|
-
{
|
|
2
|
-
"version": "0.2.0",
|
|
3
|
-
"configurations": [
|
|
4
|
-
{
|
|
5
|
-
"command": "npm run debug-angular",
|
|
6
|
-
"name": "Debug Angular",
|
|
7
|
-
"request": "launch",
|
|
8
|
-
"type": "node-terminal"
|
|
9
|
-
},
|
|
10
|
-
{
|
|
11
|
-
"command": "npm run debug-nextjs",
|
|
12
|
-
"name": "Debug NextJs",
|
|
13
|
-
"request": "launch",
|
|
14
|
-
"type": "node-terminal"
|
|
15
|
-
},
|
|
16
|
-
{
|
|
17
|
-
"command": "nvm use 20.19.4 && npm run debug-flutter",
|
|
18
|
-
"name": "Debug MAC Flutter",
|
|
19
|
-
"request": "launch",
|
|
20
|
-
"type": "node-terminal"
|
|
21
|
-
},
|
|
22
|
-
{
|
|
23
|
-
"command": "npm run debug-flutter",
|
|
24
|
-
"name": "Debug Flutter",
|
|
25
|
-
"request": "launch",
|
|
26
|
-
"type": "node-terminal"
|
|
27
|
-
}
|
|
28
|
-
]
|
|
1
|
+
{
|
|
2
|
+
"version": "0.2.0",
|
|
3
|
+
"configurations": [
|
|
4
|
+
{
|
|
5
|
+
"command": "npm run debug-angular",
|
|
6
|
+
"name": "Debug Angular",
|
|
7
|
+
"request": "launch",
|
|
8
|
+
"type": "node-terminal"
|
|
9
|
+
},
|
|
10
|
+
{
|
|
11
|
+
"command": "npm run debug-nextjs",
|
|
12
|
+
"name": "Debug NextJs",
|
|
13
|
+
"request": "launch",
|
|
14
|
+
"type": "node-terminal"
|
|
15
|
+
},
|
|
16
|
+
{
|
|
17
|
+
"command": "nvm use 20.19.4 && npm run debug-flutter",
|
|
18
|
+
"name": "Debug MAC Flutter",
|
|
19
|
+
"request": "launch",
|
|
20
|
+
"type": "node-terminal"
|
|
21
|
+
},
|
|
22
|
+
{
|
|
23
|
+
"command": "npm run debug-flutter",
|
|
24
|
+
"name": "Debug Flutter",
|
|
25
|
+
"request": "launch",
|
|
26
|
+
"type": "node-terminal"
|
|
27
|
+
}
|
|
28
|
+
]
|
|
29
29
|
}
|
package/README-OLD.md
CHANGED
|
@@ -1,210 +1,210 @@
|
|
|
1
|
-
# Swagger Generator for Angular, Next.js and Flutter
|
|
2
|
-
|
|
3
|
-
This tool automates the generation of API clients for Angular, Next.js and Flutter using a Swagger (OpenAPI) specification. It creates TypeScript models and service classes, making API integration seamless and type-safe.
|
|
4
|
-
|
|
5
|
-
## Features
|
|
6
|
-
|
|
7
|
-
- 🚀 **Automatic Model Generation** – Converts Swagger schemas into TypeScript interfaces or classes.
|
|
8
|
-
- 🔥 **API Client Generation** – Creates service methods for making API calls with authentication, headers, and request parameters.
|
|
9
|
-
- 🎯 **Framework-Specific Output**:
|
|
10
|
-
- **Angular** – Generates injectable services using `HttpClient`.
|
|
11
|
-
- **Next.js** – Provides fetch-based or Axios-based API functions, optimized for both server-side and client-side usage.
|
|
12
|
-
- **Flutter** – Provides Flutter with Dio API functions, optimized for both server-side and client-side usage.
|
|
13
|
-
- ✅ **Strong Typing** – Ensures type safety for API requests and responses.
|
|
14
|
-
- ⚡ **Customization** – Configurable options for method naming, error handling, and request structure.
|
|
15
|
-
- 🔄 **Auto-Sync with Backend** – Keeps API clients up-to-date with backend changes.
|
|
16
|
-
|
|
17
|
-
## Installation
|
|
18
|
-
|
|
19
|
-
```sh
|
|
20
|
-
npm i @devlearning/swagger-generator --save-dev
|
|
21
|
-
```
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
add npm script:
|
|
25
|
-
|
|
26
|
-
"swgen": "swgen http://localhost:7550/swagger/ApiGateway/swagger.json src/app/core/autogenerated angular moment"
|
|
27
|
-
|
|
28
|
-
params:
|
|
29
|
-
- url of swagger.json: not https
|
|
30
|
-
- output path for autogenerated files: src/app/core/autogenerated
|
|
31
|
-
- target framework/library: angular/next
|
|
32
|
-
- type of date management:
|
|
33
|
-
- angular only support momentjs, sorry :(
|
|
34
|
-
- nextjs only support date-fns
|
|
35
|
-
- dart native date management
|
|
36
|
-
|
|
37
|
-
create output path like this: src/app/core/autogenerated
|
|
38
|
-
|
|
39
|
-
## Angular configuration
|
|
40
|
-
|
|
41
|
-
create ApiService like this
|
|
42
|
-
```ts
|
|
43
|
-
import { HttpClient, HttpErrorResponse } from '@angular/common/http';
|
|
44
|
-
import { Injectable } from '@angular/core';
|
|
45
|
-
import { ApiAutogeneratedService } from '../autogenerated/api.autogenerated';
|
|
46
|
-
import { environment } from 'src/environments/environment';
|
|
47
|
-
import { ResponseError } from '../models/response-error';
|
|
48
|
-
import { throwError } from 'rxjs';
|
|
49
|
-
import * as moment from 'moment-timezone';
|
|
50
|
-
|
|
51
|
-
@Injectable({
|
|
52
|
-
providedIn: 'root',
|
|
53
|
-
})
|
|
54
|
-
export class ApiService extends ApiAutogeneratedService {
|
|
55
|
-
|
|
56
|
-
private _dateTimeFormat = /^((19|20)[0-9][0-9])[-](0[1-9]|1[012])[-](0[1-9]|[12][0-9]|3[01])[T]([01]?[0-9]|[2][0-3])[:]([0-5][0-9])[:]([0-5][0-9])[.]([0-9][0-9][0-9])([+|-]([01][0-9]|[2][0-3])[:]([0-5][0-9])){0,1}$/;
|
|
57
|
-
private _timeFormat = /^([01]?[0-9]|[2][0-3])[:]([0-5][0-9])[:]([0-5][0-9])[.]([0-9][0-9][0-9])$/;
|
|
58
|
-
private _ianaName: string = Intl.DateTimeFormat().resolvedOptions().timeZone;
|
|
59
|
-
private _dateTimeFormatString = "YYYY-MM-DDTHH:mm:ss.SSSZ";
|
|
60
|
-
private _timeFormatString = "HH:mm:ss.SSS";
|
|
61
|
-
|
|
62
|
-
public get ianaName() { return this._ianaName; }
|
|
63
|
-
|
|
64
|
-
constructor(
|
|
65
|
-
public override _http: HttpClient,
|
|
66
|
-
) {
|
|
67
|
-
super(_http, environment.BASE_URL);
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
protected override _momentToString(moment: moment.Moment) {
|
|
71
|
-
return (<moment.Moment>moment).format(this._dateTimeFormatString);
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
protected override _handleRequest<T>(request: T) {
|
|
75
|
-
if (request === null || request === undefined) {
|
|
76
|
-
return request;
|
|
77
|
-
}
|
|
78
|
-
if (typeof request !== 'object') {
|
|
79
|
-
return request;
|
|
80
|
-
}
|
|
81
|
-
|
|
82
|
-
var clonedRequest = { ...request };
|
|
83
|
-
|
|
84
|
-
for (const key of Object.keys(clonedRequest)) {
|
|
85
|
-
const value = (<any>clonedRequest)[key];
|
|
86
|
-
if (moment.isMoment(value)) {
|
|
87
|
-
(<any>clonedRequest)[key] = this._momentToString(value);
|
|
88
|
-
} else if (typeof value === 'object') {
|
|
89
|
-
this._handleRequest(value);
|
|
90
|
-
}
|
|
91
|
-
}
|
|
92
|
-
|
|
93
|
-
return clonedRequest;
|
|
94
|
-
}
|
|
95
|
-
|
|
96
|
-
protected override _handleMultipart<T>(request: T): FormData {
|
|
97
|
-
const formData = new FormData();
|
|
98
|
-
if (request === null || request === undefined) {
|
|
99
|
-
return formData;
|
|
100
|
-
}
|
|
101
|
-
|
|
102
|
-
for (const key of Object.keys(request)) {
|
|
103
|
-
const value = (<any>request)[key];
|
|
104
|
-
if (value instanceof File) {
|
|
105
|
-
formData.append(key, value, value.name);
|
|
106
|
-
} else {
|
|
107
|
-
formData.append(key, value.toString());
|
|
108
|
-
}
|
|
109
|
-
}
|
|
110
|
-
|
|
111
|
-
return formData;
|
|
112
|
-
}
|
|
113
|
-
|
|
114
|
-
public override _handleResponse<T>(response: T) {
|
|
115
|
-
if (response === null || response === undefined) {
|
|
116
|
-
return response;
|
|
117
|
-
}
|
|
118
|
-
if (typeof response !== 'object') {
|
|
119
|
-
return response;
|
|
120
|
-
}
|
|
121
|
-
|
|
122
|
-
for (const key of Object.keys(response)) {
|
|
123
|
-
const value = (<any>response)[key];
|
|
124
|
-
if (this._isDateString(value)) {
|
|
125
|
-
(<any>response)[key] = moment.tz(value, this._dateTimeFormatString, this._ianaName);
|
|
126
|
-
} else if (this._isTimeString(value)) {
|
|
127
|
-
(<any>response)[key] = moment.tz(value, this._timeFormatString, this._ianaName);
|
|
128
|
-
} else if (typeof value === 'object') {
|
|
129
|
-
this._handleResponse(value);
|
|
130
|
-
}
|
|
131
|
-
}
|
|
132
|
-
|
|
133
|
-
return response;
|
|
134
|
-
}
|
|
135
|
-
|
|
136
|
-
protected override _handleError(error: any, obs: any) {
|
|
137
|
-
let responseError = new ResponseError();
|
|
138
|
-
if (error.error instanceof Error) {
|
|
139
|
-
console.error('Api - an error occurred:', error.error.message);
|
|
140
|
-
responseError.message = error.error.message;
|
|
141
|
-
} else if (error instanceof HttpErrorResponse) {
|
|
142
|
-
responseError = ResponseError.CreateFromHttpErrorResponse(error);
|
|
143
|
-
}
|
|
144
|
-
|
|
145
|
-
console.error(`Api - error code ${error.status} message ${responseError.message} ${responseError.stacktrace}`);
|
|
146
|
-
|
|
147
|
-
return throwError(() => responseError);
|
|
148
|
-
}
|
|
149
|
-
|
|
150
|
-
private _isDateString(value: string) {
|
|
151
|
-
if (value === null || value === undefined) {
|
|
152
|
-
return false;
|
|
153
|
-
}
|
|
154
|
-
if (typeof value === 'string') {
|
|
155
|
-
return this._dateTimeFormat.test(value);
|
|
156
|
-
}
|
|
157
|
-
return false;
|
|
158
|
-
}
|
|
159
|
-
|
|
160
|
-
private _isTimeString(value: string) {
|
|
161
|
-
if (value === null || value === undefined) {
|
|
162
|
-
return false;
|
|
163
|
-
}
|
|
164
|
-
if (typeof value === 'string') {
|
|
165
|
-
return this._timeFormat.test(value);
|
|
166
|
-
}
|
|
167
|
-
return false;
|
|
168
|
-
}
|
|
169
|
-
}
|
|
170
|
-
```
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
```ts
|
|
174
|
-
import { HttpErrorResponse } from '@angular/common/http';
|
|
175
|
-
|
|
176
|
-
export class ResponseError {
|
|
177
|
-
public url: string | null;
|
|
178
|
-
public status: number | null;
|
|
179
|
-
public message: string | null;
|
|
180
|
-
public stacktrace: string | null;
|
|
181
|
-
|
|
182
|
-
constructor() {
|
|
183
|
-
this.url = null;
|
|
184
|
-
this.status = null;
|
|
185
|
-
this.message = null;
|
|
186
|
-
this.stacktrace = null;
|
|
187
|
-
}
|
|
188
|
-
|
|
189
|
-
static CreateFromHttpErrorResponse(httpErrorResponse: HttpErrorResponse) {
|
|
190
|
-
let responseError = new ResponseError();
|
|
191
|
-
|
|
192
|
-
if (httpErrorResponse.error != null && !(httpErrorResponse.error instanceof ProgressEvent)) {
|
|
193
|
-
if (httpErrorResponse.error.hasOwnProperty('message')) {
|
|
194
|
-
responseError.message = httpErrorResponse.error.message;
|
|
195
|
-
}
|
|
196
|
-
|
|
197
|
-
if (httpErrorResponse.error.hasOwnProperty('stacktrace')) {
|
|
198
|
-
responseError.stacktrace = httpErrorResponse.error.stacktrace;
|
|
199
|
-
}
|
|
200
|
-
} else {
|
|
201
|
-
responseError.message = httpErrorResponse.message;
|
|
202
|
-
}
|
|
203
|
-
|
|
204
|
-
responseError.status = httpErrorResponse.status;
|
|
205
|
-
responseError.url = httpErrorResponse.url;
|
|
206
|
-
|
|
207
|
-
return responseError;
|
|
208
|
-
}
|
|
209
|
-
}
|
|
1
|
+
# Swagger Generator for Angular, Next.js and Flutter
|
|
2
|
+
|
|
3
|
+
This tool automates the generation of API clients for Angular, Next.js and Flutter using a Swagger (OpenAPI) specification. It creates TypeScript models and service classes, making API integration seamless and type-safe.
|
|
4
|
+
|
|
5
|
+
## Features
|
|
6
|
+
|
|
7
|
+
- 🚀 **Automatic Model Generation** – Converts Swagger schemas into TypeScript interfaces or classes.
|
|
8
|
+
- 🔥 **API Client Generation** – Creates service methods for making API calls with authentication, headers, and request parameters.
|
|
9
|
+
- 🎯 **Framework-Specific Output**:
|
|
10
|
+
- **Angular** – Generates injectable services using `HttpClient`.
|
|
11
|
+
- **Next.js** – Provides fetch-based or Axios-based API functions, optimized for both server-side and client-side usage.
|
|
12
|
+
- **Flutter** – Provides Flutter with Dio API functions, optimized for both server-side and client-side usage.
|
|
13
|
+
- ✅ **Strong Typing** – Ensures type safety for API requests and responses.
|
|
14
|
+
- ⚡ **Customization** – Configurable options for method naming, error handling, and request structure.
|
|
15
|
+
- 🔄 **Auto-Sync with Backend** – Keeps API clients up-to-date with backend changes.
|
|
16
|
+
|
|
17
|
+
## Installation
|
|
18
|
+
|
|
19
|
+
```sh
|
|
20
|
+
npm i @devlearning/swagger-generator --save-dev
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
add npm script:
|
|
25
|
+
|
|
26
|
+
"swgen": "swgen http://localhost:7550/swagger/ApiGateway/swagger.json src/app/core/autogenerated angular moment"
|
|
27
|
+
|
|
28
|
+
params:
|
|
29
|
+
- url of swagger.json: not https
|
|
30
|
+
- output path for autogenerated files: src/app/core/autogenerated
|
|
31
|
+
- target framework/library: angular/next
|
|
32
|
+
- type of date management:
|
|
33
|
+
- angular only support momentjs, sorry :(
|
|
34
|
+
- nextjs only support date-fns
|
|
35
|
+
- dart native date management
|
|
36
|
+
|
|
37
|
+
create output path like this: src/app/core/autogenerated
|
|
38
|
+
|
|
39
|
+
## Angular configuration
|
|
40
|
+
|
|
41
|
+
create ApiService like this
|
|
42
|
+
```ts
|
|
43
|
+
import { HttpClient, HttpErrorResponse } from '@angular/common/http';
|
|
44
|
+
import { Injectable } from '@angular/core';
|
|
45
|
+
import { ApiAutogeneratedService } from '../autogenerated/api.autogenerated';
|
|
46
|
+
import { environment } from 'src/environments/environment';
|
|
47
|
+
import { ResponseError } from '../models/response-error';
|
|
48
|
+
import { throwError } from 'rxjs';
|
|
49
|
+
import * as moment from 'moment-timezone';
|
|
50
|
+
|
|
51
|
+
@Injectable({
|
|
52
|
+
providedIn: 'root',
|
|
53
|
+
})
|
|
54
|
+
export class ApiService extends ApiAutogeneratedService {
|
|
55
|
+
|
|
56
|
+
private _dateTimeFormat = /^((19|20)[0-9][0-9])[-](0[1-9]|1[012])[-](0[1-9]|[12][0-9]|3[01])[T]([01]?[0-9]|[2][0-3])[:]([0-5][0-9])[:]([0-5][0-9])[.]([0-9][0-9][0-9])([+|-]([01][0-9]|[2][0-3])[:]([0-5][0-9])){0,1}$/;
|
|
57
|
+
private _timeFormat = /^([01]?[0-9]|[2][0-3])[:]([0-5][0-9])[:]([0-5][0-9])[.]([0-9][0-9][0-9])$/;
|
|
58
|
+
private _ianaName: string = Intl.DateTimeFormat().resolvedOptions().timeZone;
|
|
59
|
+
private _dateTimeFormatString = "YYYY-MM-DDTHH:mm:ss.SSSZ";
|
|
60
|
+
private _timeFormatString = "HH:mm:ss.SSS";
|
|
61
|
+
|
|
62
|
+
public get ianaName() { return this._ianaName; }
|
|
63
|
+
|
|
64
|
+
constructor(
|
|
65
|
+
public override _http: HttpClient,
|
|
66
|
+
) {
|
|
67
|
+
super(_http, environment.BASE_URL);
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
protected override _momentToString(moment: moment.Moment) {
|
|
71
|
+
return (<moment.Moment>moment).format(this._dateTimeFormatString);
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
protected override _handleRequest<T>(request: T) {
|
|
75
|
+
if (request === null || request === undefined) {
|
|
76
|
+
return request;
|
|
77
|
+
}
|
|
78
|
+
if (typeof request !== 'object') {
|
|
79
|
+
return request;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
var clonedRequest = { ...request };
|
|
83
|
+
|
|
84
|
+
for (const key of Object.keys(clonedRequest)) {
|
|
85
|
+
const value = (<any>clonedRequest)[key];
|
|
86
|
+
if (moment.isMoment(value)) {
|
|
87
|
+
(<any>clonedRequest)[key] = this._momentToString(value);
|
|
88
|
+
} else if (typeof value === 'object') {
|
|
89
|
+
this._handleRequest(value);
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
return clonedRequest;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
protected override _handleMultipart<T>(request: T): FormData {
|
|
97
|
+
const formData = new FormData();
|
|
98
|
+
if (request === null || request === undefined) {
|
|
99
|
+
return formData;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
for (const key of Object.keys(request)) {
|
|
103
|
+
const value = (<any>request)[key];
|
|
104
|
+
if (value instanceof File) {
|
|
105
|
+
formData.append(key, value, value.name);
|
|
106
|
+
} else {
|
|
107
|
+
formData.append(key, value.toString());
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
return formData;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
public override _handleResponse<T>(response: T) {
|
|
115
|
+
if (response === null || response === undefined) {
|
|
116
|
+
return response;
|
|
117
|
+
}
|
|
118
|
+
if (typeof response !== 'object') {
|
|
119
|
+
return response;
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
for (const key of Object.keys(response)) {
|
|
123
|
+
const value = (<any>response)[key];
|
|
124
|
+
if (this._isDateString(value)) {
|
|
125
|
+
(<any>response)[key] = moment.tz(value, this._dateTimeFormatString, this._ianaName);
|
|
126
|
+
} else if (this._isTimeString(value)) {
|
|
127
|
+
(<any>response)[key] = moment.tz(value, this._timeFormatString, this._ianaName);
|
|
128
|
+
} else if (typeof value === 'object') {
|
|
129
|
+
this._handleResponse(value);
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
return response;
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
protected override _handleError(error: any, obs: any) {
|
|
137
|
+
let responseError = new ResponseError();
|
|
138
|
+
if (error.error instanceof Error) {
|
|
139
|
+
console.error('Api - an error occurred:', error.error.message);
|
|
140
|
+
responseError.message = error.error.message;
|
|
141
|
+
} else if (error instanceof HttpErrorResponse) {
|
|
142
|
+
responseError = ResponseError.CreateFromHttpErrorResponse(error);
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
console.error(`Api - error code ${error.status} message ${responseError.message} ${responseError.stacktrace}`);
|
|
146
|
+
|
|
147
|
+
return throwError(() => responseError);
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
private _isDateString(value: string) {
|
|
151
|
+
if (value === null || value === undefined) {
|
|
152
|
+
return false;
|
|
153
|
+
}
|
|
154
|
+
if (typeof value === 'string') {
|
|
155
|
+
return this._dateTimeFormat.test(value);
|
|
156
|
+
}
|
|
157
|
+
return false;
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
private _isTimeString(value: string) {
|
|
161
|
+
if (value === null || value === undefined) {
|
|
162
|
+
return false;
|
|
163
|
+
}
|
|
164
|
+
if (typeof value === 'string') {
|
|
165
|
+
return this._timeFormat.test(value);
|
|
166
|
+
}
|
|
167
|
+
return false;
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
```
|
|
171
|
+
|
|
172
|
+
|
|
173
|
+
```ts
|
|
174
|
+
import { HttpErrorResponse } from '@angular/common/http';
|
|
175
|
+
|
|
176
|
+
export class ResponseError {
|
|
177
|
+
public url: string | null;
|
|
178
|
+
public status: number | null;
|
|
179
|
+
public message: string | null;
|
|
180
|
+
public stacktrace: string | null;
|
|
181
|
+
|
|
182
|
+
constructor() {
|
|
183
|
+
this.url = null;
|
|
184
|
+
this.status = null;
|
|
185
|
+
this.message = null;
|
|
186
|
+
this.stacktrace = null;
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
static CreateFromHttpErrorResponse(httpErrorResponse: HttpErrorResponse) {
|
|
190
|
+
let responseError = new ResponseError();
|
|
191
|
+
|
|
192
|
+
if (httpErrorResponse.error != null && !(httpErrorResponse.error instanceof ProgressEvent)) {
|
|
193
|
+
if (httpErrorResponse.error.hasOwnProperty('message')) {
|
|
194
|
+
responseError.message = httpErrorResponse.error.message;
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
if (httpErrorResponse.error.hasOwnProperty('stacktrace')) {
|
|
198
|
+
responseError.stacktrace = httpErrorResponse.error.stacktrace;
|
|
199
|
+
}
|
|
200
|
+
} else {
|
|
201
|
+
responseError.message = httpErrorResponse.message;
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
responseError.status = httpErrorResponse.status;
|
|
205
|
+
responseError.url = httpErrorResponse.url;
|
|
206
|
+
|
|
207
|
+
return responseError;
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
210
|
```
|