@c-rex/core 0.1.13 → 0.1.14
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/OIDC.d.mts +14 -0
- package/dist/OIDC.d.ts +14 -0
- package/dist/{api/OIDC.js → OIDC.js} +92 -137
- package/dist/OIDC.js.map +1 -0
- package/dist/{api/OIDC.mjs → OIDC.mjs} +90 -135
- package/dist/OIDC.mjs.map +1 -0
- package/dist/logger.js +353 -7
- package/dist/logger.js.map +1 -1
- package/dist/logger.mjs +353 -7
- package/dist/logger.mjs.map +1 -1
- package/dist/{index.d.mts → requests.d.mts} +1 -15
- package/dist/{index.d.ts → requests.d.ts} +1 -15
- package/dist/requests.js +670 -0
- package/dist/requests.js.map +1 -0
- package/dist/requests.mjs +635 -0
- package/dist/requests.mjs.map +1 -0
- package/dist/sdk.js +18 -16
- package/dist/sdk.js.map +1 -1
- package/dist/sdk.mjs +18 -16
- package/dist/sdk.mjs.map +1 -1
- package/package.json +10 -22
- package/dist/api/OIDC.d.mts +0 -12
- package/dist/api/OIDC.d.ts +0 -12
- package/dist/api/OIDC.js.map +0 -1
- package/dist/api/OIDC.mjs.map +0 -1
- package/dist/api/cookies.d.mts +0 -17
- package/dist/api/cookies.d.ts +0 -17
- package/dist/api/cookies.js +0 -66
- package/dist/api/cookies.js.map +0 -1
- package/dist/api/cookies.mjs +0 -40
- package/dist/api/cookies.mjs.map +0 -1
- package/dist/api/rpc.d.mts +0 -16
- package/dist/api/rpc.d.ts +0 -16
- package/dist/api/rpc.js +0 -232
- package/dist/api/rpc.js.map +0 -1
- package/dist/api/rpc.mjs +0 -195
- package/dist/api/rpc.mjs.map +0 -1
- package/dist/index.js +0 -350
- package/dist/index.js.map +0 -1
- package/dist/index.mjs +0 -309
- package/dist/index.mjs.map +0 -1
|
@@ -0,0 +1,635 @@
|
|
|
1
|
+
// src/requests.ts
|
|
2
|
+
import axios from "axios";
|
|
3
|
+
|
|
4
|
+
// ../constants/src/index.ts
|
|
5
|
+
var ALL = "*";
|
|
6
|
+
var LOG_LEVELS = {
|
|
7
|
+
critical: 2,
|
|
8
|
+
error: 3,
|
|
9
|
+
warning: 4,
|
|
10
|
+
info: 6,
|
|
11
|
+
debug: 7
|
|
12
|
+
};
|
|
13
|
+
var SDK_CONFIG_KEY = "crex-sdk-config";
|
|
14
|
+
var DEFAULT_COOKIE_LIMIT = 30 * 24 * 60 * 60 * 1e3;
|
|
15
|
+
var CREX_TOKEN_HEADER_KEY = "crex-token";
|
|
16
|
+
|
|
17
|
+
// src/requests.ts
|
|
18
|
+
import { cookies as cookies2 } from "next/headers";
|
|
19
|
+
|
|
20
|
+
// src/logger.ts
|
|
21
|
+
import winston from "winston";
|
|
22
|
+
|
|
23
|
+
// src/transports/matomo.ts
|
|
24
|
+
import Transport from "winston-transport";
|
|
25
|
+
var MatomoTransport = class extends Transport {
|
|
26
|
+
matomoTransport;
|
|
27
|
+
configs;
|
|
28
|
+
/**
|
|
29
|
+
* Creates a new instance of MatomoTransport.
|
|
30
|
+
*
|
|
31
|
+
* @param configs - The application configuration containing logging settings
|
|
32
|
+
*/
|
|
33
|
+
constructor(configs) {
|
|
34
|
+
super({
|
|
35
|
+
level: configs.logs.matomo.minimumLevel,
|
|
36
|
+
silent: configs.logs.matomo.silent
|
|
37
|
+
});
|
|
38
|
+
this.matomoTransport = new Transport();
|
|
39
|
+
this.configs = configs;
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* Logs a message to Matomo if the message category is included in the configured categories.
|
|
43
|
+
*
|
|
44
|
+
* @param info - The log information including level, message, and category
|
|
45
|
+
* @param callback - Callback function to execute after logging
|
|
46
|
+
*/
|
|
47
|
+
log(info, callback) {
|
|
48
|
+
const matomoCategory = this.configs.logs.matomo.categoriesLevel;
|
|
49
|
+
if (matomoCategory.includes(info.category) || matomoCategory.includes(ALL)) {
|
|
50
|
+
this.matomoTransport.log(info, callback);
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
};
|
|
54
|
+
|
|
55
|
+
// src/transports/graylog.ts
|
|
56
|
+
import Transport2 from "winston-transport";
|
|
57
|
+
import Graylog2Transport from "winston-graylog2";
|
|
58
|
+
var GraylogTransport = class extends Transport2 {
|
|
59
|
+
graylogTransport;
|
|
60
|
+
configs;
|
|
61
|
+
/**
|
|
62
|
+
* Creates a new instance of GraylogTransport.
|
|
63
|
+
*
|
|
64
|
+
* @param configs - The application configuration containing logging settings
|
|
65
|
+
*/
|
|
66
|
+
constructor(configs) {
|
|
67
|
+
if (!configs.logs.graylog.hostname || configs.logs.graylog.port === void 0) {
|
|
68
|
+
throw new Error("Graylog hostname and port must be defined");
|
|
69
|
+
}
|
|
70
|
+
super({
|
|
71
|
+
level: configs.logs.graylog.minimumLevel,
|
|
72
|
+
silent: configs.logs.graylog.silent
|
|
73
|
+
});
|
|
74
|
+
this.configs = configs;
|
|
75
|
+
this.graylogTransport = new Graylog2Transport({
|
|
76
|
+
name: configs.logs.graylog.app,
|
|
77
|
+
silent: configs.logs.graylog.silent,
|
|
78
|
+
handleExceptions: false,
|
|
79
|
+
graylog: {
|
|
80
|
+
servers: [
|
|
81
|
+
{ host: configs.logs.graylog.hostname, port: configs.logs.graylog.port }
|
|
82
|
+
]
|
|
83
|
+
}
|
|
84
|
+
});
|
|
85
|
+
}
|
|
86
|
+
/**
|
|
87
|
+
* Logs a message to Graylog if the message category is included in the configured categories.
|
|
88
|
+
*
|
|
89
|
+
* @param info - The log information including level, message, and category
|
|
90
|
+
* @param callback - Callback function to execute after logging
|
|
91
|
+
*/
|
|
92
|
+
log(info, callback) {
|
|
93
|
+
const graylogCategory = this.configs.logs.graylog.categoriesLevel;
|
|
94
|
+
if (graylogCategory.includes(info.category) || graylogCategory.includes(ALL)) {
|
|
95
|
+
this.graylogTransport.log(info, callback);
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
};
|
|
99
|
+
|
|
100
|
+
// src/utils.ts
|
|
101
|
+
var formatIssuer = (issuer) => {
|
|
102
|
+
let newIssuer = issuer;
|
|
103
|
+
const lastChar = newIssuer.charAt(newIssuer.length - 1);
|
|
104
|
+
if (lastChar !== "/") {
|
|
105
|
+
newIssuer += "/";
|
|
106
|
+
}
|
|
107
|
+
newIssuer += ".well-known/openid-configuration";
|
|
108
|
+
return newIssuer;
|
|
109
|
+
};
|
|
110
|
+
var formatApiUrl = (apiUrl) => {
|
|
111
|
+
let newApiUrl = apiUrl;
|
|
112
|
+
const lastChar = newApiUrl.charAt(newApiUrl.length - 1);
|
|
113
|
+
if (lastChar !== "/") {
|
|
114
|
+
newApiUrl += "/";
|
|
115
|
+
}
|
|
116
|
+
newApiUrl += "iirds/v1/";
|
|
117
|
+
return newApiUrl;
|
|
118
|
+
};
|
|
119
|
+
var mergeConfigs = (defaultConfig, envVars) => {
|
|
120
|
+
const definedEnvVars = {};
|
|
121
|
+
for (const key in envVars) {
|
|
122
|
+
if (envVars[key] !== void 0) {
|
|
123
|
+
definedEnvVars[key] = envVars[key];
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
return {
|
|
127
|
+
...defaultConfig,
|
|
128
|
+
...definedEnvVars
|
|
129
|
+
};
|
|
130
|
+
};
|
|
131
|
+
var generateWarnings = (vars) => {
|
|
132
|
+
const warnings = [];
|
|
133
|
+
vars.forEach((variable) => {
|
|
134
|
+
const value = process.env[variable.key];
|
|
135
|
+
if (value === void 0) {
|
|
136
|
+
warnings.push(`Missing environment variable ${variable.key}, using default value: ${variable.default}`);
|
|
137
|
+
}
|
|
138
|
+
});
|
|
139
|
+
return warnings;
|
|
140
|
+
};
|
|
141
|
+
var createUserOIDCConfig = () => {
|
|
142
|
+
const warnings = [];
|
|
143
|
+
const userDefaults = {
|
|
144
|
+
secret: "dummy",
|
|
145
|
+
scope: "openid profile crex.ids.api crex.ids.api.public",
|
|
146
|
+
enabled: true
|
|
147
|
+
};
|
|
148
|
+
const userEnvs = {
|
|
149
|
+
id: process.env.CREX_IDS_ID,
|
|
150
|
+
secret: process.env.CREX_IDS_SECRET,
|
|
151
|
+
scope: process.env.CREX_IDS_USER_SCOPES,
|
|
152
|
+
issuer: process.env.CREX_IDS_ISSUER == void 0 ? void 0 : formatIssuer(process.env.CREX_IDS_ISSUER),
|
|
153
|
+
enabled: process.env.CREX_IDS_USER_LOGIN_ENABLE == void 0 ? void 0 : process.env.CREX_IDS_USER_LOGIN_ENABLE == "true"
|
|
154
|
+
};
|
|
155
|
+
const user = mergeConfigs(userDefaults, userEnvs);
|
|
156
|
+
if (user.enabled) {
|
|
157
|
+
const disableOIDCVars = [
|
|
158
|
+
{ key: "CREX_IDS_ID", value: process.env.CREX_IDS_ID },
|
|
159
|
+
{ key: "CREX_IDS_ISSUER", value: process.env.CREX_IDS_ISSUER }
|
|
160
|
+
];
|
|
161
|
+
disableOIDCVars.forEach((variable) => {
|
|
162
|
+
if (variable.value === void 0) {
|
|
163
|
+
user.enabled = false;
|
|
164
|
+
warnings.push(`Missing environment variable ${variable.key}, disabling client and login`);
|
|
165
|
+
}
|
|
166
|
+
});
|
|
167
|
+
}
|
|
168
|
+
if (user.enabled) {
|
|
169
|
+
const userWarningVars = [
|
|
170
|
+
{ key: "CREX_IDS_USER_SCOPES", default: user.scope },
|
|
171
|
+
{ key: "CREX_IDS_SECRET", default: user.secret },
|
|
172
|
+
{ key: "CREX_IDS_USER_LOGIN_ENABLE", default: user.enabled }
|
|
173
|
+
];
|
|
174
|
+
const aux = generateWarnings(userWarningVars);
|
|
175
|
+
warnings.push(...aux);
|
|
176
|
+
}
|
|
177
|
+
return { user, warnings };
|
|
178
|
+
};
|
|
179
|
+
var createClientOIDCConfig = () => {
|
|
180
|
+
const warnings = [];
|
|
181
|
+
const clientDefault = {
|
|
182
|
+
secret: "dummy",
|
|
183
|
+
enabled: true
|
|
184
|
+
};
|
|
185
|
+
const clientEnvs = {
|
|
186
|
+
id: process.env.CREX_IDS_ID,
|
|
187
|
+
issuer: process.env.CREX_IDS_ISSUER == void 0 ? void 0 : formatIssuer(process.env.CREX_IDS_ISSUER),
|
|
188
|
+
secret: process.env.CREX_IDS_SECRET,
|
|
189
|
+
enabled: process.env.CREX_IDS_CLIENT_LOGIN_ENABLE == void 0 ? void 0 : process.env.CREX_IDS_CLIENT_LOGIN_ENABLE == "true"
|
|
190
|
+
};
|
|
191
|
+
const client = mergeConfigs(clientDefault, clientEnvs);
|
|
192
|
+
if (client.enabled) {
|
|
193
|
+
const disableOIDCVars = [
|
|
194
|
+
{ key: "CREX_IDS_ID", value: process.env.CREX_IDS_ID },
|
|
195
|
+
{ key: "CREX_IDS_ISSUER", value: process.env.CREX_IDS_ISSUER }
|
|
196
|
+
];
|
|
197
|
+
disableOIDCVars.forEach((variable) => {
|
|
198
|
+
if (variable.value === void 0) {
|
|
199
|
+
client.enabled = false;
|
|
200
|
+
warnings.push(`Missing environment variable ${variable.key}, disabling client and login`);
|
|
201
|
+
}
|
|
202
|
+
});
|
|
203
|
+
}
|
|
204
|
+
if (client.enabled) {
|
|
205
|
+
const clientWarningVars = [
|
|
206
|
+
{ key: "CREX_IDS_SECRET", default: client.secret },
|
|
207
|
+
{ key: "CREX_IDS_CLIENT_LOGIN_ENABLE", default: client.enabled }
|
|
208
|
+
];
|
|
209
|
+
const aux = generateWarnings(clientWarningVars);
|
|
210
|
+
warnings.push(...aux);
|
|
211
|
+
}
|
|
212
|
+
return { client, warnings };
|
|
213
|
+
};
|
|
214
|
+
var createConsoleLoggerConfig = () => {
|
|
215
|
+
const warnings = [];
|
|
216
|
+
const consoleLoggerDefaults = {
|
|
217
|
+
minimumLevel: "info",
|
|
218
|
+
silent: false
|
|
219
|
+
};
|
|
220
|
+
const consoleLoggerEnvs = {
|
|
221
|
+
minimumLevel: process.env.LOG_CONSOLE_LEVEL,
|
|
222
|
+
silent: process.env.LOG_CONSOLE_SILENT == void 0 ? void 0 : process.env.LOG_CONSOLE_SILENT == "true"
|
|
223
|
+
};
|
|
224
|
+
const consoleLogger = mergeConfigs(consoleLoggerDefaults, consoleLoggerEnvs);
|
|
225
|
+
if (consoleLogger.silent == false) {
|
|
226
|
+
const consoleWarningsVars = [
|
|
227
|
+
{ key: "LOG_CONSOLE_SILENT", default: consoleLogger.silent },
|
|
228
|
+
{ key: "LOG_CONSOLE_LEVEL", default: consoleLogger.minimumLevel }
|
|
229
|
+
];
|
|
230
|
+
const aux = generateWarnings(consoleWarningsVars);
|
|
231
|
+
warnings.push(...aux);
|
|
232
|
+
}
|
|
233
|
+
return {
|
|
234
|
+
consoleLogger,
|
|
235
|
+
warnings
|
|
236
|
+
};
|
|
237
|
+
};
|
|
238
|
+
var createGraylogLoggerConfig = () => {
|
|
239
|
+
const warnings = [];
|
|
240
|
+
const graylogDefaults = {
|
|
241
|
+
app: "app name not set",
|
|
242
|
+
minimumLevel: "info",
|
|
243
|
+
silent: false,
|
|
244
|
+
hostname: "https://log.c-rex.net",
|
|
245
|
+
port: 12202,
|
|
246
|
+
categoriesLevel: ["NoLicense", "Scenario", "Document", "Search", "Notification", "History", "UserProfile"]
|
|
247
|
+
};
|
|
248
|
+
const graylogEnvs = {
|
|
249
|
+
app: process.env.LOG_GRAYLOG_APP_NAME,
|
|
250
|
+
silent: process.env.LOG_GRAYLOG_SILENT == void 0 ? void 0 : process.env.LOG_GRAYLOG_SILENT == "true",
|
|
251
|
+
hostname: process.env.LOG_GRAYLOG_HOSTNAME,
|
|
252
|
+
port: process.env.LOG_GRAYLOG_PORT == void 0 ? void 0 : Number(process.env.LOG_GRAYLOG_PORT),
|
|
253
|
+
minimumLevel: process.env.LOG_GRAYLOG_LEVEL,
|
|
254
|
+
categoriesLevel: process.env.LOG_GRAYLOG_CATEGORIES == void 0 ? void 0 : process.env.LOG_GRAYLOG_CATEGORIES.split(",")
|
|
255
|
+
};
|
|
256
|
+
const graylog = mergeConfigs(graylogDefaults, graylogEnvs);
|
|
257
|
+
if (graylog.silent == false) {
|
|
258
|
+
const graylogWarningVars = [
|
|
259
|
+
{ key: "LOG_GRAYLOG_APP_NAME", default: graylog.app },
|
|
260
|
+
{ key: "LOG_GRAYLOG_HOSTNAME", default: graylog.hostname },
|
|
261
|
+
{ key: "LOG_GRAYLOG_LEVEL", default: graylog.minimumLevel },
|
|
262
|
+
{ key: "LOG_GRAYLOG_SILENT", default: graylog.silent },
|
|
263
|
+
{ key: "LOG_GRAYLOG_CATEGORIES", default: graylog.categoriesLevel },
|
|
264
|
+
{ key: "LOG_GRAYLOG_PORT", default: graylog.port }
|
|
265
|
+
];
|
|
266
|
+
const aux = generateWarnings(graylogWarningVars);
|
|
267
|
+
warnings.push(...aux);
|
|
268
|
+
}
|
|
269
|
+
return {
|
|
270
|
+
graylog,
|
|
271
|
+
warnings
|
|
272
|
+
};
|
|
273
|
+
};
|
|
274
|
+
var createMatomoLoggerConfig = () => {
|
|
275
|
+
const warnings = [];
|
|
276
|
+
const matomoDefaults = {
|
|
277
|
+
app: "NextJsProjectName",
|
|
278
|
+
minimumLevel: "info",
|
|
279
|
+
silent: true,
|
|
280
|
+
hostname: "",
|
|
281
|
+
port: 0,
|
|
282
|
+
categoriesLevel: ["NoLicense", "Scenario", "Document", "Search", "Notification", "History", "UserProfile"]
|
|
283
|
+
};
|
|
284
|
+
const matomoEnvs = {
|
|
285
|
+
app: process.env.LOG_MATOMO_APP_NAME,
|
|
286
|
+
silent: process.env.LOG_MATOMO_SILENT == void 0 ? void 0 : process.env.LOG_MATOMO_SILENT == "true",
|
|
287
|
+
hostname: process.env.LOG_MATOMO_HOSTNAME,
|
|
288
|
+
port: process.env.LOG_MATOMO_PORT == void 0 ? void 0 : Number(process.env.LOG_MATOMO_PORT),
|
|
289
|
+
minimumLevel: process.env.LOG_MATOMO_LEVEL,
|
|
290
|
+
categoriesLevel: process.env.LOG_MATOMO_CATEGORIES == void 0 ? void 0 : process.env.LOG_MATOMO_CATEGORIES.split(",")
|
|
291
|
+
};
|
|
292
|
+
const matomo = mergeConfigs(matomoDefaults, matomoEnvs);
|
|
293
|
+
const matomoSilentVars = [
|
|
294
|
+
{ key: "LOG_MATOMO_SILENT", value: process.env.LOG_MATOMO_SILENT },
|
|
295
|
+
{ key: "LOG_MATOMO_HOSTNAME", value: process.env.LOG_MATOMO_HOSTNAME },
|
|
296
|
+
{ key: "LOG_MATOMO_PORT", value: process.env.LOG_MATOMO_PORT }
|
|
297
|
+
];
|
|
298
|
+
matomoSilentVars.forEach((variable) => {
|
|
299
|
+
if (variable.value === void 0) {
|
|
300
|
+
matomo.silent = true;
|
|
301
|
+
warnings.push(`Missing environment variable ${variable.key}, setting matomo logger to silent`);
|
|
302
|
+
}
|
|
303
|
+
});
|
|
304
|
+
if (matomo.silent == false) {
|
|
305
|
+
const matomoWarningVars = [
|
|
306
|
+
{ key: "LOG_MATOMO_APP_NAME", default: matomo.app },
|
|
307
|
+
{ key: "LOG_MATOMO_LEVEL", default: matomo.minimumLevel },
|
|
308
|
+
{ key: "LOG_MATOMO_CATEGORIES", default: matomo.categoriesLevel }
|
|
309
|
+
];
|
|
310
|
+
const aux = generateWarnings(matomoWarningVars);
|
|
311
|
+
warnings.push(...aux);
|
|
312
|
+
}
|
|
313
|
+
return {
|
|
314
|
+
matomo,
|
|
315
|
+
warnings
|
|
316
|
+
};
|
|
317
|
+
};
|
|
318
|
+
|
|
319
|
+
// src/sdk.ts
|
|
320
|
+
import { cookies } from "next/headers";
|
|
321
|
+
var CrexSDK = class {
|
|
322
|
+
userAuthConfig;
|
|
323
|
+
customerConfig;
|
|
324
|
+
cookiesConfig;
|
|
325
|
+
async getUserAuthConfig() {
|
|
326
|
+
if (this.userAuthConfig) {
|
|
327
|
+
return this.userAuthConfig;
|
|
328
|
+
}
|
|
329
|
+
if (!this.customerConfig) {
|
|
330
|
+
this.customerConfig = this.getServerConfig();
|
|
331
|
+
}
|
|
332
|
+
const user = this.customerConfig.OIDC.user;
|
|
333
|
+
const userInfoEndPoint = this.customerConfig.OIDC.issuerMetadata?.userinfo_endpoint;
|
|
334
|
+
if (user.enabled) {
|
|
335
|
+
this.userAuthConfig = {
|
|
336
|
+
providers: [
|
|
337
|
+
{
|
|
338
|
+
id: "crex",
|
|
339
|
+
name: "CREX",
|
|
340
|
+
type: "oauth",
|
|
341
|
+
version: "2.0",
|
|
342
|
+
clientId: user.id,
|
|
343
|
+
wellKnown: user.issuer,
|
|
344
|
+
clientSecret: user.secret,
|
|
345
|
+
authorization: {
|
|
346
|
+
params: {
|
|
347
|
+
scope: user.scope,
|
|
348
|
+
prompt: "login"
|
|
349
|
+
}
|
|
350
|
+
},
|
|
351
|
+
idToken: true,
|
|
352
|
+
checks: ["pkce", "state"],
|
|
353
|
+
async profile(_, tokens) {
|
|
354
|
+
const res = await fetch(userInfoEndPoint, {
|
|
355
|
+
headers: {
|
|
356
|
+
Authorization: `Bearer ${tokens.access_token}`
|
|
357
|
+
}
|
|
358
|
+
});
|
|
359
|
+
const userinfo = await res.json();
|
|
360
|
+
return {
|
|
361
|
+
id: userinfo.sub,
|
|
362
|
+
name: userinfo.name,
|
|
363
|
+
email: userinfo.email
|
|
364
|
+
};
|
|
365
|
+
},
|
|
366
|
+
callbacks: {
|
|
367
|
+
async jwt({ token, account }) {
|
|
368
|
+
if (account) {
|
|
369
|
+
token.id_token = account.id_token;
|
|
370
|
+
}
|
|
371
|
+
return token;
|
|
372
|
+
},
|
|
373
|
+
async session({ session, token }) {
|
|
374
|
+
session.id_token = token.id_token;
|
|
375
|
+
return session;
|
|
376
|
+
}
|
|
377
|
+
}
|
|
378
|
+
}
|
|
379
|
+
]
|
|
380
|
+
};
|
|
381
|
+
}
|
|
382
|
+
;
|
|
383
|
+
return this.userAuthConfig;
|
|
384
|
+
}
|
|
385
|
+
createCustomerConfig(CUSTOMER_CONFIG, shouldLog) {
|
|
386
|
+
const requiredEnvVars = ["CREX_API_URL", "NEXT_PUBLIC_API_URL"];
|
|
387
|
+
const required = requiredEnvVars.map((key) => {
|
|
388
|
+
const value = process.env[key];
|
|
389
|
+
if (value === void 0) {
|
|
390
|
+
return `Missing required environment variable: ${key}`;
|
|
391
|
+
}
|
|
392
|
+
return "";
|
|
393
|
+
}).filter((item) => item.length > 0);
|
|
394
|
+
const { user, warnings: userWarnings } = createUserOIDCConfig();
|
|
395
|
+
const { client, warnings: clientWarnings } = createClientOIDCConfig();
|
|
396
|
+
const { matomo, warnings: matomoWarnings } = createMatomoLoggerConfig();
|
|
397
|
+
const { graylog, warnings: graylogWarnings } = createGraylogLoggerConfig();
|
|
398
|
+
const { consoleLogger, warnings: consoleWarnings } = createConsoleLoggerConfig();
|
|
399
|
+
const warnings = required.concat(
|
|
400
|
+
consoleWarnings,
|
|
401
|
+
clientWarnings,
|
|
402
|
+
userWarnings,
|
|
403
|
+
graylogWarnings,
|
|
404
|
+
matomoWarnings
|
|
405
|
+
);
|
|
406
|
+
if (shouldLog) {
|
|
407
|
+
if (warnings.length > 0) console.warn(warnings.join("\n"));
|
|
408
|
+
}
|
|
409
|
+
const cookiesConfig = {
|
|
410
|
+
publicNextApiUrl: process.env.NEXT_PUBLIC_API_URL,
|
|
411
|
+
...CUSTOMER_CONFIG,
|
|
412
|
+
OIDC: {
|
|
413
|
+
clientEnabled: client.enabled,
|
|
414
|
+
userEnabled: user.enabled
|
|
415
|
+
}
|
|
416
|
+
};
|
|
417
|
+
const config = {
|
|
418
|
+
baseUrl: process.env.CREX_API_URL == void 0 ? "" : formatApiUrl(process.env.CREX_API_URL),
|
|
419
|
+
OIDC: { client, user },
|
|
420
|
+
logs: { console: consoleLogger, graylog, matomo },
|
|
421
|
+
...CUSTOMER_CONFIG
|
|
422
|
+
};
|
|
423
|
+
return { cookiesConfig, config };
|
|
424
|
+
}
|
|
425
|
+
getClientConfig = () => {
|
|
426
|
+
const jsonConfigs = cookies().get(SDK_CONFIG_KEY)?.value;
|
|
427
|
+
if (!jsonConfigs) {
|
|
428
|
+
throw new Error("Configs not found");
|
|
429
|
+
}
|
|
430
|
+
const configs = JSON.parse(jsonConfigs);
|
|
431
|
+
return configs;
|
|
432
|
+
};
|
|
433
|
+
getServerConfig() {
|
|
434
|
+
if (!global.__GLOBAL_CONFIG__) {
|
|
435
|
+
throw new Error("Server config not initialized");
|
|
436
|
+
}
|
|
437
|
+
return global.__GLOBAL_CONFIG__;
|
|
438
|
+
}
|
|
439
|
+
initializeConfig(config) {
|
|
440
|
+
if (global.__GLOBAL_CONFIG__) return global.__GLOBAL_CONFIG__;
|
|
441
|
+
global.__GLOBAL_CONFIG__ = config;
|
|
442
|
+
return global.__GLOBAL_CONFIG__;
|
|
443
|
+
}
|
|
444
|
+
updateConfigProp(key, value) {
|
|
445
|
+
if (!global.__GLOBAL_CONFIG__) {
|
|
446
|
+
throw new Error("Server config not initialized");
|
|
447
|
+
}
|
|
448
|
+
global.__GLOBAL_CONFIG__[key] = value;
|
|
449
|
+
return global.__GLOBAL_CONFIG__;
|
|
450
|
+
}
|
|
451
|
+
};
|
|
452
|
+
|
|
453
|
+
// src/logger.ts
|
|
454
|
+
var CrexLogger = class {
|
|
455
|
+
customerConfig;
|
|
456
|
+
logger;
|
|
457
|
+
/**
|
|
458
|
+
* Initializes the logger instance if it hasn't been initialized yet.
|
|
459
|
+
* Loads customer configuration and creates the logger with appropriate transports.
|
|
460
|
+
*
|
|
461
|
+
* @private
|
|
462
|
+
*/
|
|
463
|
+
async initLogger() {
|
|
464
|
+
try {
|
|
465
|
+
if (!this.customerConfig) {
|
|
466
|
+
const sdk = new CrexSDK();
|
|
467
|
+
this.customerConfig = sdk.getServerConfig();
|
|
468
|
+
}
|
|
469
|
+
if (!this.logger) {
|
|
470
|
+
this.logger = this.createLogger();
|
|
471
|
+
}
|
|
472
|
+
} catch (error) {
|
|
473
|
+
console.log("Error initializing logger:", error);
|
|
474
|
+
}
|
|
475
|
+
}
|
|
476
|
+
/**
|
|
477
|
+
* Logs a message with the specified level and optional category.
|
|
478
|
+
*
|
|
479
|
+
* @param options - Logging options
|
|
480
|
+
* @param options.level - The log level (error, warn, info, etc.)
|
|
481
|
+
* @param options.message - The message to log
|
|
482
|
+
* @param options.category - Optional category for the log message
|
|
483
|
+
*/
|
|
484
|
+
async log({ level, message, category }) {
|
|
485
|
+
await this.initLogger();
|
|
486
|
+
const timestamp = (/* @__PURE__ */ new Date()).toISOString();
|
|
487
|
+
const newMessage = `[${timestamp}] ${message}`;
|
|
488
|
+
this.logger.log(level, newMessage, category);
|
|
489
|
+
}
|
|
490
|
+
/**
|
|
491
|
+
* Creates a new Winston logger instance with configured transports.
|
|
492
|
+
*
|
|
493
|
+
* @private
|
|
494
|
+
* @returns A configured Winston logger instance
|
|
495
|
+
*/
|
|
496
|
+
createLogger() {
|
|
497
|
+
return winston.createLogger({
|
|
498
|
+
levels: LOG_LEVELS,
|
|
499
|
+
transports: [
|
|
500
|
+
new winston.transports.Console({
|
|
501
|
+
level: this.customerConfig.logs.console.minimumLevel,
|
|
502
|
+
silent: this.customerConfig.logs.console.silent
|
|
503
|
+
}),
|
|
504
|
+
new MatomoTransport(this.customerConfig),
|
|
505
|
+
new GraylogTransport(this.customerConfig)
|
|
506
|
+
]
|
|
507
|
+
});
|
|
508
|
+
}
|
|
509
|
+
};
|
|
510
|
+
|
|
511
|
+
// src/OIDC.ts
|
|
512
|
+
import { Issuer } from "openid-client";
|
|
513
|
+
var getToken = async () => {
|
|
514
|
+
try {
|
|
515
|
+
const sdk = new CrexSDK();
|
|
516
|
+
const config = sdk.getServerConfig();
|
|
517
|
+
const issuer = await Issuer.discover(config.OIDC.client.issuer);
|
|
518
|
+
const client = new issuer.Client({
|
|
519
|
+
client_id: config.OIDC.client.id,
|
|
520
|
+
client_secret: config.OIDC.client.secret
|
|
521
|
+
});
|
|
522
|
+
const tokenSet = await client.grant({ grant_type: "client_credentials" });
|
|
523
|
+
const token = tokenSet.access_token;
|
|
524
|
+
const expiresAt = tokenSet.expires_at;
|
|
525
|
+
return { token, expiresAt };
|
|
526
|
+
} catch (error) {
|
|
527
|
+
const logger = new CrexLogger();
|
|
528
|
+
logger.log({
|
|
529
|
+
level: "error",
|
|
530
|
+
message: `getToken error: ${error}`
|
|
531
|
+
});
|
|
532
|
+
return { token: "", expiresAt: 0, error: JSON.stringify(error) };
|
|
533
|
+
}
|
|
534
|
+
};
|
|
535
|
+
|
|
536
|
+
// src/requests.ts
|
|
537
|
+
var CrexApi = class {
|
|
538
|
+
customerConfig;
|
|
539
|
+
apiClient;
|
|
540
|
+
logger;
|
|
541
|
+
/**
|
|
542
|
+
* Initializes the API client if it hasn't been initialized yet.
|
|
543
|
+
* Loads customer configuration, creates the axios instance, and initializes the logger.
|
|
544
|
+
*
|
|
545
|
+
* @private
|
|
546
|
+
*/
|
|
547
|
+
async initAPI() {
|
|
548
|
+
this.logger = new CrexLogger();
|
|
549
|
+
if (!this.customerConfig) {
|
|
550
|
+
const sdk = new CrexSDK();
|
|
551
|
+
this.customerConfig = sdk.getServerConfig();
|
|
552
|
+
}
|
|
553
|
+
if (!this.apiClient) {
|
|
554
|
+
if (this.customerConfig.baseUrl.length === 0) {
|
|
555
|
+
throw new Error("CrexAPI.initAPI error: baseUrl is undefined");
|
|
556
|
+
}
|
|
557
|
+
this.apiClient = axios.create({
|
|
558
|
+
baseURL: this.customerConfig.baseUrl
|
|
559
|
+
});
|
|
560
|
+
}
|
|
561
|
+
}
|
|
562
|
+
async manageToken() {
|
|
563
|
+
try {
|
|
564
|
+
let token = "";
|
|
565
|
+
const hasToken = cookies2().get(CREX_TOKEN_HEADER_KEY);
|
|
566
|
+
if (hasToken == void 0 || hasToken.value === null) {
|
|
567
|
+
const { token: tokenResult } = await getToken();
|
|
568
|
+
if (tokenResult.length === 0) throw new Error("Token is undefined");
|
|
569
|
+
token = tokenResult;
|
|
570
|
+
} else {
|
|
571
|
+
token = hasToken.value;
|
|
572
|
+
}
|
|
573
|
+
return token;
|
|
574
|
+
} catch (error) {
|
|
575
|
+
this.logger.log({
|
|
576
|
+
level: "error",
|
|
577
|
+
message: `CrexAPI.manageToken error: ${error}`
|
|
578
|
+
});
|
|
579
|
+
throw error;
|
|
580
|
+
}
|
|
581
|
+
}
|
|
582
|
+
/**
|
|
583
|
+
* Executes an API request with caching, authentication, and retry logic.
|
|
584
|
+
*
|
|
585
|
+
* @param options - Request options
|
|
586
|
+
* @param options.url - The URL to request
|
|
587
|
+
* @param options.method - The HTTP method to use
|
|
588
|
+
* @param options.params - Optional query parameters
|
|
589
|
+
* @param options.body - Optional request body
|
|
590
|
+
* @param options.headers - Optional request headers
|
|
591
|
+
* @returns The response data
|
|
592
|
+
* @throws Error if the request fails after maximum retries
|
|
593
|
+
*/
|
|
594
|
+
async execute({
|
|
595
|
+
url,
|
|
596
|
+
method,
|
|
597
|
+
params,
|
|
598
|
+
body,
|
|
599
|
+
headers = {}
|
|
600
|
+
}) {
|
|
601
|
+
try {
|
|
602
|
+
await this.initAPI();
|
|
603
|
+
let response = void 0;
|
|
604
|
+
if (this.customerConfig.OIDC.client.enabled) {
|
|
605
|
+
const token = await this.manageToken();
|
|
606
|
+
headers = {
|
|
607
|
+
...headers,
|
|
608
|
+
Authorization: `Bearer ${token}`
|
|
609
|
+
};
|
|
610
|
+
this.apiClient.defaults.headers.common["Authorization"] = `Bearer ${token}`;
|
|
611
|
+
}
|
|
612
|
+
response = await this.apiClient.request({
|
|
613
|
+
url,
|
|
614
|
+
method,
|
|
615
|
+
data: body,
|
|
616
|
+
params,
|
|
617
|
+
headers
|
|
618
|
+
});
|
|
619
|
+
if (response) {
|
|
620
|
+
return response.data;
|
|
621
|
+
}
|
|
622
|
+
throw new Error("API.execute error: Failed to retrieve a valid response");
|
|
623
|
+
} catch (error) {
|
|
624
|
+
this.logger.log({
|
|
625
|
+
level: "error",
|
|
626
|
+
message: `CrexAPI.execute error: ${error}`
|
|
627
|
+
});
|
|
628
|
+
throw error;
|
|
629
|
+
}
|
|
630
|
+
}
|
|
631
|
+
};
|
|
632
|
+
export {
|
|
633
|
+
CrexApi
|
|
634
|
+
};
|
|
635
|
+
//# sourceMappingURL=requests.mjs.map
|