@timeback/core 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/README.md +216 -0
- package/dist/client.d.ts +147 -0
- package/dist/client.d.ts.map +1 -0
- package/dist/constants.d.ts +20 -0
- package/dist/constants.d.ts.map +1 -0
- package/dist/errors.d.ts +7 -0
- package/dist/errors.d.ts.map +1 -0
- package/dist/errors.js +1053 -0
- package/dist/index.d.ts +29 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +6932 -0
- package/dist/lib/broadcast-results.d.ts +20 -0
- package/dist/lib/broadcast-results.d.ts.map +1 -0
- package/dist/manager.d.ts +129 -0
- package/dist/manager.d.ts.map +1 -0
- package/dist/types/index.d.ts +233 -0
- package/dist/types/index.d.ts.map +1 -0
- package/dist/types.d.ts +10 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +56 -0
- package/dist/utils.d.ts +8 -0
- package/dist/utils.d.ts.map +1 -0
- package/dist/utils.js +1541 -0
- package/package.json +44 -0
package/dist/errors.js
ADDED
|
@@ -0,0 +1,1053 @@
|
|
|
1
|
+
import { createRequire } from "node:module";
|
|
2
|
+
var __create = Object.create;
|
|
3
|
+
var __getProtoOf = Object.getPrototypeOf;
|
|
4
|
+
var __defProp = Object.defineProperty;
|
|
5
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
6
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
7
|
+
var __toESM = (mod, isNodeMode, target) => {
|
|
8
|
+
target = mod != null ? __create(__getProtoOf(mod)) : {};
|
|
9
|
+
const to = isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target;
|
|
10
|
+
for (let key of __getOwnPropNames(mod))
|
|
11
|
+
if (!__hasOwnProp.call(to, key))
|
|
12
|
+
__defProp(to, key, {
|
|
13
|
+
get: () => mod[key],
|
|
14
|
+
enumerable: true
|
|
15
|
+
});
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __export = (target, all) => {
|
|
19
|
+
for (var name in all)
|
|
20
|
+
__defProp(target, name, {
|
|
21
|
+
get: all[name],
|
|
22
|
+
enumerable: true,
|
|
23
|
+
configurable: true,
|
|
24
|
+
set: (newValue) => all[name] = () => newValue
|
|
25
|
+
});
|
|
26
|
+
};
|
|
27
|
+
var __require = /* @__PURE__ */ createRequire(import.meta.url);
|
|
28
|
+
|
|
29
|
+
// ../../internal/constants/src/endpoints.ts
|
|
30
|
+
var DEFAULT_PLATFORM = "BEYOND_AI";
|
|
31
|
+
var BEYONDAI_TOKEN_URLS = {
|
|
32
|
+
staging: "https://staging-beyond-timeback-api-2-idp.auth.us-east-1.amazoncognito.com/oauth2/token",
|
|
33
|
+
production: "https://prod-beyond-timeback-api-2-idp.auth.us-east-1.amazoncognito.com/oauth2/token"
|
|
34
|
+
};
|
|
35
|
+
var BEYONDAI_API_URLS = {
|
|
36
|
+
staging: "https://api.staging.alpha-1edtech.ai",
|
|
37
|
+
production: "https://api.alpha-1edtech.ai"
|
|
38
|
+
};
|
|
39
|
+
var BEYONDAI_CALIPER_URLS = {
|
|
40
|
+
staging: "https://caliper.staging.alpha-1edtech.ai",
|
|
41
|
+
production: "https://caliper.alpha-1edtech.ai"
|
|
42
|
+
};
|
|
43
|
+
var BEYONDAI_QTI_URLS = {
|
|
44
|
+
staging: "https://qti.alpha-1edtech.ai/api",
|
|
45
|
+
production: "https://qti.alpha-1edtech.ai/api"
|
|
46
|
+
};
|
|
47
|
+
var LEARNWITHAI_TOKEN_URLS = {
|
|
48
|
+
staging: "https://platform.dev.timeback.com/auth/1.0/token",
|
|
49
|
+
production: "https://platform.timeback.com/auth/1.0/token"
|
|
50
|
+
};
|
|
51
|
+
var LEARNWITHAI_API_URLS = {
|
|
52
|
+
staging: "https://platform.dev.timeback.com",
|
|
53
|
+
production: "https://platform.timeback.com"
|
|
54
|
+
};
|
|
55
|
+
var LEARNWITHAI_CALIPER_URLS = {
|
|
56
|
+
staging: "https://platform.dev.timeback.com",
|
|
57
|
+
production: "https://platform.timeback.com"
|
|
58
|
+
};
|
|
59
|
+
var LEARNWITHAI_QTI_URLS = {
|
|
60
|
+
staging: "https://platform.dev.timeback.com",
|
|
61
|
+
production: "https://platform.timeback.com"
|
|
62
|
+
};
|
|
63
|
+
var PLATFORM_ENDPOINTS = {
|
|
64
|
+
BEYOND_AI: {
|
|
65
|
+
token: BEYONDAI_TOKEN_URLS,
|
|
66
|
+
api: BEYONDAI_API_URLS,
|
|
67
|
+
caliper: BEYONDAI_CALIPER_URLS,
|
|
68
|
+
qti: BEYONDAI_QTI_URLS
|
|
69
|
+
},
|
|
70
|
+
LEARNWITH_AI: {
|
|
71
|
+
token: LEARNWITHAI_TOKEN_URLS,
|
|
72
|
+
api: LEARNWITHAI_API_URLS,
|
|
73
|
+
caliper: LEARNWITHAI_CALIPER_URLS,
|
|
74
|
+
qti: LEARNWITHAI_QTI_URLS
|
|
75
|
+
}
|
|
76
|
+
};
|
|
77
|
+
// ../../internal/constants/src/typescript.ts
|
|
78
|
+
var TypeScriptPackages = {
|
|
79
|
+
tsc: "tsc",
|
|
80
|
+
nativePreview: "@typescript/native-preview@7.0.0-dev.20251217.1"
|
|
81
|
+
};
|
|
82
|
+
var TYPESCRIPT_PACKAGE = TypeScriptPackages.nativePreview;
|
|
83
|
+
// ../../internal/logger/src/debug.ts
|
|
84
|
+
var patterns = null;
|
|
85
|
+
var debugAll = false;
|
|
86
|
+
var debugEnvSet = false;
|
|
87
|
+
function patternToRegex(pattern) {
|
|
88
|
+
const escaped = pattern.replace(/[.+?^${}()|[\]\\]/g, "\\$&");
|
|
89
|
+
const regexStr = escaped.replace(/\*/g, ".*");
|
|
90
|
+
return new RegExp(`^${regexStr}$`);
|
|
91
|
+
}
|
|
92
|
+
function parseDebugEnv() {
|
|
93
|
+
if (patterns !== null)
|
|
94
|
+
return;
|
|
95
|
+
patterns = [];
|
|
96
|
+
if (typeof process === "undefined" || !process.env?.DEBUG) {
|
|
97
|
+
debugEnvSet = false;
|
|
98
|
+
return;
|
|
99
|
+
}
|
|
100
|
+
debugEnvSet = true;
|
|
101
|
+
const debugValue = process.env.DEBUG.trim();
|
|
102
|
+
if (debugValue === "1" || debugValue === "true" || debugValue === "*") {
|
|
103
|
+
debugAll = true;
|
|
104
|
+
return;
|
|
105
|
+
}
|
|
106
|
+
const parts = debugValue.split(",").map((p) => p.trim()).filter(Boolean);
|
|
107
|
+
for (const part of parts) {
|
|
108
|
+
if (part.startsWith("-")) {
|
|
109
|
+
patterns.push({
|
|
110
|
+
regex: patternToRegex(part.slice(1)),
|
|
111
|
+
exclude: true
|
|
112
|
+
});
|
|
113
|
+
} else {
|
|
114
|
+
patterns.push({
|
|
115
|
+
regex: patternToRegex(part),
|
|
116
|
+
exclude: false
|
|
117
|
+
});
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
const hasInclude = patterns.some((p) => !p.exclude);
|
|
121
|
+
if (!hasInclude && patterns.length > 0) {
|
|
122
|
+
debugAll = true;
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
function shouldShowDebug(scope) {
|
|
126
|
+
parseDebugEnv();
|
|
127
|
+
if (!debugEnvSet) {
|
|
128
|
+
return true;
|
|
129
|
+
}
|
|
130
|
+
if (debugAll) {
|
|
131
|
+
if (scope) {
|
|
132
|
+
for (const pattern of patterns) {
|
|
133
|
+
if (pattern.exclude && pattern.regex.test(scope)) {
|
|
134
|
+
return false;
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
return true;
|
|
139
|
+
}
|
|
140
|
+
if (!scope) {
|
|
141
|
+
return false;
|
|
142
|
+
}
|
|
143
|
+
for (const pattern of patterns) {
|
|
144
|
+
if (pattern.exclude && pattern.regex.test(scope)) {
|
|
145
|
+
return false;
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
for (const pattern of patterns) {
|
|
149
|
+
if (!pattern.exclude && pattern.regex.test(scope)) {
|
|
150
|
+
return true;
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
return false;
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
// ../../internal/logger/src/env.ts
|
|
157
|
+
function isBrowser() {
|
|
158
|
+
return typeof globalThis !== "undefined" && "window" in globalThis;
|
|
159
|
+
}
|
|
160
|
+
function detectEnvironment() {
|
|
161
|
+
if (isBrowser()) {
|
|
162
|
+
return "browser";
|
|
163
|
+
}
|
|
164
|
+
if (typeof process !== "undefined" && process.env) {
|
|
165
|
+
if (process.env["NODE_ENV"] === "test" || process.env["BUN_ENV"] === "test") {
|
|
166
|
+
return "test";
|
|
167
|
+
}
|
|
168
|
+
if (false) {}
|
|
169
|
+
if (process.env.CI || process.env.GITHUB_ACTIONS || process.env.GITLAB_CI || process.env.CIRCLECI || process.env.JENKINS_URL || process.env.BUILDKITE) {
|
|
170
|
+
return "ci";
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
return "terminal";
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
// ../../internal/logger/src/formatters/terminal.ts
|
|
177
|
+
var nodeInspect;
|
|
178
|
+
if (!isBrowser()) {
|
|
179
|
+
try {
|
|
180
|
+
const util = await import("node:util");
|
|
181
|
+
nodeInspect = util.inspect;
|
|
182
|
+
} catch {}
|
|
183
|
+
}
|
|
184
|
+
var colors = {
|
|
185
|
+
reset: "\x1B[0m",
|
|
186
|
+
bold: "\x1B[1m",
|
|
187
|
+
dim: "\x1B[2m",
|
|
188
|
+
red: "\x1B[31m",
|
|
189
|
+
yellow: "\x1B[33m",
|
|
190
|
+
blue: "\x1B[34m",
|
|
191
|
+
cyan: "\x1B[36m"
|
|
192
|
+
};
|
|
193
|
+
function getLevelColor(level) {
|
|
194
|
+
switch (level) {
|
|
195
|
+
case "debug":
|
|
196
|
+
return colors.blue;
|
|
197
|
+
case "info":
|
|
198
|
+
return colors.cyan;
|
|
199
|
+
case "warn":
|
|
200
|
+
return colors.yellow;
|
|
201
|
+
case "error":
|
|
202
|
+
return colors.red;
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
function getConsoleMethod(level) {
|
|
206
|
+
switch (level) {
|
|
207
|
+
case "debug":
|
|
208
|
+
return console.debug;
|
|
209
|
+
case "info":
|
|
210
|
+
return console.info;
|
|
211
|
+
case "warn":
|
|
212
|
+
return console.warn;
|
|
213
|
+
case "error":
|
|
214
|
+
return console.error;
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
function formatContext(context) {
|
|
218
|
+
if (nodeInspect) {
|
|
219
|
+
return nodeInspect(context, {
|
|
220
|
+
depth: null,
|
|
221
|
+
colors: true,
|
|
222
|
+
breakLength: 80,
|
|
223
|
+
compact: false
|
|
224
|
+
});
|
|
225
|
+
}
|
|
226
|
+
return JSON.stringify(context, null, 2);
|
|
227
|
+
}
|
|
228
|
+
var terminalFormatter = (entry) => {
|
|
229
|
+
const levelColor = getLevelColor(entry.level);
|
|
230
|
+
const consoleMethod = getConsoleMethod(entry.level);
|
|
231
|
+
const levelUpper = entry.level.toUpperCase().padEnd(5);
|
|
232
|
+
const isoString = entry.timestamp.toISOString().replace(/\.\d{3}Z$/, "");
|
|
233
|
+
const timestamp = `${colors.dim}[${isoString}]${colors.reset}`;
|
|
234
|
+
const level = `${levelColor}${levelUpper}${colors.reset}`;
|
|
235
|
+
const scope = entry.scope ? `${colors.bold}[${entry.scope}]${colors.reset} ` : "";
|
|
236
|
+
const prefix = `${timestamp} ${level} ${scope}${entry.message}`;
|
|
237
|
+
if (entry.context && Object.keys(entry.context).length > 0) {
|
|
238
|
+
consoleMethod(prefix, formatContext(entry.context));
|
|
239
|
+
} else {
|
|
240
|
+
consoleMethod(prefix);
|
|
241
|
+
}
|
|
242
|
+
};
|
|
243
|
+
// ../../internal/logger/src/formatters/ci.ts
|
|
244
|
+
var LEVEL_PREFIX = {
|
|
245
|
+
debug: "[DEBUG]",
|
|
246
|
+
info: "[INFO]",
|
|
247
|
+
warn: "[WARN]",
|
|
248
|
+
error: "[ERROR]"
|
|
249
|
+
};
|
|
250
|
+
function formatContext2(context) {
|
|
251
|
+
return Object.entries(context).map(([key, value]) => `${key}=${formatValue(value)}`).join(" ");
|
|
252
|
+
}
|
|
253
|
+
function formatValue(value) {
|
|
254
|
+
if (typeof value === "string")
|
|
255
|
+
return value;
|
|
256
|
+
if (typeof value === "number")
|
|
257
|
+
return String(value);
|
|
258
|
+
if (typeof value === "boolean")
|
|
259
|
+
return String(value);
|
|
260
|
+
if (value === null)
|
|
261
|
+
return "null";
|
|
262
|
+
if (value === undefined)
|
|
263
|
+
return "undefined";
|
|
264
|
+
return JSON.stringify(value);
|
|
265
|
+
}
|
|
266
|
+
var ciFormatter = (entry) => {
|
|
267
|
+
const parts = [];
|
|
268
|
+
parts.push(entry.timestamp.toISOString());
|
|
269
|
+
parts.push(LEVEL_PREFIX[entry.level]);
|
|
270
|
+
if (entry.scope) {
|
|
271
|
+
parts.push(`[${entry.scope}]`);
|
|
272
|
+
}
|
|
273
|
+
parts.push(entry.message);
|
|
274
|
+
if (entry.context && Object.keys(entry.context).length > 0) {
|
|
275
|
+
parts.push(formatContext2(entry.context));
|
|
276
|
+
}
|
|
277
|
+
console.log(parts.join(" "));
|
|
278
|
+
};
|
|
279
|
+
// ../../internal/logger/src/formatters/production.ts
|
|
280
|
+
var productionFormatter = (entry) => {
|
|
281
|
+
const output = {
|
|
282
|
+
timestamp: entry.timestamp.toISOString(),
|
|
283
|
+
level: entry.level,
|
|
284
|
+
...entry.scope && { scope: entry.scope },
|
|
285
|
+
msg: entry.message
|
|
286
|
+
};
|
|
287
|
+
if (entry.context && Object.keys(entry.context).length > 0) {
|
|
288
|
+
Object.assign(output, entry.context);
|
|
289
|
+
}
|
|
290
|
+
console.log(JSON.stringify(output));
|
|
291
|
+
};
|
|
292
|
+
// ../../internal/logger/src/formatters/browser.ts
|
|
293
|
+
var LEVEL_STYLES = {
|
|
294
|
+
debug: "color: gray",
|
|
295
|
+
info: "color: #0ea5e9",
|
|
296
|
+
warn: "color: #f59e0b",
|
|
297
|
+
error: "color: #ef4444; font-weight: bold"
|
|
298
|
+
};
|
|
299
|
+
var LEVEL_METHODS = {
|
|
300
|
+
debug: "log",
|
|
301
|
+
info: "info",
|
|
302
|
+
warn: "warn",
|
|
303
|
+
error: "error"
|
|
304
|
+
};
|
|
305
|
+
var browserFormatter = (entry) => {
|
|
306
|
+
const method = LEVEL_METHODS[entry.level];
|
|
307
|
+
const style = LEVEL_STYLES[entry.level];
|
|
308
|
+
const prefix = entry.scope ? `[${entry.scope}]` : "";
|
|
309
|
+
const label = `%c${prefix} ${entry.message}`;
|
|
310
|
+
if (entry.context && Object.keys(entry.context).length > 0) {
|
|
311
|
+
console[method](label, style, entry.context);
|
|
312
|
+
} else {
|
|
313
|
+
console[method](label, style);
|
|
314
|
+
}
|
|
315
|
+
};
|
|
316
|
+
// ../../internal/logger/src/logger.ts
|
|
317
|
+
var LOG_LEVELS = ["debug", "info", "warn", "error"];
|
|
318
|
+
function getFormatter(env) {
|
|
319
|
+
switch (env) {
|
|
320
|
+
case "terminal":
|
|
321
|
+
return terminalFormatter;
|
|
322
|
+
case "ci":
|
|
323
|
+
return ciFormatter;
|
|
324
|
+
case "production":
|
|
325
|
+
return productionFormatter;
|
|
326
|
+
case "browser":
|
|
327
|
+
return browserFormatter;
|
|
328
|
+
case "test":
|
|
329
|
+
return () => {};
|
|
330
|
+
}
|
|
331
|
+
}
|
|
332
|
+
function getDefaultMinLevel() {
|
|
333
|
+
if (typeof process !== "undefined" && process.env?.DEBUG) {
|
|
334
|
+
return "debug";
|
|
335
|
+
}
|
|
336
|
+
return "info";
|
|
337
|
+
}
|
|
338
|
+
function shouldLog(level, minLevel) {
|
|
339
|
+
return LOG_LEVELS.indexOf(level) >= LOG_LEVELS.indexOf(minLevel);
|
|
340
|
+
}
|
|
341
|
+
|
|
342
|
+
class Logger {
|
|
343
|
+
scope;
|
|
344
|
+
minLevel;
|
|
345
|
+
environment;
|
|
346
|
+
formatter;
|
|
347
|
+
defaultContext;
|
|
348
|
+
constructor(options = {}) {
|
|
349
|
+
this.scope = options.scope;
|
|
350
|
+
this.minLevel = options.minLevel ?? getDefaultMinLevel();
|
|
351
|
+
this.defaultContext = options.defaultContext ?? {};
|
|
352
|
+
this.environment = options.environment ?? detectEnvironment();
|
|
353
|
+
this.formatter = getFormatter(this.environment);
|
|
354
|
+
}
|
|
355
|
+
child(scope) {
|
|
356
|
+
const childScope = this.scope ? `${this.scope}:${scope}` : scope;
|
|
357
|
+
return new Logger({
|
|
358
|
+
scope: childScope,
|
|
359
|
+
minLevel: this.minLevel,
|
|
360
|
+
environment: this.environment,
|
|
361
|
+
defaultContext: { ...this.defaultContext }
|
|
362
|
+
});
|
|
363
|
+
}
|
|
364
|
+
withContext(context) {
|
|
365
|
+
return new Logger({
|
|
366
|
+
scope: this.scope,
|
|
367
|
+
minLevel: this.minLevel,
|
|
368
|
+
environment: this.environment,
|
|
369
|
+
defaultContext: { ...this.defaultContext, ...context }
|
|
370
|
+
});
|
|
371
|
+
}
|
|
372
|
+
debug(message, context) {
|
|
373
|
+
this.log("debug", message, context);
|
|
374
|
+
}
|
|
375
|
+
info(message, context) {
|
|
376
|
+
this.log("info", message, context);
|
|
377
|
+
}
|
|
378
|
+
warn(message, context) {
|
|
379
|
+
this.log("warn", message, context);
|
|
380
|
+
}
|
|
381
|
+
error(message, context) {
|
|
382
|
+
this.log("error", message, context);
|
|
383
|
+
}
|
|
384
|
+
log(level, message, context) {
|
|
385
|
+
if (level === "debug" && !shouldShowDebug(this.scope)) {
|
|
386
|
+
return;
|
|
387
|
+
}
|
|
388
|
+
if (!shouldLog(level, this.minLevel)) {
|
|
389
|
+
return;
|
|
390
|
+
}
|
|
391
|
+
const entry = {
|
|
392
|
+
level,
|
|
393
|
+
message,
|
|
394
|
+
scope: this.scope,
|
|
395
|
+
context: context || Object.keys(this.defaultContext).length > 0 ? { ...this.defaultContext, ...context } : undefined,
|
|
396
|
+
timestamp: new Date
|
|
397
|
+
};
|
|
398
|
+
this.formatter(entry);
|
|
399
|
+
}
|
|
400
|
+
}
|
|
401
|
+
function createLogger(options = {}) {
|
|
402
|
+
return new Logger(options);
|
|
403
|
+
}
|
|
404
|
+
// ../../internal/client-infra/src/auth/token-manager.ts
|
|
405
|
+
function isDebug() {
|
|
406
|
+
try {
|
|
407
|
+
const debug = typeof process === "undefined" ? undefined : process.env["DEBUG"];
|
|
408
|
+
return debug === "1" || debug === "true";
|
|
409
|
+
} catch {
|
|
410
|
+
return false;
|
|
411
|
+
}
|
|
412
|
+
}
|
|
413
|
+
var log = createLogger({ scope: "auth", minLevel: isDebug() ? "debug" : "warn" });
|
|
414
|
+
|
|
415
|
+
class TokenManager {
|
|
416
|
+
config;
|
|
417
|
+
accessToken = null;
|
|
418
|
+
tokenExpiry = 0;
|
|
419
|
+
pendingRequest = null;
|
|
420
|
+
fetchFn;
|
|
421
|
+
constructor(config) {
|
|
422
|
+
this.config = config;
|
|
423
|
+
this.fetchFn = config.fetch ?? globalThis.fetch.bind(globalThis);
|
|
424
|
+
}
|
|
425
|
+
async getToken() {
|
|
426
|
+
if (this.accessToken && Date.now() < this.tokenExpiry) {
|
|
427
|
+
log.debug("Using cached token");
|
|
428
|
+
return this.accessToken;
|
|
429
|
+
}
|
|
430
|
+
if (this.pendingRequest) {
|
|
431
|
+
log.debug("Waiting for in-flight token request");
|
|
432
|
+
return this.pendingRequest;
|
|
433
|
+
}
|
|
434
|
+
this.pendingRequest = this.fetchToken();
|
|
435
|
+
try {
|
|
436
|
+
return await this.pendingRequest;
|
|
437
|
+
} finally {
|
|
438
|
+
this.pendingRequest = null;
|
|
439
|
+
}
|
|
440
|
+
}
|
|
441
|
+
async fetchToken() {
|
|
442
|
+
log.debug("Fetching new access token...");
|
|
443
|
+
const { clientId, clientSecret } = this.config.credentials;
|
|
444
|
+
const credentials = btoa(`${clientId}:${clientSecret}`);
|
|
445
|
+
const start = performance.now();
|
|
446
|
+
const response = await this.fetchFn(this.config.tokenUrl, {
|
|
447
|
+
method: "POST",
|
|
448
|
+
headers: {
|
|
449
|
+
Authorization: `Basic ${credentials}`,
|
|
450
|
+
"Content-Type": "application/x-www-form-urlencoded"
|
|
451
|
+
},
|
|
452
|
+
body: "grant_type=client_credentials"
|
|
453
|
+
});
|
|
454
|
+
const duration = Math.round(performance.now() - start);
|
|
455
|
+
if (!response.ok) {
|
|
456
|
+
log.error(`Token request failed: ${response.status} ${response.statusText}`);
|
|
457
|
+
throw new Error(`Failed to obtain access token: ${response.status} ${response.statusText}`);
|
|
458
|
+
}
|
|
459
|
+
const data = await response.json();
|
|
460
|
+
this.accessToken = data.access_token;
|
|
461
|
+
this.tokenExpiry = Date.now() + (data.expires_in - 60) * 1000;
|
|
462
|
+
log.debug(`Token acquired (${duration}ms, expires in ${data.expires_in}s)`);
|
|
463
|
+
return this.accessToken;
|
|
464
|
+
}
|
|
465
|
+
invalidate() {
|
|
466
|
+
log.debug("Token invalidated");
|
|
467
|
+
this.accessToken = null;
|
|
468
|
+
this.tokenExpiry = 0;
|
|
469
|
+
}
|
|
470
|
+
}
|
|
471
|
+
// ../../internal/client-infra/src/config/paths.ts
|
|
472
|
+
var BEYONDAI_PATHS = {
|
|
473
|
+
caliper: {
|
|
474
|
+
send: "/caliper/event",
|
|
475
|
+
validate: "/caliper/event/validate",
|
|
476
|
+
list: "/caliper/events",
|
|
477
|
+
get: "/caliper/events/{id}",
|
|
478
|
+
jobStatus: "/jobs/{id}/status"
|
|
479
|
+
},
|
|
480
|
+
oneroster: {
|
|
481
|
+
rostering: "/ims/oneroster/rostering/v1p2",
|
|
482
|
+
gradebook: "/ims/oneroster/gradebook/v1p2",
|
|
483
|
+
resources: "/ims/oneroster/resources/v1p2"
|
|
484
|
+
},
|
|
485
|
+
edubridge: {
|
|
486
|
+
base: "/edubridge"
|
|
487
|
+
}
|
|
488
|
+
};
|
|
489
|
+
var LEARNWITHAI_PATHS = {
|
|
490
|
+
caliper: {
|
|
491
|
+
send: "/events/1.0/",
|
|
492
|
+
validate: null,
|
|
493
|
+
list: null,
|
|
494
|
+
get: null,
|
|
495
|
+
jobStatus: null
|
|
496
|
+
},
|
|
497
|
+
oneroster: {
|
|
498
|
+
rostering: "/rostering/1.0",
|
|
499
|
+
gradebook: "/gradebook/1.0",
|
|
500
|
+
resources: "/resources/1.0"
|
|
501
|
+
},
|
|
502
|
+
edubridge: null
|
|
503
|
+
};
|
|
504
|
+
var PLATFORM_PATHS = {
|
|
505
|
+
BEYOND_AI: BEYONDAI_PATHS,
|
|
506
|
+
LEARNWITH_AI: LEARNWITHAI_PATHS
|
|
507
|
+
};
|
|
508
|
+
|
|
509
|
+
// ../../internal/client-infra/src/config/provider.ts
|
|
510
|
+
function isEnvConfig(config) {
|
|
511
|
+
return "env" in config && !("baseUrl" in config) && !("services" in config);
|
|
512
|
+
}
|
|
513
|
+
function isExplicitConfig(config) {
|
|
514
|
+
return "baseUrl" in config && !("services" in config);
|
|
515
|
+
}
|
|
516
|
+
function isServicesConfig(config) {
|
|
517
|
+
return "services" in config;
|
|
518
|
+
}
|
|
519
|
+
function resolvePathProfiles(pathProfile, customPaths) {
|
|
520
|
+
const basePaths = pathProfile ? PLATFORM_PATHS[pathProfile] ?? BEYONDAI_PATHS : BEYONDAI_PATHS;
|
|
521
|
+
return {
|
|
522
|
+
caliper: customPaths?.caliper ?? basePaths.caliper,
|
|
523
|
+
oneroster: customPaths?.oneroster ?? basePaths.oneroster,
|
|
524
|
+
edubridge: customPaths?.edubridge ?? basePaths.edubridge
|
|
525
|
+
};
|
|
526
|
+
}
|
|
527
|
+
|
|
528
|
+
class TimebackProvider {
|
|
529
|
+
platform;
|
|
530
|
+
env;
|
|
531
|
+
auth;
|
|
532
|
+
timeout;
|
|
533
|
+
endpoints;
|
|
534
|
+
authUrl;
|
|
535
|
+
pathProfiles;
|
|
536
|
+
tokenManagers = new Map;
|
|
537
|
+
constructor(config) {
|
|
538
|
+
this.timeout = config.timeout ?? 30000;
|
|
539
|
+
if (isEnvConfig(config)) {
|
|
540
|
+
this.auth = config.auth;
|
|
541
|
+
const platform = config.platform ?? DEFAULT_PLATFORM;
|
|
542
|
+
const env = config.env;
|
|
543
|
+
this.platform = platform;
|
|
544
|
+
this.env = env;
|
|
545
|
+
const platformEndpoints = PLATFORM_ENDPOINTS[platform];
|
|
546
|
+
if (!platformEndpoints) {
|
|
547
|
+
throw new Error(`Unknown platform: ${platform}`);
|
|
548
|
+
}
|
|
549
|
+
this.authUrl = platformEndpoints.token[env];
|
|
550
|
+
this.pathProfiles = PLATFORM_PATHS[platform] ?? BEYONDAI_PATHS;
|
|
551
|
+
this.endpoints = {
|
|
552
|
+
oneroster: {
|
|
553
|
+
baseUrl: platformEndpoints.api[env],
|
|
554
|
+
authUrl: this.authUrl
|
|
555
|
+
},
|
|
556
|
+
edubridge: {
|
|
557
|
+
baseUrl: platformEndpoints.api[env],
|
|
558
|
+
authUrl: this.authUrl
|
|
559
|
+
},
|
|
560
|
+
caliper: {
|
|
561
|
+
baseUrl: platformEndpoints.caliper[env],
|
|
562
|
+
authUrl: this.authUrl
|
|
563
|
+
},
|
|
564
|
+
qti: {
|
|
565
|
+
baseUrl: platformEndpoints.qti[env],
|
|
566
|
+
authUrl: this.authUrl
|
|
567
|
+
}
|
|
568
|
+
};
|
|
569
|
+
} else if (isExplicitConfig(config)) {
|
|
570
|
+
this.auth = config.auth;
|
|
571
|
+
this.authUrl = config.authUrl;
|
|
572
|
+
this.pathProfiles = resolvePathProfiles(config.pathProfile, config.paths);
|
|
573
|
+
this.endpoints = {
|
|
574
|
+
oneroster: { baseUrl: config.baseUrl, authUrl: this.authUrl },
|
|
575
|
+
edubridge: { baseUrl: config.baseUrl, authUrl: this.authUrl },
|
|
576
|
+
caliper: { baseUrl: config.baseUrl, authUrl: this.authUrl },
|
|
577
|
+
qti: { baseUrl: config.baseUrl, authUrl: this.authUrl }
|
|
578
|
+
};
|
|
579
|
+
} else if (isServicesConfig(config)) {
|
|
580
|
+
this.auth = config.auth;
|
|
581
|
+
this.authUrl = config.authUrl;
|
|
582
|
+
this.pathProfiles = resolvePathProfiles(config.pathProfile, config.paths);
|
|
583
|
+
this.endpoints = {};
|
|
584
|
+
for (const [service, baseUrl] of Object.entries(config.services)) {
|
|
585
|
+
if (baseUrl) {
|
|
586
|
+
this.endpoints[service] = {
|
|
587
|
+
baseUrl,
|
|
588
|
+
authUrl: this.authUrl
|
|
589
|
+
};
|
|
590
|
+
}
|
|
591
|
+
}
|
|
592
|
+
} else {
|
|
593
|
+
throw new Error("Invalid provider configuration");
|
|
594
|
+
}
|
|
595
|
+
}
|
|
596
|
+
getEndpoint(service) {
|
|
597
|
+
const endpoint = this.endpoints[service];
|
|
598
|
+
if (!endpoint) {
|
|
599
|
+
throw new Error(`Service "${service}" is not configured in this provider`);
|
|
600
|
+
}
|
|
601
|
+
return endpoint;
|
|
602
|
+
}
|
|
603
|
+
hasService(service) {
|
|
604
|
+
return service in this.endpoints;
|
|
605
|
+
}
|
|
606
|
+
getAvailableServices() {
|
|
607
|
+
return Object.keys(this.endpoints);
|
|
608
|
+
}
|
|
609
|
+
getTokenUrl() {
|
|
610
|
+
return this.authUrl;
|
|
611
|
+
}
|
|
612
|
+
getEndpointWithPaths(service) {
|
|
613
|
+
const endpoint = this.getEndpoint(service);
|
|
614
|
+
const paths = this.getServicePaths(service);
|
|
615
|
+
return { ...endpoint, paths };
|
|
616
|
+
}
|
|
617
|
+
getPaths() {
|
|
618
|
+
return this.pathProfiles;
|
|
619
|
+
}
|
|
620
|
+
getServicePaths(service) {
|
|
621
|
+
const paths = this.pathProfiles[service];
|
|
622
|
+
if (!paths) {
|
|
623
|
+
throw new Error(`Service "${service}" is not supported on ${this.platform ?? "this"} platform.`);
|
|
624
|
+
}
|
|
625
|
+
return paths;
|
|
626
|
+
}
|
|
627
|
+
hasServiceSupport(service) {
|
|
628
|
+
return this.pathProfiles[service] !== null;
|
|
629
|
+
}
|
|
630
|
+
getTokenProvider(service) {
|
|
631
|
+
const endpoint = this.getEndpoint(service);
|
|
632
|
+
const { authUrl } = endpoint;
|
|
633
|
+
if (!authUrl) {
|
|
634
|
+
return;
|
|
635
|
+
}
|
|
636
|
+
if (!this.auth) {
|
|
637
|
+
throw new Error(`Service "${service}" requires authentication but no credentials were provided`);
|
|
638
|
+
}
|
|
639
|
+
let manager = this.tokenManagers.get(authUrl);
|
|
640
|
+
if (!manager) {
|
|
641
|
+
manager = new TokenManager({
|
|
642
|
+
tokenUrl: authUrl,
|
|
643
|
+
credentials: {
|
|
644
|
+
clientId: this.auth.clientId,
|
|
645
|
+
clientSecret: this.auth.clientSecret
|
|
646
|
+
}
|
|
647
|
+
});
|
|
648
|
+
this.tokenManagers.set(authUrl, manager);
|
|
649
|
+
}
|
|
650
|
+
return manager;
|
|
651
|
+
}
|
|
652
|
+
async checkAuth() {
|
|
653
|
+
if (!this.authUrl || !this.auth) {
|
|
654
|
+
throw new Error("No auth configured on this provider");
|
|
655
|
+
}
|
|
656
|
+
const startTime = Date.now();
|
|
657
|
+
let manager = this.tokenManagers.get(this.authUrl);
|
|
658
|
+
if (!manager) {
|
|
659
|
+
manager = new TokenManager({
|
|
660
|
+
tokenUrl: this.authUrl,
|
|
661
|
+
credentials: {
|
|
662
|
+
clientId: this.auth.clientId,
|
|
663
|
+
clientSecret: this.auth.clientSecret
|
|
664
|
+
}
|
|
665
|
+
});
|
|
666
|
+
this.tokenManagers.set(this.authUrl, manager);
|
|
667
|
+
}
|
|
668
|
+
try {
|
|
669
|
+
await manager.getToken();
|
|
670
|
+
return {
|
|
671
|
+
ok: true,
|
|
672
|
+
latencyMs: Date.now() - startTime,
|
|
673
|
+
checks: { tokenAcquisition: true }
|
|
674
|
+
};
|
|
675
|
+
} catch (error) {
|
|
676
|
+
return {
|
|
677
|
+
ok: false,
|
|
678
|
+
latencyMs: Date.now() - startTime,
|
|
679
|
+
error: error instanceof Error ? error.message : String(error),
|
|
680
|
+
checks: { tokenAcquisition: false }
|
|
681
|
+
};
|
|
682
|
+
}
|
|
683
|
+
}
|
|
684
|
+
invalidateTokens() {
|
|
685
|
+
for (const manager of this.tokenManagers.values()) {
|
|
686
|
+
manager.invalidate?.();
|
|
687
|
+
}
|
|
688
|
+
this.tokenManagers.clear();
|
|
689
|
+
}
|
|
690
|
+
}
|
|
691
|
+
// ../../internal/client-infra/src/utils/utils.ts
|
|
692
|
+
function getEnv(key) {
|
|
693
|
+
try {
|
|
694
|
+
return typeof process === "undefined" ? undefined : process.env[key];
|
|
695
|
+
} catch {
|
|
696
|
+
return;
|
|
697
|
+
}
|
|
698
|
+
}
|
|
699
|
+
// ../../internal/client-infra/src/config/registry.ts
|
|
700
|
+
var DEFAULT_PROVIDER_REGISTRY = {
|
|
701
|
+
defaultPlatform: DEFAULT_PLATFORM,
|
|
702
|
+
templates: {
|
|
703
|
+
BEYOND_AI: {
|
|
704
|
+
staging: { platform: "BEYOND_AI", env: "staging" },
|
|
705
|
+
production: { platform: "BEYOND_AI", env: "production" }
|
|
706
|
+
},
|
|
707
|
+
LEARNWITH_AI: {
|
|
708
|
+
staging: { platform: "LEARNWITH_AI", env: "staging" },
|
|
709
|
+
production: { platform: "LEARNWITH_AI", env: "production" }
|
|
710
|
+
}
|
|
711
|
+
}
|
|
712
|
+
};
|
|
713
|
+
|
|
714
|
+
// ../../internal/client-infra/src/config/resolve.ts
|
|
715
|
+
function validateEnv(env) {
|
|
716
|
+
if (env !== "staging" && env !== "production") {
|
|
717
|
+
throw new Error(`Invalid env "${env}": must be "staging" or "production"`);
|
|
718
|
+
}
|
|
719
|
+
return env;
|
|
720
|
+
}
|
|
721
|
+
function validateAuth(auth, envVars) {
|
|
722
|
+
const clientId = auth?.clientId ?? getEnv(envVars.clientId);
|
|
723
|
+
const clientSecret = auth?.clientSecret ?? getEnv(envVars.clientSecret);
|
|
724
|
+
if (!clientId) {
|
|
725
|
+
throw new Error(`Missing clientId: provide in config or set ${envVars.clientId}`);
|
|
726
|
+
}
|
|
727
|
+
if (!clientSecret) {
|
|
728
|
+
throw new Error(`Missing clientSecret: provide in config or set ${envVars.clientSecret}`);
|
|
729
|
+
}
|
|
730
|
+
return { clientId, clientSecret };
|
|
731
|
+
}
|
|
732
|
+
function validateAuthOptional(auth, envVars, requireAuth) {
|
|
733
|
+
if (!requireAuth) {
|
|
734
|
+
return;
|
|
735
|
+
}
|
|
736
|
+
return validateAuth(auth, envVars);
|
|
737
|
+
}
|
|
738
|
+
function buildMissingEnvError(envVars) {
|
|
739
|
+
const baseUrl = getEnv(envVars.baseUrl);
|
|
740
|
+
const authUrl = getEnv(envVars.authUrl);
|
|
741
|
+
const clientId = getEnv(envVars.clientId);
|
|
742
|
+
const clientSecret = getEnv(envVars.clientSecret);
|
|
743
|
+
if (baseUrl === undefined && clientId === undefined) {
|
|
744
|
+
const hint = envVars.env ?? envVars.baseUrl;
|
|
745
|
+
return `Missing env: provide in config or set ${hint}`;
|
|
746
|
+
}
|
|
747
|
+
const missing = [];
|
|
748
|
+
if (baseUrl === undefined) {
|
|
749
|
+
missing.push(envVars.env ?? envVars.baseUrl);
|
|
750
|
+
}
|
|
751
|
+
if (baseUrl !== undefined && authUrl === undefined) {
|
|
752
|
+
missing.push(envVars.authUrl);
|
|
753
|
+
}
|
|
754
|
+
if (clientId === undefined) {
|
|
755
|
+
missing.push(envVars.clientId);
|
|
756
|
+
}
|
|
757
|
+
if (clientSecret === undefined) {
|
|
758
|
+
missing.push(envVars.clientSecret);
|
|
759
|
+
}
|
|
760
|
+
return `Missing environment variables: ${missing.join(", ")}`;
|
|
761
|
+
}
|
|
762
|
+
var MODE_TRANSPORT = {
|
|
763
|
+
name: "transport",
|
|
764
|
+
matches(config) {
|
|
765
|
+
return "transport" in config && !!config.transport;
|
|
766
|
+
},
|
|
767
|
+
resolve(config) {
|
|
768
|
+
const c = config;
|
|
769
|
+
return { mode: "transport", transport: c.transport };
|
|
770
|
+
}
|
|
771
|
+
};
|
|
772
|
+
var MODE_PROVIDER = {
|
|
773
|
+
name: "provider",
|
|
774
|
+
matches(config) {
|
|
775
|
+
return "provider" in config && !!config.provider;
|
|
776
|
+
},
|
|
777
|
+
resolve(config) {
|
|
778
|
+
const c = config;
|
|
779
|
+
return { mode: "provider", provider: c.provider };
|
|
780
|
+
}
|
|
781
|
+
};
|
|
782
|
+
var MODE_ENV_CONFIG = {
|
|
783
|
+
name: "env-config",
|
|
784
|
+
matches(config) {
|
|
785
|
+
return "env" in config;
|
|
786
|
+
},
|
|
787
|
+
resolve(config, envVars, registry) {
|
|
788
|
+
const c = config;
|
|
789
|
+
const env = validateEnv(c.env);
|
|
790
|
+
const auth = validateAuth(c.auth, envVars);
|
|
791
|
+
const platform = c.platform ?? registry.defaultPlatform;
|
|
792
|
+
const platformTemplates = registry.templates[platform];
|
|
793
|
+
if (!platformTemplates) {
|
|
794
|
+
const available = Object.keys(registry.templates).join(", ");
|
|
795
|
+
throw new Error(`Unknown platform "${platform}": available platforms are ${available}`);
|
|
796
|
+
}
|
|
797
|
+
const template = platformTemplates[env];
|
|
798
|
+
if (!template) {
|
|
799
|
+
const available = Object.keys(platformTemplates).join(", ");
|
|
800
|
+
throw new Error(`Unknown env "${env}" for platform "${platform}": available environments are ${available}`);
|
|
801
|
+
}
|
|
802
|
+
return {
|
|
803
|
+
mode: "provider",
|
|
804
|
+
provider: new TimebackProvider({
|
|
805
|
+
platform: template.platform,
|
|
806
|
+
env: template.env,
|
|
807
|
+
auth,
|
|
808
|
+
timeout: c.timeout
|
|
809
|
+
})
|
|
810
|
+
};
|
|
811
|
+
}
|
|
812
|
+
};
|
|
813
|
+
var MODE_EXPLICIT_CONFIG = {
|
|
814
|
+
name: "explicit-config",
|
|
815
|
+
matches(config) {
|
|
816
|
+
return "baseUrl" in config;
|
|
817
|
+
},
|
|
818
|
+
resolve(config, envVars) {
|
|
819
|
+
const c = config;
|
|
820
|
+
const authUrl = c.authUrl ?? c.auth?.authUrl;
|
|
821
|
+
const requireAuth = !!authUrl;
|
|
822
|
+
const auth = validateAuthOptional(c.auth, envVars, requireAuth);
|
|
823
|
+
return {
|
|
824
|
+
mode: "provider",
|
|
825
|
+
provider: new TimebackProvider({
|
|
826
|
+
baseUrl: c.baseUrl,
|
|
827
|
+
authUrl,
|
|
828
|
+
auth,
|
|
829
|
+
timeout: c.timeout,
|
|
830
|
+
pathProfile: c.pathProfile,
|
|
831
|
+
paths: c.paths
|
|
832
|
+
})
|
|
833
|
+
};
|
|
834
|
+
}
|
|
835
|
+
};
|
|
836
|
+
var MODE_ENV_FALLBACK_PLATFORM = {
|
|
837
|
+
name: "env-fallback-platform",
|
|
838
|
+
matches(config, envVars) {
|
|
839
|
+
if (Object.keys(config).length > 0)
|
|
840
|
+
return false;
|
|
841
|
+
const env = envVars.env ? getEnv(envVars.env) : undefined;
|
|
842
|
+
const clientId = getEnv(envVars.clientId);
|
|
843
|
+
const clientSecret = getEnv(envVars.clientSecret);
|
|
844
|
+
return env !== undefined && clientId !== undefined && clientSecret !== undefined;
|
|
845
|
+
},
|
|
846
|
+
resolve(_config, envVars, registry) {
|
|
847
|
+
const env = validateEnv(getEnv(envVars.env));
|
|
848
|
+
const clientId = getEnv(envVars.clientId);
|
|
849
|
+
const clientSecret = getEnv(envVars.clientSecret);
|
|
850
|
+
const platform = registry.defaultPlatform;
|
|
851
|
+
const template = registry.templates[platform]?.[env];
|
|
852
|
+
if (!template) {
|
|
853
|
+
throw new Error(`Unknown env "${env}" for platform "${platform}"`);
|
|
854
|
+
}
|
|
855
|
+
return {
|
|
856
|
+
mode: "provider",
|
|
857
|
+
provider: new TimebackProvider({
|
|
858
|
+
platform: template.platform,
|
|
859
|
+
env: template.env,
|
|
860
|
+
auth: { clientId, clientSecret }
|
|
861
|
+
})
|
|
862
|
+
};
|
|
863
|
+
}
|
|
864
|
+
};
|
|
865
|
+
var MODE_ENV_FALLBACK_EXPLICIT = {
|
|
866
|
+
name: "env-fallback-explicit",
|
|
867
|
+
matches(config, envVars) {
|
|
868
|
+
if (Object.keys(config).length > 0)
|
|
869
|
+
return false;
|
|
870
|
+
const baseUrl = getEnv(envVars.baseUrl);
|
|
871
|
+
const authUrl = getEnv(envVars.authUrl);
|
|
872
|
+
const clientId = getEnv(envVars.clientId);
|
|
873
|
+
const clientSecret = getEnv(envVars.clientSecret);
|
|
874
|
+
return baseUrl !== undefined && authUrl !== undefined && clientId !== undefined && clientSecret !== undefined;
|
|
875
|
+
},
|
|
876
|
+
resolve(_config, envVars) {
|
|
877
|
+
const baseUrl = getEnv(envVars.baseUrl);
|
|
878
|
+
const authUrl = getEnv(envVars.authUrl);
|
|
879
|
+
const clientId = getEnv(envVars.clientId);
|
|
880
|
+
const clientSecret = getEnv(envVars.clientSecret);
|
|
881
|
+
return {
|
|
882
|
+
mode: "provider",
|
|
883
|
+
provider: new TimebackProvider({
|
|
884
|
+
baseUrl,
|
|
885
|
+
authUrl,
|
|
886
|
+
auth: { clientId, clientSecret }
|
|
887
|
+
})
|
|
888
|
+
};
|
|
889
|
+
}
|
|
890
|
+
};
|
|
891
|
+
var MODE_TOKEN_PROVIDER = {
|
|
892
|
+
name: "token-provider",
|
|
893
|
+
matches(config) {
|
|
894
|
+
return "tokenProvider" in config;
|
|
895
|
+
},
|
|
896
|
+
resolve() {
|
|
897
|
+
throw new Error("TokenProvider mode is not supported with provider pattern. " + "Use { provider: TimebackProvider } or { env, auth } instead.");
|
|
898
|
+
}
|
|
899
|
+
};
|
|
900
|
+
var MODES = [
|
|
901
|
+
MODE_TRANSPORT,
|
|
902
|
+
MODE_PROVIDER,
|
|
903
|
+
MODE_ENV_CONFIG,
|
|
904
|
+
MODE_TOKEN_PROVIDER,
|
|
905
|
+
MODE_EXPLICIT_CONFIG,
|
|
906
|
+
MODE_ENV_FALLBACK_PLATFORM,
|
|
907
|
+
MODE_ENV_FALLBACK_EXPLICIT
|
|
908
|
+
];
|
|
909
|
+
function resolveToProvider(config, envVars, registry = DEFAULT_PROVIDER_REGISTRY) {
|
|
910
|
+
for (const mode of MODES) {
|
|
911
|
+
if (mode.matches(config, envVars)) {
|
|
912
|
+
return mode.resolve(config, envVars, registry);
|
|
913
|
+
}
|
|
914
|
+
}
|
|
915
|
+
throw new Error(buildMissingEnvError(envVars));
|
|
916
|
+
}
|
|
917
|
+
// ../../internal/client-infra/src/errors/errors.ts
|
|
918
|
+
class ApiError extends Error {
|
|
919
|
+
statusCode;
|
|
920
|
+
response;
|
|
921
|
+
name = "ApiError";
|
|
922
|
+
constructor(message, statusCode, response) {
|
|
923
|
+
super(message);
|
|
924
|
+
this.statusCode = statusCode;
|
|
925
|
+
this.response = response;
|
|
926
|
+
}
|
|
927
|
+
get minorCodes() {
|
|
928
|
+
const ims = this.response;
|
|
929
|
+
if (!ims?.imsx_CodeMinor?.imsx_codeMinorField) {
|
|
930
|
+
return [];
|
|
931
|
+
}
|
|
932
|
+
return ims.imsx_CodeMinor.imsx_codeMinorField.map((field) => ({
|
|
933
|
+
field: field.imsx_codeMinorFieldName,
|
|
934
|
+
value: field.imsx_codeMinorFieldValue
|
|
935
|
+
}));
|
|
936
|
+
}
|
|
937
|
+
get details() {
|
|
938
|
+
const ims = this.response;
|
|
939
|
+
return ims?.imsx_error_details ?? [];
|
|
940
|
+
}
|
|
941
|
+
}
|
|
942
|
+
|
|
943
|
+
class UnauthorizedError extends ApiError {
|
|
944
|
+
name = "UnauthorizedError";
|
|
945
|
+
constructor(message = "Unauthorized", response) {
|
|
946
|
+
super(message, 401, response);
|
|
947
|
+
}
|
|
948
|
+
}
|
|
949
|
+
|
|
950
|
+
class ForbiddenError extends ApiError {
|
|
951
|
+
name = "ForbiddenError";
|
|
952
|
+
constructor(message = "Forbidden", response) {
|
|
953
|
+
super(message, 403, response);
|
|
954
|
+
}
|
|
955
|
+
}
|
|
956
|
+
|
|
957
|
+
class NotFoundError extends ApiError {
|
|
958
|
+
name = "NotFoundError";
|
|
959
|
+
constructor(message = "Not Found", response) {
|
|
960
|
+
super(message, 404, response);
|
|
961
|
+
}
|
|
962
|
+
}
|
|
963
|
+
|
|
964
|
+
class ValidationError extends ApiError {
|
|
965
|
+
name = "ValidationError";
|
|
966
|
+
constructor(message = "Validation Error", response) {
|
|
967
|
+
super(message, 422, response);
|
|
968
|
+
}
|
|
969
|
+
}
|
|
970
|
+
function isApiError(error) {
|
|
971
|
+
if (!(error instanceof Error))
|
|
972
|
+
return false;
|
|
973
|
+
return "statusCode" in error && "response" in error;
|
|
974
|
+
}
|
|
975
|
+
// ../../internal/client-infra/src/filter/where/where.ts
|
|
976
|
+
function escapeValue(value) {
|
|
977
|
+
if (value instanceof Date) {
|
|
978
|
+
return value.toISOString();
|
|
979
|
+
}
|
|
980
|
+
if (typeof value === "boolean") {
|
|
981
|
+
return value ? "true" : "false";
|
|
982
|
+
}
|
|
983
|
+
if (typeof value === "number") {
|
|
984
|
+
return String(value);
|
|
985
|
+
}
|
|
986
|
+
return value.replaceAll("'", "''");
|
|
987
|
+
}
|
|
988
|
+
function formatValue2(value) {
|
|
989
|
+
const escaped = escapeValue(value);
|
|
990
|
+
const needsQuotes = typeof value === "string" || value instanceof Date;
|
|
991
|
+
return needsQuotes ? `'${escaped}'` : escaped;
|
|
992
|
+
}
|
|
993
|
+
function fieldToConditions(field, condition) {
|
|
994
|
+
if (typeof condition === "string" || typeof condition === "number" || typeof condition === "boolean" || condition instanceof Date) {
|
|
995
|
+
return [`${field}=${formatValue2(condition)}`];
|
|
996
|
+
}
|
|
997
|
+
if (typeof condition === "object" && condition !== null) {
|
|
998
|
+
const ops = condition;
|
|
999
|
+
const conditions = [];
|
|
1000
|
+
if (ops.ne !== undefined) {
|
|
1001
|
+
conditions.push(`${field}!=${formatValue2(ops.ne)}`);
|
|
1002
|
+
}
|
|
1003
|
+
if (ops.gt !== undefined) {
|
|
1004
|
+
conditions.push(`${field}>${formatValue2(ops.gt)}`);
|
|
1005
|
+
}
|
|
1006
|
+
if (ops.gte !== undefined) {
|
|
1007
|
+
conditions.push(`${field}>=${formatValue2(ops.gte)}`);
|
|
1008
|
+
}
|
|
1009
|
+
if (ops.lt !== undefined) {
|
|
1010
|
+
conditions.push(`${field}<${formatValue2(ops.lt)}`);
|
|
1011
|
+
}
|
|
1012
|
+
if (ops.lte !== undefined) {
|
|
1013
|
+
conditions.push(`${field}<=${formatValue2(ops.lte)}`);
|
|
1014
|
+
}
|
|
1015
|
+
if (ops.contains !== undefined) {
|
|
1016
|
+
conditions.push(`${field}~${formatValue2(ops.contains)}`);
|
|
1017
|
+
}
|
|
1018
|
+
if (ops.in !== undefined && ops.in.length > 0) {
|
|
1019
|
+
const inConditions = ops.in.map((v) => `${field}=${formatValue2(v)}`);
|
|
1020
|
+
const joined = inConditions.join(" OR ");
|
|
1021
|
+
conditions.push(inConditions.length > 1 ? `(${joined})` : joined);
|
|
1022
|
+
}
|
|
1023
|
+
if (ops.notIn !== undefined && ops.notIn.length > 0) {
|
|
1024
|
+
const notInConditions = ops.notIn.map((v) => `${field}!=${formatValue2(v)}`);
|
|
1025
|
+
conditions.push(notInConditions.join(" AND "));
|
|
1026
|
+
}
|
|
1027
|
+
return conditions;
|
|
1028
|
+
}
|
|
1029
|
+
return [];
|
|
1030
|
+
}
|
|
1031
|
+
function isOrCondition(clause) {
|
|
1032
|
+
return "OR" in clause && Array.isArray(clause.OR);
|
|
1033
|
+
}
|
|
1034
|
+
function whereToFilter(where) {
|
|
1035
|
+
if (isOrCondition(where)) {
|
|
1036
|
+
const orParts = where.OR.map((clause) => whereToFilter(clause)).filter((s) => s !== undefined);
|
|
1037
|
+
return orParts.length > 0 ? orParts.join(" OR ") : undefined;
|
|
1038
|
+
}
|
|
1039
|
+
const conditions = [];
|
|
1040
|
+
for (const [field, condition] of Object.entries(where)) {
|
|
1041
|
+
if (condition !== undefined) {
|
|
1042
|
+
conditions.push(...fieldToConditions(field, condition));
|
|
1043
|
+
}
|
|
1044
|
+
}
|
|
1045
|
+
return conditions.length > 0 ? conditions.join(" AND ") : undefined;
|
|
1046
|
+
}
|
|
1047
|
+
export {
|
|
1048
|
+
ValidationError,
|
|
1049
|
+
UnauthorizedError,
|
|
1050
|
+
NotFoundError,
|
|
1051
|
+
ForbiddenError,
|
|
1052
|
+
ApiError
|
|
1053
|
+
};
|