@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
package/dist/index.mjs
DELETED
|
@@ -1,309 +0,0 @@
|
|
|
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/config.ts
|
|
101
|
-
import { cookies } from "next/headers";
|
|
102
|
-
var getClientConfig = () => {
|
|
103
|
-
const jsonConfigs = cookies().get(SDK_CONFIG_KEY)?.value;
|
|
104
|
-
if (!jsonConfigs) {
|
|
105
|
-
throw new Error("Configs not found");
|
|
106
|
-
}
|
|
107
|
-
const configs = JSON.parse(jsonConfigs);
|
|
108
|
-
return configs;
|
|
109
|
-
};
|
|
110
|
-
function initializeConfig(config) {
|
|
111
|
-
if (global.__GLOBAL_CONFIG__) return global.__GLOBAL_CONFIG__;
|
|
112
|
-
global.__GLOBAL_CONFIG__ = config;
|
|
113
|
-
return global.__GLOBAL_CONFIG__;
|
|
114
|
-
}
|
|
115
|
-
function getServerConfig() {
|
|
116
|
-
if (!global.__GLOBAL_CONFIG__) {
|
|
117
|
-
throw new Error("Server config not initialized");
|
|
118
|
-
}
|
|
119
|
-
return global.__GLOBAL_CONFIG__;
|
|
120
|
-
}
|
|
121
|
-
function updateConfigProp(key, value) {
|
|
122
|
-
if (!global.__GLOBAL_CONFIG__) {
|
|
123
|
-
throw new Error("Server config not initialized");
|
|
124
|
-
}
|
|
125
|
-
global.__GLOBAL_CONFIG__[key] = value;
|
|
126
|
-
return global.__GLOBAL_CONFIG__;
|
|
127
|
-
}
|
|
128
|
-
|
|
129
|
-
// src/logger.ts
|
|
130
|
-
var CrexLogger = class {
|
|
131
|
-
customerConfig;
|
|
132
|
-
logger;
|
|
133
|
-
/**
|
|
134
|
-
* Initializes the logger instance if it hasn't been initialized yet.
|
|
135
|
-
* Loads customer configuration and creates the logger with appropriate transports.
|
|
136
|
-
*
|
|
137
|
-
* @private
|
|
138
|
-
*/
|
|
139
|
-
async initLogger() {
|
|
140
|
-
try {
|
|
141
|
-
if (!this.customerConfig) {
|
|
142
|
-
this.customerConfig = getServerConfig();
|
|
143
|
-
}
|
|
144
|
-
if (!this.logger) {
|
|
145
|
-
this.logger = this.createLogger();
|
|
146
|
-
}
|
|
147
|
-
} catch (error) {
|
|
148
|
-
console.log("Error initializing logger:", error);
|
|
149
|
-
}
|
|
150
|
-
}
|
|
151
|
-
/**
|
|
152
|
-
* Logs a message with the specified level and optional category.
|
|
153
|
-
*
|
|
154
|
-
* @param options - Logging options
|
|
155
|
-
* @param options.level - The log level (error, warn, info, etc.)
|
|
156
|
-
* @param options.message - The message to log
|
|
157
|
-
* @param options.category - Optional category for the log message
|
|
158
|
-
*/
|
|
159
|
-
async log({ level, message, category }) {
|
|
160
|
-
await this.initLogger();
|
|
161
|
-
const timestamp = (/* @__PURE__ */ new Date()).toISOString();
|
|
162
|
-
const newMessage = `[${timestamp}] ${message}`;
|
|
163
|
-
this.logger.log(level, newMessage, category);
|
|
164
|
-
}
|
|
165
|
-
/**
|
|
166
|
-
* Creates a new Winston logger instance with configured transports.
|
|
167
|
-
*
|
|
168
|
-
* @private
|
|
169
|
-
* @returns A configured Winston logger instance
|
|
170
|
-
*/
|
|
171
|
-
createLogger() {
|
|
172
|
-
return winston.createLogger({
|
|
173
|
-
levels: LOG_LEVELS,
|
|
174
|
-
transports: [
|
|
175
|
-
new winston.transports.Console({
|
|
176
|
-
level: this.customerConfig.logs.console.minimumLevel,
|
|
177
|
-
silent: this.customerConfig.logs.console.silent
|
|
178
|
-
}),
|
|
179
|
-
new MatomoTransport(this.customerConfig),
|
|
180
|
-
new GraylogTransport(this.customerConfig)
|
|
181
|
-
]
|
|
182
|
-
});
|
|
183
|
-
}
|
|
184
|
-
};
|
|
185
|
-
|
|
186
|
-
// src/OIDC.ts
|
|
187
|
-
import { Issuer } from "openid-client";
|
|
188
|
-
var getToken = async () => {
|
|
189
|
-
try {
|
|
190
|
-
const config = getServerConfig();
|
|
191
|
-
const issuer = await Issuer.discover(config.OIDC.client.issuer);
|
|
192
|
-
const client = new issuer.Client({
|
|
193
|
-
client_id: config.OIDC.client.id,
|
|
194
|
-
client_secret: config.OIDC.client.secret
|
|
195
|
-
});
|
|
196
|
-
const tokenSet = await client.grant({ grant_type: "client_credentials" });
|
|
197
|
-
const token = tokenSet.access_token;
|
|
198
|
-
const expiresAt = tokenSet.expires_at;
|
|
199
|
-
return { token, expiresAt };
|
|
200
|
-
} catch (error) {
|
|
201
|
-
const logger = new CrexLogger();
|
|
202
|
-
logger.log({
|
|
203
|
-
level: "error",
|
|
204
|
-
message: `getToken error: ${error}`
|
|
205
|
-
});
|
|
206
|
-
return { token: "", expiresAt: 0, error: JSON.stringify(error) };
|
|
207
|
-
}
|
|
208
|
-
};
|
|
209
|
-
|
|
210
|
-
// src/requests.ts
|
|
211
|
-
var CrexApi = class {
|
|
212
|
-
customerConfig;
|
|
213
|
-
apiClient;
|
|
214
|
-
logger;
|
|
215
|
-
/**
|
|
216
|
-
* Initializes the API client if it hasn't been initialized yet.
|
|
217
|
-
* Loads customer configuration, creates the axios instance, and initializes the logger.
|
|
218
|
-
*
|
|
219
|
-
* @private
|
|
220
|
-
*/
|
|
221
|
-
async initAPI() {
|
|
222
|
-
this.logger = new CrexLogger();
|
|
223
|
-
if (!this.customerConfig) {
|
|
224
|
-
this.customerConfig = getServerConfig();
|
|
225
|
-
}
|
|
226
|
-
if (!this.apiClient) {
|
|
227
|
-
this.apiClient = axios.create({
|
|
228
|
-
baseURL: this.customerConfig.baseUrl
|
|
229
|
-
});
|
|
230
|
-
}
|
|
231
|
-
}
|
|
232
|
-
async manageToken() {
|
|
233
|
-
try {
|
|
234
|
-
let token = "";
|
|
235
|
-
const hasToken = cookies2().get(CREX_TOKEN_HEADER_KEY);
|
|
236
|
-
if (hasToken == void 0 || hasToken.value === null) {
|
|
237
|
-
const { token: tokenResult } = await getToken();
|
|
238
|
-
if (tokenResult.length === 0) throw new Error("Token is undefined");
|
|
239
|
-
token = tokenResult;
|
|
240
|
-
} else {
|
|
241
|
-
token = hasToken.value;
|
|
242
|
-
}
|
|
243
|
-
return token;
|
|
244
|
-
} catch (error) {
|
|
245
|
-
this.logger.log({
|
|
246
|
-
level: "error",
|
|
247
|
-
message: `CrexAPI.manageToken error: ${error}`
|
|
248
|
-
});
|
|
249
|
-
throw error;
|
|
250
|
-
}
|
|
251
|
-
}
|
|
252
|
-
/**
|
|
253
|
-
* Executes an API request with caching, authentication, and retry logic.
|
|
254
|
-
*
|
|
255
|
-
* @param options - Request options
|
|
256
|
-
* @param options.url - The URL to request
|
|
257
|
-
* @param options.method - The HTTP method to use
|
|
258
|
-
* @param options.params - Optional query parameters
|
|
259
|
-
* @param options.body - Optional request body
|
|
260
|
-
* @param options.headers - Optional request headers
|
|
261
|
-
* @returns The response data
|
|
262
|
-
* @throws Error if the request fails after maximum retries
|
|
263
|
-
*/
|
|
264
|
-
async execute({
|
|
265
|
-
url,
|
|
266
|
-
method,
|
|
267
|
-
params,
|
|
268
|
-
body,
|
|
269
|
-
headers = {}
|
|
270
|
-
}) {
|
|
271
|
-
try {
|
|
272
|
-
await this.initAPI();
|
|
273
|
-
let response = void 0;
|
|
274
|
-
if (this.customerConfig.OIDC.client.enabled) {
|
|
275
|
-
const token = await this.manageToken();
|
|
276
|
-
headers = {
|
|
277
|
-
...headers,
|
|
278
|
-
Authorization: `Bearer ${token}`
|
|
279
|
-
};
|
|
280
|
-
this.apiClient.defaults.headers.common["Authorization"] = `Bearer ${token}`;
|
|
281
|
-
}
|
|
282
|
-
response = await this.apiClient.request({
|
|
283
|
-
url,
|
|
284
|
-
method,
|
|
285
|
-
data: body,
|
|
286
|
-
params,
|
|
287
|
-
headers
|
|
288
|
-
});
|
|
289
|
-
if (response) {
|
|
290
|
-
return response.data;
|
|
291
|
-
}
|
|
292
|
-
throw new Error("API.execute error: Failed to retrieve a valid response");
|
|
293
|
-
} catch (error) {
|
|
294
|
-
this.logger.log({
|
|
295
|
-
level: "error",
|
|
296
|
-
message: `CrexAPI.execute error: ${error}`
|
|
297
|
-
});
|
|
298
|
-
throw error;
|
|
299
|
-
}
|
|
300
|
-
}
|
|
301
|
-
};
|
|
302
|
-
export {
|
|
303
|
-
CrexApi,
|
|
304
|
-
getClientConfig,
|
|
305
|
-
getServerConfig,
|
|
306
|
-
initializeConfig,
|
|
307
|
-
updateConfigProp
|
|
308
|
-
};
|
|
309
|
-
//# sourceMappingURL=index.mjs.map
|
package/dist/index.mjs.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/requests.ts","../../constants/src/index.ts","../src/logger.ts","../src/transports/matomo.ts","../src/transports/graylog.ts","../src/config.ts","../src/OIDC.ts"],"sourcesContent":["import axios, { AxiosResponse, Method, AxiosInstance } from \"axios\";\nimport { CREX_TOKEN_HEADER_KEY } from \"@c-rex/constants\";\nimport { ConfigInterface } from \"@c-rex/interfaces\";\nimport { cookies } from \"next/headers\";\nimport { CrexLogger } from \"./logger\";\nimport { getServerConfig } from \"./config\";\nimport { getToken } from \"./OIDC\";\n\n/**\n * Interface for API response with generic data type.\n */\ninterface APIGenericResponse<T> extends AxiosResponse {\n data: T;\n statusCode: number;\n}\n\n/**\n * Interface for API call parameters.\n */\ninterface CallParams {\n url: string;\n method: Method;\n body?: any;\n headers?: any;\n params?: any;\n}\n\n/**\n * API client class for the CREX application.\n * Handles API requests with caching, authentication, and retry logic.\n */\nexport class CrexApi {\n private customerConfig!: ConfigInterface;\n private apiClient!: AxiosInstance;\n private logger!: CrexLogger;\n\n /**\n * Initializes the API client if it hasn't been initialized yet.\n * Loads customer configuration, creates the axios instance, and initializes the logger.\n * \n * @private\n */\n private async initAPI() {\n this.logger = new CrexLogger();\n\n if (!this.customerConfig) {\n this.customerConfig = getServerConfig();\n }\n\n\n if (!this.apiClient) {\n this.apiClient = axios.create({\n baseURL: this.customerConfig.baseUrl,\n })\n }\n }\n\n private async manageToken() {\n try {\n let token = \"\";\n const hasToken = cookies().get(CREX_TOKEN_HEADER_KEY);\n\n if (hasToken == undefined || hasToken.value === null) {\n const { token: tokenResult } = await getToken();\n\n if (tokenResult.length === 0) throw new Error(\"Token is undefined\");\n\n token = tokenResult;\n } else {\n token = hasToken.value;\n }\n\n return token;\n } catch (error) {\n this.logger.log({\n level: \"error\",\n message: `CrexAPI.manageToken error: ${error}`\n });\n\n throw error\n }\n }\n\n /**\n * Executes an API request with caching, authentication, and retry logic.\n * \n * @param options - Request options\n * @param options.url - The URL to request\n * @param options.method - The HTTP method to use\n * @param options.params - Optional query parameters\n * @param options.body - Optional request body\n * @param options.headers - Optional request headers\n * @returns The response data\n * @throws Error if the request fails after maximum retries\n */\n async execute<T>({\n url,\n method,\n params,\n body,\n headers = {},\n }: CallParams): Promise<T> {\n try {\n await this.initAPI();\n\n let response: APIGenericResponse<T> | undefined = undefined;\n\n if (this.customerConfig.OIDC.client.enabled) {\n const token = await this.manageToken();\n\n headers = {\n ...headers,\n Authorization: `Bearer ${token}`,\n };\n\n this.apiClient.defaults.headers.common['Authorization'] = `Bearer ${token}`;\n }\n\n response = await this.apiClient.request({\n url,\n method,\n data: body,\n params,\n headers,\n });\n\n if (response) {\n return response.data;\n }\n\n throw new Error(\"API.execute error: Failed to retrieve a valid response\");\n } catch (error) {\n this.logger.log({\n level: \"error\",\n message: `CrexAPI.execute error: ${error}`\n });\n throw error;\n }\n }\n}","export const ALL = \"*\"\n\nexport const LOG_CATEGORIES = [\n \"NoLicense\",\n \"Scenario\",\n \"Favorites\",\n \"Subscription\",\n \"Share\",\n \"Document\",\n \"Search\",\n \"History\",\n \"Notification\",\n \"UserProfile\",\n] as const;\n\nexport const LOG_LEVELS = {\n critical: 2,\n error: 3,\n warning: 4,\n info: 6,\n debug: 7,\n} as const;\n\nexport const RESULT_VIEW_STYLES = [\n \"cards\",\n \"table\",\n \"table-with-images\",\n] as const;\n\nexport const API = {\n MAX_RETRY: 3,\n API_TIMEOUT: 10000,\n RETRY_DELAY: 500,\n API_HEADERS: {\n \"content-Type\": \"application/json\",\n },\n};\n\nexport const SDK_CONFIG_KEY = \"crex-sdk-config\";\n\nexport const CONTENT_LANG_KEY = \"CONTENT_LANG_KEY\";\n\nexport const AVAILABLE_CONTENT_LANG_KEY = \"AVAILABLE_CONTENT_LANG_KEY\";\n\nexport const UI_LANG_KEY = \"UI_LANG_KEY\";\n\nexport const FLAGS_BY_LANG = {\n \"en\": \"US\",\n \"de\": \"DE\",\n};\n\nexport const DEFAULT_LANG = \"en-US\";\n\nexport const EN_LANG = \"en\";\n\nexport const UI_LANG_OPTIONS = [\"en-us\", \"de-de\"];\n\nexport const TOPICS_TYPE_AND_LINK = \"topics\";\nexport const BLOG_TYPE_AND_LINK = \"blog\";\nexport const DOCUMENTS_TYPE_AND_LINK = \"documents\";\n\nexport const TOPIC = \"TOPIC\";\nexport const DOCUMENT = \"DOCUMENT\";\nexport const PACKAGE = \"PACKAGE\";\nexport const FRAGMENT = \"FRAGMENT\";\n\nexport const RESULT_TYPES = {\n TOPIC: TOPIC,\n DOCUMENT: DOCUMENT,\n PACKAGE: PACKAGE,\n FRAGMENT: FRAGMENT\n} as const;\n\nexport const FILES_EXTENSIONS = {\n PDF: \"application/pdf\",\n HTML: \"text/html\",\n} as const;\n\nexport const DEFAULT_COOKIE_LIMIT = 30 * 24 * 60 * 60 * 1000; // 30 days in milliseconds\n\nexport const ICONS_BY_FILE_EXTENSION = {\n \"application/pdf\": \"FaFilePdf\",\n} as const;\n\nexport const DEFAULT_ICON = \"file\";\n\nexport const CREX_TOKEN_HEADER_KEY = \"crex-token\";\n\nexport const WILD_CARD_OPTIONS = {\n BOTH: \"BOTH\",\n END: \"END\",\n START: \"START\",\n NONE: \"NONE\",\n} as const;\n\nexport const OPERATOR_OPTIONS = {\n AND: \"AND\",\n OR: \"OR\",\n} as const;\n\nexport const ARTICLE_PAGE_LAYOUT = {\n BLOG: \"BLOG\",\n DOCUMENT: \"DOCUMENT\",\n} as const;\n\nexport const DEVICE_OPTIONS = {\n MOBILE: \"mobile\",\n TABLET: \"tablet\",\n DESKTOP: \"desktop\",\n}\nexport const BREAKPOINTS = {\n sm: 640,\n md: 768,\n lg: 1024,\n xl: 1280,\n \"2xl\": 1536,\n};\n\nexport const MARKER_COLORS = [\n \"red-500\",\n \"orange-500\",\n \"yellow-400\",\n \"green-500\",\n \"teal-500\",\n \"blue-500\",\n \"sky-500\",\n \"purple-500\",\n \"pink-500\",\n \"gray-500\",\n \"neutral-800\",\n \"cyan-500\",\n \"lime-500\",\n \"amber-500\",\n \"indigo-500\",\n];","import winston from \"winston\";\nimport { MatomoTransport } from \"./transports/matomo\";\nimport { GraylogTransport } from \"./transports/graylog\";\nimport { ConfigInterface } from \"@c-rex/interfaces\";\nimport { LogCategoriesType, LogLevelType } from \"@c-rex/types\";\nimport { LOG_LEVELS } from \"@c-rex/constants\";\nimport { getServerConfig } from \"./config\";\n\n\n/**\n * Logger class for the CREX application.\n * Provides logging functionality with multiple transports (Console, Matomo, Graylog).\n */\nexport class CrexLogger {\n private customerConfig!: ConfigInterface;\n public logger!: winston.Logger;\n\n /**\n * Initializes the logger instance if it hasn't been initialized yet.\n * Loads customer configuration and creates the logger with appropriate transports.\n * \n * @private\n */\n private async initLogger() {\n try {\n if (!this.customerConfig) {\n this.customerConfig = getServerConfig();\n }\n if (!this.logger) {\n this.logger = this.createLogger();\n }\n } catch (error) {\n console.log(\"Error initializing logger:\", error);\n }\n }\n\n /**\n * Logs a message with the specified level and optional category.\n * \n * @param options - Logging options\n * @param options.level - The log level (error, warn, info, etc.)\n * @param options.message - The message to log\n * @param options.category - Optional category for the log message\n */\n public async log({ level, message, category }: {\n level: LogLevelType,\n message: string,\n category?: LogCategoriesType\n }) {\n await this.initLogger();\n const timestamp = new Date().toISOString();\n const newMessage = `[${timestamp}] ${message}`;\n this.logger.log(level, newMessage, category);\n }\n\n /**\n * Creates a new Winston logger instance with configured transports.\n * \n * @private\n * @returns A configured Winston logger instance\n */\n private createLogger() {\n return winston.createLogger({\n levels: LOG_LEVELS,\n transports: [\n new winston.transports.Console({\n level: this.customerConfig.logs.console.minimumLevel,\n silent: this.customerConfig.logs.console.silent,\n }),\n new MatomoTransport(this.customerConfig),\n new GraylogTransport(this.customerConfig),\n ],\n });\n }\n}","import Transport from \"winston-transport\";\nimport { LogCategoriesType, LogLevelType } from \"@c-rex/types\";\nimport { ALL } from \"@c-rex/constants\";\nimport { ConfigInterface } from \"@c-rex/interfaces\";\n\n/**\n * Winston transport for sending logs to Matomo analytics.\n * Extends the base Winston transport with Matomo-specific functionality.\n */\nexport class MatomoTransport extends Transport {\n public matomoTransport: any;\n private configs: ConfigInterface;\n\n /**\n * Creates a new instance of MatomoTransport.\n * \n * @param configs - The application configuration containing logging settings\n */\n constructor(configs: ConfigInterface) {\n super({\n level: configs.logs.matomo.minimumLevel,\n silent: configs.logs.matomo.silent,\n });\n\n this.matomoTransport = new Transport();\n this.configs = configs;\n }\n\n /**\n * Logs a message to Matomo if the message category is included in the configured categories.\n * \n * @param info - The log information including level, message, and category\n * @param callback - Callback function to execute after logging\n */\n log(\n info: { level: LogLevelType, message: string, category: LogCategoriesType },\n callback: () => void,\n ): void {\n const matomoCategory = this.configs.logs.matomo.categoriesLevel\n\n if (matomoCategory.includes(info.category) || matomoCategory.includes(ALL)) {\n this.matomoTransport.log(info, callback);\n }\n }\n}\n","import Transport from \"winston-transport\";\nimport Graylog2Transport from \"winston-graylog2\";\nimport { LogCategoriesType, LogLevelType } from \"@c-rex/types\";\nimport { ALL } from \"@c-rex/constants\";\nimport { ConfigInterface } from \"@c-rex/interfaces\";\n\n/**\n * Winston transport for sending logs to Graylog.\n * Extends the base Winston transport with Graylog-specific functionality.\n */\nexport class GraylogTransport extends Transport {\n public graylogTransport: any;\n private configs: ConfigInterface\n\n /**\n * Creates a new instance of GraylogTransport.\n * \n * @param configs - The application configuration containing logging settings\n */\n constructor(configs: ConfigInterface) {\n if (!configs.logs.graylog.hostname || configs.logs.graylog.port === undefined) {\n throw new Error(\"Graylog hostname and port must be defined\");\n }\n\n super({\n level: configs.logs.graylog.minimumLevel,\n silent: configs.logs.graylog.silent,\n });\n\n this.configs = configs;\n this.graylogTransport = new Graylog2Transport({\n name: configs.logs.graylog.app,\n silent: configs.logs.graylog.silent,\n handleExceptions: false,\n graylog: {\n servers: [\n { host: configs.logs.graylog.hostname, port: configs.logs.graylog.port }\n ],\n },\n });\n }\n\n /**\n * Logs a message to Graylog if the message category is included in the configured categories.\n * \n * @param info - The log information including level, message, and category\n * @param callback - Callback function to execute after logging\n */\n log(\n info: { level: LogLevelType, message: string, category: LogCategoriesType },\n callback: () => void,\n ): void {\n const graylogCategory = this.configs.logs.graylog.categoriesLevel\n\n if (graylogCategory.includes(info.category) || graylogCategory.includes(ALL)) {\n this.graylogTransport.log(info, callback);\n }\n }\n}\n","import { ConfigInterface, CookiesConfigs } from \"@c-rex/interfaces\";\nimport { SDK_CONFIG_KEY } from '@c-rex/constants';\nimport { cookies } from 'next/headers';\n\n/**\n * Retrieves and parses configuration data from a cookie.\n * @returns The parsed configuration object\n * @throws Error if the configuration cookie is not found or cannot be parsed\n */\nexport const getClientConfig = (): CookiesConfigs => {\n const jsonConfigs = cookies().get(SDK_CONFIG_KEY)?.value;\n if (!jsonConfigs) {\n throw new Error('Configs not found');\n }\n\n const configs: CookiesConfigs = JSON.parse(jsonConfigs);\n\n return configs;\n}\n\ndeclare global {\n // eslint-disable-next-line no-var\n var __GLOBAL_CONFIG__: ConfigInterface | null;\n}\n\nexport function initializeConfig(config: ConfigInterface) {\n if (global.__GLOBAL_CONFIG__) return global.__GLOBAL_CONFIG__;\n global.__GLOBAL_CONFIG__ = config;\n return global.__GLOBAL_CONFIG__;\n}\n\nexport function getServerConfig() {\n if (!global.__GLOBAL_CONFIG__) {\n throw new Error('Server config not initialized');\n }\n return global.__GLOBAL_CONFIG__;\n}\n\nexport function updateConfigProp(key: keyof ConfigInterface, value: any) {\n if (!global.__GLOBAL_CONFIG__) {\n throw new Error('Server config not initialized');\n }\n global.__GLOBAL_CONFIG__[key] = value;\n return global.__GLOBAL_CONFIG__;\n}","import { Issuer } from 'openid-client';\nimport { getServerConfig } from './config';\nimport { CrexLogger } from './logger';\nimport { CrexSDK } from './sdk';\n\n/**\n * Retrieves an access token using client credentials flow from the configured OIDC provider\n * \n * @returns NextResponse with success status or error message\n * @throws Error if token retrieval fails\n */\nexport const getToken = async (): Promise<{ token: string; expiresAt: number; error?: string }> => {\n try {\n const config = getServerConfig();\n const issuer = await Issuer.discover(config.OIDC.client.issuer);\n const client = new issuer.Client({\n client_id: config.OIDC.client.id,\n client_secret: config.OIDC.client.secret,\n });\n const tokenSet = await client.grant({ grant_type: 'client_credentials' });\n\n const token = tokenSet.access_token!;\n const expiresAt = tokenSet.expires_at!;\n\n return { token, expiresAt };\n\n } catch (error) {\n const logger = new CrexLogger()\n logger.log({\n level: \"error\",\n message: `getToken error: ${error}`\n })\n return { token: '', expiresAt: 0, error: JSON.stringify(error) };\n\n }\n}\n\nexport const getIssuerMetadata = async (): Promise<any> => {\n const sdk = new CrexSDK();\n const config = sdk.getServerConfig();\n const issuer = await Issuer.discover(config.OIDC.client.issuer);\n return issuer.metadata;\n}\n"],"mappings":";AAAA,OAAO,WAAqD;;;ACArD,IAAM,MAAM;AAeZ,IAAM,aAAa;AAAA,EACtB,UAAU;AAAA,EACV,OAAO;AAAA,EACP,SAAS;AAAA,EACT,MAAM;AAAA,EACN,OAAO;AACX;AAiBO,IAAM,iBAAiB;AAwCvB,IAAM,uBAAuB,KAAK,KAAK,KAAK,KAAK;AAQjD,IAAM,wBAAwB;;;ADnFrC,SAAS,WAAAA,gBAAe;;;AEHxB,OAAO,aAAa;;;ACApB,OAAO,eAAe;AASf,IAAM,kBAAN,cAA8B,UAAU;AAAA,EACpC;AAAA,EACC;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAOR,YAAY,SAA0B;AAClC,UAAM;AAAA,MACF,OAAO,QAAQ,KAAK,OAAO;AAAA,MAC3B,QAAQ,QAAQ,KAAK,OAAO;AAAA,IAChC,CAAC;AAED,SAAK,kBAAkB,IAAI,UAAU;AACrC,SAAK,UAAU;AAAA,EACnB;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAQA,IACI,MACA,UACI;AACJ,UAAM,iBAAiB,KAAK,QAAQ,KAAK,OAAO;AAE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