@vulog/aima-client 1.0.0
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.d.mts +20 -0
- package/dist/index.d.ts +20 -0
- package/dist/index.js +168 -0
- package/dist/index.mjs +131 -0
- package/package.json +44 -0
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { AxiosInstance } from 'axios';
|
|
2
|
+
|
|
3
|
+
type ClientOptions = {
|
|
4
|
+
fleetId: string;
|
|
5
|
+
name?: string;
|
|
6
|
+
fleetMaster?: string;
|
|
7
|
+
baseUrl: string;
|
|
8
|
+
clientId: string;
|
|
9
|
+
clientSecret: string;
|
|
10
|
+
apiKey: string;
|
|
11
|
+
secure?: boolean;
|
|
12
|
+
};
|
|
13
|
+
type Client = AxiosInstance & {
|
|
14
|
+
refreshToken?: string;
|
|
15
|
+
signInWithPassword?: (username: string, password: string) => Promise<void>;
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
declare const getClient: (options: ClientOptions) => Client;
|
|
19
|
+
|
|
20
|
+
export { type ClientOptions, getClient };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { AxiosInstance } from 'axios';
|
|
2
|
+
|
|
3
|
+
type ClientOptions = {
|
|
4
|
+
fleetId: string;
|
|
5
|
+
name?: string;
|
|
6
|
+
fleetMaster?: string;
|
|
7
|
+
baseUrl: string;
|
|
8
|
+
clientId: string;
|
|
9
|
+
clientSecret: string;
|
|
10
|
+
apiKey: string;
|
|
11
|
+
secure?: boolean;
|
|
12
|
+
};
|
|
13
|
+
type Client = AxiosInstance & {
|
|
14
|
+
refreshToken?: string;
|
|
15
|
+
signInWithPassword?: (username: string, password: string) => Promise<void>;
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
declare const getClient: (options: ClientOptions) => Client;
|
|
19
|
+
|
|
20
|
+
export { type ClientOptions, getClient };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,168 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __create = Object.create;
|
|
3
|
+
var __defProp = Object.defineProperty;
|
|
4
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
7
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
8
|
+
var __export = (target, all) => {
|
|
9
|
+
for (var name in all)
|
|
10
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
11
|
+
};
|
|
12
|
+
var __copyProps = (to, from, except, desc) => {
|
|
13
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
14
|
+
for (let key of __getOwnPropNames(from))
|
|
15
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
16
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
17
|
+
}
|
|
18
|
+
return to;
|
|
19
|
+
};
|
|
20
|
+
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
21
|
+
// If the importer is in node compatibility mode or this is not an ESM
|
|
22
|
+
// file that has been converted to a CommonJS file using a Babel-
|
|
23
|
+
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
24
|
+
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
25
|
+
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
26
|
+
mod
|
|
27
|
+
));
|
|
28
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
29
|
+
|
|
30
|
+
// src/index.ts
|
|
31
|
+
var src_exports = {};
|
|
32
|
+
__export(src_exports, {
|
|
33
|
+
getClient: () => getClient_default
|
|
34
|
+
});
|
|
35
|
+
module.exports = __toCommonJS(src_exports);
|
|
36
|
+
|
|
37
|
+
// src/getClient.ts
|
|
38
|
+
var import_axios = __toESM(require("axios"));
|
|
39
|
+
var import_lodash = __toESM(require("lodash"));
|
|
40
|
+
var import_lru_cache = require("lru-cache");
|
|
41
|
+
var clientCache = new import_lru_cache.LRUCache({ max: 100 });
|
|
42
|
+
var formatError = (error) => {
|
|
43
|
+
const { response: { status, data } = { status: 500 } } = error ?? {};
|
|
44
|
+
return {
|
|
45
|
+
formattedError: {
|
|
46
|
+
status,
|
|
47
|
+
data
|
|
48
|
+
},
|
|
49
|
+
originalError: error
|
|
50
|
+
};
|
|
51
|
+
};
|
|
52
|
+
var getClient = (options) => {
|
|
53
|
+
if (clientCache.has(options.name ?? options.fleetId)) {
|
|
54
|
+
const { options: cachedOptions, client: client2 } = clientCache.get(options.fleetId);
|
|
55
|
+
if (import_lodash.default.isEqual(cachedOptions, options)) {
|
|
56
|
+
return client2;
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
const client = import_axios.default.create({
|
|
60
|
+
baseURL: import_lodash.default.trimEnd(options.baseUrl, "/"),
|
|
61
|
+
timeout: 3e4,
|
|
62
|
+
headers: {
|
|
63
|
+
"Cache-Control": "no-cache",
|
|
64
|
+
"Content-Type": "application/json",
|
|
65
|
+
"X-Api-Key": options.apiKey
|
|
66
|
+
},
|
|
67
|
+
withCredentials: false
|
|
68
|
+
});
|
|
69
|
+
const clientCredentialsAuthentification = async () => {
|
|
70
|
+
const params = new URLSearchParams();
|
|
71
|
+
params.append("client_id", options.clientId);
|
|
72
|
+
params.append("client_secret", options.clientSecret);
|
|
73
|
+
params.append("securityOptions", "SSL_OP_NO_SSLv3");
|
|
74
|
+
params.append("grant_type", "client_credentials");
|
|
75
|
+
const { data: token } = await import_axios.default.post(
|
|
76
|
+
`${import_lodash.default.trimEnd(options.baseUrl, "/")}/auth/realms/${options.fleetMaster ?? options.fleetId}/protocol/openid-connect/token`,
|
|
77
|
+
params,
|
|
78
|
+
{
|
|
79
|
+
timeout: 3e4,
|
|
80
|
+
headers: {
|
|
81
|
+
"Content-Type": "application/x-www-form-urlencoded"
|
|
82
|
+
},
|
|
83
|
+
withCredentials: false
|
|
84
|
+
}
|
|
85
|
+
);
|
|
86
|
+
client.defaults.headers.Authorization = `Bearer ${token.access_token}`;
|
|
87
|
+
return token.access_token;
|
|
88
|
+
};
|
|
89
|
+
const refreshTokenAuthentification = async () => {
|
|
90
|
+
if (!client.refreshToken) {
|
|
91
|
+
throw new Error("No refresh token available");
|
|
92
|
+
}
|
|
93
|
+
const params = new URLSearchParams();
|
|
94
|
+
params.append("client_id", options.clientId);
|
|
95
|
+
params.append("client_secret", options.clientSecret);
|
|
96
|
+
params.append("securityOptions", "SSL_OP_NO_SSLv3");
|
|
97
|
+
params.append("grant_type", "refresh_token");
|
|
98
|
+
params.append("refresh_token", client.refreshToken);
|
|
99
|
+
const { data: token } = await import_axios.default.post(
|
|
100
|
+
`${import_lodash.default.trimEnd(options.baseUrl, "/")}/auth/realms/${options.fleetMaster ?? options.fleetId}/protocol/openid-connect/token`,
|
|
101
|
+
params,
|
|
102
|
+
{
|
|
103
|
+
timeout: 3e4,
|
|
104
|
+
headers: {
|
|
105
|
+
"Content-Type": "application/x-www-form-urlencoded"
|
|
106
|
+
},
|
|
107
|
+
withCredentials: false
|
|
108
|
+
}
|
|
109
|
+
);
|
|
110
|
+
client.defaults.headers.Authorization = `Bearer ${token.access_token}`;
|
|
111
|
+
client.refreshToken = token.refresh_token;
|
|
112
|
+
return token.access_token;
|
|
113
|
+
};
|
|
114
|
+
client.signInWithPassword = async (username, password) => {
|
|
115
|
+
if (!options.secure) {
|
|
116
|
+
throw new Error("Not secure");
|
|
117
|
+
}
|
|
118
|
+
const params = new URLSearchParams();
|
|
119
|
+
params.append("client_id", options.clientId);
|
|
120
|
+
params.append("client_secret", options.clientSecret);
|
|
121
|
+
params.append("securityOptions", "SSL_OP_NO_SSLv3");
|
|
122
|
+
params.append("grant_type", "password");
|
|
123
|
+
params.append("username", username);
|
|
124
|
+
params.append("password", password);
|
|
125
|
+
const { data: token } = await import_axios.default.post(
|
|
126
|
+
`${import_lodash.default.trimEnd(options.baseUrl, "/")}/auth/realms/${options.fleetMaster ?? options.fleetId}/protocol/openid-connect/token`,
|
|
127
|
+
params,
|
|
128
|
+
{
|
|
129
|
+
timeout: 3e4,
|
|
130
|
+
headers: {
|
|
131
|
+
"Content-Type": "application/x-www-form-urlencoded"
|
|
132
|
+
},
|
|
133
|
+
withCredentials: false
|
|
134
|
+
}
|
|
135
|
+
);
|
|
136
|
+
client.defaults.headers.Authorization = `Bearer ${token.access_token}`;
|
|
137
|
+
client.refreshToken = token.refresh_token;
|
|
138
|
+
};
|
|
139
|
+
client.interceptors.response.use(
|
|
140
|
+
(response) => response,
|
|
141
|
+
(error) => {
|
|
142
|
+
const { config, response: { status } = { status: 500 } } = error;
|
|
143
|
+
const originalRequest = config;
|
|
144
|
+
if (originalRequest.attemptCount === void 0) {
|
|
145
|
+
originalRequest.attemptCount = 0;
|
|
146
|
+
}
|
|
147
|
+
if (originalRequest.attemptCount === 5) {
|
|
148
|
+
return Promise.reject(formatError(error));
|
|
149
|
+
}
|
|
150
|
+
if (status === 401) {
|
|
151
|
+
return new Promise((resolve, reject) => {
|
|
152
|
+
(options.secure ? refreshTokenAuthentification() : clientCredentialsAuthentification()).then((token) => {
|
|
153
|
+
originalRequest.headers.Authorization = `Bearer ${token}`;
|
|
154
|
+
resolve(client.request(originalRequest));
|
|
155
|
+
}).catch(() => reject(formatError(error)));
|
|
156
|
+
});
|
|
157
|
+
}
|
|
158
|
+
return Promise.reject(formatError(error));
|
|
159
|
+
}
|
|
160
|
+
);
|
|
161
|
+
clientCache.set(options.name ?? options.fleetId, { options, client });
|
|
162
|
+
return client;
|
|
163
|
+
};
|
|
164
|
+
var getClient_default = getClient;
|
|
165
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
166
|
+
0 && (module.exports = {
|
|
167
|
+
getClient
|
|
168
|
+
});
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
// src/getClient.ts
|
|
2
|
+
import axios from "axios";
|
|
3
|
+
import _ from "lodash";
|
|
4
|
+
import { LRUCache } from "lru-cache";
|
|
5
|
+
var clientCache = new LRUCache({ max: 100 });
|
|
6
|
+
var formatError = (error) => {
|
|
7
|
+
const { response: { status, data } = { status: 500 } } = error ?? {};
|
|
8
|
+
return {
|
|
9
|
+
formattedError: {
|
|
10
|
+
status,
|
|
11
|
+
data
|
|
12
|
+
},
|
|
13
|
+
originalError: error
|
|
14
|
+
};
|
|
15
|
+
};
|
|
16
|
+
var getClient = (options) => {
|
|
17
|
+
if (clientCache.has(options.name ?? options.fleetId)) {
|
|
18
|
+
const { options: cachedOptions, client: client2 } = clientCache.get(options.fleetId);
|
|
19
|
+
if (_.isEqual(cachedOptions, options)) {
|
|
20
|
+
return client2;
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
const client = axios.create({
|
|
24
|
+
baseURL: _.trimEnd(options.baseUrl, "/"),
|
|
25
|
+
timeout: 3e4,
|
|
26
|
+
headers: {
|
|
27
|
+
"Cache-Control": "no-cache",
|
|
28
|
+
"Content-Type": "application/json",
|
|
29
|
+
"X-Api-Key": options.apiKey
|
|
30
|
+
},
|
|
31
|
+
withCredentials: false
|
|
32
|
+
});
|
|
33
|
+
const clientCredentialsAuthentification = async () => {
|
|
34
|
+
const params = new URLSearchParams();
|
|
35
|
+
params.append("client_id", options.clientId);
|
|
36
|
+
params.append("client_secret", options.clientSecret);
|
|
37
|
+
params.append("securityOptions", "SSL_OP_NO_SSLv3");
|
|
38
|
+
params.append("grant_type", "client_credentials");
|
|
39
|
+
const { data: token } = await axios.post(
|
|
40
|
+
`${_.trimEnd(options.baseUrl, "/")}/auth/realms/${options.fleetMaster ?? options.fleetId}/protocol/openid-connect/token`,
|
|
41
|
+
params,
|
|
42
|
+
{
|
|
43
|
+
timeout: 3e4,
|
|
44
|
+
headers: {
|
|
45
|
+
"Content-Type": "application/x-www-form-urlencoded"
|
|
46
|
+
},
|
|
47
|
+
withCredentials: false
|
|
48
|
+
}
|
|
49
|
+
);
|
|
50
|
+
client.defaults.headers.Authorization = `Bearer ${token.access_token}`;
|
|
51
|
+
return token.access_token;
|
|
52
|
+
};
|
|
53
|
+
const refreshTokenAuthentification = async () => {
|
|
54
|
+
if (!client.refreshToken) {
|
|
55
|
+
throw new Error("No refresh token available");
|
|
56
|
+
}
|
|
57
|
+
const params = new URLSearchParams();
|
|
58
|
+
params.append("client_id", options.clientId);
|
|
59
|
+
params.append("client_secret", options.clientSecret);
|
|
60
|
+
params.append("securityOptions", "SSL_OP_NO_SSLv3");
|
|
61
|
+
params.append("grant_type", "refresh_token");
|
|
62
|
+
params.append("refresh_token", client.refreshToken);
|
|
63
|
+
const { data: token } = await axios.post(
|
|
64
|
+
`${_.trimEnd(options.baseUrl, "/")}/auth/realms/${options.fleetMaster ?? options.fleetId}/protocol/openid-connect/token`,
|
|
65
|
+
params,
|
|
66
|
+
{
|
|
67
|
+
timeout: 3e4,
|
|
68
|
+
headers: {
|
|
69
|
+
"Content-Type": "application/x-www-form-urlencoded"
|
|
70
|
+
},
|
|
71
|
+
withCredentials: false
|
|
72
|
+
}
|
|
73
|
+
);
|
|
74
|
+
client.defaults.headers.Authorization = `Bearer ${token.access_token}`;
|
|
75
|
+
client.refreshToken = token.refresh_token;
|
|
76
|
+
return token.access_token;
|
|
77
|
+
};
|
|
78
|
+
client.signInWithPassword = async (username, password) => {
|
|
79
|
+
if (!options.secure) {
|
|
80
|
+
throw new Error("Not secure");
|
|
81
|
+
}
|
|
82
|
+
const params = new URLSearchParams();
|
|
83
|
+
params.append("client_id", options.clientId);
|
|
84
|
+
params.append("client_secret", options.clientSecret);
|
|
85
|
+
params.append("securityOptions", "SSL_OP_NO_SSLv3");
|
|
86
|
+
params.append("grant_type", "password");
|
|
87
|
+
params.append("username", username);
|
|
88
|
+
params.append("password", password);
|
|
89
|
+
const { data: token } = await axios.post(
|
|
90
|
+
`${_.trimEnd(options.baseUrl, "/")}/auth/realms/${options.fleetMaster ?? options.fleetId}/protocol/openid-connect/token`,
|
|
91
|
+
params,
|
|
92
|
+
{
|
|
93
|
+
timeout: 3e4,
|
|
94
|
+
headers: {
|
|
95
|
+
"Content-Type": "application/x-www-form-urlencoded"
|
|
96
|
+
},
|
|
97
|
+
withCredentials: false
|
|
98
|
+
}
|
|
99
|
+
);
|
|
100
|
+
client.defaults.headers.Authorization = `Bearer ${token.access_token}`;
|
|
101
|
+
client.refreshToken = token.refresh_token;
|
|
102
|
+
};
|
|
103
|
+
client.interceptors.response.use(
|
|
104
|
+
(response) => response,
|
|
105
|
+
(error) => {
|
|
106
|
+
const { config, response: { status } = { status: 500 } } = error;
|
|
107
|
+
const originalRequest = config;
|
|
108
|
+
if (originalRequest.attemptCount === void 0) {
|
|
109
|
+
originalRequest.attemptCount = 0;
|
|
110
|
+
}
|
|
111
|
+
if (originalRequest.attemptCount === 5) {
|
|
112
|
+
return Promise.reject(formatError(error));
|
|
113
|
+
}
|
|
114
|
+
if (status === 401) {
|
|
115
|
+
return new Promise((resolve, reject) => {
|
|
116
|
+
(options.secure ? refreshTokenAuthentification() : clientCredentialsAuthentification()).then((token) => {
|
|
117
|
+
originalRequest.headers.Authorization = `Bearer ${token}`;
|
|
118
|
+
resolve(client.request(originalRequest));
|
|
119
|
+
}).catch(() => reject(formatError(error)));
|
|
120
|
+
});
|
|
121
|
+
}
|
|
122
|
+
return Promise.reject(formatError(error));
|
|
123
|
+
}
|
|
124
|
+
);
|
|
125
|
+
clientCache.set(options.name ?? options.fleetId, { options, client });
|
|
126
|
+
return client;
|
|
127
|
+
};
|
|
128
|
+
var getClient_default = getClient;
|
|
129
|
+
export {
|
|
130
|
+
getClient_default as getClient
|
|
131
|
+
};
|
package/package.json
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@vulog/aima-client",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"main": "dist/index.js",
|
|
5
|
+
"module": "dist/index.mjs",
|
|
6
|
+
"types": "dist/index.d.ts",
|
|
7
|
+
"files": [
|
|
8
|
+
"dist"
|
|
9
|
+
],
|
|
10
|
+
"scripts": {
|
|
11
|
+
"build": "tsup",
|
|
12
|
+
"dev": "tsup --watch",
|
|
13
|
+
"test": "vitest run",
|
|
14
|
+
"test:watch": "vitest"
|
|
15
|
+
},
|
|
16
|
+
"keywords": [
|
|
17
|
+
"AIMA",
|
|
18
|
+
"VULOG",
|
|
19
|
+
"HTTP",
|
|
20
|
+
"CLIENT"
|
|
21
|
+
],
|
|
22
|
+
"author": "Vulog",
|
|
23
|
+
"license": "ISC",
|
|
24
|
+
"devDependencies": {
|
|
25
|
+
"@types/lodash": "^4.17.12",
|
|
26
|
+
"@types/node": "^22.7.9",
|
|
27
|
+
"eslint-config-airbnb-base": "^15.0.0",
|
|
28
|
+
"eslint-config-airbnb-typescript": "^18.0.0",
|
|
29
|
+
"eslint-config-prettier": "^9.1.0",
|
|
30
|
+
"eslint-plugin-import": "^2.31.0",
|
|
31
|
+
"eslint-plugin-prettier": "^5.2.1",
|
|
32
|
+
"eslint-plugin-unused-imports": "^4.1.4",
|
|
33
|
+
"prettier": "^3.3.3",
|
|
34
|
+
"tsup": "^8.3.0",
|
|
35
|
+
"typescript": "^5.6.3",
|
|
36
|
+
"vitest": "^2.1.3"
|
|
37
|
+
},
|
|
38
|
+
"dependencies": {
|
|
39
|
+
"axios": "^1.7.7",
|
|
40
|
+
"lodash": "^4.17.21",
|
|
41
|
+
"lru-cache": "^11.0.1"
|
|
42
|
+
},
|
|
43
|
+
"description": ""
|
|
44
|
+
}
|