@grandlinex/swagger-mate 1.3.3 → 1.3.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.
Files changed (33) hide show
  1. package/dist/cjs/Swagger/Client/SwaggerClient.d.ts +0 -3
  2. package/dist/cjs/Swagger/Client/SwaggerClient.js +5 -38
  3. package/dist/cjs/index.d.ts +0 -1
  4. package/dist/cjs/index.js +0 -1
  5. package/dist/mjs/Swagger/Client/SwaggerClient.d.ts +0 -3
  6. package/dist/mjs/Swagger/Client/SwaggerClient.js +5 -38
  7. package/dist/mjs/index.d.ts +0 -1
  8. package/dist/mjs/index.js +0 -1
  9. package/package.json +7 -7
  10. package/res/templates/class/ApiCon.ts +1 -1
  11. package/res/templates/class/CApiCon.ts +1 -1
  12. package/res/templates/class/IApiCon.ts +1 -1
  13. package/res/templates/class/index.ts +2 -3
  14. package/dist/cjs/Swagger/debug/BaseCon.d.ts +0 -172
  15. package/dist/cjs/Swagger/debug/BaseCon.js +0 -380
  16. package/dist/cjs/Swagger/debug/FetchCon.d.ts +0 -3
  17. package/dist/cjs/Swagger/debug/FetchCon.js +0 -79
  18. package/dist/cjs/Swagger/debug/NodeCon.d.ts +0 -3
  19. package/dist/cjs/Swagger/debug/NodeCon.js +0 -158
  20. package/dist/cjs/Swagger/debug/index.d.ts +0 -5
  21. package/dist/cjs/Swagger/debug/index.js +0 -27
  22. package/dist/mjs/Swagger/debug/BaseCon.d.ts +0 -172
  23. package/dist/mjs/Swagger/debug/BaseCon.js +0 -373
  24. package/dist/mjs/Swagger/debug/FetchCon.d.ts +0 -3
  25. package/dist/mjs/Swagger/debug/FetchCon.js +0 -77
  26. package/dist/mjs/Swagger/debug/NodeCon.d.ts +0 -3
  27. package/dist/mjs/Swagger/debug/NodeCon.js +0 -120
  28. package/dist/mjs/Swagger/debug/index.d.ts +0 -5
  29. package/dist/mjs/Swagger/debug/index.js +0 -5
  30. package/res/templates/class/AxiosCon.ts +0 -49
  31. package/res/templates/class/BaseCon.ts +0 -476
  32. package/res/templates/class/FetchCon.ts +0 -93
  33. package/res/templates/class/NodeCon.ts +0 -172
@@ -1,380 +0,0 @@
1
- "use strict";
2
- /**
3
- * THIS FILE IS AUTOMATICALLY GENERATED
4
- * DO NOT EDIT THIS FILE
5
- */
6
- var __importDefault = (this && this.__importDefault) || function (mod) {
7
- return (mod && mod.__esModule) ? mod : { "default": mod };
8
- };
9
- Object.defineProperty(exports, "__esModule", { value: true });
10
- exports.isErrorType = isErrorType;
11
- const form_data_1 = __importDefault(require("form-data"));
12
- function isErrorType(x) {
13
- return x && typeof x === 'object' && x.type === 'error';
14
- }
15
- /**
16
- * BaseCon provides a minimal client for interacting with an HTTP backend.
17
- * It manages connection state, authentication tokens, and reconnection
18
- * logic while delegating actual HTTP requests to a supplied {@link ConHandle}.
19
- *
20
- * @class
21
- */
22
- class BaseCon {
23
- constructor(conf) {
24
- this.api = conf.endpoint;
25
- this.logger = conf.logger ?? console.log;
26
- this.disconnected = true;
27
- this.noAuth = false;
28
- this.authorization = null;
29
- this.con = conf.con;
30
- this.reconnect = async () => {
31
- this.disconnected = true;
32
- return false;
33
- };
34
- this.onReconnect = () => Promise.resolve(true);
35
- this.handle = this.handle.bind(this);
36
- }
37
- /**
38
- * Retrieves the API endpoint.
39
- *
40
- * @return {string} The API endpoint string.
41
- */
42
- getApiEndpoint() {
43
- return this.api;
44
- }
45
- /**
46
- * Sets the API endpoint URL used by the client.
47
- *
48
- * @param {string} endpoint - The full URL of the API endpoint.
49
- * @returns {void}
50
- */
51
- setApiEndpoint(endpoint) {
52
- this.api = endpoint;
53
- }
54
- /**
55
- * Indicates whether the instance is considered connected.
56
- *
57
- * The instance is regarded as connected when it either does not require authentication
58
- * (`noAuth` is true) or it has an authorization token set (`authorization` is not null),
59
- * and it is not currently marked as disconnected.
60
- *
61
- * @return {boolean} `true` if the instance is connected, `false` otherwise.
62
- */
63
- isConnected() {
64
- return (this.noAuth || this.authorization !== null) && !this.disconnected;
65
- }
66
- /**
67
- * Returns the current authorization token.
68
- *
69
- * @return {string} The authorization token or an empty string if none is set.
70
- */
71
- token() {
72
- return this.authorization || '';
73
- }
74
- p(path, config) {
75
- let pp = path;
76
- if (config?.param) {
77
- const e = Object.keys(config.param);
78
- for (const d of e) {
79
- pp = pp.replace(`{${d}}`, `${config.param[d]}`);
80
- }
81
- }
82
- if (config?.query) {
83
- const e = Object.keys(config.query);
84
- const slices = [];
85
- for (const d of e) {
86
- if (config.query[d]) {
87
- slices.push(`${d}=${config.query[d]}`);
88
- }
89
- }
90
- if (slices.length > 0) {
91
- pp += `?${slices.join('&')}`;
92
- }
93
- }
94
- return `${this.api}${pp}`;
95
- }
96
- /**
97
- * Sends a ping request to the API to verify connectivity and version availability.
98
- *
99
- * @return {boolean} `true` if the API responded with a 200 status code and a valid version object; `false` otherwise.
100
- */
101
- async ping() {
102
- try {
103
- const version = await this.con.get(this.p('/version'));
104
- return version.data?.api !== undefined && version.code === 200;
105
- }
106
- catch (e) {
107
- this.logger('ping failed');
108
- return false;
109
- }
110
- }
111
- /**
112
- * Validates the current authentication token by performing a ping and a test request
113
- * to the backend. The method first ensures connectivity via {@link ping}. If the ping
114
- * succeeds, it attempts to retrieve a token from the `/test/auth` endpoint using the
115
- * current token in the `Authorization` header. The operation is considered successful
116
- * if the response status code is 200 or 201.
117
- *
118
- * If any step fails, an error is logged and the method returns {@code false}. On
119
- * success, it returns {@code true}.
120
- *
121
- * @return {Promise<boolean>} A promise that resolves to {@code true} if the token
122
- * test succeeds, otherwise {@code false}.
123
- */
124
- async testToken() {
125
- const ping = await this.ping();
126
- if (ping) {
127
- try {
128
- const con = await this.con.get(this.p('/test/auth'), {
129
- headers: {
130
- Authorization: this.token(),
131
- },
132
- });
133
- return con.code === 200 || con.code === 201;
134
- }
135
- catch (e) {
136
- this.logger(e);
137
- this.logger('cant connect to backend');
138
- }
139
- }
140
- this.logger('test ping failed');
141
- return false;
142
- }
143
- /**
144
- * Attempts to establish a connection to the backend without authentication.
145
- *
146
- * This method sends a ping request. If the ping succeeds, it clears any
147
- * existing authorization data, marks the instance as connected,
148
- * enables the no‑authentication mode, and returns `true`. If the ping
149
- * fails, it logs a warning, clears authorization, marks the instance
150
- * as disconnected, and returns `false`.
151
- *
152
- * @return {Promise<boolean>} `true` when a connection is successfully
153
- * established without authentication, otherwise `false`.
154
- */
155
- async connectNoAuth() {
156
- const ping = await this.ping();
157
- if (ping) {
158
- this.authorization = null;
159
- this.disconnected = false;
160
- this.noAuth = true;
161
- return true;
162
- }
163
- this.logger('cant connect to backend');
164
- this.authorization = null;
165
- this.disconnected = true;
166
- return false;
167
- }
168
- /**
169
- * Forces a connection using the provided bearer token.
170
- *
171
- * @param {string} token The token to be used for authentication.
172
- * @returns {void}
173
- */
174
- forceConnectWithToken(token) {
175
- this.authorization = `Bearer ${token}`;
176
- this.disconnected = false;
177
- this.noAuth = false;
178
- }
179
- /**
180
- * Establishes a connection to the backend using the supplied credentials.
181
- * Performs a health‑check ping first; if successful, it requests an authentication
182
- * token from the `/token` endpoint. When the token is obtained, the method
183
- * updates internal state (authorization header, connection flags) and, unless
184
- * a dry run is requested, sets up a reconnection routine. Any errors are
185
- * logged and the method resolves to `false`.
186
- *
187
- * @param {string} email - The user's email address for authentication.
188
- * @param {string} pw - The password (or token) for the specified user.
189
- * @param {boolean} [dry=false] - If `true`, the method performs a dry run
190
- * without persisting credentials or configuring reconnection logic.
191
- *
192
- * @returns Promise<boolean> `true` if the connection was successfully
193
- * established, otherwise `false`.
194
- */
195
- async connect(email, pw, dry = false) {
196
- const ping = await this.ping();
197
- if (ping) {
198
- try {
199
- const token = await this.con.post(this.p('/token'), {
200
- username: email,
201
- token: pw,
202
- });
203
- if (token.code === 200 || token.code === 201) {
204
- if (!dry) {
205
- this.authorization = `Bearer ${token.data?.token}`;
206
- this.disconnected = false;
207
- this.noAuth = false;
208
- this.reconnect = async () => {
209
- return ((await this.connect(email, pw)) && (await this.testToken()));
210
- };
211
- }
212
- return true;
213
- }
214
- }
215
- catch (e) {
216
- this.logger(e);
217
- }
218
- }
219
- this.logger('cant connect to backend');
220
- this.authorization = null;
221
- this.disconnected = true;
222
- this.noAuth = false;
223
- return false;
224
- }
225
- /**
226
- * Performs an HTTP request using the client’s internal connection.
227
- *
228
- * The method verifies that the client is connected before attempting a request.
229
- * It automatically injects the authorization token, permanent headers, and any
230
- * headers supplied in `config`. If the request body is a `FormData` instance
231
- * (or provides a `getHeaders` method), the appropriate form headers are added.
232
- *
233
- * Response handling:
234
- * - `200` or `201`: returns `success: true` with the received data.
235
- * - `498`: attempts to reconnect and retries the request once.
236
- * - `401`: logs an authentication error, marks the client as disconnected.
237
- * - `403` and other status codes: return `success: false` with the status code.
238
- *
239
- * @param {'POST'|'GET'|'PATCH'|'DELETE'} type The HTTP method to use.
240
- * @param {string} path The endpoint path relative to the base URL.
241
- * @param {J} [body] Optional request payload. May be `FormData` or a plain object.
242
- * @param {ConHandleConfig} [config] Optional Axios-like configuration for the request.
243
- * @returns {Promise<HandleRes<T>>} A promise that resolves to a `HandleRes` object
244
- * containing the response data, status code, any error information, and headers.
245
- */
246
- async handle(type, path, body, config) {
247
- if (!this.isConnected()) {
248
- this.logger('Disconnected');
249
- return {
250
- success: false,
251
- data: null,
252
- code: -1,
253
- };
254
- }
255
- let formHeader = null;
256
- const cK = body;
257
- if (cK &&
258
- (cK instanceof form_data_1.default || typeof cK?.getHeaders === 'function')) {
259
- formHeader = cK?.getHeaders?.() || {};
260
- }
261
- else {
262
- formHeader = {};
263
- }
264
- let dat;
265
- switch (type) {
266
- case 'GET':
267
- dat = await this.con.get(this.p(path, config), {
268
- ...config,
269
- headers: {
270
- Authorization: this.token(),
271
- ...formHeader,
272
- ...config?.headers,
273
- ...this.permanentHeader,
274
- },
275
- });
276
- break;
277
- case 'POST':
278
- dat = await this.con.post(this.p(path, config), body, {
279
- ...config,
280
- headers: {
281
- Authorization: this.token(),
282
- ...formHeader,
283
- ...config?.headers,
284
- ...this.permanentHeader,
285
- },
286
- });
287
- break;
288
- case 'PATCH':
289
- dat = await this.con.patch(this.p(path, config), body, {
290
- ...config,
291
- headers: {
292
- Authorization: this.token(),
293
- ...formHeader,
294
- ...config?.headers,
295
- ...this.permanentHeader,
296
- },
297
- });
298
- break;
299
- case 'DELETE':
300
- dat = await this.con.delete(this.p(path, config), {
301
- ...config,
302
- headers: {
303
- Authorization: this.token(),
304
- ...formHeader,
305
- ...config?.headers,
306
- ...this.permanentHeader,
307
- },
308
- });
309
- break;
310
- default:
311
- return {
312
- success: false,
313
- data: null,
314
- code: -1,
315
- };
316
- }
317
- let error;
318
- if (isErrorType(dat.data)) {
319
- error = dat.data;
320
- }
321
- let x = false;
322
- switch (dat.code) {
323
- case 200:
324
- case 201:
325
- return {
326
- success: true,
327
- data: dat.data,
328
- code: dat.code,
329
- error,
330
- headers: dat.headers,
331
- };
332
- case 498:
333
- x = await this.reconnect();
334
- if (x) {
335
- this.onReconnect(this).catch((dx) => {
336
- this.logger(dx);
337
- });
338
- return this.handle(type, path, body, config);
339
- }
340
- this.reconnect = async () => {
341
- this.disconnected = true;
342
- return false;
343
- };
344
- this.disconnected = true;
345
- return {
346
- success: false,
347
- data: null,
348
- code: dat.code,
349
- error,
350
- headers: dat.headers,
351
- };
352
- case 401:
353
- this.logger('AUTH NOT VALID');
354
- return {
355
- success: false,
356
- data: null,
357
- code: dat.code,
358
- error,
359
- headers: dat.headers,
360
- };
361
- case 403:
362
- return {
363
- success: false,
364
- data: null,
365
- error,
366
- code: dat.code,
367
- headers: dat.headers,
368
- };
369
- default:
370
- return {
371
- success: false,
372
- data: null,
373
- error,
374
- code: dat.code,
375
- headers: dat.headers,
376
- };
377
- }
378
- }
379
- }
380
- exports.default = BaseCon;
@@ -1,3 +0,0 @@
1
- import { ConHandle } from './BaseCon.js';
2
- declare const FetchCon: ConHandle;
3
- export default FetchCon;
@@ -1,79 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- async function fcTransform(r) {
4
- const res = await r;
5
- const head = {};
6
- res.headers.forEach((val, key) => {
7
- head[key] = val;
8
- });
9
- let data = null;
10
- if (head['content-type']?.includes('application/json')) {
11
- data = await res.json();
12
- }
13
- else if (head['content-type']?.includes('form-data')) {
14
- data = await res.formData();
15
- }
16
- else if (head['content-type']?.includes('octet-stream')) {
17
- data = await res.arrayBuffer();
18
- }
19
- else if (head['content-type']?.includes('text/plain')) {
20
- data = await res.text();
21
- }
22
- return {
23
- code: res.status,
24
- data,
25
- headers: head,
26
- };
27
- }
28
- function bodyTransform(r, headers) {
29
- if (!r) {
30
- return {
31
- headers,
32
- };
33
- }
34
- if (r instanceof FormData) {
35
- return {
36
- body: r,
37
- headers: {
38
- ...headers,
39
- },
40
- };
41
- }
42
- return {
43
- body: JSON.stringify(r),
44
- headers: {
45
- ...headers,
46
- 'content-type': 'application/json',
47
- },
48
- };
49
- }
50
- const FetchCon = {
51
- get: async (url, config) => {
52
- return fcTransform(fetch(url, {
53
- method: 'GET',
54
- headers: {
55
- accept: 'application/json, text/plain, */*',
56
- ...config?.headers,
57
- },
58
- }));
59
- },
60
- post: async (url, body, config) => {
61
- return fcTransform(fetch(url, {
62
- method: 'POST',
63
- ...bodyTransform(body, config?.headers),
64
- }));
65
- },
66
- patch: async (url, body, config) => {
67
- return fcTransform(fetch(url, {
68
- method: 'PATCH',
69
- ...bodyTransform(body, config?.headers),
70
- }));
71
- },
72
- delete: async (url, config) => {
73
- return fcTransform(fetch(url, {
74
- method: 'DELETE',
75
- headers: config?.headers,
76
- }));
77
- },
78
- };
79
- exports.default = FetchCon;
@@ -1,3 +0,0 @@
1
- import { ConHandle } from './BaseCon.js';
2
- declare const NodeCon: ConHandle;
3
- export default NodeCon;
@@ -1,158 +0,0 @@
1
- "use strict";
2
- var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
- if (k2 === undefined) k2 = k;
4
- var desc = Object.getOwnPropertyDescriptor(m, k);
5
- if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
- desc = { enumerable: true, get: function() { return m[k]; } };
7
- }
8
- Object.defineProperty(o, k2, desc);
9
- }) : (function(o, m, k, k2) {
10
- if (k2 === undefined) k2 = k;
11
- o[k2] = m[k];
12
- }));
13
- var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
- Object.defineProperty(o, "default", { enumerable: true, value: v });
15
- }) : function(o, v) {
16
- o["default"] = v;
17
- });
18
- var __importStar = (this && this.__importStar) || (function () {
19
- var ownKeys = function(o) {
20
- ownKeys = Object.getOwnPropertyNames || function (o) {
21
- var ar = [];
22
- for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
23
- return ar;
24
- };
25
- return ownKeys(o);
26
- };
27
- return function (mod) {
28
- if (mod && mod.__esModule) return mod;
29
- var result = {};
30
- if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
31
- __setModuleDefault(result, mod);
32
- return result;
33
- };
34
- })();
35
- var __importDefault = (this && this.__importDefault) || function (mod) {
36
- return (mod && mod.__esModule) ? mod : { "default": mod };
37
- };
38
- Object.defineProperty(exports, "__esModule", { value: true });
39
- const form_data_1 = __importDefault(require("form-data"));
40
- const http = __importStar(require("http"));
41
- const https = __importStar(require("https"));
42
- function selectClient(url) {
43
- if (url.startsWith('https')) {
44
- return https;
45
- }
46
- return http;
47
- }
48
- async function fcTransform(message, raw) {
49
- let data = null;
50
- if (message.headers['content-type']?.includes('application/json')) {
51
- data = JSON.parse(raw);
52
- }
53
- else if (message.headers['content-type']?.includes('form-data')) {
54
- data = null; // await res.formData()
55
- }
56
- else if (message.headers['content-type']?.includes('octet-stream')) {
57
- data = Buffer.from(raw);
58
- }
59
- else if (message.headers['content-type']?.startsWith('text/')) {
60
- data = Buffer.from(raw).toString('utf8');
61
- }
62
- return {
63
- code: message.statusCode || -1,
64
- data,
65
- headers: message.headers,
66
- };
67
- }
68
- function bodyTransform(r, headers) {
69
- if (!r) {
70
- return {
71
- headers: headers || {},
72
- body: undefined,
73
- };
74
- }
75
- if (r instanceof form_data_1.default) {
76
- return {
77
- body: r,
78
- headers: {
79
- ...headers,
80
- 'content-type': 'multipart/form-data',
81
- },
82
- };
83
- }
84
- return {
85
- body: JSON.stringify(r),
86
- headers: {
87
- ...headers,
88
- 'content-type': 'application/json',
89
- },
90
- };
91
- }
92
- async function makeRequest(url, option, body, config) {
93
- return new Promise((resolve) => {
94
- let headers = config?.headers || {};
95
- let transForm = null;
96
- if (body) {
97
- let safeHeaders;
98
- if (option.headers &&
99
- typeof option.headers === 'object' &&
100
- !Array.isArray(option.headers)) {
101
- safeHeaders = option.headers;
102
- }
103
- transForm = bodyTransform(body, safeHeaders);
104
- headers = {
105
- ...headers,
106
- ...transForm.headers,
107
- };
108
- }
109
- const req = selectClient(url)
110
- .request(url, {
111
- headers,
112
- ...option,
113
- }, (res) => {
114
- let data = '';
115
- // A chunk of data has been received.
116
- res.on('data', (chunk) => {
117
- data += chunk;
118
- });
119
- // The whole response has been received. Print out the result.
120
- res.on('end', () => {
121
- resolve(fcTransform(res, data));
122
- });
123
- })
124
- .on('error', (err) => {
125
- console.log(`Error: ${err.message}`);
126
- resolve({
127
- code: -1,
128
- data: null,
129
- headers: {},
130
- });
131
- });
132
- if (transForm && transForm.body) {
133
- req.write(transForm.body);
134
- }
135
- req.end();
136
- });
137
- }
138
- const NodeCon = {
139
- get: async (url, config) => {
140
- return makeRequest(url, {}, undefined, config);
141
- },
142
- post: async (url, body, config) => {
143
- return makeRequest(url, {
144
- method: 'POST',
145
- }, body, config);
146
- },
147
- patch: async (url, body, config) => {
148
- return makeRequest(url, {
149
- method: 'PATCH',
150
- }, body, config);
151
- },
152
- delete: async (url, config) => {
153
- return makeRequest(url, {
154
- method: 'DELETE',
155
- }, undefined, config);
156
- },
157
- };
158
- exports.default = NodeCon;
@@ -1,5 +0,0 @@
1
- import BaseCon from './BaseCon.js';
2
- import NodeCon from './NodeCon.js';
3
- import FetchCon from './FetchCon.js';
4
- export * from './BaseCon.js';
5
- export { NodeCon, FetchCon, BaseCon };
@@ -1,27 +0,0 @@
1
- "use strict";
2
- var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
- if (k2 === undefined) k2 = k;
4
- var desc = Object.getOwnPropertyDescriptor(m, k);
5
- if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
- desc = { enumerable: true, get: function() { return m[k]; } };
7
- }
8
- Object.defineProperty(o, k2, desc);
9
- }) : (function(o, m, k, k2) {
10
- if (k2 === undefined) k2 = k;
11
- o[k2] = m[k];
12
- }));
13
- var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
- for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
- };
16
- var __importDefault = (this && this.__importDefault) || function (mod) {
17
- return (mod && mod.__esModule) ? mod : { "default": mod };
18
- };
19
- Object.defineProperty(exports, "__esModule", { value: true });
20
- exports.BaseCon = exports.FetchCon = exports.NodeCon = void 0;
21
- const BaseCon_js_1 = __importDefault(require("./BaseCon.js"));
22
- exports.BaseCon = BaseCon_js_1.default;
23
- const NodeCon_js_1 = __importDefault(require("./NodeCon.js"));
24
- exports.NodeCon = NodeCon_js_1.default;
25
- const FetchCon_js_1 = __importDefault(require("./FetchCon.js"));
26
- exports.FetchCon = FetchCon_js_1.default;
27
- __exportStar(require("./BaseCon.js"), exports);