@grandlinex/swagger-mate 1.3.2 → 1.3.4
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/dist/cjs/Swagger/Meta/SwaggerTypes.d.ts +3 -2
- package/dist/cjs/Swagger/Path/ESchemaEditor.d.ts +51 -0
- package/dist/cjs/Swagger/Path/ESchemaEditor.js +151 -0
- package/dist/cjs/Swagger/Path/SPathUtil.d.ts +7 -76
- package/dist/cjs/Swagger/Path/SPathUtil.js +10 -182
- package/dist/cjs/Swagger/debug/BaseCon.d.ts +35 -5
- package/dist/cjs/Swagger/debug/BaseCon.js +67 -15
- package/dist/cjs/index.d.ts +1 -0
- package/dist/cjs/index.js +1 -0
- package/dist/mjs/Swagger/Meta/SwaggerTypes.d.ts +3 -2
- package/dist/mjs/Swagger/Path/ESchemaEditor.d.ts +51 -0
- package/dist/mjs/Swagger/Path/ESchemaEditor.js +147 -0
- package/dist/mjs/Swagger/Path/SPathUtil.d.ts +7 -76
- package/dist/mjs/Swagger/Path/SPathUtil.js +11 -183
- package/dist/mjs/Swagger/debug/BaseCon.d.ts +35 -5
- package/dist/mjs/Swagger/debug/BaseCon.js +67 -15
- package/dist/mjs/index.d.ts +1 -0
- package/dist/mjs/index.js +1 -0
- package/package.json +1 -1
- package/res/templates/class/BaseCon.ts +74 -17
|
@@ -1,30 +1,6 @@
|
|
|
1
|
-
import { getEntityMeta, XUtil
|
|
1
|
+
import { getEntityMeta, XUtil } from '@grandlinex/core';
|
|
2
2
|
import map from './SUtilMap.js';
|
|
3
|
-
import {
|
|
4
|
-
function resolveDBType(dType) {
|
|
5
|
-
switch (dType) {
|
|
6
|
-
case 'int':
|
|
7
|
-
case 'long':
|
|
8
|
-
return 'integer';
|
|
9
|
-
case 'double':
|
|
10
|
-
case 'float':
|
|
11
|
-
return 'number';
|
|
12
|
-
case 'blob':
|
|
13
|
-
return 'string';
|
|
14
|
-
case 'string':
|
|
15
|
-
case 'text':
|
|
16
|
-
case 'uuid':
|
|
17
|
-
return 'string';
|
|
18
|
-
case 'boolean':
|
|
19
|
-
return 'boolean';
|
|
20
|
-
case 'date':
|
|
21
|
-
return 'string';
|
|
22
|
-
case 'json':
|
|
23
|
-
return 'object';
|
|
24
|
-
default:
|
|
25
|
-
throw Error('TypeNotSupported');
|
|
26
|
-
}
|
|
27
|
-
}
|
|
3
|
+
import { ESchemaEditor } from './ESchemaEditor.js';
|
|
28
4
|
export default class SPathUtil {
|
|
29
5
|
/**
|
|
30
6
|
* Generates a default response mapping for the specified HTTP status types.
|
|
@@ -261,32 +237,12 @@ export default class SPathUtil {
|
|
|
261
237
|
* If no metadata is found for the provided entity, the method returns `undefined`.
|
|
262
238
|
*
|
|
263
239
|
* @param {T} entity - The entity instance for which to create a schema.
|
|
264
|
-
* @returns {
|
|
240
|
+
* @returns {SSchemaElObj | undefined} The generated schema object, or `undefined`
|
|
265
241
|
* if the entity's metadata could not be retrieved.
|
|
242
|
+
* @deprecated Use {@link ESchemaEditor.schemaFromEntity} instead.
|
|
266
243
|
*/
|
|
267
244
|
static schemaFromEntity(entity) {
|
|
268
|
-
|
|
269
|
-
type: 'object',
|
|
270
|
-
properties: {},
|
|
271
|
-
};
|
|
272
|
-
const meta = getEntityMeta(entity);
|
|
273
|
-
if (!meta) {
|
|
274
|
-
return undefined;
|
|
275
|
-
}
|
|
276
|
-
schema.description = `Entity: ${meta.name}`;
|
|
277
|
-
schema.required = [];
|
|
278
|
-
const keys = Object.keys(entity);
|
|
279
|
-
keys.forEach((k) => {
|
|
280
|
-
const cMeta = getColumnMeta(entity, k);
|
|
281
|
-
if (cMeta && schema.properties) {
|
|
282
|
-
schema.required.push(k);
|
|
283
|
-
schema.properties[k] = {
|
|
284
|
-
type: resolveDBType(cMeta.dataType),
|
|
285
|
-
nullable: cMeta.canBeNull,
|
|
286
|
-
};
|
|
287
|
-
}
|
|
288
|
-
});
|
|
289
|
-
return schema;
|
|
245
|
+
return ESchemaEditor.schemaFromEntity(entity);
|
|
290
246
|
}
|
|
291
247
|
/**
|
|
292
248
|
* Generates a content schema object for the given entity. The schema contains a description derived from the entity metadata and a JSON content schema based on the entity's structure.
|
|
@@ -303,7 +259,7 @@ export default class SPathUtil {
|
|
|
303
259
|
description: `Entity: ${meta.name}`,
|
|
304
260
|
content: {
|
|
305
261
|
'application/json': {
|
|
306
|
-
schema:
|
|
262
|
+
schema: ESchemaEditor.schemaFromEntity(entity),
|
|
307
263
|
},
|
|
308
264
|
},
|
|
309
265
|
};
|
|
@@ -319,7 +275,7 @@ export default class SPathUtil {
|
|
|
319
275
|
e.forEach((el) => {
|
|
320
276
|
const meta = getEntityMeta(el);
|
|
321
277
|
if (meta) {
|
|
322
|
-
out[meta.name] =
|
|
278
|
+
out[meta.name] = ESchemaEditor.schemaFromEntity(el);
|
|
323
279
|
}
|
|
324
280
|
});
|
|
325
281
|
return out;
|
|
@@ -339,140 +295,12 @@ export default class SPathUtil {
|
|
|
339
295
|
i: {
|
|
340
296
|
type: 'integer',
|
|
341
297
|
},
|
|
342
|
-
dat:
|
|
343
|
-
join_map:
|
|
298
|
+
dat: ESchemaEditor.schemaFromEntity(entity),
|
|
299
|
+
join_map: ESchemaEditor.schemaFromEntity(entityMap),
|
|
344
300
|
},
|
|
345
301
|
};
|
|
346
302
|
}
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
*
|
|
350
|
-
* @param {CoreEntity} entity
|
|
351
|
-
* The entity for which the schema should be extended.
|
|
352
|
-
*
|
|
353
|
-
* @param {...Object} options
|
|
354
|
-
* One or more objects defining schema extensions. Each object may contain:
|
|
355
|
-
* - `key` (string): The property key to add or extend.
|
|
356
|
-
* - `list` (boolean, optional): Indicates whether the property is a list.
|
|
357
|
-
* - `entity` (CoreEntity, optional): The entity type for the property.
|
|
358
|
-
* - `schema` (SSchemaEl, optional): A custom schema definition for the property.
|
|
359
|
-
* - `required` (boolean, optional): Whether the property is required.
|
|
360
|
-
*
|
|
361
|
-
* @return {SSchemaEl}
|
|
362
|
-
* The resulting schema element. If a single property is returned by
|
|
363
|
-
* `extendEntitySchema`, its schema is returned directly; otherwise an
|
|
364
|
-
* object schema with a type of `'object'` is returned.
|
|
365
|
-
*/
|
|
366
|
-
static extendEntitySchemaObject(entity, ...options) {
|
|
367
|
-
const schema = this.extendEntitySchema(entity, ...options);
|
|
368
|
-
const ent = Object.entries(schema);
|
|
369
|
-
if (ent.length === 1) {
|
|
370
|
-
return ent[0][1];
|
|
371
|
-
}
|
|
372
|
-
return {
|
|
373
|
-
type: 'object',
|
|
374
|
-
};
|
|
375
|
-
}
|
|
376
|
-
/**
|
|
377
|
-
* Extends the schema of a given {@link CoreEntity} with additional properties.
|
|
378
|
-
*
|
|
379
|
-
* @param {CoreEntity} entity
|
|
380
|
-
* The entity whose schema will be extended.
|
|
381
|
-
*
|
|
382
|
-
* @param {...{
|
|
383
|
-
* key: string,
|
|
384
|
-
* list?: boolean,
|
|
385
|
-
* entity?: CoreEntity,
|
|
386
|
-
* schema?: SSchemaEl,
|
|
387
|
-
* required?: boolean
|
|
388
|
-
* }} options
|
|
389
|
-
* One or more option objects specifying the extensions to apply. Each option
|
|
390
|
-
* may provide either a direct schema (`schema`) or an entity reference
|
|
391
|
-
* (`entity`). The `list` flag indicates whether the property should be
|
|
392
|
-
* represented as an array of the provided schema. The `required` flag
|
|
393
|
-
* adds the property to the schema’s required list.
|
|
394
|
-
*
|
|
395
|
-
* @returns {SKey<SSchemaEl>}
|
|
396
|
-
* An object containing the updated schema for the entity, keyed by the
|
|
397
|
-
* entity’s name. If the entity metadata cannot be found, an empty
|
|
398
|
-
* object is returned.
|
|
399
|
-
*/
|
|
400
|
-
static extendEntitySchema(entity, ...options) {
|
|
401
|
-
const meta = getEntityMeta(entity);
|
|
402
|
-
if (meta) {
|
|
403
|
-
const schema = SPathUtil.schemaEntryGen(entity)[meta.name];
|
|
404
|
-
if (schema && !isSwaggerRef(schema) && schema.properties) {
|
|
405
|
-
for (const option of options) {
|
|
406
|
-
if (option.schema) {
|
|
407
|
-
if (option.list) {
|
|
408
|
-
schema.properties[option.key] = {
|
|
409
|
-
type: 'array',
|
|
410
|
-
items: option.schema,
|
|
411
|
-
};
|
|
412
|
-
}
|
|
413
|
-
else {
|
|
414
|
-
schema.properties[option.key] = option.schema;
|
|
415
|
-
}
|
|
416
|
-
}
|
|
417
|
-
else if (option.entity) {
|
|
418
|
-
const eMeta = getEntityMeta(option.entity);
|
|
419
|
-
if (eMeta) {
|
|
420
|
-
const scheme = SPathUtil.schemaEntryGen(option.entity)[eMeta.name];
|
|
421
|
-
if (option.list) {
|
|
422
|
-
schema.properties[option.key] = {
|
|
423
|
-
type: 'array',
|
|
424
|
-
items: scheme,
|
|
425
|
-
};
|
|
426
|
-
}
|
|
427
|
-
else {
|
|
428
|
-
schema.properties[option.key] = scheme;
|
|
429
|
-
}
|
|
430
|
-
}
|
|
431
|
-
}
|
|
432
|
-
if (option.required) {
|
|
433
|
-
schema.required = [...(schema.required || []), option.key];
|
|
434
|
-
}
|
|
435
|
-
}
|
|
436
|
-
}
|
|
437
|
-
return {
|
|
438
|
-
[meta.name]: schema,
|
|
439
|
-
};
|
|
440
|
-
}
|
|
441
|
-
return {};
|
|
442
|
-
}
|
|
443
|
-
/**
|
|
444
|
-
* Reduces the entity schema to a single schema element or a generic object.
|
|
445
|
-
*
|
|
446
|
-
* @param entity The entity whose schema should be reduced.
|
|
447
|
-
* @param keys Optional list of keys to include in the reduced schema. If omitted, all keys are considered.
|
|
448
|
-
* @return Returns the schema element of the sole key if only one key is present; otherwise, returns a generic object schema with type `'object'`. */
|
|
449
|
-
static reduceEntitySchemaObject(entity, ...keys) {
|
|
450
|
-
const schema = this.reduceEntitySchema(entity, ...keys);
|
|
451
|
-
const ent = Object.entries(schema);
|
|
452
|
-
if (ent.length === 1) {
|
|
453
|
-
return ent[0][1];
|
|
454
|
-
}
|
|
455
|
-
return {
|
|
456
|
-
type: 'object',
|
|
457
|
-
};
|
|
458
|
-
}
|
|
459
|
-
/**
|
|
460
|
-
* Creates a reduced version of an entity's schema by excluding specified properties.
|
|
461
|
-
*
|
|
462
|
-
* @param {CoreEntity} entity - The entity whose schema is to be processed.
|
|
463
|
-
* @param {...string} keys - Property names to remove from the schema's `properties` and `required` lists.
|
|
464
|
-
*
|
|
465
|
-
* @returns */
|
|
466
|
-
static reduceEntitySchema(entity, ...keys) {
|
|
467
|
-
const meta = getEntityMeta(entity);
|
|
468
|
-
if (meta) {
|
|
469
|
-
const schema = SPathUtil.schemaEntryGen(entity)[meta.name];
|
|
470
|
-
if (schema && !isSwaggerRef(schema) && schema.properties) {
|
|
471
|
-
schema.properties = Object.fromEntries(Object.entries(schema.properties).filter(([e]) => !keys.includes(e)));
|
|
472
|
-
schema.required = (schema.required || []).filter((e) => !keys.includes(e));
|
|
473
|
-
}
|
|
474
|
-
return { [meta.name]: schema };
|
|
475
|
-
}
|
|
476
|
-
return {};
|
|
303
|
+
static editor(e) {
|
|
304
|
+
return ESchemaEditor.fromEntity(e);
|
|
477
305
|
}
|
|
478
306
|
}
|
|
@@ -55,10 +55,21 @@ export default class BaseCon {
|
|
|
55
55
|
con: ConHandle;
|
|
56
56
|
reconnect: () => Promise<boolean>;
|
|
57
57
|
onReconnect: (con: BaseCon) => Promise<boolean>;
|
|
58
|
+
/**
|
|
59
|
+
* Initializes a new instance of the client.
|
|
60
|
+
*
|
|
61
|
+
* @param {Object} conf Configuration object.
|
|
62
|
+
* @param {ConHandle} conf.con - Connection handle.
|
|
63
|
+
* @param {string} conf.endpoint - API endpoint URL.
|
|
64
|
+
* @param {(arg: any) => void} [conf.logger] - Optional logger function; defaults to console.log.
|
|
65
|
+
* @param {Record<string, string>} [conf.permanentHeader] - Optional permanent headers to include in requests.
|
|
66
|
+
* @return {void}
|
|
67
|
+
*/
|
|
58
68
|
constructor(conf: {
|
|
59
69
|
con: ConHandle;
|
|
60
70
|
endpoint: string;
|
|
61
71
|
logger?: (arg: any) => void;
|
|
72
|
+
permanentHeader?: Record<string, string>;
|
|
62
73
|
});
|
|
63
74
|
/**
|
|
64
75
|
* Retrieves the API endpoint.
|
|
@@ -84,11 +95,17 @@ export default class BaseCon {
|
|
|
84
95
|
*/
|
|
85
96
|
isConnected(): boolean;
|
|
86
97
|
/**
|
|
87
|
-
*
|
|
98
|
+
* Retrieves the current authorization token.
|
|
99
|
+
*
|
|
100
|
+
* @return {string|null} The token string used for authorization, or {@code null} if no token is set.
|
|
101
|
+
*/
|
|
102
|
+
token(): string | null;
|
|
103
|
+
/**
|
|
104
|
+
* Returns the bearer token string if an authorization value has been set.
|
|
88
105
|
*
|
|
89
|
-
* @return {string} The
|
|
106
|
+
* @return {string|null} The bearer token prefixed with "Bearer ", or {@code null} when no authorization is present.
|
|
90
107
|
*/
|
|
91
|
-
|
|
108
|
+
getBearer(): string | null;
|
|
92
109
|
private p;
|
|
93
110
|
/**
|
|
94
111
|
* Sends a ping request to the API to verify connectivity and version availability.
|
|
@@ -96,6 +113,18 @@ export default class BaseCon {
|
|
|
96
113
|
* @return {boolean} `true` if the API responded with a 200 status code and a valid version object; `false` otherwise.
|
|
97
114
|
*/
|
|
98
115
|
ping(): Promise<boolean>;
|
|
116
|
+
/**
|
|
117
|
+
* Builds the Authorization header for HTTP requests.
|
|
118
|
+
*
|
|
119
|
+
* If the instance has an authorization token, this method returns an object
|
|
120
|
+
* containing a single `Authorization` header with the Bearer token value.
|
|
121
|
+
* When no token is available, an empty header object is returned.
|
|
122
|
+
*
|
|
123
|
+
* @return {Record<string, string>} The headers object containing the
|
|
124
|
+
* `Authorization` header when applicable, otherwise an empty object.
|
|
125
|
+
*/
|
|
126
|
+
private tokenHeader;
|
|
127
|
+
setPermanentHeader(header: Record<string, string>): void;
|
|
99
128
|
/**
|
|
100
129
|
* Validates the current authentication token by performing a ping and a test request
|
|
101
130
|
* to the backend. The method first ensures connectivity via {@link ping}. If the ping
|
|
@@ -126,10 +155,11 @@ export default class BaseCon {
|
|
|
126
155
|
/**
|
|
127
156
|
* Forces a connection using the provided bearer token.
|
|
128
157
|
*
|
|
129
|
-
* @param {string} token The token to be used for authentication.
|
|
158
|
+
* @param {string|null} token The token to be used for authentication.
|
|
130
159
|
* @returns {void}
|
|
131
160
|
*/
|
|
132
|
-
|
|
161
|
+
forceConnect(token: string | null): void;
|
|
162
|
+
coppyFrom(con: BaseCon): void;
|
|
133
163
|
/**
|
|
134
164
|
* Establishes a connection to the backend using the supplied credentials.
|
|
135
165
|
* Performs a health‑check ping first; if successful, it requests an authentication
|
|
@@ -14,6 +14,16 @@ export function isErrorType(x) {
|
|
|
14
14
|
* @class
|
|
15
15
|
*/
|
|
16
16
|
export default class BaseCon {
|
|
17
|
+
/**
|
|
18
|
+
* Initializes a new instance of the client.
|
|
19
|
+
*
|
|
20
|
+
* @param {Object} conf Configuration object.
|
|
21
|
+
* @param {ConHandle} conf.con - Connection handle.
|
|
22
|
+
* @param {string} conf.endpoint - API endpoint URL.
|
|
23
|
+
* @param {(arg: any) => void} [conf.logger] - Optional logger function; defaults to console.log.
|
|
24
|
+
* @param {Record<string, string>} [conf.permanentHeader] - Optional permanent headers to include in requests.
|
|
25
|
+
* @return {void}
|
|
26
|
+
*/
|
|
17
27
|
constructor(conf) {
|
|
18
28
|
this.api = conf.endpoint;
|
|
19
29
|
this.logger = conf.logger ?? console.log;
|
|
@@ -21,6 +31,7 @@ export default class BaseCon {
|
|
|
21
31
|
this.noAuth = false;
|
|
22
32
|
this.authorization = null;
|
|
23
33
|
this.con = conf.con;
|
|
34
|
+
this.permanentHeader = conf.permanentHeader || {};
|
|
24
35
|
this.reconnect = async () => {
|
|
25
36
|
this.disconnected = true;
|
|
26
37
|
return false;
|
|
@@ -58,12 +69,23 @@ export default class BaseCon {
|
|
|
58
69
|
return (this.noAuth || this.authorization !== null) && !this.disconnected;
|
|
59
70
|
}
|
|
60
71
|
/**
|
|
61
|
-
*
|
|
72
|
+
* Retrieves the current authorization token.
|
|
62
73
|
*
|
|
63
|
-
* @return {string} The authorization
|
|
74
|
+
* @return {string|null} The token string used for authorization, or {@code null} if no token is set.
|
|
64
75
|
*/
|
|
65
76
|
token() {
|
|
66
|
-
return this.authorization
|
|
77
|
+
return this.authorization;
|
|
78
|
+
}
|
|
79
|
+
/**
|
|
80
|
+
* Returns the bearer token string if an authorization value has been set.
|
|
81
|
+
*
|
|
82
|
+
* @return {string|null} The bearer token prefixed with "Bearer ", or {@code null} when no authorization is present.
|
|
83
|
+
*/
|
|
84
|
+
getBearer() {
|
|
85
|
+
if (this.authorization) {
|
|
86
|
+
return `Bearer ${this.authorization}`;
|
|
87
|
+
}
|
|
88
|
+
return null;
|
|
67
89
|
}
|
|
68
90
|
p(path, config) {
|
|
69
91
|
let pp = path;
|
|
@@ -102,6 +124,27 @@ export default class BaseCon {
|
|
|
102
124
|
return false;
|
|
103
125
|
}
|
|
104
126
|
}
|
|
127
|
+
/**
|
|
128
|
+
* Builds the Authorization header for HTTP requests.
|
|
129
|
+
*
|
|
130
|
+
* If the instance has an authorization token, this method returns an object
|
|
131
|
+
* containing a single `Authorization` header with the Bearer token value.
|
|
132
|
+
* When no token is available, an empty header object is returned.
|
|
133
|
+
*
|
|
134
|
+
* @return {Record<string, string>} The headers object containing the
|
|
135
|
+
* `Authorization` header when applicable, otherwise an empty object.
|
|
136
|
+
*/
|
|
137
|
+
tokenHeader() {
|
|
138
|
+
if (this.authorization) {
|
|
139
|
+
return {
|
|
140
|
+
Authorization: this.getBearer(),
|
|
141
|
+
};
|
|
142
|
+
}
|
|
143
|
+
return {};
|
|
144
|
+
}
|
|
145
|
+
setPermanentHeader(header) {
|
|
146
|
+
this.permanentHeader = header;
|
|
147
|
+
}
|
|
105
148
|
/**
|
|
106
149
|
* Validates the current authentication token by performing a ping and a test request
|
|
107
150
|
* to the backend. The method first ensures connectivity via {@link ping}. If the ping
|
|
@@ -120,9 +163,7 @@ export default class BaseCon {
|
|
|
120
163
|
if (ping) {
|
|
121
164
|
try {
|
|
122
165
|
const con = await this.con.get(this.p('/test/auth'), {
|
|
123
|
-
headers:
|
|
124
|
-
Authorization: this.token(),
|
|
125
|
-
},
|
|
166
|
+
headers: this.tokenHeader(),
|
|
126
167
|
});
|
|
127
168
|
return con.code === 200 || con.code === 201;
|
|
128
169
|
}
|
|
@@ -157,18 +198,29 @@ export default class BaseCon {
|
|
|
157
198
|
this.logger('cant connect to backend');
|
|
158
199
|
this.authorization = null;
|
|
159
200
|
this.disconnected = true;
|
|
201
|
+
this.noAuth = false;
|
|
160
202
|
return false;
|
|
161
203
|
}
|
|
162
204
|
/**
|
|
163
205
|
* Forces a connection using the provided bearer token.
|
|
164
206
|
*
|
|
165
|
-
* @param {string} token The token to be used for authentication.
|
|
207
|
+
* @param {string|null} token The token to be used for authentication.
|
|
166
208
|
* @returns {void}
|
|
167
209
|
*/
|
|
168
|
-
|
|
169
|
-
|
|
210
|
+
forceConnect(token) {
|
|
211
|
+
if (token) {
|
|
212
|
+
this.authorization = token.replace('Bearer ', '').replace('bearer ', '');
|
|
213
|
+
}
|
|
214
|
+
else {
|
|
215
|
+
this.authorization = null;
|
|
216
|
+
}
|
|
170
217
|
this.disconnected = false;
|
|
171
|
-
this.noAuth =
|
|
218
|
+
this.noAuth = token === null;
|
|
219
|
+
}
|
|
220
|
+
coppyFrom(con) {
|
|
221
|
+
this.authorization = con.authorization;
|
|
222
|
+
this.disconnected = con.disconnected;
|
|
223
|
+
this.noAuth = con.noAuth;
|
|
172
224
|
}
|
|
173
225
|
/**
|
|
174
226
|
* Establishes a connection to the backend using the supplied credentials.
|
|
@@ -196,7 +248,7 @@ export default class BaseCon {
|
|
|
196
248
|
});
|
|
197
249
|
if (token.code === 200 || token.code === 201) {
|
|
198
250
|
if (!dry) {
|
|
199
|
-
this.authorization =
|
|
251
|
+
this.authorization = token.data.token;
|
|
200
252
|
this.disconnected = false;
|
|
201
253
|
this.noAuth = false;
|
|
202
254
|
this.reconnect = async () => {
|
|
@@ -261,7 +313,7 @@ export default class BaseCon {
|
|
|
261
313
|
dat = await this.con.get(this.p(path, config), {
|
|
262
314
|
...config,
|
|
263
315
|
headers: {
|
|
264
|
-
|
|
316
|
+
...this.tokenHeader(),
|
|
265
317
|
...formHeader,
|
|
266
318
|
...config?.headers,
|
|
267
319
|
...this.permanentHeader,
|
|
@@ -272,7 +324,7 @@ export default class BaseCon {
|
|
|
272
324
|
dat = await this.con.post(this.p(path, config), body, {
|
|
273
325
|
...config,
|
|
274
326
|
headers: {
|
|
275
|
-
|
|
327
|
+
...this.tokenHeader(),
|
|
276
328
|
...formHeader,
|
|
277
329
|
...config?.headers,
|
|
278
330
|
...this.permanentHeader,
|
|
@@ -283,7 +335,7 @@ export default class BaseCon {
|
|
|
283
335
|
dat = await this.con.patch(this.p(path, config), body, {
|
|
284
336
|
...config,
|
|
285
337
|
headers: {
|
|
286
|
-
|
|
338
|
+
...this.tokenHeader(),
|
|
287
339
|
...formHeader,
|
|
288
340
|
...config?.headers,
|
|
289
341
|
...this.permanentHeader,
|
|
@@ -294,7 +346,7 @@ export default class BaseCon {
|
|
|
294
346
|
dat = await this.con.delete(this.p(path, config), {
|
|
295
347
|
...config,
|
|
296
348
|
headers: {
|
|
297
|
-
|
|
349
|
+
...this.tokenHeader(),
|
|
298
350
|
...formHeader,
|
|
299
351
|
...config?.headers,
|
|
300
352
|
...this.permanentHeader,
|
package/dist/mjs/index.d.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import SwaggerUtil from './Swagger/SwaggerUtil.js';
|
|
2
2
|
import SPathUtil from './Swagger/Path/SPathUtil.js';
|
|
3
3
|
import SwaggerClient from './Swagger/Client/SwaggerClient.js';
|
|
4
|
+
export * from './Swagger/Path/ESchemaEditor.js';
|
|
4
5
|
export * from './Swagger/debug/index.js';
|
|
5
6
|
export * from './Swagger/annotation/index.js';
|
|
6
7
|
export * from './Swagger/Meta/Swagger.js';
|
package/dist/mjs/index.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import SwaggerUtil from './Swagger/SwaggerUtil.js';
|
|
2
2
|
import SPathUtil from './Swagger/Path/SPathUtil.js';
|
|
3
3
|
import SwaggerClient from './Swagger/Client/SwaggerClient.js';
|
|
4
|
+
export * from './Swagger/Path/ESchemaEditor.js';
|
|
4
5
|
export * from './Swagger/debug/index.js';
|
|
5
6
|
export * from './Swagger/annotation/index.js';
|
|
6
7
|
export * from './Swagger/Meta/Swagger.js';
|
package/package.json
CHANGED
|
@@ -65,7 +65,7 @@ export interface ConHandle {
|
|
|
65
65
|
export default class BaseCon {
|
|
66
66
|
private api: string;
|
|
67
67
|
|
|
68
|
-
private permanentHeader:
|
|
68
|
+
private permanentHeader: Record<string, string>;
|
|
69
69
|
|
|
70
70
|
private authorization: string | null;
|
|
71
71
|
|
|
@@ -81,10 +81,21 @@ export default class BaseCon {
|
|
|
81
81
|
|
|
82
82
|
onReconnect: (con: BaseCon) => Promise<boolean>;
|
|
83
83
|
|
|
84
|
+
/**
|
|
85
|
+
* Initializes a new instance of the client.
|
|
86
|
+
*
|
|
87
|
+
* @param {Object} conf Configuration object.
|
|
88
|
+
* @param {ConHandle} conf.con - Connection handle.
|
|
89
|
+
* @param {string} conf.endpoint - API endpoint URL.
|
|
90
|
+
* @param {(arg: any) => void} [conf.logger] - Optional logger function; defaults to console.log.
|
|
91
|
+
* @param {Record<string, string>} [conf.permanentHeader] - Optional permanent headers to include in requests.
|
|
92
|
+
* @return {void}
|
|
93
|
+
*/
|
|
84
94
|
constructor(conf: {
|
|
85
95
|
con: ConHandle;
|
|
86
96
|
endpoint: string;
|
|
87
97
|
logger?: (arg: any) => void;
|
|
98
|
+
permanentHeader?: Record<string, string>;
|
|
88
99
|
}) {
|
|
89
100
|
this.api = conf.endpoint;
|
|
90
101
|
this.logger = conf.logger ?? console.log;
|
|
@@ -92,6 +103,7 @@ export default class BaseCon {
|
|
|
92
103
|
this.noAuth = false;
|
|
93
104
|
this.authorization = null;
|
|
94
105
|
this.con = conf.con;
|
|
106
|
+
this.permanentHeader = conf.permanentHeader || {};
|
|
95
107
|
this.reconnect = async () => {
|
|
96
108
|
this.disconnected = true;
|
|
97
109
|
return false;
|
|
@@ -133,12 +145,24 @@ export default class BaseCon {
|
|
|
133
145
|
}
|
|
134
146
|
|
|
135
147
|
/**
|
|
136
|
-
*
|
|
148
|
+
* Retrieves the current authorization token.
|
|
149
|
+
*
|
|
150
|
+
* @return {string|null} The token string used for authorization, or {@code null} if no token is set.
|
|
151
|
+
*/
|
|
152
|
+
token(): string | null {
|
|
153
|
+
return this.authorization;
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
/**
|
|
157
|
+
* Returns the bearer token string if an authorization value has been set.
|
|
137
158
|
*
|
|
138
|
-
* @return {string} The
|
|
159
|
+
* @return {string|null} The bearer token prefixed with "Bearer ", or {@code null} when no authorization is present.
|
|
139
160
|
*/
|
|
140
|
-
|
|
141
|
-
|
|
161
|
+
getBearer(): string | null {
|
|
162
|
+
if (this.authorization) {
|
|
163
|
+
return `Bearer ${this.authorization}`;
|
|
164
|
+
}
|
|
165
|
+
return null;
|
|
142
166
|
}
|
|
143
167
|
|
|
144
168
|
private p(path: string, config?: ConHandleConfig) {
|
|
@@ -179,6 +203,29 @@ export default class BaseCon {
|
|
|
179
203
|
}
|
|
180
204
|
}
|
|
181
205
|
|
|
206
|
+
/**
|
|
207
|
+
* Builds the Authorization header for HTTP requests.
|
|
208
|
+
*
|
|
209
|
+
* If the instance has an authorization token, this method returns an object
|
|
210
|
+
* containing a single `Authorization` header with the Bearer token value.
|
|
211
|
+
* When no token is available, an empty header object is returned.
|
|
212
|
+
*
|
|
213
|
+
* @return {Record<string, string>} The headers object containing the
|
|
214
|
+
* `Authorization` header when applicable, otherwise an empty object.
|
|
215
|
+
*/
|
|
216
|
+
private tokenHeader(): Record<string, string> {
|
|
217
|
+
if (this.authorization) {
|
|
218
|
+
return {
|
|
219
|
+
Authorization: this.getBearer()!,
|
|
220
|
+
};
|
|
221
|
+
}
|
|
222
|
+
return {};
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
setPermanentHeader(header: Record<string, string>): void {
|
|
226
|
+
this.permanentHeader = header;
|
|
227
|
+
}
|
|
228
|
+
|
|
182
229
|
/**
|
|
183
230
|
* Validates the current authentication token by performing a ping and a test request
|
|
184
231
|
* to the backend. The method first ensures connectivity via {@link ping}. If the ping
|
|
@@ -199,9 +246,7 @@ export default class BaseCon {
|
|
|
199
246
|
const con = await this.con.get<{ token: string }>(
|
|
200
247
|
this.p('/test/auth'),
|
|
201
248
|
{
|
|
202
|
-
headers:
|
|
203
|
-
Authorization: this.token(),
|
|
204
|
-
},
|
|
249
|
+
headers: this.tokenHeader(),
|
|
205
250
|
},
|
|
206
251
|
);
|
|
207
252
|
return con.code === 200 || con.code === 201;
|
|
@@ -238,19 +283,31 @@ export default class BaseCon {
|
|
|
238
283
|
this.logger('cant connect to backend');
|
|
239
284
|
this.authorization = null;
|
|
240
285
|
this.disconnected = true;
|
|
286
|
+
this.noAuth = false;
|
|
241
287
|
return false;
|
|
242
288
|
}
|
|
243
289
|
|
|
244
290
|
/**
|
|
245
291
|
* Forces a connection using the provided bearer token.
|
|
246
292
|
*
|
|
247
|
-
* @param {string} token The token to be used for authentication.
|
|
293
|
+
* @param {string|null} token The token to be used for authentication.
|
|
248
294
|
* @returns {void}
|
|
249
295
|
*/
|
|
250
|
-
|
|
251
|
-
|
|
296
|
+
forceConnect(token: string | null): void {
|
|
297
|
+
if (token) {
|
|
298
|
+
this.authorization = token.replace('Bearer ', '').replace('bearer ', '');
|
|
299
|
+
} else {
|
|
300
|
+
this.authorization = null;
|
|
301
|
+
}
|
|
302
|
+
|
|
252
303
|
this.disconnected = false;
|
|
253
|
-
this.noAuth =
|
|
304
|
+
this.noAuth = token === null;
|
|
305
|
+
}
|
|
306
|
+
|
|
307
|
+
coppyFrom(con: BaseCon): void {
|
|
308
|
+
this.authorization = con.authorization;
|
|
309
|
+
this.disconnected = con.disconnected;
|
|
310
|
+
this.noAuth = con.noAuth;
|
|
254
311
|
}
|
|
255
312
|
|
|
256
313
|
/**
|
|
@@ -289,7 +346,7 @@ export default class BaseCon {
|
|
|
289
346
|
});
|
|
290
347
|
if (token.code === 200 || token.code === 201) {
|
|
291
348
|
if (!dry) {
|
|
292
|
-
this.authorization =
|
|
349
|
+
this.authorization = token.data!.token;
|
|
293
350
|
this.disconnected = false;
|
|
294
351
|
this.noAuth = false;
|
|
295
352
|
this.reconnect = async () => {
|
|
@@ -363,7 +420,7 @@ export default class BaseCon {
|
|
|
363
420
|
dat = await this.con.get<T>(this.p(path, config), {
|
|
364
421
|
...config,
|
|
365
422
|
headers: {
|
|
366
|
-
|
|
423
|
+
...this.tokenHeader(),
|
|
367
424
|
...formHeader,
|
|
368
425
|
...config?.headers,
|
|
369
426
|
...this.permanentHeader,
|
|
@@ -374,7 +431,7 @@ export default class BaseCon {
|
|
|
374
431
|
dat = await this.con.post<T, J>(this.p(path, config), body, {
|
|
375
432
|
...config,
|
|
376
433
|
headers: {
|
|
377
|
-
|
|
434
|
+
...this.tokenHeader(),
|
|
378
435
|
...formHeader,
|
|
379
436
|
...config?.headers,
|
|
380
437
|
...this.permanentHeader,
|
|
@@ -385,7 +442,7 @@ export default class BaseCon {
|
|
|
385
442
|
dat = await this.con.patch<T, J>(this.p(path, config), body, {
|
|
386
443
|
...config,
|
|
387
444
|
headers: {
|
|
388
|
-
|
|
445
|
+
...this.tokenHeader(),
|
|
389
446
|
...formHeader,
|
|
390
447
|
...config?.headers,
|
|
391
448
|
...this.permanentHeader,
|
|
@@ -396,7 +453,7 @@ export default class BaseCon {
|
|
|
396
453
|
dat = await this.con.delete<T>(this.p(path, config), {
|
|
397
454
|
...config,
|
|
398
455
|
headers: {
|
|
399
|
-
|
|
456
|
+
...this.tokenHeader(),
|
|
400
457
|
...formHeader,
|
|
401
458
|
...config?.headers,
|
|
402
459
|
...this.permanentHeader,
|