@vulog/aima-client 1.2.30 → 1.2.31
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/index.cjs +281 -0
- package/dist/index.d.cts +43 -0
- package/dist/index.d.mts +31 -29
- package/dist/index.mjs +249 -285
- package/package.json +18 -5
- package/{tsup.config.ts → tsdown.config.ts} +1 -1
- package/vitest.config.ts +3 -3
- package/dist/index.d.ts +0 -41
- package/dist/index.js +0 -330
- /package/{.eslintrc.js → .eslintrc.cjs} +0 -0
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,281 @@
|
|
|
1
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
2
|
+
//#region \0rolldown/runtime.js
|
|
3
|
+
var __create = Object.create;
|
|
4
|
+
var __defProp = Object.defineProperty;
|
|
5
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
6
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
7
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
8
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
9
|
+
var __copyProps = (to, from, except, desc) => {
|
|
10
|
+
if (from && typeof from === "object" || typeof from === "function") for (var keys = __getOwnPropNames(from), i = 0, n = keys.length, key; i < n; i++) {
|
|
11
|
+
key = keys[i];
|
|
12
|
+
if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, {
|
|
13
|
+
get: ((k) => from[k]).bind(null, key),
|
|
14
|
+
enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
|
|
15
|
+
});
|
|
16
|
+
}
|
|
17
|
+
return to;
|
|
18
|
+
};
|
|
19
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", {
|
|
20
|
+
value: mod,
|
|
21
|
+
enumerable: true
|
|
22
|
+
}) : target, mod));
|
|
23
|
+
//#endregion
|
|
24
|
+
let axios = require("axios");
|
|
25
|
+
axios = __toESM(axios);
|
|
26
|
+
let es_toolkit = require("es-toolkit");
|
|
27
|
+
let lru_cache = require("lru-cache");
|
|
28
|
+
//#region src/CurlHelper.ts
|
|
29
|
+
var CurlHelper = class {
|
|
30
|
+
request;
|
|
31
|
+
constructor(config) {
|
|
32
|
+
this.request = config;
|
|
33
|
+
}
|
|
34
|
+
getHeaders() {
|
|
35
|
+
let { headers } = this.request;
|
|
36
|
+
let curlHeaders = "";
|
|
37
|
+
if (headers.hasOwnProperty("common")) headers = this.request.headers[this.request.method];
|
|
38
|
+
for (const property in this.request.headers) if (![
|
|
39
|
+
"common",
|
|
40
|
+
"delete",
|
|
41
|
+
"get",
|
|
42
|
+
"head",
|
|
43
|
+
"patch",
|
|
44
|
+
"post",
|
|
45
|
+
"put"
|
|
46
|
+
].includes(property)) headers[property] = this.request.headers[property];
|
|
47
|
+
for (const property in headers) if ({}.hasOwnProperty.call(headers, property)) {
|
|
48
|
+
const header = `${property}:${headers[property]}`;
|
|
49
|
+
curlHeaders = `${curlHeaders} -H '${header}'`;
|
|
50
|
+
}
|
|
51
|
+
return curlHeaders.trim();
|
|
52
|
+
}
|
|
53
|
+
getMethod() {
|
|
54
|
+
return `-X ${this.request.method.toUpperCase()}`;
|
|
55
|
+
}
|
|
56
|
+
getBody() {
|
|
57
|
+
if (typeof this.request.data !== "undefined" && this.request.data !== "" && this.request.data !== null && this.request.method.toUpperCase() !== "GET") return `--data '${typeof this.request.data === "object" || Object.prototype.toString.call(this.request.data) === "[object Array]" ? JSON.stringify(this.request.data) : this.request.data}'`.trim();
|
|
58
|
+
return "";
|
|
59
|
+
}
|
|
60
|
+
getUrl() {
|
|
61
|
+
if (this.request.baseURL) {
|
|
62
|
+
const baseUrl = this.request.baseURL;
|
|
63
|
+
const { url } = this.request;
|
|
64
|
+
return (url.startsWith("http") ? url : `${baseUrl}/${url}`).replace(/\/{2,}/g, "/").replace("http:/", "http://").replace("https:/", "https://");
|
|
65
|
+
}
|
|
66
|
+
return this.request.url;
|
|
67
|
+
}
|
|
68
|
+
getQueryString() {
|
|
69
|
+
if (this.request.paramsSerializer) {
|
|
70
|
+
const params = this.request.paramsSerializer(this.request.params);
|
|
71
|
+
if (!params || params.length === 0) return "";
|
|
72
|
+
if (params.startsWith("?")) return params;
|
|
73
|
+
return `?${params}`;
|
|
74
|
+
}
|
|
75
|
+
let params = "";
|
|
76
|
+
let i = 0;
|
|
77
|
+
for (const param in this.request.params) if ({}.hasOwnProperty.call(this.request.params, param)) {
|
|
78
|
+
params += i !== 0 ? `&${param}=${this.request.params[param]}` : `?${param}=${this.request.params[param]}`;
|
|
79
|
+
i += 1;
|
|
80
|
+
}
|
|
81
|
+
return params;
|
|
82
|
+
}
|
|
83
|
+
getBuiltURL() {
|
|
84
|
+
let url = this.getUrl();
|
|
85
|
+
if (this.getQueryString() !== "") url += this.getQueryString();
|
|
86
|
+
return url.trim();
|
|
87
|
+
}
|
|
88
|
+
generateCommand() {
|
|
89
|
+
return `curl ${this.getMethod()} "${this.getBuiltURL()}" ${this.getHeaders()} ${this.getBody()}`.trim().replace(/\s{2,}/g, " ");
|
|
90
|
+
}
|
|
91
|
+
};
|
|
92
|
+
//#endregion
|
|
93
|
+
//#region src/getClient.ts
|
|
94
|
+
const clientCache = new lru_cache.LRUCache({ max: 100 });
|
|
95
|
+
const tokenCache = new lru_cache.LRUCache({ max: 100 });
|
|
96
|
+
const getMemoryStore = (options) => ({
|
|
97
|
+
getToken: async () => {
|
|
98
|
+
const log = options.onLog ?? console.log;
|
|
99
|
+
log("getMemoryStore.getToken", options.name ?? options.fleetId);
|
|
100
|
+
if (tokenCache.has(options.name ?? options.fleetId)) {
|
|
101
|
+
log("getMemoryStore.getToken", tokenCache.get(options.name ?? options.fleetId));
|
|
102
|
+
return tokenCache.get(options.name ?? options.fleetId);
|
|
103
|
+
}
|
|
104
|
+
},
|
|
105
|
+
setToken: async (token) => {
|
|
106
|
+
(options.onLog ?? console.log)("getMemoryStore.setToken", options.name ?? options.fleetId, token);
|
|
107
|
+
tokenCache.set(options.name ?? options.fleetId, token);
|
|
108
|
+
}
|
|
109
|
+
});
|
|
110
|
+
const formatError = (error) => {
|
|
111
|
+
if (error instanceof axios.AxiosError) return {
|
|
112
|
+
originalError: error.toJSON(),
|
|
113
|
+
formattedError: {
|
|
114
|
+
status: error.response?.status ?? error.status,
|
|
115
|
+
data: error.response?.data,
|
|
116
|
+
message: error.message
|
|
117
|
+
}
|
|
118
|
+
};
|
|
119
|
+
return {
|
|
120
|
+
formattedError: {},
|
|
121
|
+
originalError: JSON.parse(JSON.stringify(error, Object.getOwnPropertyNames(error)))
|
|
122
|
+
};
|
|
123
|
+
};
|
|
124
|
+
const getClient = (options) => {
|
|
125
|
+
if (clientCache.has(options.name ?? options.fleetId)) {
|
|
126
|
+
const { options: cachedOptions, client } = clientCache.get(options.fleetId);
|
|
127
|
+
if ((0, es_toolkit.isEqual)(cachedOptions, options)) return client;
|
|
128
|
+
}
|
|
129
|
+
const client = axios.default.create({
|
|
130
|
+
baseURL: (0, es_toolkit.trimEnd)(options.baseUrl, "/"),
|
|
131
|
+
timeout: 3e4,
|
|
132
|
+
headers: {
|
|
133
|
+
"Cache-Control": "no-cache",
|
|
134
|
+
"Content-Type": "application/json",
|
|
135
|
+
"X-Api-Key": options.apiKey,
|
|
136
|
+
"User-Agent": options.userAgent ?? `aima-node/1.2.31 ${options.fleetId}`
|
|
137
|
+
},
|
|
138
|
+
withCredentials: false
|
|
139
|
+
});
|
|
140
|
+
client.clientOptions = options;
|
|
141
|
+
const clientCredentialsAuthentification = async () => {
|
|
142
|
+
const params = new URLSearchParams();
|
|
143
|
+
params.append("client_id", options.clientId);
|
|
144
|
+
params.append("client_secret", options.clientSecret);
|
|
145
|
+
params.append("securityOptions", "SSL_OP_NO_SSLv3");
|
|
146
|
+
params.append("grant_type", "client_credentials");
|
|
147
|
+
const { data: token } = await axios.default.post(`${(0, es_toolkit.trimEnd)(options.baseUrl, "/")}/auth/realms/${options.fleetMaster ?? options.fleetId}/protocol/openid-connect/token`, params, {
|
|
148
|
+
timeout: 3e4,
|
|
149
|
+
headers: { "Content-Type": "application/x-www-form-urlencoded" },
|
|
150
|
+
withCredentials: false
|
|
151
|
+
});
|
|
152
|
+
await (options.store ?? getMemoryStore(options)).setToken({
|
|
153
|
+
accessToken: token.access_token,
|
|
154
|
+
refreshToken: token.refresh_token
|
|
155
|
+
});
|
|
156
|
+
return token.access_token;
|
|
157
|
+
};
|
|
158
|
+
const refreshTokenAuthentification = async () => {
|
|
159
|
+
const store = options.store ?? getMemoryStore(options);
|
|
160
|
+
const oldToken = await store.getToken();
|
|
161
|
+
if (!oldToken?.refreshToken) throw new Error("No refresh token available");
|
|
162
|
+
const params = new URLSearchParams();
|
|
163
|
+
params.append("client_id", options.clientId);
|
|
164
|
+
params.append("client_secret", options.clientSecret);
|
|
165
|
+
params.append("securityOptions", "SSL_OP_NO_SSLv3");
|
|
166
|
+
params.append("grant_type", "refresh_token");
|
|
167
|
+
params.append("refresh_token", oldToken.refreshToken);
|
|
168
|
+
const { data: token } = await axios.default.post(`${(0, es_toolkit.trimEnd)(options.baseUrl, "/")}/auth/realms/${options.fleetMaster ?? options.fleetId}/protocol/openid-connect/token`, params, {
|
|
169
|
+
timeout: 3e4,
|
|
170
|
+
headers: { "Content-Type": "application/x-www-form-urlencoded" },
|
|
171
|
+
withCredentials: false
|
|
172
|
+
});
|
|
173
|
+
await store.setToken({
|
|
174
|
+
accessToken: token.access_token,
|
|
175
|
+
refreshToken: token.refresh_token
|
|
176
|
+
});
|
|
177
|
+
return token.access_token;
|
|
178
|
+
};
|
|
179
|
+
client.signInWithPassword = async (username, password) => {
|
|
180
|
+
if (!options.secure) throw new Error("Not secure");
|
|
181
|
+
const params = new URLSearchParams();
|
|
182
|
+
params.append("client_id", options.clientId);
|
|
183
|
+
params.append("client_secret", options.clientSecret);
|
|
184
|
+
params.append("securityOptions", "SSL_OP_NO_SSLv3");
|
|
185
|
+
params.append("grant_type", "password");
|
|
186
|
+
params.append("username", username);
|
|
187
|
+
params.append("password", password);
|
|
188
|
+
const { data: token } = await axios.default.post(`${(0, es_toolkit.trimEnd)(options.baseUrl, "/")}/auth/realms/${options.fleetMaster ?? options.fleetId}/protocol/openid-connect/token`, params, {
|
|
189
|
+
timeout: 3e4,
|
|
190
|
+
headers: { "Content-Type": "application/x-www-form-urlencoded" },
|
|
191
|
+
withCredentials: false
|
|
192
|
+
});
|
|
193
|
+
const newToken = {
|
|
194
|
+
accessToken: token.access_token,
|
|
195
|
+
refreshToken: token.refresh_token
|
|
196
|
+
};
|
|
197
|
+
await (options.store ?? getMemoryStore(options)).setToken(newToken);
|
|
198
|
+
return newToken;
|
|
199
|
+
};
|
|
200
|
+
client.interceptors.request.use(async (request) => {
|
|
201
|
+
const newRequest = request;
|
|
202
|
+
const token = await (options.store ?? getMemoryStore(options)).getToken();
|
|
203
|
+
if (token?.accessToken) newRequest.headers.Authorization = `Bearer ${token.accessToken}`;
|
|
204
|
+
if (options.logCurl) {
|
|
205
|
+
const curl = new CurlHelper(newRequest).generateCommand();
|
|
206
|
+
if (options.onLog) options.onLog({
|
|
207
|
+
curl,
|
|
208
|
+
message: "getClient > Curl command"
|
|
209
|
+
});
|
|
210
|
+
else console.log({
|
|
211
|
+
curl,
|
|
212
|
+
message: "getClient > Curl command"
|
|
213
|
+
});
|
|
214
|
+
}
|
|
215
|
+
return newRequest;
|
|
216
|
+
});
|
|
217
|
+
let isRefreshing = false;
|
|
218
|
+
let refreshSubscribers = [];
|
|
219
|
+
const executorRefresh = (config) => {
|
|
220
|
+
return new Promise((resolve, reject) => {
|
|
221
|
+
refreshSubscribers.push((token, error) => {
|
|
222
|
+
if (error) {
|
|
223
|
+
reject(formatError(error));
|
|
224
|
+
return;
|
|
225
|
+
}
|
|
226
|
+
resolve(client.request(config));
|
|
227
|
+
});
|
|
228
|
+
});
|
|
229
|
+
};
|
|
230
|
+
client.interceptors.response.use((response) => {
|
|
231
|
+
if (options.logResponse) {
|
|
232
|
+
const finalUrl = new CurlHelper(response.config).getBuiltURL();
|
|
233
|
+
const { data, headers } = response;
|
|
234
|
+
const dataLog = response.config.responseType !== "arraybuffer" && response.config.responseType !== "blob" ? data : "ArrayBuffer or blob";
|
|
235
|
+
if (options.onLog) options.onLog({
|
|
236
|
+
finalUrl,
|
|
237
|
+
data: dataLog,
|
|
238
|
+
headers,
|
|
239
|
+
message: "getClient > Response"
|
|
240
|
+
});
|
|
241
|
+
else console.log({
|
|
242
|
+
finalUrl,
|
|
243
|
+
data: dataLog,
|
|
244
|
+
headers,
|
|
245
|
+
message: "getClient > Response"
|
|
246
|
+
});
|
|
247
|
+
}
|
|
248
|
+
return response;
|
|
249
|
+
}, (error) => {
|
|
250
|
+
const { config, response: { status } = { status: 500 } } = error;
|
|
251
|
+
const originalRequest = config;
|
|
252
|
+
if (originalRequest.attemptCount === void 0) originalRequest.attemptCount = 0;
|
|
253
|
+
if (originalRequest.attemptCount === 5) return Promise.reject(formatError(error));
|
|
254
|
+
if (status === 401) {
|
|
255
|
+
originalRequest.attemptCount += 1;
|
|
256
|
+
if (!isRefreshing) {
|
|
257
|
+
isRefreshing = true;
|
|
258
|
+
let authentification;
|
|
259
|
+
if (options.secure) authentification = refreshTokenAuthentification;
|
|
260
|
+
else authentification = clientCredentialsAuthentification;
|
|
261
|
+
authentification().then((accessToken) => {
|
|
262
|
+
refreshSubscribers.forEach((cb) => cb(accessToken));
|
|
263
|
+
}).catch((errorAuth) => {
|
|
264
|
+
refreshSubscribers.forEach((cb) => cb(void 0, errorAuth));
|
|
265
|
+
}).finally(() => {
|
|
266
|
+
isRefreshing = false;
|
|
267
|
+
refreshSubscribers = [];
|
|
268
|
+
});
|
|
269
|
+
}
|
|
270
|
+
return executorRefresh(originalRequest);
|
|
271
|
+
}
|
|
272
|
+
return Promise.reject(formatError(error));
|
|
273
|
+
});
|
|
274
|
+
clientCache.set(options.name ?? options.fleetId, {
|
|
275
|
+
options,
|
|
276
|
+
client
|
|
277
|
+
});
|
|
278
|
+
return client;
|
|
279
|
+
};
|
|
280
|
+
//#endregion
|
|
281
|
+
exports.getClient = getClient;
|
package/dist/index.d.cts
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import { AxiosInstance } from "axios";
|
|
2
|
+
|
|
3
|
+
//#region src/types.d.ts
|
|
4
|
+
type Token = {
|
|
5
|
+
accessToken: string;
|
|
6
|
+
refreshToken: string;
|
|
7
|
+
};
|
|
8
|
+
type Store = {
|
|
9
|
+
getToken: () => Promise<Token | undefined>;
|
|
10
|
+
setToken: (token: Token) => Promise<void>;
|
|
11
|
+
};
|
|
12
|
+
type ClientOptions = {
|
|
13
|
+
fleetId: string;
|
|
14
|
+
name?: string;
|
|
15
|
+
fleetMaster?: string;
|
|
16
|
+
baseUrl: string;
|
|
17
|
+
clientId: string;
|
|
18
|
+
clientSecret: string;
|
|
19
|
+
apiKey: string;
|
|
20
|
+
secure?: boolean;
|
|
21
|
+
logCurl?: boolean;
|
|
22
|
+
logResponse?: boolean;
|
|
23
|
+
store?: Store;
|
|
24
|
+
onLog?: (...args: any[]) => void;
|
|
25
|
+
userAgent?: string;
|
|
26
|
+
};
|
|
27
|
+
type ClientError = {
|
|
28
|
+
formattedError: {
|
|
29
|
+
status?: number;
|
|
30
|
+
data?: any;
|
|
31
|
+
message?: string;
|
|
32
|
+
};
|
|
33
|
+
originalError: any;
|
|
34
|
+
};
|
|
35
|
+
type Client = AxiosInstance & {
|
|
36
|
+
signInWithPassword: (username: string, password: string) => Promise<Token>;
|
|
37
|
+
clientOptions: ClientOptions;
|
|
38
|
+
};
|
|
39
|
+
//#endregion
|
|
40
|
+
//#region src/getClient.d.ts
|
|
41
|
+
declare const getClient: (options: ClientOptions) => Client;
|
|
42
|
+
//#endregion
|
|
43
|
+
export { type Client, type ClientError, type ClientOptions, type Store, type Token, getClient };
|
package/dist/index.d.mts
CHANGED
|
@@ -1,41 +1,43 @@
|
|
|
1
|
-
import { AxiosInstance } from
|
|
1
|
+
import { AxiosInstance } from "axios";
|
|
2
2
|
|
|
3
|
+
//#region src/types.d.ts
|
|
3
4
|
type Token = {
|
|
4
|
-
|
|
5
|
-
|
|
5
|
+
accessToken: string;
|
|
6
|
+
refreshToken: string;
|
|
6
7
|
};
|
|
7
8
|
type Store = {
|
|
8
|
-
|
|
9
|
-
|
|
9
|
+
getToken: () => Promise<Token | undefined>;
|
|
10
|
+
setToken: (token: Token) => Promise<void>;
|
|
10
11
|
};
|
|
11
12
|
type ClientOptions = {
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
13
|
+
fleetId: string;
|
|
14
|
+
name?: string;
|
|
15
|
+
fleetMaster?: string;
|
|
16
|
+
baseUrl: string;
|
|
17
|
+
clientId: string;
|
|
18
|
+
clientSecret: string;
|
|
19
|
+
apiKey: string;
|
|
20
|
+
secure?: boolean;
|
|
21
|
+
logCurl?: boolean;
|
|
22
|
+
logResponse?: boolean;
|
|
23
|
+
store?: Store;
|
|
24
|
+
onLog?: (...args: any[]) => void;
|
|
25
|
+
userAgent?: string;
|
|
25
26
|
};
|
|
26
27
|
type ClientError = {
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
28
|
+
formattedError: {
|
|
29
|
+
status?: number;
|
|
30
|
+
data?: any;
|
|
31
|
+
message?: string;
|
|
32
|
+
};
|
|
33
|
+
originalError: any;
|
|
33
34
|
};
|
|
34
35
|
type Client = AxiosInstance & {
|
|
35
|
-
|
|
36
|
-
|
|
36
|
+
signInWithPassword: (username: string, password: string) => Promise<Token>;
|
|
37
|
+
clientOptions: ClientOptions;
|
|
37
38
|
};
|
|
38
|
-
|
|
39
|
+
//#endregion
|
|
40
|
+
//#region src/getClient.d.ts
|
|
39
41
|
declare const getClient: (options: ClientOptions) => Client;
|
|
40
|
-
|
|
41
|
-
export { type Client, type ClientError, type ClientOptions, type Store, type Token, getClient };
|
|
42
|
+
//#endregion
|
|
43
|
+
export { type Client, type ClientError, type ClientOptions, type Store, type Token, getClient };
|