@devlearning/swagger-generator 1.0.3 → 1.0.5

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