@c-rex/services 0.1.11 → 0.1.13
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.d.mts +8 -1
- package/dist/index.d.ts +8 -1
- package/dist/index.js +148 -115
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +144 -111
- package/dist/index.mjs.map +1 -1
- package/package.json +1 -1
package/dist/index.mjs
CHANGED
|
@@ -10,14 +10,7 @@ var LOG_LEVELS = {
|
|
|
10
10
|
info: 6,
|
|
11
11
|
debug: 7
|
|
12
12
|
};
|
|
13
|
-
var
|
|
14
|
-
MAX_RETRY: 3,
|
|
15
|
-
API_TIMEOUT: 1e4,
|
|
16
|
-
RETRY_DELAY: 500,
|
|
17
|
-
API_HEADERS: {
|
|
18
|
-
"content-Type": "application/json"
|
|
19
|
-
}
|
|
20
|
-
};
|
|
13
|
+
var SDK_CONFIG_KEY = "crex-sdk-config";
|
|
21
14
|
var CONTENT_LANG_KEY = "CONTENT_LANG_KEY";
|
|
22
15
|
var UI_LANG_KEY = "UI_LANG_KEY";
|
|
23
16
|
var FLAGS_BY_LANG = {
|
|
@@ -49,7 +42,7 @@ var OPERATOR_OPTIONS = {
|
|
|
49
42
|
};
|
|
50
43
|
|
|
51
44
|
// ../core/src/requests.ts
|
|
52
|
-
import { cookies } from "next/headers";
|
|
45
|
+
import { cookies as cookies2 } from "next/headers";
|
|
53
46
|
|
|
54
47
|
// ../core/src/logger.ts
|
|
55
48
|
import winston from "winston";
|
|
@@ -112,7 +105,6 @@ var GraylogTransport = class extends Transport2 {
|
|
|
112
105
|
handleExceptions: false,
|
|
113
106
|
graylog: {
|
|
114
107
|
servers: [
|
|
115
|
-
{ host: "localhost", port: 12201 },
|
|
116
108
|
{ host: configs.logs.graylog.hostname, port: configs.logs.graylog.port }
|
|
117
109
|
]
|
|
118
110
|
}
|
|
@@ -132,8 +124,24 @@ var GraylogTransport = class extends Transport2 {
|
|
|
132
124
|
}
|
|
133
125
|
};
|
|
134
126
|
|
|
127
|
+
// ../core/src/config.ts
|
|
128
|
+
import { cookies } from "next/headers";
|
|
129
|
+
var getClientConfig = () => {
|
|
130
|
+
const jsonConfigs = cookies().get(SDK_CONFIG_KEY)?.value;
|
|
131
|
+
if (!jsonConfigs) {
|
|
132
|
+
throw new Error("Configs not found");
|
|
133
|
+
}
|
|
134
|
+
const configs = JSON.parse(jsonConfigs);
|
|
135
|
+
return configs;
|
|
136
|
+
};
|
|
137
|
+
function getServerConfig() {
|
|
138
|
+
if (!global.__GLOBAL_CONFIG__) {
|
|
139
|
+
throw new Error("Server config not initialized");
|
|
140
|
+
}
|
|
141
|
+
return global.__GLOBAL_CONFIG__;
|
|
142
|
+
}
|
|
143
|
+
|
|
135
144
|
// ../core/src/logger.ts
|
|
136
|
-
import { getServerConfigs } from "@c-rex/utils/next-cookies";
|
|
137
145
|
var CrexLogger = class {
|
|
138
146
|
customerConfig;
|
|
139
147
|
logger;
|
|
@@ -146,13 +154,13 @@ var CrexLogger = class {
|
|
|
146
154
|
async initLogger() {
|
|
147
155
|
try {
|
|
148
156
|
if (!this.customerConfig) {
|
|
149
|
-
this.customerConfig =
|
|
157
|
+
this.customerConfig = getServerConfig();
|
|
150
158
|
}
|
|
151
159
|
if (!this.logger) {
|
|
152
160
|
this.logger = this.createLogger();
|
|
153
161
|
}
|
|
154
162
|
} catch (error) {
|
|
155
|
-
console.
|
|
163
|
+
console.log("Error initializing logger:", error);
|
|
156
164
|
}
|
|
157
165
|
}
|
|
158
166
|
/**
|
|
@@ -165,7 +173,9 @@ var CrexLogger = class {
|
|
|
165
173
|
*/
|
|
166
174
|
async log({ level, message, category }) {
|
|
167
175
|
await this.initLogger();
|
|
168
|
-
|
|
176
|
+
const timestamp = (/* @__PURE__ */ new Date()).toISOString();
|
|
177
|
+
const newMessage = `[${timestamp}] ${message}`;
|
|
178
|
+
this.logger.log(level, newMessage, category);
|
|
169
179
|
}
|
|
170
180
|
/**
|
|
171
181
|
* Creates a new Winston logger instance with configured transports.
|
|
@@ -188,13 +198,35 @@ var CrexLogger = class {
|
|
|
188
198
|
}
|
|
189
199
|
};
|
|
190
200
|
|
|
201
|
+
// ../core/src/OIDC.ts
|
|
202
|
+
import { Issuer } from "openid-client";
|
|
203
|
+
var getToken = async () => {
|
|
204
|
+
try {
|
|
205
|
+
const config = getServerConfig();
|
|
206
|
+
const issuer = await Issuer.discover(config.OIDC.client.issuer);
|
|
207
|
+
const client = new issuer.Client({
|
|
208
|
+
client_id: config.OIDC.client.id,
|
|
209
|
+
client_secret: config.OIDC.client.secret
|
|
210
|
+
});
|
|
211
|
+
const tokenSet = await client.grant({ grant_type: "client_credentials" });
|
|
212
|
+
const token = tokenSet.access_token;
|
|
213
|
+
const expiresAt = tokenSet.expires_at;
|
|
214
|
+
return { token, expiresAt };
|
|
215
|
+
} catch (error) {
|
|
216
|
+
const logger = new CrexLogger();
|
|
217
|
+
logger.log({
|
|
218
|
+
level: "error",
|
|
219
|
+
message: `getToken error: ${error}`
|
|
220
|
+
});
|
|
221
|
+
return { token: "", expiresAt: 0, error: JSON.stringify(error) };
|
|
222
|
+
}
|
|
223
|
+
};
|
|
224
|
+
|
|
191
225
|
// ../core/src/requests.ts
|
|
192
|
-
import { getServerConfigs as getServerConfigs2, getClientConfigs } from "@c-rex/utils/next-cookies";
|
|
193
226
|
var CrexApi = class {
|
|
194
227
|
customerConfig;
|
|
195
228
|
apiClient;
|
|
196
229
|
logger;
|
|
197
|
-
publicNextApiUrl;
|
|
198
230
|
/**
|
|
199
231
|
* Initializes the API client if it hasn't been initialized yet.
|
|
200
232
|
* Loads customer configuration, creates the axios instance, and initializes the logger.
|
|
@@ -204,10 +236,7 @@ var CrexApi = class {
|
|
|
204
236
|
async initAPI() {
|
|
205
237
|
this.logger = new CrexLogger();
|
|
206
238
|
if (!this.customerConfig) {
|
|
207
|
-
this.customerConfig =
|
|
208
|
-
}
|
|
209
|
-
if (!this.publicNextApiUrl) {
|
|
210
|
-
this.publicNextApiUrl = await getClientConfigs().publicNextApiUrl;
|
|
239
|
+
this.customerConfig = getServerConfig();
|
|
211
240
|
}
|
|
212
241
|
if (!this.apiClient) {
|
|
213
242
|
this.apiClient = axios.create({
|
|
@@ -215,34 +244,13 @@ var CrexApi = class {
|
|
|
215
244
|
});
|
|
216
245
|
}
|
|
217
246
|
}
|
|
218
|
-
async getToken() {
|
|
219
|
-
for (let retry = 0; retry < API.MAX_RETRY; retry++) {
|
|
220
|
-
try {
|
|
221
|
-
const response = await fetch(`${this.publicNextApiUrl}/api/token`, {
|
|
222
|
-
method: "POST",
|
|
223
|
-
credentials: "include"
|
|
224
|
-
});
|
|
225
|
-
const { token } = await response.json();
|
|
226
|
-
return token;
|
|
227
|
-
} catch (error) {
|
|
228
|
-
this.logger.log({
|
|
229
|
-
level: "error",
|
|
230
|
-
message: `utils.getToken ${retry + 1}\xBA error when request token. Error: ${error}`
|
|
231
|
-
});
|
|
232
|
-
await new Promise((resolve) => setTimeout(resolve, API.RETRY_DELAY));
|
|
233
|
-
if (retry === API.MAX_RETRY) {
|
|
234
|
-
throw error;
|
|
235
|
-
}
|
|
236
|
-
}
|
|
237
|
-
}
|
|
238
|
-
}
|
|
239
247
|
async manageToken() {
|
|
240
248
|
try {
|
|
241
249
|
let token = "";
|
|
242
|
-
const hasToken =
|
|
250
|
+
const hasToken = cookies2().get(CREX_TOKEN_HEADER_KEY);
|
|
243
251
|
if (hasToken == void 0 || hasToken.value === null) {
|
|
244
|
-
const tokenResult = await
|
|
245
|
-
if (tokenResult ===
|
|
252
|
+
const { token: tokenResult } = await getToken();
|
|
253
|
+
if (tokenResult.length === 0) throw new Error("Token is undefined");
|
|
246
254
|
token = tokenResult;
|
|
247
255
|
} else {
|
|
248
256
|
token = hasToken.value;
|
|
@@ -275,40 +283,35 @@ var CrexApi = class {
|
|
|
275
283
|
body,
|
|
276
284
|
headers = {}
|
|
277
285
|
}) {
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
for (let retry = 0; retry < API.MAX_RETRY; retry++) {
|
|
289
|
-
try {
|
|
290
|
-
response = await this.apiClient.request({
|
|
291
|
-
url,
|
|
292
|
-
method,
|
|
293
|
-
data: body,
|
|
294
|
-
params,
|
|
295
|
-
headers
|
|
296
|
-
});
|
|
297
|
-
break;
|
|
298
|
-
} catch (error) {
|
|
299
|
-
this.logger.log({
|
|
300
|
-
level: "error",
|
|
301
|
-
message: `API.execute ${retry + 1}\xBA error when request ${url}. Error: ${error}`
|
|
302
|
-
});
|
|
303
|
-
if (retry === API.MAX_RETRY - 1) {
|
|
304
|
-
throw error;
|
|
305
|
-
}
|
|
286
|
+
try {
|
|
287
|
+
await this.initAPI();
|
|
288
|
+
let response = void 0;
|
|
289
|
+
if (this.customerConfig.OIDC.client.enabled) {
|
|
290
|
+
const token = await this.manageToken();
|
|
291
|
+
headers = {
|
|
292
|
+
...headers,
|
|
293
|
+
Authorization: `Bearer ${token}`
|
|
294
|
+
};
|
|
295
|
+
this.apiClient.defaults.headers.common["Authorization"] = `Bearer ${token}`;
|
|
306
296
|
}
|
|
297
|
+
response = await this.apiClient.request({
|
|
298
|
+
url,
|
|
299
|
+
method,
|
|
300
|
+
data: body,
|
|
301
|
+
params,
|
|
302
|
+
headers
|
|
303
|
+
});
|
|
304
|
+
if (response) {
|
|
305
|
+
return response.data;
|
|
306
|
+
}
|
|
307
|
+
throw new Error("API.execute error: Failed to retrieve a valid response");
|
|
308
|
+
} catch (error) {
|
|
309
|
+
this.logger.log({
|
|
310
|
+
level: "error",
|
|
311
|
+
message: `CrexAPI.execute error: ${error}`
|
|
312
|
+
});
|
|
313
|
+
throw error;
|
|
307
314
|
}
|
|
308
|
-
if (response) {
|
|
309
|
-
return response.data;
|
|
310
|
-
}
|
|
311
|
-
throw new Error("API.execute error: Failed to retrieve a valid response");
|
|
312
315
|
}
|
|
313
316
|
};
|
|
314
317
|
|
|
@@ -338,10 +341,15 @@ import { clsx } from "clsx";
|
|
|
338
341
|
import { twMerge } from "tailwind-merge";
|
|
339
342
|
|
|
340
343
|
// ../utils/src/params.ts
|
|
341
|
-
var createParams = (fieldsList, key = "Fields") =>
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
}
|
|
344
|
+
var createParams = (fieldsList, key = "Fields") => {
|
|
345
|
+
if (!fieldsList || fieldsList.length === 0) {
|
|
346
|
+
return [];
|
|
347
|
+
}
|
|
348
|
+
return fieldsList.map((item) => ({
|
|
349
|
+
key,
|
|
350
|
+
value: item
|
|
351
|
+
}));
|
|
352
|
+
};
|
|
345
353
|
var generateQueryParams = (params) => {
|
|
346
354
|
const queryParams = params.map((param) => `${encodeURIComponent(param.key)}=${encodeURIComponent(param.value)}`).join("&");
|
|
347
355
|
return queryParams;
|
|
@@ -489,12 +497,11 @@ var DirectoryNodesService = class extends BaseService {
|
|
|
489
497
|
};
|
|
490
498
|
|
|
491
499
|
// src/transforms/documentTypes.ts
|
|
492
|
-
import {
|
|
493
|
-
|
|
494
|
-
var transformDocumentTypes = (data) => {
|
|
500
|
+
import { cookies as cookies3 } from "next/headers";
|
|
501
|
+
var transformDocumentTypes = async (data) => {
|
|
495
502
|
const labels = [];
|
|
496
|
-
const config =
|
|
497
|
-
const contentLanguage =
|
|
503
|
+
const config = getClientConfig();
|
|
504
|
+
const contentLanguage = cookies3().get(CONTENT_LANG_KEY)?.value || config.languageSwitcher.default;
|
|
498
505
|
const language = contentLanguage.split("-")[0];
|
|
499
506
|
data.items.forEach((documentItem) => {
|
|
500
507
|
const label = documentItem.labels.find((item) => item.language.toLowerCase() === language.toLowerCase());
|
|
@@ -529,13 +536,12 @@ var DocumentTypesService = class extends BaseService {
|
|
|
529
536
|
};
|
|
530
537
|
|
|
531
538
|
// src/transforms/information.ts
|
|
532
|
-
import {
|
|
533
|
-
import { cookies as cookies3 } from "next/headers";
|
|
539
|
+
import { cookies as cookies4 } from "next/headers";
|
|
534
540
|
import { CrexLogger as CrexLogger2 } from "@c-rex/core/logger";
|
|
535
541
|
var transformInformationUnits = async (data) => {
|
|
536
542
|
const logger = new CrexLogger2();
|
|
537
|
-
const frontEndConfig =
|
|
538
|
-
const backEndConfig =
|
|
543
|
+
const frontEndConfig = getClientConfig();
|
|
544
|
+
const backEndConfig = getServerConfig();
|
|
539
545
|
const filteredTags = {};
|
|
540
546
|
const items = data.items.map((item) => {
|
|
541
547
|
const type = item.class.labels.filter((item2) => item2.language === EN_LANG)[0].value.toUpperCase();
|
|
@@ -547,34 +553,34 @@ var transformInformationUnits = async (data) => {
|
|
|
547
553
|
link = `/documents/${item.shortId}`;
|
|
548
554
|
}
|
|
549
555
|
let title = "NO TITLE";
|
|
556
|
+
if (item.titles) {
|
|
557
|
+
title = item.titles[0].value;
|
|
558
|
+
} else if (item.labels) {
|
|
559
|
+
title = item.labels[0].value;
|
|
560
|
+
}
|
|
550
561
|
let language = "NO LANGUAGE";
|
|
551
|
-
|
|
552
|
-
|
|
553
|
-
|
|
554
|
-
|
|
555
|
-
|
|
556
|
-
|
|
557
|
-
language = item.labels[0].language;
|
|
558
|
-
}
|
|
559
|
-
} catch {
|
|
560
|
-
logger.log({
|
|
561
|
-
level: "error",
|
|
562
|
-
message: `No label or title on item ${item.shortId}`
|
|
563
|
-
});
|
|
562
|
+
if (item.languages.length > 0) {
|
|
563
|
+
language = item.languages[0];
|
|
564
|
+
}
|
|
565
|
+
let versionOf = [];
|
|
566
|
+
if (item?.versionOf && item.versionOf?.labels) {
|
|
567
|
+
versionOf = item.versionOf.labels.map((item2) => item2.language).filter((value) => value !== void 0);
|
|
564
568
|
}
|
|
565
569
|
return {
|
|
566
570
|
language,
|
|
567
571
|
title,
|
|
568
572
|
type,
|
|
569
573
|
localeType: "",
|
|
574
|
+
revision: item.revision,
|
|
570
575
|
shortId: item.shortId,
|
|
576
|
+
multipleVersions: versionOf,
|
|
571
577
|
disabled: frontEndConfig.results.disabledResults.includes(type),
|
|
572
578
|
link,
|
|
573
579
|
files
|
|
574
580
|
};
|
|
575
581
|
});
|
|
576
582
|
if (data.tags) {
|
|
577
|
-
const contentLang = (
|
|
583
|
+
const contentLang = (cookies4().get(CONTENT_LANG_KEY)?.value || EN_LANG).toLowerCase();
|
|
578
584
|
const splittedContentLang = contentLang.split("-")[0];
|
|
579
585
|
for (const [key, value] of Object.entries(data.tags)) {
|
|
580
586
|
if (!value || !value.items || value.items.length === 0) {
|
|
@@ -644,8 +650,10 @@ var transformSuggestions = (data, query) => {
|
|
|
644
650
|
|
|
645
651
|
// src/informationUnits.ts
|
|
646
652
|
var InformationUnitsService = class extends BaseService {
|
|
647
|
-
|
|
653
|
+
directoryNodesService;
|
|
654
|
+
constructor(directoryNodesService) {
|
|
648
655
|
super("InformationUnits/");
|
|
656
|
+
this.directoryNodesService = directoryNodesService || new DirectoryNodesService();
|
|
649
657
|
}
|
|
650
658
|
/**
|
|
651
659
|
* Retrieves a list of information units based on specified criteria.
|
|
@@ -675,7 +683,6 @@ var InformationUnitsService = class extends BaseService {
|
|
|
675
683
|
const remainFilters = createParams(filters, "Filter");
|
|
676
684
|
const restrictions = createParams(restrict, "Restrict");
|
|
677
685
|
const remainTags = createParams(tags, "Tags");
|
|
678
|
-
const languageParam = `VALUES ?lang { ${languages.map((lang) => `"${lang}"`).join(" ")} } ?s iirds:language ?lang .`;
|
|
679
686
|
const params = [
|
|
680
687
|
{ key: "pageSize", value: "12" },
|
|
681
688
|
{ key: "wildcard", value: wildcard.toLowerCase() },
|
|
@@ -688,6 +695,7 @@ var InformationUnitsService = class extends BaseService {
|
|
|
688
695
|
...restrictions
|
|
689
696
|
];
|
|
690
697
|
if (languages.length > 0) {
|
|
698
|
+
const languageParam = `VALUES ?lang { ${languages.map((lang) => `"${lang}"`).join(" ")} } ?s iirds:language ?lang .`;
|
|
691
699
|
params.push({ key: "sparqlWhere", value: languageParam });
|
|
692
700
|
}
|
|
693
701
|
if (queries.length > 0) {
|
|
@@ -748,13 +756,39 @@ var InformationUnitsService = class extends BaseService {
|
|
|
748
756
|
transformer: (data) => transformSuggestions(data, query)
|
|
749
757
|
});
|
|
750
758
|
}
|
|
759
|
+
async getDocumentInfo({ shortId }) {
|
|
760
|
+
const response = await this.getItem({
|
|
761
|
+
id: shortId,
|
|
762
|
+
shouldGetAllFields: true
|
|
763
|
+
});
|
|
764
|
+
const directoryNodes = response.directoryNodes;
|
|
765
|
+
if (directoryNodes.length === 0 || !directoryNodes[0]) {
|
|
766
|
+
return {};
|
|
767
|
+
}
|
|
768
|
+
let id = directoryNodes[0].shortId;
|
|
769
|
+
let node = await this.directoryNodesService.getItem(id);
|
|
770
|
+
if (!node.ancestors || node.ancestors.length === 0) return {};
|
|
771
|
+
const hasInfo = node.informationUnits?.[0];
|
|
772
|
+
const hasLabel = node.labels?.[0];
|
|
773
|
+
if (!hasInfo || !hasLabel) return {};
|
|
774
|
+
const lastIndex = node.ancestors[0]?.length - 1;
|
|
775
|
+
const ancestorNode = node.ancestors[0][lastIndex];
|
|
776
|
+
if (!ancestorNode?.shortId) return {};
|
|
777
|
+
id = ancestorNode.shortId;
|
|
778
|
+
node = await this.directoryNodesService.getItem(id);
|
|
779
|
+
const ancestorInfoUnit = node.informationUnits?.[0];
|
|
780
|
+
const ancestorLabel = ancestorInfoUnit?.labels?.[0];
|
|
781
|
+
return {
|
|
782
|
+
documentId: ancestorInfoUnit?.shortId,
|
|
783
|
+
label: ancestorLabel?.value || ""
|
|
784
|
+
};
|
|
785
|
+
}
|
|
751
786
|
};
|
|
752
787
|
|
|
753
788
|
// src/language.ts
|
|
754
|
-
import { getClientConfigs as getClientConfigs4 } from "@c-rex/utils/next-cookies";
|
|
755
789
|
var LanguageService = class extends BaseService {
|
|
756
790
|
constructor() {
|
|
757
|
-
const configs =
|
|
791
|
+
const configs = getClientConfig();
|
|
758
792
|
super(configs.languageSwitcher.endpoint);
|
|
759
793
|
}
|
|
760
794
|
/**
|
|
@@ -791,13 +825,12 @@ var LanguageService = class extends BaseService {
|
|
|
791
825
|
};
|
|
792
826
|
|
|
793
827
|
// src/transforms/topics.ts
|
|
794
|
-
import {
|
|
795
|
-
import { cookies as cookies4 } from "next/headers";
|
|
828
|
+
import { cookies as cookies5 } from "next/headers";
|
|
796
829
|
import { CrexLogger as CrexLogger3 } from "@c-rex/core/logger";
|
|
797
830
|
var transformTopics = async (data) => {
|
|
798
831
|
const logger = new CrexLogger3();
|
|
799
|
-
const config =
|
|
800
|
-
const uiLang = (
|
|
832
|
+
const config = getClientConfig();
|
|
833
|
+
const uiLang = (cookies5().get(UI_LANG_KEY)?.value || config.languageSwitcher.default).toLowerCase();
|
|
801
834
|
const items = data.items.map((item) => {
|
|
802
835
|
const type = item.class.labels.filter((item2) => item2.language === EN_LANG)[0].value.toUpperCase();
|
|
803
836
|
let link = `/topics/${item.shortId}`;
|