@calimero-network/mero-js 0.1.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/LICENSE +21 -0
- package/README.md +443 -0
- package/dist/admin-api/admin-client.d.ts +38 -0
- package/dist/admin-api/admin-client.d.ts.map +1 -0
- package/dist/admin-api/admin-client.js +104 -0
- package/dist/admin-api/admin-client.js.map +1 -0
- package/dist/admin-api/admin-factory.d.ts +8 -0
- package/dist/admin-api/admin-factory.d.ts.map +1 -0
- package/dist/admin-api/admin-factory.js +42 -0
- package/dist/admin-api/admin-factory.js.map +1 -0
- package/dist/admin-api/admin-types.d.ts +213 -0
- package/dist/admin-api/admin-types.d.ts.map +1 -0
- package/dist/admin-api/admin-types.js +3 -0
- package/dist/admin-api/admin-types.js.map +1 -0
- package/dist/admin-api/index.d.ts +4 -0
- package/dist/admin-api/index.d.ts.map +1 -0
- package/dist/admin-api/index.js +5 -0
- package/dist/admin-api/index.js.map +1 -0
- package/dist/auth-api/auth-client.d.ts +34 -0
- package/dist/auth-api/auth-client.d.ts.map +1 -0
- package/dist/auth-api/auth-client.js +112 -0
- package/dist/auth-api/auth-client.js.map +1 -0
- package/dist/auth-api/auth-factory.d.ts +8 -0
- package/dist/auth-api/auth-factory.d.ts.map +1 -0
- package/dist/auth-api/auth-factory.js +42 -0
- package/dist/auth-api/auth-factory.js.map +1 -0
- package/dist/auth-api/auth-types.d.ts +127 -0
- package/dist/auth-api/auth-types.d.ts.map +1 -0
- package/dist/auth-api/auth-types.js +3 -0
- package/dist/auth-api/auth-types.js.map +1 -0
- package/dist/auth-api/index.d.ts +4 -0
- package/dist/auth-api/index.d.ts.map +1 -0
- package/dist/auth-api/index.js +5 -0
- package/dist/auth-api/index.js.map +1 -0
- package/dist/http-client/api-response.d.ts +16 -0
- package/dist/http-client/api-response.d.ts.map +1 -0
- package/dist/http-client/api-response.js +2 -0
- package/dist/http-client/api-response.js.map +1 -0
- package/dist/http-client/http-factory.d.ts +32 -0
- package/dist/http-client/http-factory.d.ts.map +1 -0
- package/dist/http-client/http-factory.js +52 -0
- package/dist/http-client/http-factory.js.map +1 -0
- package/dist/http-client/http-types.d.ts +42 -0
- package/dist/http-client/http-types.d.ts.map +1 -0
- package/dist/http-client/http-types.js +2 -0
- package/dist/http-client/http-types.js.map +1 -0
- package/dist/http-client/index.d.ts +8 -0
- package/dist/http-client/index.d.ts.map +1 -0
- package/dist/http-client/index.js +12 -0
- package/dist/http-client/index.js.map +1 -0
- package/dist/http-client/retry.d.ts +6 -0
- package/dist/http-client/retry.d.ts.map +1 -0
- package/dist/http-client/retry.js +83 -0
- package/dist/http-client/retry.js.map +1 -0
- package/dist/http-client/signal-utils.d.ts +3 -0
- package/dist/http-client/signal-utils.d.ts.map +1 -0
- package/dist/http-client/signal-utils.js +42 -0
- package/dist/http-client/signal-utils.js.map +1 -0
- package/dist/http-client/web-client.d.ts +38 -0
- package/dist/http-client/web-client.d.ts.map +1 -0
- package/dist/http-client/web-client.js +296 -0
- package/dist/http-client/web-client.js.map +1 -0
- package/dist/index.browser.mjs +2 -0
- package/dist/index.browser.mjs.map +7 -0
- package/dist/index.cjs +995 -0
- package/dist/index.cjs.map +7 -0
- package/dist/index.d.ts +6 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +12 -0
- package/dist/index.js.map +1 -0
- package/dist/index.mjs +973 -0
- package/dist/index.mjs.map +7 -0
- package/dist/mero-js.d.ts +77 -0
- package/dist/mero-js.d.ts.map +1 -0
- package/dist/mero-js.js +175 -0
- package/dist/mero-js.js.map +1 -0
- package/dist/types/api-response.d.ts +16 -0
- package/dist/types/api-response.d.ts.map +1 -0
- package/dist/types/api-response.js +2 -0
- package/dist/types/api-response.js.map +1 -0
- package/dist/types/index.d.ts +2 -0
- package/dist/types/index.d.ts.map +1 -0
- package/dist/types/index.js +2 -0
- package/dist/types/index.js.map +1 -0
- package/package.json +86 -0
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,995 @@
|
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
3
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
4
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
5
|
+
var __export = (target, all) => {
|
|
6
|
+
for (var name in all)
|
|
7
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
8
|
+
};
|
|
9
|
+
var __copyProps = (to, from, except, desc) => {
|
|
10
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
11
|
+
for (let key of __getOwnPropNames(from))
|
|
12
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
13
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
14
|
+
}
|
|
15
|
+
return to;
|
|
16
|
+
};
|
|
17
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
18
|
+
|
|
19
|
+
// src/index.ts
|
|
20
|
+
var index_exports = {};
|
|
21
|
+
__export(index_exports, {
|
|
22
|
+
AdminApiClient: () => AdminApiClient,
|
|
23
|
+
AuthApiClient: () => AuthApiClient,
|
|
24
|
+
HTTPError: () => HTTPError,
|
|
25
|
+
MeroJs: () => MeroJs,
|
|
26
|
+
WebHttpClient: () => WebHttpClient,
|
|
27
|
+
combineSignals: () => combineSignals,
|
|
28
|
+
createAdminApiClient: () => createAdminApiClient,
|
|
29
|
+
createAdminApiClientFromHttpClient: () => createAdminApiClientFromHttpClient,
|
|
30
|
+
createAuthApiClient: () => createAuthApiClient,
|
|
31
|
+
createAuthApiClientFromHttpClient: () => createAuthApiClientFromHttpClient,
|
|
32
|
+
createBrowserAdminApiClient: () => createBrowserAdminApiClient,
|
|
33
|
+
createBrowserAuthApiClient: () => createBrowserAuthApiClient,
|
|
34
|
+
createBrowserHttpClient: () => createBrowserHttpClient,
|
|
35
|
+
createHttpClient: () => createHttpClient,
|
|
36
|
+
createMeroJs: () => createMeroJs,
|
|
37
|
+
createNodeAdminApiClient: () => createNodeAdminApiClient,
|
|
38
|
+
createNodeAuthApiClient: () => createNodeAuthApiClient,
|
|
39
|
+
createNodeHttpClient: () => createNodeHttpClient,
|
|
40
|
+
createRetryableMethod: () => createRetryableMethod,
|
|
41
|
+
createTimeoutSignal: () => createTimeoutSignal,
|
|
42
|
+
createUniversalHttpClient: () => createUniversalHttpClient,
|
|
43
|
+
withRetry: () => withRetry
|
|
44
|
+
});
|
|
45
|
+
module.exports = __toCommonJS(index_exports);
|
|
46
|
+
|
|
47
|
+
// src/http-client/signal-utils.ts
|
|
48
|
+
function combineSignals(signals) {
|
|
49
|
+
const list = signals.filter(Boolean);
|
|
50
|
+
if (list.length === 0) return void 0;
|
|
51
|
+
const AbortSignalAny = AbortSignal;
|
|
52
|
+
if (typeof AbortSignalAny.any === "function") {
|
|
53
|
+
try {
|
|
54
|
+
return AbortSignalAny.any(list);
|
|
55
|
+
} catch {
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
const controller = new AbortController();
|
|
59
|
+
const onAbort = (evt) => {
|
|
60
|
+
controller.abort(evt.target.reason);
|
|
61
|
+
for (const s of list) s.removeEventListener("abort", onAbort);
|
|
62
|
+
};
|
|
63
|
+
for (const s of list) {
|
|
64
|
+
if (s.aborted) return AbortSignal.abort(s.reason);
|
|
65
|
+
s.addEventListener("abort", onAbort, { once: true });
|
|
66
|
+
}
|
|
67
|
+
return controller.signal;
|
|
68
|
+
}
|
|
69
|
+
function createTimeoutSignal(timeoutMs) {
|
|
70
|
+
if (typeof AbortSignal.timeout === "function") {
|
|
71
|
+
return AbortSignal.timeout(timeoutMs);
|
|
72
|
+
}
|
|
73
|
+
const controller = new AbortController();
|
|
74
|
+
setTimeout(() => {
|
|
75
|
+
controller.abort(new DOMException("Timeout", "TimeoutError"));
|
|
76
|
+
}, timeoutMs);
|
|
77
|
+
return controller.signal;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
// src/http-client/web-client.ts
|
|
81
|
+
var HTTPError = class extends Error {
|
|
82
|
+
constructor(status, statusText, url, headers, bodyText) {
|
|
83
|
+
super(`HTTP ${status} ${statusText}`);
|
|
84
|
+
this.status = status;
|
|
85
|
+
this.statusText = statusText;
|
|
86
|
+
this.url = url;
|
|
87
|
+
this.headers = headers;
|
|
88
|
+
this.bodyText = bodyText;
|
|
89
|
+
this.name = "HTTPError";
|
|
90
|
+
}
|
|
91
|
+
toJSON() {
|
|
92
|
+
return {
|
|
93
|
+
status: this.status,
|
|
94
|
+
statusText: this.statusText,
|
|
95
|
+
url: this.url,
|
|
96
|
+
headers: headersToRecord(this.headers),
|
|
97
|
+
bodyText: this.bodyText
|
|
98
|
+
};
|
|
99
|
+
}
|
|
100
|
+
};
|
|
101
|
+
function headersToRecord(headers) {
|
|
102
|
+
const record = {};
|
|
103
|
+
headers.forEach((value, key) => {
|
|
104
|
+
record[key] = value;
|
|
105
|
+
});
|
|
106
|
+
return record;
|
|
107
|
+
}
|
|
108
|
+
var WebHttpClient = class {
|
|
109
|
+
constructor(transport) {
|
|
110
|
+
this.transport = transport;
|
|
111
|
+
}
|
|
112
|
+
async get(path, init) {
|
|
113
|
+
return this.request(path, { ...init, method: "GET" });
|
|
114
|
+
}
|
|
115
|
+
async post(path, body, init) {
|
|
116
|
+
return this.request(path, {
|
|
117
|
+
...init,
|
|
118
|
+
method: "POST",
|
|
119
|
+
body: body ? JSON.stringify(body) : void 0,
|
|
120
|
+
headers: {
|
|
121
|
+
"Content-Type": "application/json",
|
|
122
|
+
...init?.headers
|
|
123
|
+
}
|
|
124
|
+
});
|
|
125
|
+
}
|
|
126
|
+
async put(path, body, init) {
|
|
127
|
+
return this.request(path, {
|
|
128
|
+
...init,
|
|
129
|
+
method: "PUT",
|
|
130
|
+
body: body ? JSON.stringify(body) : void 0,
|
|
131
|
+
headers: {
|
|
132
|
+
"Content-Type": "application/json",
|
|
133
|
+
...init?.headers
|
|
134
|
+
}
|
|
135
|
+
});
|
|
136
|
+
}
|
|
137
|
+
async delete(path, init) {
|
|
138
|
+
return this.request(path, { ...init, method: "DELETE" });
|
|
139
|
+
}
|
|
140
|
+
async patch(path, body, init) {
|
|
141
|
+
return this.request(path, {
|
|
142
|
+
...init,
|
|
143
|
+
method: "PATCH",
|
|
144
|
+
body: body ? JSON.stringify(body) : void 0,
|
|
145
|
+
headers: {
|
|
146
|
+
"Content-Type": "application/json",
|
|
147
|
+
...init?.headers
|
|
148
|
+
}
|
|
149
|
+
});
|
|
150
|
+
}
|
|
151
|
+
async head(path, init) {
|
|
152
|
+
const response = await this.makeRequest(path, {
|
|
153
|
+
...init,
|
|
154
|
+
method: "HEAD",
|
|
155
|
+
parse: "response"
|
|
156
|
+
});
|
|
157
|
+
return {
|
|
158
|
+
headers: headersToRecord(response.headers),
|
|
159
|
+
status: response.status
|
|
160
|
+
};
|
|
161
|
+
}
|
|
162
|
+
async request(path, init) {
|
|
163
|
+
return this.makeRequest(path, init);
|
|
164
|
+
}
|
|
165
|
+
async makeRequest(path, init) {
|
|
166
|
+
const url = this.buildUrl(path);
|
|
167
|
+
const hasTauriInternals = typeof window !== "undefined" && "__TAURI_INTERNALS__" in window;
|
|
168
|
+
const hasTauri = typeof window !== "undefined" && "__TAURI__" in window;
|
|
169
|
+
const isTauri = hasTauriInternals || hasTauri || this.transport.credentials === "omit";
|
|
170
|
+
if (isTauri) {
|
|
171
|
+
const headers2 = await this.buildHeaders(init?.headers);
|
|
172
|
+
let headersObj2;
|
|
173
|
+
if (headers2 instanceof Headers) {
|
|
174
|
+
headersObj2 = {};
|
|
175
|
+
headers2.forEach((value, key) => {
|
|
176
|
+
headersObj2[key] = value;
|
|
177
|
+
});
|
|
178
|
+
} else {
|
|
179
|
+
headersObj2 = headers2;
|
|
180
|
+
}
|
|
181
|
+
const requestInit2 = {};
|
|
182
|
+
if (init?.method && init.method !== "GET") {
|
|
183
|
+
requestInit2.method = init.method;
|
|
184
|
+
}
|
|
185
|
+
if (headersObj2 && Object.keys(headersObj2).length > 0) {
|
|
186
|
+
requestInit2.headers = headersObj2;
|
|
187
|
+
}
|
|
188
|
+
if (init?.body !== void 0 && init.body !== null) {
|
|
189
|
+
requestInit2.body = init.body;
|
|
190
|
+
}
|
|
191
|
+
if (this.transport.credentials !== void 0) {
|
|
192
|
+
requestInit2.credentials = this.transport.credentials;
|
|
193
|
+
}
|
|
194
|
+
try {
|
|
195
|
+
const response = await globalThis.fetch(url, requestInit2);
|
|
196
|
+
if (!response.ok) {
|
|
197
|
+
const bodyText = await this.getBodyText(response);
|
|
198
|
+
throw new HTTPError(
|
|
199
|
+
response.status,
|
|
200
|
+
response.statusText,
|
|
201
|
+
url,
|
|
202
|
+
response.headers,
|
|
203
|
+
bodyText
|
|
204
|
+
);
|
|
205
|
+
}
|
|
206
|
+
return this.parseResponse(response, init?.parse);
|
|
207
|
+
} catch (error) {
|
|
208
|
+
if (error instanceof HTTPError) {
|
|
209
|
+
throw error;
|
|
210
|
+
}
|
|
211
|
+
throw new HTTPError(
|
|
212
|
+
0,
|
|
213
|
+
"Network Error",
|
|
214
|
+
url,
|
|
215
|
+
new Headers(),
|
|
216
|
+
error instanceof Error ? error.message : "Unknown error"
|
|
217
|
+
);
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
const signal = this.createAbortSignal(init);
|
|
221
|
+
const headers = await this.buildHeaders(init?.headers);
|
|
222
|
+
let headersObj;
|
|
223
|
+
if (headers instanceof Headers) {
|
|
224
|
+
headersObj = {};
|
|
225
|
+
headers.forEach((value, key) => {
|
|
226
|
+
headersObj[key] = value;
|
|
227
|
+
});
|
|
228
|
+
} else {
|
|
229
|
+
headersObj = headers;
|
|
230
|
+
}
|
|
231
|
+
const requestInit = {
|
|
232
|
+
method: init?.method || "GET",
|
|
233
|
+
headers: headersObj
|
|
234
|
+
};
|
|
235
|
+
if (init?.body !== void 0) {
|
|
236
|
+
requestInit.body = init.body;
|
|
237
|
+
}
|
|
238
|
+
if (signal) {
|
|
239
|
+
requestInit.signal = signal;
|
|
240
|
+
}
|
|
241
|
+
if (this.transport.credentials !== void 0) {
|
|
242
|
+
requestInit.credentials = this.transport.credentials;
|
|
243
|
+
}
|
|
244
|
+
if (init?.mode !== void 0) {
|
|
245
|
+
requestInit.mode = init.mode;
|
|
246
|
+
}
|
|
247
|
+
if (init?.cache !== void 0) {
|
|
248
|
+
requestInit.cache = init.cache;
|
|
249
|
+
}
|
|
250
|
+
if (init?.redirect !== void 0) {
|
|
251
|
+
requestInit.redirect = init.redirect;
|
|
252
|
+
}
|
|
253
|
+
if (init?.referrer !== void 0) {
|
|
254
|
+
requestInit.referrer = init.referrer;
|
|
255
|
+
}
|
|
256
|
+
if (init?.referrerPolicy !== void 0) {
|
|
257
|
+
requestInit.referrerPolicy = init.referrerPolicy;
|
|
258
|
+
}
|
|
259
|
+
if (init?.integrity !== void 0) {
|
|
260
|
+
requestInit.integrity = init.integrity;
|
|
261
|
+
}
|
|
262
|
+
if (init?.keepalive !== void 0) {
|
|
263
|
+
requestInit.keepalive = init.keepalive;
|
|
264
|
+
}
|
|
265
|
+
try {
|
|
266
|
+
const response = await this.transport.fetch(url, requestInit);
|
|
267
|
+
if (!response.ok) {
|
|
268
|
+
const bodyText = await this.getBodyText(response);
|
|
269
|
+
throw new HTTPError(
|
|
270
|
+
response.status,
|
|
271
|
+
response.statusText,
|
|
272
|
+
url,
|
|
273
|
+
response.headers,
|
|
274
|
+
bodyText
|
|
275
|
+
);
|
|
276
|
+
}
|
|
277
|
+
return this.parseResponse(response, init?.parse);
|
|
278
|
+
} catch (error) {
|
|
279
|
+
if (error instanceof HTTPError) {
|
|
280
|
+
throw error;
|
|
281
|
+
}
|
|
282
|
+
throw new HTTPError(
|
|
283
|
+
0,
|
|
284
|
+
"Network Error",
|
|
285
|
+
url,
|
|
286
|
+
new Headers(),
|
|
287
|
+
error instanceof Error ? error.message : "Unknown error"
|
|
288
|
+
);
|
|
289
|
+
}
|
|
290
|
+
}
|
|
291
|
+
buildUrl(path) {
|
|
292
|
+
if (path.startsWith("http://") || path.startsWith("https://")) {
|
|
293
|
+
return path;
|
|
294
|
+
}
|
|
295
|
+
const baseUrl = this.transport.baseUrl;
|
|
296
|
+
if (path.startsWith("/")) {
|
|
297
|
+
const base = baseUrl.endsWith("/") ? baseUrl.slice(0, -1) : baseUrl;
|
|
298
|
+
return `${base}${path}`;
|
|
299
|
+
} else {
|
|
300
|
+
const base = baseUrl.endsWith("/") ? baseUrl : `${baseUrl}/`;
|
|
301
|
+
return `${base}${path}`;
|
|
302
|
+
}
|
|
303
|
+
}
|
|
304
|
+
createAbortSignal(init) {
|
|
305
|
+
const isTauri = typeof window !== "undefined" && "__TAURI_INTERNALS__" in window;
|
|
306
|
+
if (isTauri) {
|
|
307
|
+
return void 0;
|
|
308
|
+
}
|
|
309
|
+
const signals = [];
|
|
310
|
+
if (this.transport.defaultAbortSignal) {
|
|
311
|
+
signals.push(this.transport.defaultAbortSignal);
|
|
312
|
+
}
|
|
313
|
+
if (init?.signal) {
|
|
314
|
+
signals.push(init.signal);
|
|
315
|
+
}
|
|
316
|
+
const timeoutMs = init?.timeoutMs || this.transport.timeoutMs;
|
|
317
|
+
if (timeoutMs) {
|
|
318
|
+
signals.push(createTimeoutSignal(timeoutMs));
|
|
319
|
+
}
|
|
320
|
+
return signals.length > 0 ? combineSignals(signals) : void 0;
|
|
321
|
+
}
|
|
322
|
+
async buildHeaders(initHeaders) {
|
|
323
|
+
const headers = {
|
|
324
|
+
...this.transport.defaultHeaders
|
|
325
|
+
};
|
|
326
|
+
if (this.transport.getAuthToken) {
|
|
327
|
+
try {
|
|
328
|
+
const token = await this.transport.getAuthToken();
|
|
329
|
+
if (token && token.trim() !== "") {
|
|
330
|
+
headers.Authorization = `Bearer ${token}`;
|
|
331
|
+
}
|
|
332
|
+
} catch (error) {
|
|
333
|
+
}
|
|
334
|
+
}
|
|
335
|
+
if (initHeaders) {
|
|
336
|
+
if (initHeaders instanceof Headers) {
|
|
337
|
+
initHeaders.forEach((value, key) => {
|
|
338
|
+
headers[key] = value;
|
|
339
|
+
});
|
|
340
|
+
} else if (Array.isArray(initHeaders)) {
|
|
341
|
+
initHeaders.forEach(([key, value]) => {
|
|
342
|
+
headers[key] = value;
|
|
343
|
+
});
|
|
344
|
+
} else {
|
|
345
|
+
Object.assign(headers, initHeaders);
|
|
346
|
+
}
|
|
347
|
+
}
|
|
348
|
+
return headers;
|
|
349
|
+
}
|
|
350
|
+
async parseResponse(response, parse) {
|
|
351
|
+
switch (parse) {
|
|
352
|
+
case "text":
|
|
353
|
+
return await response.text();
|
|
354
|
+
case "blob":
|
|
355
|
+
return await response.blob();
|
|
356
|
+
case "arrayBuffer":
|
|
357
|
+
return await response.arrayBuffer();
|
|
358
|
+
case "response":
|
|
359
|
+
return response;
|
|
360
|
+
case "json":
|
|
361
|
+
default:
|
|
362
|
+
return await response.json();
|
|
363
|
+
}
|
|
364
|
+
}
|
|
365
|
+
async getBodyText(response) {
|
|
366
|
+
try {
|
|
367
|
+
const text = await response.text();
|
|
368
|
+
return text.length > 65536 ? text.slice(0, 65536) + "..." : text;
|
|
369
|
+
} catch {
|
|
370
|
+
return "";
|
|
371
|
+
}
|
|
372
|
+
}
|
|
373
|
+
};
|
|
374
|
+
|
|
375
|
+
// src/http-client/http-factory.ts
|
|
376
|
+
function createHttpClient(transport) {
|
|
377
|
+
return new WebHttpClient(transport);
|
|
378
|
+
}
|
|
379
|
+
function createBrowserHttpClient(options) {
|
|
380
|
+
const transport = {
|
|
381
|
+
fetch: globalThis.fetch,
|
|
382
|
+
baseUrl: options.baseUrl,
|
|
383
|
+
getAuthToken: options.getAuthToken,
|
|
384
|
+
onTokenRefresh: options.onTokenRefresh,
|
|
385
|
+
defaultHeaders: options.defaultHeaders,
|
|
386
|
+
timeoutMs: options.timeoutMs,
|
|
387
|
+
credentials: options.credentials,
|
|
388
|
+
// No default credentials
|
|
389
|
+
defaultAbortSignal: options.defaultAbortSignal
|
|
390
|
+
};
|
|
391
|
+
return createHttpClient(transport);
|
|
392
|
+
}
|
|
393
|
+
function createNodeHttpClient(options) {
|
|
394
|
+
const fetchImpl = options.fetch ?? globalThis.fetch;
|
|
395
|
+
if (!fetchImpl) {
|
|
396
|
+
throw new Error(
|
|
397
|
+
"No fetch implementation available. Please provide a fetch implementation (e.g., undici.fetch) or use Node.js 18+ which has native fetch support."
|
|
398
|
+
);
|
|
399
|
+
}
|
|
400
|
+
const transport = {
|
|
401
|
+
fetch: fetchImpl,
|
|
402
|
+
baseUrl: options.baseUrl,
|
|
403
|
+
getAuthToken: options.getAuthToken,
|
|
404
|
+
onTokenRefresh: options.onTokenRefresh,
|
|
405
|
+
defaultHeaders: options.defaultHeaders,
|
|
406
|
+
timeoutMs: options.timeoutMs,
|
|
407
|
+
credentials: options.credentials,
|
|
408
|
+
// Node.js doesn't have default credentials
|
|
409
|
+
defaultAbortSignal: options.defaultAbortSignal
|
|
410
|
+
};
|
|
411
|
+
return createHttpClient(transport);
|
|
412
|
+
}
|
|
413
|
+
function createUniversalHttpClient(options) {
|
|
414
|
+
if (typeof window !== "undefined") {
|
|
415
|
+
return createBrowserHttpClient(options);
|
|
416
|
+
} else {
|
|
417
|
+
return createNodeHttpClient(options);
|
|
418
|
+
}
|
|
419
|
+
}
|
|
420
|
+
|
|
421
|
+
// src/http-client/retry.ts
|
|
422
|
+
function defaultRetryCondition(error, attempt) {
|
|
423
|
+
if (attempt <= 0) return false;
|
|
424
|
+
const errorWithName = error;
|
|
425
|
+
const name = errorWithName?.name;
|
|
426
|
+
if (name === "TimeoutError") return true;
|
|
427
|
+
if (name === "AbortError") return false;
|
|
428
|
+
const errorWithStatus = error;
|
|
429
|
+
if ("status" in errorWithStatus && typeof errorWithStatus.status === "number") {
|
|
430
|
+
const status = errorWithStatus.status;
|
|
431
|
+
return status >= 500 || status === 429;
|
|
432
|
+
}
|
|
433
|
+
if (name === "TypeError") return true;
|
|
434
|
+
return false;
|
|
435
|
+
}
|
|
436
|
+
function calculateDelay(attempt) {
|
|
437
|
+
const baseDelayMs = 250;
|
|
438
|
+
const delay = baseDelayMs * Math.pow(2, attempt - 1);
|
|
439
|
+
const jitter = (Math.random() - 0.5) * 0.4 * delay;
|
|
440
|
+
return Math.max(0, delay + jitter);
|
|
441
|
+
}
|
|
442
|
+
async function withRetry(fn, options = {}) {
|
|
443
|
+
const { attempts = 3 } = options;
|
|
444
|
+
let lastError;
|
|
445
|
+
for (let attempt = 1; attempt <= attempts; attempt++) {
|
|
446
|
+
try {
|
|
447
|
+
return await fn(attempt);
|
|
448
|
+
} catch (error) {
|
|
449
|
+
lastError = error;
|
|
450
|
+
if (!defaultRetryCondition(lastError, attempts - attempt)) {
|
|
451
|
+
throw lastError;
|
|
452
|
+
}
|
|
453
|
+
let delayMs = calculateDelay(attempt);
|
|
454
|
+
const errorWithHeaders = lastError;
|
|
455
|
+
const hdrs = errorWithHeaders.headers;
|
|
456
|
+
const retryAfter = hdrs?.get?.("Retry-After");
|
|
457
|
+
if (retryAfter) {
|
|
458
|
+
const seconds = parseInt(retryAfter, 10);
|
|
459
|
+
if (!isNaN(seconds)) {
|
|
460
|
+
delayMs = Math.max(delayMs, seconds * 1e3);
|
|
461
|
+
} else {
|
|
462
|
+
const date = new Date(retryAfter);
|
|
463
|
+
if (!isNaN(date.getTime())) {
|
|
464
|
+
const waitTime = Math.max(0, date.getTime() - Date.now());
|
|
465
|
+
delayMs = Math.max(delayMs, Math.min(waitTime, 6e4));
|
|
466
|
+
}
|
|
467
|
+
}
|
|
468
|
+
}
|
|
469
|
+
await new Promise((resolve) => setTimeout(resolve, delayMs));
|
|
470
|
+
}
|
|
471
|
+
}
|
|
472
|
+
throw lastError || new Error("Retry failed without error");
|
|
473
|
+
}
|
|
474
|
+
function createRetryableMethod(method, retryOptions = {}) {
|
|
475
|
+
return async (...args) => {
|
|
476
|
+
return withRetry(() => method(...args), retryOptions);
|
|
477
|
+
};
|
|
478
|
+
}
|
|
479
|
+
|
|
480
|
+
// src/auth-api/auth-client.ts
|
|
481
|
+
var AuthApiClient = class {
|
|
482
|
+
constructor(httpClient) {
|
|
483
|
+
this.httpClient = httpClient;
|
|
484
|
+
}
|
|
485
|
+
// Health and Status Endpoints
|
|
486
|
+
async getHealth() {
|
|
487
|
+
const response = await this.httpClient.get("/auth/health");
|
|
488
|
+
if (!response.data) {
|
|
489
|
+
throw new Error("Health response data is null");
|
|
490
|
+
}
|
|
491
|
+
return response.data;
|
|
492
|
+
}
|
|
493
|
+
async getIdentity() {
|
|
494
|
+
const response = await this.httpClient.get(
|
|
495
|
+
"/admin/identity"
|
|
496
|
+
);
|
|
497
|
+
if (!response.data) {
|
|
498
|
+
throw new Error("Identity response data is null");
|
|
499
|
+
}
|
|
500
|
+
return response.data;
|
|
501
|
+
}
|
|
502
|
+
async getProviders() {
|
|
503
|
+
const response = await this.httpClient.get(
|
|
504
|
+
"/auth/providers"
|
|
505
|
+
);
|
|
506
|
+
if (!response.data) {
|
|
507
|
+
throw new Error("Providers response data is null");
|
|
508
|
+
}
|
|
509
|
+
return response.data;
|
|
510
|
+
}
|
|
511
|
+
// Authentication Endpoints
|
|
512
|
+
async getLoginPage() {
|
|
513
|
+
return this.httpClient.get("/auth/login", { parse: "text" });
|
|
514
|
+
}
|
|
515
|
+
async generateTokens(request) {
|
|
516
|
+
return this.httpClient.post("/auth/token", request);
|
|
517
|
+
}
|
|
518
|
+
async refreshToken(request) {
|
|
519
|
+
return this.httpClient.post("/auth/refresh", request);
|
|
520
|
+
}
|
|
521
|
+
async generateMockTokens(request) {
|
|
522
|
+
return this.httpClient.post("/auth/mock-token", request);
|
|
523
|
+
}
|
|
524
|
+
async getChallenge() {
|
|
525
|
+
return this.httpClient.get("/auth/challenge");
|
|
526
|
+
}
|
|
527
|
+
async validateToken(token) {
|
|
528
|
+
try {
|
|
529
|
+
const response = await this.validateTokenGet(token);
|
|
530
|
+
return {
|
|
531
|
+
valid: response.status === 200,
|
|
532
|
+
headers: response.headers,
|
|
533
|
+
status: response.status
|
|
534
|
+
};
|
|
535
|
+
} catch (error) {
|
|
536
|
+
return {
|
|
537
|
+
valid: false,
|
|
538
|
+
headers: {},
|
|
539
|
+
status: 401
|
|
540
|
+
};
|
|
541
|
+
}
|
|
542
|
+
}
|
|
543
|
+
async validateTokenGet(token) {
|
|
544
|
+
const response = await this.httpClient.head("/auth/validate", {
|
|
545
|
+
headers: { Authorization: `Bearer ${token}` }
|
|
546
|
+
});
|
|
547
|
+
return {
|
|
548
|
+
status: response.status,
|
|
549
|
+
headers: response.headers
|
|
550
|
+
};
|
|
551
|
+
}
|
|
552
|
+
async isAuthed() {
|
|
553
|
+
return this.httpClient.get("/auth/is-authed");
|
|
554
|
+
}
|
|
555
|
+
// Token Management Endpoints
|
|
556
|
+
async revokeTokens(request) {
|
|
557
|
+
return this.httpClient.post("/admin/revoke", request);
|
|
558
|
+
}
|
|
559
|
+
// Key Management Endpoints
|
|
560
|
+
async listRootKeys() {
|
|
561
|
+
const response = await this.httpClient.get("/admin/keys");
|
|
562
|
+
if (!response.data) {
|
|
563
|
+
throw new Error("Root keys response data is null");
|
|
564
|
+
}
|
|
565
|
+
return response.data;
|
|
566
|
+
}
|
|
567
|
+
async createRootKey(request) {
|
|
568
|
+
return this.httpClient.post("/admin/keys", request);
|
|
569
|
+
}
|
|
570
|
+
async deleteRootKey(keyId) {
|
|
571
|
+
return this.httpClient.delete(`/admin/keys/${keyId}`);
|
|
572
|
+
}
|
|
573
|
+
// Client Management Endpoints
|
|
574
|
+
async listClientKeys() {
|
|
575
|
+
const response = await this.httpClient.get(
|
|
576
|
+
"/admin/keys/clients"
|
|
577
|
+
);
|
|
578
|
+
if (!response.data) {
|
|
579
|
+
throw new Error("Client keys response data is null");
|
|
580
|
+
}
|
|
581
|
+
return response.data;
|
|
582
|
+
}
|
|
583
|
+
async generateClientKey(request) {
|
|
584
|
+
return this.httpClient.post("/admin/client-key", request);
|
|
585
|
+
}
|
|
586
|
+
async deleteClientKey(keyId, clientId) {
|
|
587
|
+
return this.httpClient.delete(
|
|
588
|
+
`/admin/keys/${keyId}/clients/${clientId}`
|
|
589
|
+
);
|
|
590
|
+
}
|
|
591
|
+
// Permission Management Endpoints
|
|
592
|
+
async getKeyPermissions(keyId) {
|
|
593
|
+
return this.httpClient.get(
|
|
594
|
+
`/admin/keys/${keyId}/permissions`
|
|
595
|
+
);
|
|
596
|
+
}
|
|
597
|
+
async updateKeyPermissions(keyId, permissions) {
|
|
598
|
+
return this.httpClient.put(
|
|
599
|
+
`/admin/keys/${keyId}/permissions`,
|
|
600
|
+
{ permissions }
|
|
601
|
+
);
|
|
602
|
+
}
|
|
603
|
+
};
|
|
604
|
+
|
|
605
|
+
// src/auth-api/auth-factory.ts
|
|
606
|
+
var MockHttpClient = class {
|
|
607
|
+
async get() {
|
|
608
|
+
throw new Error(
|
|
609
|
+
"HTTP client not implemented - use createAuthApiClientFromHttpClient with a real HTTP client"
|
|
610
|
+
);
|
|
611
|
+
}
|
|
612
|
+
async post() {
|
|
613
|
+
throw new Error(
|
|
614
|
+
"HTTP client not implemented - use createAuthApiClientFromHttpClient with a real HTTP client"
|
|
615
|
+
);
|
|
616
|
+
}
|
|
617
|
+
async put() {
|
|
618
|
+
throw new Error(
|
|
619
|
+
"HTTP client not implemented - use createAuthApiClientFromHttpClient with a real HTTP client"
|
|
620
|
+
);
|
|
621
|
+
}
|
|
622
|
+
async delete() {
|
|
623
|
+
throw new Error(
|
|
624
|
+
"HTTP client not implemented - use createAuthApiClientFromHttpClient with a real HTTP client"
|
|
625
|
+
);
|
|
626
|
+
}
|
|
627
|
+
async patch() {
|
|
628
|
+
throw new Error(
|
|
629
|
+
"HTTP client not implemented - use createAuthApiClientFromHttpClient with a real HTTP client"
|
|
630
|
+
);
|
|
631
|
+
}
|
|
632
|
+
async head() {
|
|
633
|
+
throw new Error(
|
|
634
|
+
"HTTP client not implemented - use createAuthApiClientFromHttpClient with a real HTTP client"
|
|
635
|
+
);
|
|
636
|
+
}
|
|
637
|
+
async request() {
|
|
638
|
+
throw new Error(
|
|
639
|
+
"HTTP client not implemented - use createAuthApiClientFromHttpClient with a real HTTP client"
|
|
640
|
+
);
|
|
641
|
+
}
|
|
642
|
+
};
|
|
643
|
+
function createBrowserAuthApiClient(_config) {
|
|
644
|
+
const httpClient = new MockHttpClient();
|
|
645
|
+
return new AuthApiClient(httpClient);
|
|
646
|
+
}
|
|
647
|
+
function createNodeAuthApiClient(_config) {
|
|
648
|
+
const httpClient = new MockHttpClient();
|
|
649
|
+
return new AuthApiClient(httpClient);
|
|
650
|
+
}
|
|
651
|
+
function createAuthApiClient(_config) {
|
|
652
|
+
const httpClient = new MockHttpClient();
|
|
653
|
+
return new AuthApiClient(httpClient);
|
|
654
|
+
}
|
|
655
|
+
function createAuthApiClientFromHttpClient(httpClient, _config) {
|
|
656
|
+
return new AuthApiClient(httpClient);
|
|
657
|
+
}
|
|
658
|
+
|
|
659
|
+
// src/admin-api/admin-client.ts
|
|
660
|
+
var AdminApiClient = class {
|
|
661
|
+
constructor(httpClient) {
|
|
662
|
+
this.httpClient = httpClient;
|
|
663
|
+
}
|
|
664
|
+
// Health and Status Endpoints
|
|
665
|
+
async healthCheck() {
|
|
666
|
+
const response = await this.httpClient.get("/health");
|
|
667
|
+
if (!response.data) {
|
|
668
|
+
throw new Error("Health response data is null");
|
|
669
|
+
}
|
|
670
|
+
return response.data;
|
|
671
|
+
}
|
|
672
|
+
async isAuthed() {
|
|
673
|
+
return this.httpClient.get("/is-authed");
|
|
674
|
+
}
|
|
675
|
+
// Application Management Endpoints
|
|
676
|
+
async installApplication(request) {
|
|
677
|
+
return this.httpClient.post(
|
|
678
|
+
"/install-application",
|
|
679
|
+
request
|
|
680
|
+
);
|
|
681
|
+
}
|
|
682
|
+
async installDevApplication(request) {
|
|
683
|
+
return this.httpClient.post(
|
|
684
|
+
"/install-dev-application",
|
|
685
|
+
request
|
|
686
|
+
);
|
|
687
|
+
}
|
|
688
|
+
async uninstallApplication(appId) {
|
|
689
|
+
return this.httpClient.delete(
|
|
690
|
+
`/applications/${appId}`
|
|
691
|
+
);
|
|
692
|
+
}
|
|
693
|
+
async listApplications() {
|
|
694
|
+
return this.httpClient.get("/applications");
|
|
695
|
+
}
|
|
696
|
+
async getApplication(appId) {
|
|
697
|
+
return this.httpClient.get(
|
|
698
|
+
`/applications/${appId}`
|
|
699
|
+
);
|
|
700
|
+
}
|
|
701
|
+
// Context Management Endpoints
|
|
702
|
+
async createContext(request) {
|
|
703
|
+
return this.httpClient.post("/contexts", request);
|
|
704
|
+
}
|
|
705
|
+
async deleteContext(contextId) {
|
|
706
|
+
return this.httpClient.delete(
|
|
707
|
+
`/contexts/${contextId}`
|
|
708
|
+
);
|
|
709
|
+
}
|
|
710
|
+
async getContexts() {
|
|
711
|
+
return this.httpClient.get("/contexts");
|
|
712
|
+
}
|
|
713
|
+
async getContext(contextId) {
|
|
714
|
+
return this.httpClient.get(`/contexts/${contextId}`);
|
|
715
|
+
}
|
|
716
|
+
// Blob Management Endpoints
|
|
717
|
+
async uploadBlob(request) {
|
|
718
|
+
return this.httpClient.post("/blobs", request);
|
|
719
|
+
}
|
|
720
|
+
async deleteBlob(blobId) {
|
|
721
|
+
return this.httpClient.delete(`/blobs/${blobId}`);
|
|
722
|
+
}
|
|
723
|
+
async listBlobs() {
|
|
724
|
+
return this.httpClient.get("/blobs");
|
|
725
|
+
}
|
|
726
|
+
async getBlob(blobId) {
|
|
727
|
+
return this.httpClient.get(`/blobs/${blobId}`);
|
|
728
|
+
}
|
|
729
|
+
// Alias Management Endpoints
|
|
730
|
+
async createAlias(request) {
|
|
731
|
+
return this.httpClient.post("/alias", request);
|
|
732
|
+
}
|
|
733
|
+
async deleteAlias(aliasId) {
|
|
734
|
+
return this.httpClient.delete(`/alias/${aliasId}`);
|
|
735
|
+
}
|
|
736
|
+
async listAliases() {
|
|
737
|
+
return this.httpClient.get("/alias");
|
|
738
|
+
}
|
|
739
|
+
async getAlias(aliasId) {
|
|
740
|
+
return this.httpClient.get(`/alias/${aliasId}`);
|
|
741
|
+
}
|
|
742
|
+
// Network Management Endpoints
|
|
743
|
+
async getNetworkPeers() {
|
|
744
|
+
return this.httpClient.get("/network/peers");
|
|
745
|
+
}
|
|
746
|
+
async getNetworkStats() {
|
|
747
|
+
return this.httpClient.get("/network/stats");
|
|
748
|
+
}
|
|
749
|
+
async getNetworkConfig() {
|
|
750
|
+
return this.httpClient.get("/network/config");
|
|
751
|
+
}
|
|
752
|
+
async updateNetworkConfig(request) {
|
|
753
|
+
return this.httpClient.put(
|
|
754
|
+
"/network/config",
|
|
755
|
+
request
|
|
756
|
+
);
|
|
757
|
+
}
|
|
758
|
+
async getPeersCount() {
|
|
759
|
+
return this.httpClient.get("/network/peers/count");
|
|
760
|
+
}
|
|
761
|
+
// System Management Endpoints
|
|
762
|
+
async getSystemInfo() {
|
|
763
|
+
return this.httpClient.get("/system/info");
|
|
764
|
+
}
|
|
765
|
+
async getSystemLogs() {
|
|
766
|
+
return this.httpClient.get("/system/logs");
|
|
767
|
+
}
|
|
768
|
+
async getSystemMetrics() {
|
|
769
|
+
return this.httpClient.get("/system/metrics");
|
|
770
|
+
}
|
|
771
|
+
async restartSystem() {
|
|
772
|
+
return this.httpClient.post("/system/restart");
|
|
773
|
+
}
|
|
774
|
+
async shutdownSystem() {
|
|
775
|
+
return this.httpClient.post("/system/shutdown");
|
|
776
|
+
}
|
|
777
|
+
};
|
|
778
|
+
|
|
779
|
+
// src/admin-api/admin-factory.ts
|
|
780
|
+
var MockHttpClient2 = class {
|
|
781
|
+
async get() {
|
|
782
|
+
throw new Error(
|
|
783
|
+
"HTTP client not implemented - use createAdminApiClientFromHttpClient with a real HTTP client"
|
|
784
|
+
);
|
|
785
|
+
}
|
|
786
|
+
async post() {
|
|
787
|
+
throw new Error(
|
|
788
|
+
"HTTP client not implemented - use createAdminApiClientFromHttpClient with a real HTTP client"
|
|
789
|
+
);
|
|
790
|
+
}
|
|
791
|
+
async put() {
|
|
792
|
+
throw new Error(
|
|
793
|
+
"HTTP client not implemented - use createAdminApiClientFromHttpClient with a real HTTP client"
|
|
794
|
+
);
|
|
795
|
+
}
|
|
796
|
+
async delete() {
|
|
797
|
+
throw new Error(
|
|
798
|
+
"HTTP client not implemented - use createAdminApiClientFromHttpClient with a real HTTP client"
|
|
799
|
+
);
|
|
800
|
+
}
|
|
801
|
+
async patch() {
|
|
802
|
+
throw new Error(
|
|
803
|
+
"HTTP client not implemented - use createAdminApiClientFromHttpClient with a real HTTP client"
|
|
804
|
+
);
|
|
805
|
+
}
|
|
806
|
+
async head() {
|
|
807
|
+
throw new Error(
|
|
808
|
+
"HTTP client not implemented - use createAdminApiClientFromHttpClient with a real HTTP client"
|
|
809
|
+
);
|
|
810
|
+
}
|
|
811
|
+
async request() {
|
|
812
|
+
throw new Error(
|
|
813
|
+
"HTTP client not implemented - use createAdminApiClientFromHttpClient with a real HTTP client"
|
|
814
|
+
);
|
|
815
|
+
}
|
|
816
|
+
};
|
|
817
|
+
function createBrowserAdminApiClient(_config) {
|
|
818
|
+
const httpClient = new MockHttpClient2();
|
|
819
|
+
return new AdminApiClient(httpClient);
|
|
820
|
+
}
|
|
821
|
+
function createNodeAdminApiClient(_config) {
|
|
822
|
+
const httpClient = new MockHttpClient2();
|
|
823
|
+
return new AdminApiClient(httpClient);
|
|
824
|
+
}
|
|
825
|
+
function createAdminApiClient(_config) {
|
|
826
|
+
const httpClient = new MockHttpClient2();
|
|
827
|
+
return new AdminApiClient(httpClient);
|
|
828
|
+
}
|
|
829
|
+
function createAdminApiClientFromHttpClient(httpClient, _config) {
|
|
830
|
+
return new AdminApiClient(httpClient);
|
|
831
|
+
}
|
|
832
|
+
|
|
833
|
+
// src/mero-js.ts
|
|
834
|
+
var MeroJs = class {
|
|
835
|
+
constructor(config) {
|
|
836
|
+
this.tokenData = null;
|
|
837
|
+
this.refreshPromise = null;
|
|
838
|
+
this.config = {
|
|
839
|
+
timeoutMs: 1e4,
|
|
840
|
+
...config
|
|
841
|
+
};
|
|
842
|
+
const isTauri = typeof window !== "undefined" && "__TAURI_INTERNALS__" in window;
|
|
843
|
+
this.httpClient = createBrowserHttpClient({
|
|
844
|
+
baseUrl: this.config.baseUrl,
|
|
845
|
+
getAuthToken: async () => {
|
|
846
|
+
const token = await this.getValidToken();
|
|
847
|
+
return token?.access_token || "";
|
|
848
|
+
},
|
|
849
|
+
timeoutMs: this.config.timeoutMs,
|
|
850
|
+
credentials: this.config.requestCredentials ?? (isTauri ? "omit" : void 0)
|
|
851
|
+
});
|
|
852
|
+
this.authClient = createAuthApiClientFromHttpClient(this.httpClient, {
|
|
853
|
+
baseUrl: this.config.baseUrl,
|
|
854
|
+
getAuthToken: async () => {
|
|
855
|
+
const token = await this.getValidToken();
|
|
856
|
+
return token?.access_token || "";
|
|
857
|
+
},
|
|
858
|
+
timeoutMs: this.config.timeoutMs
|
|
859
|
+
});
|
|
860
|
+
this.adminClient = createAdminApiClientFromHttpClient(this.httpClient, {
|
|
861
|
+
baseUrl: this.config.baseUrl,
|
|
862
|
+
getAuthToken: async () => {
|
|
863
|
+
const token = await this.getValidToken();
|
|
864
|
+
return token?.access_token || "";
|
|
865
|
+
},
|
|
866
|
+
timeoutMs: this.config.timeoutMs
|
|
867
|
+
});
|
|
868
|
+
}
|
|
869
|
+
/**
|
|
870
|
+
* Get the Auth API client
|
|
871
|
+
*/
|
|
872
|
+
get auth() {
|
|
873
|
+
return this.authClient;
|
|
874
|
+
}
|
|
875
|
+
/**
|
|
876
|
+
* Get the Admin API client
|
|
877
|
+
*/
|
|
878
|
+
get admin() {
|
|
879
|
+
return this.adminClient;
|
|
880
|
+
}
|
|
881
|
+
/**
|
|
882
|
+
* Authenticate with the provided credentials
|
|
883
|
+
* This will create the root key on first use
|
|
884
|
+
*/
|
|
885
|
+
async authenticate(credentials) {
|
|
886
|
+
const creds = credentials || this.config.credentials;
|
|
887
|
+
if (!creds) {
|
|
888
|
+
throw new Error("No credentials provided for authentication");
|
|
889
|
+
}
|
|
890
|
+
try {
|
|
891
|
+
const requestBody = {
|
|
892
|
+
auth_method: "user_password",
|
|
893
|
+
public_key: creds.username,
|
|
894
|
+
client_name: "mero-js-sdk",
|
|
895
|
+
permissions: ["admin"],
|
|
896
|
+
timestamp: Math.floor(Date.now() / 1e3),
|
|
897
|
+
provider_data: {
|
|
898
|
+
username: creds.username,
|
|
899
|
+
password: creds.password
|
|
900
|
+
}
|
|
901
|
+
};
|
|
902
|
+
const response = await this.authClient.generateTokens(requestBody);
|
|
903
|
+
this.tokenData = {
|
|
904
|
+
access_token: response.data.access_token,
|
|
905
|
+
refresh_token: response.data.refresh_token,
|
|
906
|
+
expires_at: Date.now() + 24 * 60 * 60 * 1e3
|
|
907
|
+
// Default to 24 hours
|
|
908
|
+
};
|
|
909
|
+
return this.tokenData;
|
|
910
|
+
} catch (error) {
|
|
911
|
+
throw new Error(
|
|
912
|
+
`Authentication failed: ${error instanceof Error ? error.message : "Unknown error"}`
|
|
913
|
+
);
|
|
914
|
+
}
|
|
915
|
+
}
|
|
916
|
+
/**
|
|
917
|
+
* Get a valid token, refreshing if necessary
|
|
918
|
+
*/
|
|
919
|
+
async getValidToken() {
|
|
920
|
+
if (!this.tokenData) {
|
|
921
|
+
return null;
|
|
922
|
+
}
|
|
923
|
+
const bufferTime = 5 * 60 * 1e3;
|
|
924
|
+
if (Date.now() >= this.tokenData.expires_at - bufferTime) {
|
|
925
|
+
return await this.refreshToken();
|
|
926
|
+
}
|
|
927
|
+
return this.tokenData;
|
|
928
|
+
}
|
|
929
|
+
/**
|
|
930
|
+
* Refresh the access token using the refresh token
|
|
931
|
+
*/
|
|
932
|
+
async refreshToken() {
|
|
933
|
+
if (!this.tokenData?.refresh_token) {
|
|
934
|
+
throw new Error("No refresh token available");
|
|
935
|
+
}
|
|
936
|
+
if (this.refreshPromise) {
|
|
937
|
+
return this.refreshPromise;
|
|
938
|
+
}
|
|
939
|
+
this.refreshPromise = this.performTokenRefresh();
|
|
940
|
+
try {
|
|
941
|
+
const newToken = await this.refreshPromise;
|
|
942
|
+
return newToken;
|
|
943
|
+
} finally {
|
|
944
|
+
this.refreshPromise = null;
|
|
945
|
+
}
|
|
946
|
+
}
|
|
947
|
+
/**
|
|
948
|
+
* Perform the actual token refresh
|
|
949
|
+
*/
|
|
950
|
+
async performTokenRefresh() {
|
|
951
|
+
if (!this.tokenData?.refresh_token) {
|
|
952
|
+
throw new Error("No refresh token available");
|
|
953
|
+
}
|
|
954
|
+
try {
|
|
955
|
+
const response = await this.authClient.refreshToken({
|
|
956
|
+
access_token: this.tokenData.access_token,
|
|
957
|
+
refresh_token: this.tokenData.refresh_token
|
|
958
|
+
});
|
|
959
|
+
this.tokenData = {
|
|
960
|
+
access_token: response.data.access_token,
|
|
961
|
+
refresh_token: response.data.refresh_token,
|
|
962
|
+
expires_at: Date.now() + 24 * 60 * 60 * 1e3
|
|
963
|
+
// Default to 24 hours
|
|
964
|
+
};
|
|
965
|
+
return this.tokenData;
|
|
966
|
+
} catch (error) {
|
|
967
|
+
this.clearToken();
|
|
968
|
+
throw new Error(
|
|
969
|
+
`Token refresh failed: ${error instanceof Error ? error.message : "Unknown error"}`
|
|
970
|
+
);
|
|
971
|
+
}
|
|
972
|
+
}
|
|
973
|
+
/**
|
|
974
|
+
* Clear the current token
|
|
975
|
+
*/
|
|
976
|
+
clearToken() {
|
|
977
|
+
this.tokenData = null;
|
|
978
|
+
}
|
|
979
|
+
/**
|
|
980
|
+
* Check if the SDK is authenticated
|
|
981
|
+
*/
|
|
982
|
+
isAuthenticated() {
|
|
983
|
+
return this.tokenData !== null;
|
|
984
|
+
}
|
|
985
|
+
/**
|
|
986
|
+
* Get the current token data (for debugging)
|
|
987
|
+
*/
|
|
988
|
+
getTokenData() {
|
|
989
|
+
return this.tokenData;
|
|
990
|
+
}
|
|
991
|
+
};
|
|
992
|
+
function createMeroJs(config) {
|
|
993
|
+
return new MeroJs(config);
|
|
994
|
+
}
|
|
995
|
+
//# sourceMappingURL=index.cjs.map
|