@grandlinex/swagger-mate 1.2.2 → 1.3.2

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 (37) hide show
  1. package/dist/cjs/Swagger/Client/ClientUtil.d.ts +18 -4
  2. package/dist/cjs/Swagger/Client/ClientUtil.js +51 -8
  3. package/dist/cjs/Swagger/Client/InterfaceTemplate.js +1 -1
  4. package/dist/cjs/Swagger/Client/SwaggerClient.js +5 -5
  5. package/dist/cjs/Swagger/Meta/SwaggerTypes.d.ts +1 -0
  6. package/dist/cjs/Swagger/Path/SPathUtil.d.ts +173 -2
  7. package/dist/cjs/Swagger/Path/SPathUtil.js +261 -5
  8. package/dist/cjs/Swagger/SwaggerUtil.d.ts +18 -2
  9. package/dist/cjs/Swagger/SwaggerUtil.js +32 -5
  10. package/dist/cjs/Swagger/annotation/index.d.ts +8 -3
  11. package/dist/cjs/Swagger/debug/BaseCon.d.ts +115 -11
  12. package/dist/cjs/Swagger/debug/BaseCon.js +142 -38
  13. package/dist/cjs/cli.js +5 -1
  14. package/dist/mjs/Swagger/Client/ClientUtil.d.ts +18 -4
  15. package/dist/mjs/Swagger/Client/ClientUtil.js +50 -8
  16. package/dist/mjs/Swagger/Client/InterfaceTemplate.js +2 -2
  17. package/dist/mjs/Swagger/Client/SwaggerClient.js +5 -5
  18. package/dist/mjs/Swagger/Meta/SwaggerTypes.d.ts +1 -0
  19. package/dist/mjs/Swagger/Path/SPathUtil.d.ts +173 -2
  20. package/dist/mjs/Swagger/Path/SPathUtil.js +261 -5
  21. package/dist/mjs/Swagger/SwaggerUtil.d.ts +18 -2
  22. package/dist/mjs/Swagger/SwaggerUtil.js +33 -6
  23. package/dist/mjs/Swagger/annotation/index.d.ts +8 -3
  24. package/dist/mjs/Swagger/debug/BaseCon.d.ts +115 -11
  25. package/dist/mjs/Swagger/debug/BaseCon.js +142 -38
  26. package/dist/mjs/cli.js +5 -1
  27. package/package.json +10 -8
  28. package/res/html/rapi-doc/index.html +28 -0
  29. package/res/html/rapi-doc/rapidoc-min.js +3915 -0
  30. package/res/html/{index.html → swagger-ui/index.html} +11 -3
  31. package/res/html/swagger-ui/swagger-ui-bundle.js +2 -0
  32. package/res/html/swagger-ui/swagger-ui-standalone-preset.js +2 -0
  33. package/res/html/swagger-ui/swagger-ui.css +3 -0
  34. package/res/templates/class/BaseCon.ts +160 -61
  35. package/res/html/swagger-ui-bundle.js +0 -2
  36. package/res/html/swagger-ui-standalone-preset.js +0 -2
  37. package/res/html/swagger-ui.css +0 -3
@@ -54,18 +54,26 @@ export interface ConHandle {
54
54
  config?: ConHandleConfig,
55
55
  ): Promise<ConHandleResponse<T>>;
56
56
  }
57
+
58
+ /**
59
+ * BaseCon provides a minimal client for interacting with an HTTP backend.
60
+ * It manages connection state, authentication tokens, and reconnection
61
+ * logic while delegating actual HTTP requests to a supplied {@link ConHandle}.
62
+ *
63
+ * @class
64
+ */
57
65
  export default class BaseCon {
58
- api: string;
66
+ private api: string;
59
67
 
60
- permanentHeader: undefined | Record<string, string>;
68
+ private permanentHeader: undefined | Record<string, string>;
61
69
 
62
- authorization: string | null;
70
+ private authorization: string | null;
63
71
 
64
- disconnected: boolean;
72
+ private noAuth: boolean;
65
73
 
66
- failFlag: boolean;
74
+ private disconnected: boolean;
67
75
 
68
- logger: (arg: any) => void;
76
+ private readonly logger: (arg: any) => void;
69
77
 
70
78
  con: ConHandle;
71
79
 
@@ -79,10 +87,10 @@ export default class BaseCon {
79
87
  logger?: (arg: any) => void;
80
88
  }) {
81
89
  this.api = conf.endpoint;
82
- this.logger = conf.logger || console.log;
90
+ this.logger = conf.logger ?? console.log;
83
91
  this.disconnected = true;
92
+ this.noAuth = false;
84
93
  this.authorization = null;
85
- this.failFlag = false;
86
94
  this.con = conf.con;
87
95
  this.reconnect = async () => {
88
96
  this.disconnected = true;
@@ -92,17 +100,48 @@ export default class BaseCon {
92
100
  this.handle = this.handle.bind(this);
93
101
  }
94
102
 
95
- // Class helper functions
103
+ /**
104
+ * Retrieves the API endpoint.
105
+ *
106
+ * @return {string} The API endpoint string.
107
+ */
108
+ getApiEndpoint(): string {
109
+ return this.api;
110
+ }
96
111
 
97
- isConnected() {
98
- return this.authorization !== null && !this.disconnected;
112
+ /**
113
+ * Sets the API endpoint URL used by the client.
114
+ *
115
+ * @param {string} endpoint - The full URL of the API endpoint.
116
+ * @returns {void}
117
+ */
118
+ setApiEndpoint(endpoint: string): void {
119
+ this.api = endpoint;
99
120
  }
100
121
 
101
- token() {
122
+ /**
123
+ * Indicates whether the instance is considered connected.
124
+ *
125
+ * The instance is regarded as connected when it either does not require authentication
126
+ * (`noAuth` is true) or it has an authorization token set (`authorization` is not null),
127
+ * and it is not currently marked as disconnected.
128
+ *
129
+ * @return {boolean} `true` if the instance is connected, `false` otherwise.
130
+ */
131
+ isConnected(): boolean {
132
+ return (this.noAuth || this.authorization !== null) && !this.disconnected;
133
+ }
134
+
135
+ /**
136
+ * Returns the current authorization token.
137
+ *
138
+ * @return {string} The authorization token or an empty string if none is set.
139
+ */
140
+ token(): string {
102
141
  return this.authorization || '';
103
142
  }
104
143
 
105
- p(path: string, config?: ConHandleConfig) {
144
+ private p(path: string, config?: ConHandleConfig) {
106
145
  let pp = path;
107
146
  if (config?.param) {
108
147
  const e = Object.keys(config.param);
@@ -125,46 +164,36 @@ export default class BaseCon {
125
164
  return `${this.api}${pp}`;
126
165
  }
127
166
 
167
+ /**
168
+ * Sends a ping request to the API to verify connectivity and version availability.
169
+ *
170
+ * @return {boolean} `true` if the API responded with a 200 status code and a valid version object; `false` otherwise.
171
+ */
128
172
  async ping(): Promise<boolean> {
129
173
  try {
130
174
  const version = await this.con.get<{ api: number }>(this.p('/version'));
131
- return version.data?.api === 1 && version.code === 200;
175
+ return version.data?.api !== undefined && version.code === 200;
132
176
  } catch (e) {
133
177
  this.logger('ping failed');
134
178
  return false;
135
179
  }
136
180
  }
137
181
 
138
- async test(email: string, password: string): Promise<boolean> {
139
- const ping = await this.ping();
140
-
141
- if (ping) {
142
- try {
143
- this.logger({ email, password });
144
- const con = await this.con.post<
145
- { token: string },
146
- {
147
- username: string;
148
- token: string;
149
- }
150
- >(this.p('/token'), {
151
- username: email,
152
- token: password,
153
- });
154
- return con.code === 200 || con.code === 201;
155
- } catch (e) {
156
- this.logger(e);
157
- this.logger('cant connect to backend');
158
- }
159
- }
160
- this.logger('test ping failed');
161
-
162
- return false;
163
- }
164
-
182
+ /**
183
+ * Validates the current authentication token by performing a ping and a test request
184
+ * to the backend. The method first ensures connectivity via {@link ping}. If the ping
185
+ * succeeds, it attempts to retrieve a token from the `/test/auth` endpoint using the
186
+ * current token in the `Authorization` header. The operation is considered successful
187
+ * if the response status code is 200 or 201.
188
+ *
189
+ * If any step fails, an error is logged and the method returns {@code false}. On
190
+ * success, it returns {@code true}.
191
+ *
192
+ * @return {Promise<boolean>} A promise that resolves to {@code true} if the token
193
+ * test succeeds, otherwise {@code false}.
194
+ */
165
195
  async testToken(): Promise<boolean> {
166
196
  const ping = await this.ping();
167
-
168
197
  if (ping) {
169
198
  try {
170
199
  const con = await this.con.get<{ token: string }>(
@@ -186,9 +215,66 @@ export default class BaseCon {
186
215
  return false;
187
216
  }
188
217
 
189
- async connect(email: string, pw: string): Promise<boolean> {
218
+ /**
219
+ * Attempts to establish a connection to the backend without authentication.
220
+ *
221
+ * This method sends a ping request. If the ping succeeds, it clears any
222
+ * existing authorization data, marks the instance as connected,
223
+ * enables the no‑authentication mode, and returns `true`. If the ping
224
+ * fails, it logs a warning, clears authorization, marks the instance
225
+ * as disconnected, and returns `false`.
226
+ *
227
+ * @return {Promise<boolean>} `true` when a connection is successfully
228
+ * established without authentication, otherwise `false`.
229
+ */
230
+ async connectNoAuth(): Promise<boolean> {
190
231
  const ping = await this.ping();
232
+ if (ping) {
233
+ this.authorization = null;
234
+ this.disconnected = false;
235
+ this.noAuth = true;
236
+ return true;
237
+ }
238
+ this.logger('cant connect to backend');
239
+ this.authorization = null;
240
+ this.disconnected = true;
241
+ return false;
242
+ }
191
243
 
244
+ /**
245
+ * Forces a connection using the provided bearer token.
246
+ *
247
+ * @param {string} token The token to be used for authentication.
248
+ * @returns {void}
249
+ */
250
+ forceConnectWithToken(token: string): void {
251
+ this.authorization = `Bearer ${token}`;
252
+ this.disconnected = false;
253
+ this.noAuth = false;
254
+ }
255
+
256
+ /**
257
+ * Establishes a connection to the backend using the supplied credentials.
258
+ * Performs a health‑check ping first; if successful, it requests an authentication
259
+ * token from the `/token` endpoint. When the token is obtained, the method
260
+ * updates internal state (authorization header, connection flags) and, unless
261
+ * a dry run is requested, sets up a reconnection routine. Any errors are
262
+ * logged and the method resolves to `false`.
263
+ *
264
+ * @param {string} email - The user's email address for authentication.
265
+ * @param {string} pw - The password (or token) for the specified user.
266
+ * @param {boolean} [dry=false] - If `true`, the method performs a dry run
267
+ * without persisting credentials or configuring reconnection logic.
268
+ *
269
+ * @returns Promise<boolean> `true` if the connection was successfully
270
+ * established, otherwise `false`.
271
+ */
272
+ async connect(
273
+ email: string,
274
+ pw: string,
275
+ dry: boolean = false,
276
+ ): Promise<boolean> {
277
+ const ping = await this.ping();
192
278
  if (ping) {
193
279
  try {
194
280
  const token = await this.con.post<
@@ -201,13 +287,17 @@ export default class BaseCon {
201
287
  username: email,
202
288
  token: pw,
203
289
  });
204
- // TODO check token
205
290
  if (token.code === 200 || token.code === 201) {
206
- this.authorization = `Bearer ${token.data?.token}`;
207
- this.disconnected = false;
208
- this.reconnect = async () => {
209
- return (await this.connect(email, pw)) && (await this.testToken());
210
- };
291
+ if (!dry) {
292
+ this.authorization = `Bearer ${token.data?.token}`;
293
+ this.disconnected = false;
294
+ this.noAuth = false;
295
+ this.reconnect = async () => {
296
+ return (
297
+ (await this.connect(email, pw)) && (await this.testToken())
298
+ );
299
+ };
300
+ }
211
301
  return true;
212
302
  }
213
303
  } catch (e) {
@@ -217,24 +307,38 @@ export default class BaseCon {
217
307
  this.logger('cant connect to backend');
218
308
  this.authorization = null;
219
309
  this.disconnected = true;
310
+ this.noAuth = false;
220
311
  return false;
221
312
  }
222
313
 
223
314
  /**
224
- * Enable client before auth
315
+ * Performs an HTTP request using the client’s internal connection.
316
+ *
317
+ * The method verifies that the client is connected before attempting a request.
318
+ * It automatically injects the authorization token, permanent headers, and any
319
+ * headers supplied in `config`. If the request body is a `FormData` instance
320
+ * (or provides a `getHeaders` method), the appropriate form headers are added.
321
+ *
322
+ * Response handling:
323
+ * - `200` or `201`: returns `success: true` with the received data.
324
+ * - `498`: attempts to reconnect and retries the request once.
325
+ * - `401`: logs an authentication error, marks the client as disconnected.
326
+ * - `403` and other status codes: return `success: false` with the status code.
327
+ *
328
+ * @param {'POST'|'GET'|'PATCH'|'DELETE'} type The HTTP method to use.
329
+ * @param {string} path The endpoint path relative to the base URL.
330
+ * @param {J} [body] Optional request payload. May be `FormData` or a plain object.
331
+ * @param {ConHandleConfig} [config] Optional Axios-like configuration for the request.
332
+ * @returns {Promise<HandleRes<T>>} A promise that resolves to a `HandleRes` object
333
+ * containing the response data, status code, any error information, and headers.
225
334
  */
226
- fakeEnableClient() {
227
- this.authorization = 'DEBUG';
228
- this.disconnected = false;
229
- }
230
-
231
335
  async handle<T, J>(
232
336
  type: 'POST' | 'GET' | 'PATCH' | 'DELETE',
233
337
  path: string,
234
338
  body?: J,
235
339
  config?: ConHandleConfig,
236
340
  ): Promise<HandleRes<T>> {
237
- if (!this.authorization || this.disconnected) {
341
+ if (!this.isConnected()) {
238
342
  this.logger('Disconnected');
239
343
  return {
240
344
  success: false,
@@ -322,7 +426,6 @@ export default class BaseCon {
322
426
  error,
323
427
  headers: dat.headers,
324
428
  };
325
-
326
429
  case 498:
327
430
  x = await this.reconnect();
328
431
  if (x) {
@@ -335,7 +438,6 @@ export default class BaseCon {
335
438
  this.disconnected = true;
336
439
  return false;
337
440
  };
338
-
339
441
  this.disconnected = true;
340
442
  return {
341
443
  success: false,
@@ -344,10 +446,8 @@ export default class BaseCon {
344
446
  error,
345
447
  headers: dat.headers,
346
448
  };
347
-
348
449
  case 401:
349
450
  this.logger('AUTH NOT VALID');
350
- this.disconnected = true;
351
451
  return {
352
452
  success: false,
353
453
  data: null,
@@ -363,7 +463,6 @@ export default class BaseCon {
363
463
  code: dat.code,
364
464
  headers: dat.headers,
365
465
  };
366
-
367
466
  default:
368
467
  return {
369
468
  success: false,