@grandlinex/swagger-mate 1.3.3 → 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.
|
@@ -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
|
|
@@ -20,6 +20,16 @@ function isErrorType(x) {
|
|
|
20
20
|
* @class
|
|
21
21
|
*/
|
|
22
22
|
class BaseCon {
|
|
23
|
+
/**
|
|
24
|
+
* Initializes a new instance of the client.
|
|
25
|
+
*
|
|
26
|
+
* @param {Object} conf Configuration object.
|
|
27
|
+
* @param {ConHandle} conf.con - Connection handle.
|
|
28
|
+
* @param {string} conf.endpoint - API endpoint URL.
|
|
29
|
+
* @param {(arg: any) => void} [conf.logger] - Optional logger function; defaults to console.log.
|
|
30
|
+
* @param {Record<string, string>} [conf.permanentHeader] - Optional permanent headers to include in requests.
|
|
31
|
+
* @return {void}
|
|
32
|
+
*/
|
|
23
33
|
constructor(conf) {
|
|
24
34
|
this.api = conf.endpoint;
|
|
25
35
|
this.logger = conf.logger ?? console.log;
|
|
@@ -27,6 +37,7 @@ class BaseCon {
|
|
|
27
37
|
this.noAuth = false;
|
|
28
38
|
this.authorization = null;
|
|
29
39
|
this.con = conf.con;
|
|
40
|
+
this.permanentHeader = conf.permanentHeader || {};
|
|
30
41
|
this.reconnect = async () => {
|
|
31
42
|
this.disconnected = true;
|
|
32
43
|
return false;
|
|
@@ -64,12 +75,23 @@ class BaseCon {
|
|
|
64
75
|
return (this.noAuth || this.authorization !== null) && !this.disconnected;
|
|
65
76
|
}
|
|
66
77
|
/**
|
|
67
|
-
*
|
|
78
|
+
* Retrieves the current authorization token.
|
|
68
79
|
*
|
|
69
|
-
* @return {string} The authorization
|
|
80
|
+
* @return {string|null} The token string used for authorization, or {@code null} if no token is set.
|
|
70
81
|
*/
|
|
71
82
|
token() {
|
|
72
|
-
return this.authorization
|
|
83
|
+
return this.authorization;
|
|
84
|
+
}
|
|
85
|
+
/**
|
|
86
|
+
* Returns the bearer token string if an authorization value has been set.
|
|
87
|
+
*
|
|
88
|
+
* @return {string|null} The bearer token prefixed with "Bearer ", or {@code null} when no authorization is present.
|
|
89
|
+
*/
|
|
90
|
+
getBearer() {
|
|
91
|
+
if (this.authorization) {
|
|
92
|
+
return `Bearer ${this.authorization}`;
|
|
93
|
+
}
|
|
94
|
+
return null;
|
|
73
95
|
}
|
|
74
96
|
p(path, config) {
|
|
75
97
|
let pp = path;
|
|
@@ -108,6 +130,27 @@ class BaseCon {
|
|
|
108
130
|
return false;
|
|
109
131
|
}
|
|
110
132
|
}
|
|
133
|
+
/**
|
|
134
|
+
* Builds the Authorization header for HTTP requests.
|
|
135
|
+
*
|
|
136
|
+
* If the instance has an authorization token, this method returns an object
|
|
137
|
+
* containing a single `Authorization` header with the Bearer token value.
|
|
138
|
+
* When no token is available, an empty header object is returned.
|
|
139
|
+
*
|
|
140
|
+
* @return {Record<string, string>} The headers object containing the
|
|
141
|
+
* `Authorization` header when applicable, otherwise an empty object.
|
|
142
|
+
*/
|
|
143
|
+
tokenHeader() {
|
|
144
|
+
if (this.authorization) {
|
|
145
|
+
return {
|
|
146
|
+
Authorization: this.getBearer(),
|
|
147
|
+
};
|
|
148
|
+
}
|
|
149
|
+
return {};
|
|
150
|
+
}
|
|
151
|
+
setPermanentHeader(header) {
|
|
152
|
+
this.permanentHeader = header;
|
|
153
|
+
}
|
|
111
154
|
/**
|
|
112
155
|
* Validates the current authentication token by performing a ping and a test request
|
|
113
156
|
* to the backend. The method first ensures connectivity via {@link ping}. If the ping
|
|
@@ -126,9 +169,7 @@ class BaseCon {
|
|
|
126
169
|
if (ping) {
|
|
127
170
|
try {
|
|
128
171
|
const con = await this.con.get(this.p('/test/auth'), {
|
|
129
|
-
headers:
|
|
130
|
-
Authorization: this.token(),
|
|
131
|
-
},
|
|
172
|
+
headers: this.tokenHeader(),
|
|
132
173
|
});
|
|
133
174
|
return con.code === 200 || con.code === 201;
|
|
134
175
|
}
|
|
@@ -163,18 +204,29 @@ class BaseCon {
|
|
|
163
204
|
this.logger('cant connect to backend');
|
|
164
205
|
this.authorization = null;
|
|
165
206
|
this.disconnected = true;
|
|
207
|
+
this.noAuth = false;
|
|
166
208
|
return false;
|
|
167
209
|
}
|
|
168
210
|
/**
|
|
169
211
|
* Forces a connection using the provided bearer token.
|
|
170
212
|
*
|
|
171
|
-
* @param {string} token The token to be used for authentication.
|
|
213
|
+
* @param {string|null} token The token to be used for authentication.
|
|
172
214
|
* @returns {void}
|
|
173
215
|
*/
|
|
174
|
-
|
|
175
|
-
|
|
216
|
+
forceConnect(token) {
|
|
217
|
+
if (token) {
|
|
218
|
+
this.authorization = token.replace('Bearer ', '').replace('bearer ', '');
|
|
219
|
+
}
|
|
220
|
+
else {
|
|
221
|
+
this.authorization = null;
|
|
222
|
+
}
|
|
176
223
|
this.disconnected = false;
|
|
177
|
-
this.noAuth =
|
|
224
|
+
this.noAuth = token === null;
|
|
225
|
+
}
|
|
226
|
+
coppyFrom(con) {
|
|
227
|
+
this.authorization = con.authorization;
|
|
228
|
+
this.disconnected = con.disconnected;
|
|
229
|
+
this.noAuth = con.noAuth;
|
|
178
230
|
}
|
|
179
231
|
/**
|
|
180
232
|
* Establishes a connection to the backend using the supplied credentials.
|
|
@@ -202,7 +254,7 @@ class BaseCon {
|
|
|
202
254
|
});
|
|
203
255
|
if (token.code === 200 || token.code === 201) {
|
|
204
256
|
if (!dry) {
|
|
205
|
-
this.authorization =
|
|
257
|
+
this.authorization = token.data.token;
|
|
206
258
|
this.disconnected = false;
|
|
207
259
|
this.noAuth = false;
|
|
208
260
|
this.reconnect = async () => {
|
|
@@ -267,7 +319,7 @@ class BaseCon {
|
|
|
267
319
|
dat = await this.con.get(this.p(path, config), {
|
|
268
320
|
...config,
|
|
269
321
|
headers: {
|
|
270
|
-
|
|
322
|
+
...this.tokenHeader(),
|
|
271
323
|
...formHeader,
|
|
272
324
|
...config?.headers,
|
|
273
325
|
...this.permanentHeader,
|
|
@@ -278,7 +330,7 @@ class BaseCon {
|
|
|
278
330
|
dat = await this.con.post(this.p(path, config), body, {
|
|
279
331
|
...config,
|
|
280
332
|
headers: {
|
|
281
|
-
|
|
333
|
+
...this.tokenHeader(),
|
|
282
334
|
...formHeader,
|
|
283
335
|
...config?.headers,
|
|
284
336
|
...this.permanentHeader,
|
|
@@ -289,7 +341,7 @@ class BaseCon {
|
|
|
289
341
|
dat = await this.con.patch(this.p(path, config), body, {
|
|
290
342
|
...config,
|
|
291
343
|
headers: {
|
|
292
|
-
|
|
344
|
+
...this.tokenHeader(),
|
|
293
345
|
...formHeader,
|
|
294
346
|
...config?.headers,
|
|
295
347
|
...this.permanentHeader,
|
|
@@ -300,7 +352,7 @@ class BaseCon {
|
|
|
300
352
|
dat = await this.con.delete(this.p(path, config), {
|
|
301
353
|
...config,
|
|
302
354
|
headers: {
|
|
303
|
-
|
|
355
|
+
...this.tokenHeader(),
|
|
304
356
|
...formHeader,
|
|
305
357
|
...config?.headers,
|
|
306
358
|
...this.permanentHeader,
|
|
@@ -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/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,
|