@c-rex/services 0.1.7 → 0.1.9

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 CHANGED
@@ -8,12 +8,17 @@ declare const WILD_CARD_OPTIONS: {
8
8
  readonly START: "START";
9
9
  readonly NONE: "NONE";
10
10
  };
11
+ declare const OPERATOR_OPTIONS: {
12
+ readonly AND: "AND";
13
+ readonly OR: "OR";
14
+ };
11
15
 
12
16
  type QueryParams = {
13
17
  key: string;
14
18
  value: string;
15
19
  };
16
20
  type WildCardType = keyof typeof WILD_CARD_OPTIONS;
21
+ type OperatorType = keyof typeof OPERATOR_OPTIONS;
17
22
 
18
23
  /**
19
24
  * Base service class that provides common functionality for API interactions.
@@ -130,15 +135,16 @@ declare class InformationUnitsService extends BaseService {
130
135
  * @returns A promise that resolves to the information units response
131
136
  * @throws Error if the API request fails
132
137
  */
133
- getList({ queries, page, fields, filters, restrict, languages, wildcard, operator, like, }: {
138
+ getList({ queries, page, fields, filters, tags, restrict, languages, wildcard, operator, like, }: {
134
139
  queries?: string;
135
140
  page?: number;
136
141
  filters?: string[];
137
142
  restrict?: string[];
138
143
  fields?: string[];
144
+ tags?: string[];
139
145
  languages?: string[];
140
146
  wildcard?: WildCardType;
141
- operator?: string;
147
+ operator?: OperatorType;
142
148
  like?: boolean;
143
149
  }): Promise<informationUnitsResponse>;
144
150
  /**
@@ -150,8 +156,9 @@ declare class InformationUnitsService extends BaseService {
150
156
  * @returns A promise that resolves to the information unit data
151
157
  * @throws Error if the API request fails
152
158
  */
153
- getItem({ id }: {
159
+ getItem({ id, shouldGetAllFields }: {
154
160
  id: string;
161
+ shouldGetAllFields?: boolean;
155
162
  }): Promise<informationUnitsItems>;
156
163
  /**
157
164
  * Retrieves autocomplete suggestions based on a query prefix.
@@ -191,7 +198,7 @@ type getListProps = {
191
198
  fields?: string[];
192
199
  languages?: string[];
193
200
  wildcard?: WildCardType;
194
- operator?: string;
201
+ operator?: OperatorType;
195
202
  like?: boolean;
196
203
  pageSize?: number;
197
204
  };
package/dist/index.d.ts CHANGED
@@ -8,12 +8,17 @@ declare const WILD_CARD_OPTIONS: {
8
8
  readonly START: "START";
9
9
  readonly NONE: "NONE";
10
10
  };
11
+ declare const OPERATOR_OPTIONS: {
12
+ readonly AND: "AND";
13
+ readonly OR: "OR";
14
+ };
11
15
 
12
16
  type QueryParams = {
13
17
  key: string;
14
18
  value: string;
15
19
  };
16
20
  type WildCardType = keyof typeof WILD_CARD_OPTIONS;
21
+ type OperatorType = keyof typeof OPERATOR_OPTIONS;
17
22
 
18
23
  /**
19
24
  * Base service class that provides common functionality for API interactions.
@@ -130,15 +135,16 @@ declare class InformationUnitsService extends BaseService {
130
135
  * @returns A promise that resolves to the information units response
131
136
  * @throws Error if the API request fails
132
137
  */
133
- getList({ queries, page, fields, filters, restrict, languages, wildcard, operator, like, }: {
138
+ getList({ queries, page, fields, filters, tags, restrict, languages, wildcard, operator, like, }: {
134
139
  queries?: string;
135
140
  page?: number;
136
141
  filters?: string[];
137
142
  restrict?: string[];
138
143
  fields?: string[];
144
+ tags?: string[];
139
145
  languages?: string[];
140
146
  wildcard?: WildCardType;
141
- operator?: string;
147
+ operator?: OperatorType;
142
148
  like?: boolean;
143
149
  }): Promise<informationUnitsResponse>;
144
150
  /**
@@ -150,8 +156,9 @@ declare class InformationUnitsService extends BaseService {
150
156
  * @returns A promise that resolves to the information unit data
151
157
  * @throws Error if the API request fails
152
158
  */
153
- getItem({ id }: {
159
+ getItem({ id, shouldGetAllFields }: {
154
160
  id: string;
161
+ shouldGetAllFields?: boolean;
155
162
  }): Promise<informationUnitsItems>;
156
163
  /**
157
164
  * Retrieves autocomplete suggestions based on a query prefix.
@@ -191,7 +198,7 @@ type getListProps = {
191
198
  fields?: string[];
192
199
  languages?: string[];
193
200
  wildcard?: WildCardType;
194
- operator?: string;
201
+ operator?: OperatorType;
195
202
  like?: boolean;
196
203
  pageSize?: number;
197
204
  };
package/dist/index.js CHANGED
@@ -60,6 +60,7 @@ var API = {
60
60
  };
61
61
  var SDK_CONFIG_KEY = "crex-sdk-config";
62
62
  var CONTENT_LANG_KEY = "CONTENT_LANG_KEY";
63
+ var UI_LANG_KEY = "UI_LANG_KEY";
63
64
  var FLAGS_BY_LANG = {
64
65
  "en": "US",
65
66
  "de": "DE"
@@ -260,7 +261,7 @@ var CrexApi = class {
260
261
  }
261
262
  async getToken() {
262
263
  try {
263
- const response = await fetch(`${process.env.NEXT_PUBLIC_API_URL}/api/token`, {
264
+ const response = await fetch(`${this.customerConfig.publicNextApiUrl}/api/token`, {
264
265
  method: "POST",
265
266
  credentials: "include"
266
267
  });
@@ -289,7 +290,7 @@ var CrexApi = class {
289
290
  } catch (error) {
290
291
  this.logger.log({
291
292
  level: "error",
292
- message: `utils.manageToken error: ${error}`
293
+ message: `CrexAPI.manageToken error: ${error}`
293
294
  });
294
295
  throw error;
295
296
  }
@@ -351,13 +352,13 @@ var CrexApi = class {
351
352
  };
352
353
 
353
354
  // ../utils/src/utils.ts
354
- var _generateShaKey = async (input) => {
355
- const encoder = new TextEncoder();
356
- const data = encoder.encode(input);
357
- const hashBuffer = await crypto.subtle.digest("SHA-1", data);
358
- const hashArray = Array.from(new Uint8Array(hashBuffer));
359
- const base64url = btoa(String.fromCharCode(...hashArray)).replace(/\+/g, "-").replace(/\//g, "_").replace(/=+$/, "");
360
- return base64url.slice(0, 12);
355
+ var _generateShaKey = (input) => {
356
+ let hash = 0;
357
+ for (let i = 0; i < input.length; i++) {
358
+ hash = (hash << 5) - hash + input.charCodeAt(i);
359
+ hash |= 0;
360
+ }
361
+ return Math.abs(hash).toString(16);
361
362
  };
362
363
  var getCountryCodeByLang = (lang) => {
363
364
  const mappedKeys = Object.keys(FLAGS_BY_LANG);
@@ -379,9 +380,13 @@ var formatDateToLocale = (date, locale) => {
379
380
  }).format(dateAux);
380
381
  };
381
382
 
383
+ // ../utils/src/classMerge.ts
384
+ var import_clsx = require("clsx");
385
+ var import_tailwind_merge = require("tailwind-merge");
386
+
382
387
  // ../utils/src/call.ts
383
388
  var call = async (method, params) => {
384
- const shaKey = await _generateShaKey(JSON.stringify({ method, params }));
389
+ const shaKey = _generateShaKey(JSON.stringify({ method, params }));
385
390
  const cache = localStorage.getItem(shaKey);
386
391
  if (cache !== null) {
387
392
  const { data, expireDate } = JSON.parse(cache);
@@ -391,7 +396,7 @@ var call = async (method, params) => {
391
396
  localStorage.removeItem(shaKey);
392
397
  }
393
398
  }
394
- const res = await fetch(`${process.env.NEXT_PUBLIC_API_URL}/api/rpc`, {
399
+ const res = await fetch(`/api/rpc`, {
395
400
  method: "POST",
396
401
  headers: { "Content-Type": "application/json" },
397
402
  body: JSON.stringify({ method, params }),
@@ -408,57 +413,40 @@ var call = async (method, params) => {
408
413
  return json.data;
409
414
  };
410
415
 
411
- // ../utils/src/classMerge.ts
412
- var import_clsx = require("clsx");
413
- var import_tailwind_merge = require("tailwind-merge");
414
-
415
416
  // ../utils/src/params.ts
416
417
  var createParams = (fieldsList, key = "Fields") => fieldsList.map((item) => ({
417
418
  key,
418
419
  value: item
419
420
  }));
420
421
  var generateQueryParams = (params) => {
421
- const queryParams = params.map(
422
- (param) => `${encodeURIComponent(param.key)}=${encodeURIComponent(param.value)}`
423
- ).join("&");
422
+ const queryParams = params.map((param) => `${encodeURIComponent(param.key)}=${encodeURIComponent(param.value)}`).join("&");
424
423
  return queryParams;
425
424
  };
426
425
 
427
426
  // ../utils/src/renditions.ts
428
427
  var getFileRenditions = ({ renditions }) => {
429
428
  if (renditions == void 0 || renditions.length == 0) {
430
- return {
431
- filesToDownload: [],
432
- filesToOpen: []
433
- };
429
+ return {};
434
430
  }
435
431
  const filteredRenditions = renditions.filter(
436
432
  (item) => item.format != "application/xhtml+xml" && item.format != "application/json" && item.format != "application/llm+xml"
437
433
  );
438
434
  if (filteredRenditions.length == 0 || filteredRenditions[0] == void 0) {
439
- return {
440
- filesToDownload: [],
441
- filesToOpen: []
442
- };
443
- }
444
- const filesToDownload = filteredRenditions.map((item) => {
445
- const filteredLinks = item.links.filter((item2) => item2.rel == "download");
446
- return {
447
- format: item.format,
448
- link: filteredLinks[0].href
449
- };
450
- });
451
- const filesToOpen = filteredRenditions.map((item) => {
452
- const filteredLinks = item.links.filter((item2) => item2.rel == "view");
453
- return {
454
- format: item.format,
455
- link: filteredLinks[0].href
456
- };
435
+ return {};
436
+ }
437
+ const result = {};
438
+ filteredRenditions.forEach((item) => {
439
+ const key = item.format;
440
+ if (result[key] == void 0) {
441
+ result[key] = {
442
+ view: "",
443
+ download: ""
444
+ };
445
+ }
446
+ result[key].download = item.links.filter((link) => link.rel == "download")[0]?.href;
447
+ result[key].view = item.links.filter((link) => link.rel == "view")[0]?.href;
457
448
  });
458
- return {
459
- filesToDownload,
460
- filesToOpen
461
- };
449
+ return result;
462
450
  };
463
451
 
464
452
  // src/baseService.ts
@@ -634,7 +622,7 @@ var transformInformationUnits = async (data) => {
634
622
  const filteredTags = {};
635
623
  const items = data.items.map((item) => {
636
624
  const type = item.class.labels.filter((item2) => item2.language === EN_LANG)[0].value.toUpperCase();
637
- const { filesToOpen, filesToDownload } = getFileRenditions({ renditions: item?.renditions });
625
+ const files = getFileRenditions({ renditions: item?.renditions });
638
626
  let link = `/topics/${item.shortId}`;
639
627
  if (config.results.articlePageLayout == "BLOG") {
640
628
  link = `/blog/${item.shortId}`;
@@ -665,8 +653,7 @@ var transformInformationUnits = async (data) => {
665
653
  shortId: item.shortId,
666
654
  disabled: config.results.disabledResults.includes(type),
667
655
  link,
668
- filesToOpen,
669
- filesToDownload
656
+ files
670
657
  };
671
658
  });
672
659
  if (data.tags) {
@@ -760,6 +747,7 @@ var InformationUnitsService = class extends BaseService {
760
747
  page = 1,
761
748
  fields = [],
762
749
  filters = [],
750
+ tags = [],
763
751
  restrict = [],
764
752
  languages = [],
765
753
  wildcard = WILD_CARD_OPTIONS.BOTH,
@@ -769,15 +757,15 @@ var InformationUnitsService = class extends BaseService {
769
757
  const remainFields = createParams(fields, "Fields");
770
758
  const remainFilters = createParams(filters, "Filter");
771
759
  const restrictions = createParams(restrict, "Restrict");
760
+ const remainTags = createParams(tags, "Tags");
772
761
  const languageParam = `VALUES ?lang { ${languages.map((lang) => `"${lang}"`).join(" ")} } ?s iirds:language ?lang .`;
773
762
  const params = [
774
763
  { key: "pageSize", value: "12" },
775
764
  { key: "wildcard", value: wildcard.toLowerCase() },
776
765
  { key: "PageNumber", value: page.toString() },
777
766
  { key: "Operator", value: operator },
778
- { key: "tags", value: "true" },
779
- // TODO use customer config values and add a param to each one
780
767
  { key: "Like", value: like.toString() },
768
+ ...remainTags,
781
769
  ...remainFields,
782
770
  ...remainFilters,
783
771
  ...restrictions
@@ -804,18 +792,21 @@ var InformationUnitsService = class extends BaseService {
804
792
  * @returns A promise that resolves to the information unit data
805
793
  * @throws Error if the API request fails
806
794
  */
807
- async getItem({ id }) {
808
- const params = [
809
- { key: "Fields", value: "renditions" },
810
- { key: "Fields", value: "directoryNodes" },
811
- { key: "Fields", value: "versionOf" },
812
- { key: "Fields", value: "titles" },
813
- { key: "Fields", value: "languages" },
814
- { key: "Fields", value: "labels" },
815
- { key: "Fields", value: "packages" },
816
- { key: "Fields", value: "created" },
817
- { key: "Fields", value: "revision" }
818
- ];
795
+ async getItem({ id, shouldGetAllFields = false }) {
796
+ const params = [];
797
+ if (!shouldGetAllFields) {
798
+ params.push(
799
+ { key: "Fields", value: "renditions" },
800
+ { key: "Fields", value: "directoryNodes" },
801
+ { key: "Fields", value: "versionOf" },
802
+ { key: "Fields", value: "titles" },
803
+ { key: "Fields", value: "languages" },
804
+ { key: "Fields", value: "labels" },
805
+ { key: "Fields", value: "packages" },
806
+ { key: "Fields", value: "created" },
807
+ { key: "Fields", value: "revision" }
808
+ );
809
+ }
819
810
  return await this.request({
820
811
  path: id,
821
812
  params
@@ -889,7 +880,7 @@ var import_logger3 = require("@c-rex/core/logger");
889
880
  var transformTopics = async (data) => {
890
881
  const logger = new import_logger3.CrexLogger();
891
882
  const config = (0, import_next_cookies5.getConfigs)();
892
- const contentLang = ((0, import_headers4.cookies)().get(CONTENT_LANG_KEY)?.value || config.languageSwitcher.default).toLowerCase();
883
+ const uiLang = ((0, import_headers4.cookies)().get(UI_LANG_KEY)?.value || config.languageSwitcher.default).toLowerCase();
893
884
  const items = data.items.map((item) => {
894
885
  const type = item.class.labels.filter((item2) => item2.language === EN_LANG)[0].value.toUpperCase();
895
886
  let link = `/topics/${item.shortId}`;
@@ -926,7 +917,7 @@ var transformTopics = async (data) => {
926
917
  ;
927
918
  let categoryType = type;
928
919
  if (item.applicableForTypes && item.applicableForTypes.length > 0) {
929
- const splittedContentLang = contentLang.split("-")[0];
920
+ const splittedContentLang = uiLang.split("-")[0];
930
921
  categoryType = item.applicableForTypes[0].labels.find((item2) => item2.language === splittedContentLang)?.value || categoryType;
931
922
  }
932
923
  return {
@@ -935,7 +926,7 @@ var transformTopics = async (data) => {
935
926
  type: categoryType,
936
927
  localeType: "",
937
928
  shortId: item.shortId,
938
- created: formatDateToLocale(item.created, contentLang),
929
+ created: formatDateToLocale(item.created, uiLang),
939
930
  disabled: config.results.disabledResults.includes(type),
940
931
  link,
941
932
  renditionUrl,
@@ -979,21 +970,17 @@ var TopicsService = class extends BaseService {
979
970
  }) {
980
971
  const remainFields = createParams(fields, "Fields");
981
972
  const remainFilters = createParams(filters, "Filter");
982
- const languageParam = `VALUES ?lang { ${languages.map((lang) => `"${lang}"`).join(" ")} } ?s iirds:language ?lang .`;
983
973
  const params = [
984
974
  { key: "pageSize", value: pageSize.toString() },
985
975
  { key: "PageNumber", value: page.toString() },
986
976
  { key: "Sort", value: "-created;-score" },
977
+ { key: "Operator", value: operator },
978
+ { key: "wildcard", value: wildcard.toLowerCase() },
987
979
  ...remainFields,
988
980
  ...remainFilters
989
981
  ];
990
- if (wildcard.length > 0) {
991
- params.push({ key: "Operator", value: operator });
992
- }
993
- if (wildcard.length > 0) {
994
- params.push({ key: "wildcard", value: wildcard.toLowerCase() });
995
- }
996
982
  if (languages.length > 0) {
983
+ const languageParam = `VALUES ?lang { ${languages.map((lang) => `"${lang}"`).join(" ")} } ?s iirds:language ?lang .`;
997
984
  params.push({ key: "sparqlWhere", value: languageParam });
998
985
  }
999
986
  if (queries.length > 0) {