@c-rex/services 0.0.4 → 0.0.6

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/package.json CHANGED
@@ -1,27 +1,24 @@
1
1
  {
2
2
  "name": "@c-rex/services",
3
- "version": "0.0.4",
4
- "main": "dist/index.js",
5
- "module": "dist/index.mjs",
6
- "types": "dist/index.d.ts",
3
+ "version": "0.0.6",
7
4
  "files": [
8
- "dist"
5
+ "src"
9
6
  ],
10
7
  "publishConfig": {
11
8
  "access": "public"
12
9
  },
13
10
  "exports": {
14
11
  ".": {
15
- "types": "./dist/index.d.ts",
16
- "import": "./dist/index.mjs",
17
- "require": "./dist/index.js",
18
- "default": "./dist/index.js"
12
+ "types": "./src/index.ts",
13
+ "import": "./src/index.ts",
14
+ "require": "./src/index.ts",
15
+ "default": "./src/index.ts"
19
16
  },
20
17
  "./package.json": "./package.json"
21
18
  },
22
19
  "scripts": {
23
- "dev": "tsup src/index.ts --format cjs,esm --dts --watch",
24
- "build": "tsup src/index.ts --format cjs,esm --dts",
20
+ "dev": "echo 'Nothing to build using raw TypeScript files'",
21
+ "build": "echo 'No build needed'",
25
22
  "test:watch": "jest --watch",
26
23
  "test": "jest"
27
24
  },
@@ -0,0 +1,98 @@
1
+ import { BaseService } from '../baseService';
2
+ import { CrexSDK } from '@c-rex/core';
3
+ import { QueryParams } from '@c-rex/types';
4
+
5
+ // Mock CrexSDK
6
+ jest.mock('@c-rex/core', () => ({
7
+ CrexSDK: {
8
+ getInstance: jest.fn().mockReturnValue({
9
+ api: {
10
+ execute: jest.fn()
11
+ },
12
+ logger: {
13
+ log: jest.fn()
14
+ }
15
+ })
16
+ }
17
+ }));
18
+
19
+ describe('BaseService', () => {
20
+ let service: BaseService;
21
+ const mockEndpoint = '/api/test';
22
+
23
+ beforeEach(() => {
24
+ jest.clearAllMocks();
25
+ service = new BaseService(mockEndpoint);
26
+ });
27
+
28
+ describe('request', () => {
29
+ it('should make a basic GET request', async () => {
30
+ const mockResponse = { data: { id: 1 } };
31
+ const sdk = CrexSDK.getInstance();
32
+ (sdk.api.execute as jest.Mock).mockResolvedValue(mockResponse);
33
+
34
+ const result = await service['request']({});
35
+
36
+ expect(sdk.api.execute).toHaveBeenCalledWith({
37
+ url: mockEndpoint,
38
+ method: 'get',
39
+ headers: {}
40
+ });
41
+ expect(result).toEqual(mockResponse.data);
42
+ });
43
+
44
+ it('should handle query parameters', async () => {
45
+ const params: QueryParams[] = [
46
+ { key: 'search', value: 'test' }
47
+ ];
48
+ const mockResponse = { data: { id: 1 } };
49
+ const sdk = CrexSDK.getInstance();
50
+ (sdk.api.execute as jest.Mock).mockResolvedValue(mockResponse);
51
+
52
+ await service['request']({ params });
53
+
54
+ expect(sdk.api.execute).toHaveBeenCalledWith({
55
+ url: `${mockEndpoint}?search=test`,
56
+ method: 'get',
57
+ headers: {}
58
+ });
59
+ });
60
+
61
+ it('should use custom transformer', async () => {
62
+ const mockResponse = { data: { id: 1 } };
63
+ const transformer = (response: any) => ({ transformed: response.data });
64
+ const sdk = CrexSDK.getInstance();
65
+ (sdk.api.execute as jest.Mock).mockResolvedValue(mockResponse);
66
+
67
+ const result = await service['request']({ transformer });
68
+
69
+ expect(result).toEqual({ transformed: mockResponse.data });
70
+ });
71
+
72
+ it('should handle errors', async () => {
73
+ const error = new Error('API Error');
74
+ const sdk = CrexSDK.getInstance();
75
+ (sdk.api.execute as jest.Mock).mockRejectedValue(error);
76
+
77
+ await expect(service['request']({})).rejects.toThrow('API Error');
78
+ expect(sdk.logger.log).toHaveBeenCalledWith(
79
+ 'error',
80
+ expect.stringContaining('API Error')
81
+ );
82
+ });
83
+
84
+ it('should support different HTTP methods', async () => {
85
+ const mockResponse = { data: { id: 1 } };
86
+ const sdk = CrexSDK.getInstance();
87
+ (sdk.api.execute as jest.Mock).mockResolvedValue(mockResponse);
88
+
89
+ await service['request']({ method: 'post' });
90
+
91
+ expect(sdk.api.execute).toHaveBeenCalledWith({
92
+ url: mockEndpoint,
93
+ method: 'post',
94
+ headers: {}
95
+ });
96
+ });
97
+ });
98
+ });
@@ -0,0 +1,84 @@
1
+ import { DirectoryNodesService } from '../';
2
+ import { CrexSDK } from '@c-rex/core';
3
+ import { informationUnitsDirectories } from '@c-rex/interfaces';
4
+
5
+ jest.mock('@c-rex/core', () => ({
6
+ CrexSDK: {
7
+ getInstance: jest.fn().mockReturnValue({
8
+ api: {
9
+ execute: jest.fn()
10
+ },
11
+ logger: {
12
+ log: jest.fn()
13
+ }
14
+ })
15
+ }
16
+ }));
17
+
18
+ describe('DirectoryNodesService', () => {
19
+ let service: DirectoryNodesService;
20
+
21
+ const LABELS = {
22
+ language: "en",
23
+ value: "test",
24
+ }
25
+ const idShortID = {
26
+ id: "123",
27
+ shortId: "123"
28
+ }
29
+ const DefaultLinksRequest = {
30
+ rel: "rel",
31
+ href: "href",
32
+ method: "method",
33
+ }
34
+ const ITEM: informationUnitsDirectories = {
35
+ labels: [LABELS],
36
+ links: [DefaultLinksRequest],
37
+ class: {
38
+ labels: [LABELS],
39
+ ...idShortID
40
+ },
41
+ ...idShortID
42
+ };
43
+ const mockDirectoryNode = {
44
+ childNodes: [ITEM],
45
+ parents: [idShortID],
46
+ informationUnits: [idShortID],
47
+ ...ITEM,
48
+ };
49
+
50
+ beforeEach(() => {
51
+ jest.clearAllMocks();
52
+ service = new DirectoryNodesService();
53
+ });
54
+
55
+ describe('getItem', () => {
56
+ it('should fetch a directory node by id', async () => {
57
+ const nodeId = '123';
58
+ const sdk = CrexSDK.getInstance();
59
+ (sdk.api.execute as jest.Mock).mockResolvedValue({ data: mockDirectoryNode });
60
+
61
+ const result = await service.getItem(nodeId);
62
+
63
+ expect(sdk.api.execute).toHaveBeenCalledWith({
64
+ url: `DirectoryNodes/${nodeId}`,
65
+ method: 'get',
66
+ headers: {}
67
+ });
68
+ expect(result).toEqual(mockDirectoryNode);
69
+ });
70
+
71
+ it('should handle errors when fetching directory node', async () => {
72
+ const nodeId = '123';
73
+ const error = new Error('Failed to fetch directory node');
74
+ const sdk = CrexSDK.getInstance();
75
+ (sdk.api.execute as jest.Mock).mockRejectedValue(error);
76
+
77
+ await expect(service.getItem(nodeId)).rejects.toThrow('Failed to fetch directory node');
78
+ expect(sdk.logger.log).toHaveBeenCalledWith(
79
+ 'error',
80
+ expect.stringContaining('Failed to fetch directory node')
81
+ );
82
+ });
83
+ });
84
+ });
@@ -0,0 +1,75 @@
1
+ import { DocumentTypesService } from '../';
2
+ import { CrexSDK } from '@c-rex/core';
3
+ import { createParams, generateQueryParams } from '@c-rex/utils';
4
+ import { transformDocumentTypes } from '../transforms/documentTypes';
5
+
6
+ jest.mock('@c-rex/core', () => ({
7
+ CrexSDK: {
8
+ getInstance: jest.fn().mockReturnValue({
9
+ api: {
10
+ execute: jest.fn()
11
+ },
12
+ logger: {
13
+ log: jest.fn()
14
+ }
15
+ })
16
+ }
17
+ }));
18
+
19
+ jest.mock('@c-rex/utils', () => ({
20
+ createParams: jest.fn(),
21
+ generateQueryParams: jest.fn()
22
+ }));
23
+
24
+ jest.mock('../transforms/documentTypes', () => ({
25
+ transformDocumentTypes: jest.fn()
26
+ }));
27
+
28
+ describe('DocumentTypesService', () => {
29
+ let service: DocumentTypesService;
30
+ const mockLabels = ['label1', 'label2'];
31
+
32
+ beforeEach(() => {
33
+ jest.clearAllMocks();
34
+ service = new DocumentTypesService();
35
+ });
36
+
37
+ describe('getLabels', () => {
38
+ it('should fetch document type labels with correct parameters', async () => {
39
+ const fields = ['field1', 'field2'];
40
+ const sdk = CrexSDK.getInstance();
41
+
42
+ (createParams as jest.Mock).mockReturnValue([]);
43
+ (generateQueryParams as jest.Mock).mockReturnValue("mockedParams=true");
44
+ (transformDocumentTypes as jest.Mock).mockReturnValue(mockLabels);
45
+ (sdk.api.execute as jest.Mock).mockResolvedValue({ data: mockLabels });
46
+
47
+ const result = await service.getLabels(fields);
48
+
49
+ expect(createParams).toHaveBeenCalledWith(fields, 'Fields');
50
+
51
+ expect(sdk.api.execute).toHaveBeenCalledWith({
52
+ url: 'DocumentTypes/?mockedParams=true',
53
+ method: 'get',
54
+ headers: {},
55
+ });
56
+ expect(transformDocumentTypes).toHaveBeenCalledWith({ data: mockLabels });
57
+ expect(result).toEqual(mockLabels);
58
+ });
59
+
60
+ it('should handle errors when fetching labels', async () => {
61
+ const fields = ['field1'];
62
+ const error = new Error('Failed to fetch labels');
63
+ const sdk = CrexSDK.getInstance();
64
+
65
+ (createParams as jest.Mock).mockReturnValue([]);
66
+ (sdk.api.execute as jest.Mock).mockRejectedValue(error);
67
+
68
+ await expect(service.getLabels(fields)).rejects.toThrow('Failed to fetch labels');
69
+ expect(sdk.logger.log).toHaveBeenCalledWith(
70
+ 'error',
71
+ expect.stringContaining('Failed to fetch labels')
72
+ );
73
+ });
74
+ });
75
+ });
@@ -0,0 +1,161 @@
1
+ import { InformationUnitsService } from '../';
2
+ import { CrexSDK } from '@c-rex/core';
3
+ import { createParams, generateQueryParams } from '@c-rex/utils';
4
+ import { transformInformationUnits } from '../transforms/information';
5
+
6
+ jest.mock('@c-rex/core', () => ({
7
+ CrexSDK: {
8
+ getInstance: jest.fn().mockReturnValue({
9
+ api: {
10
+ execute: jest.fn()
11
+ },
12
+ logger: {
13
+ log: jest.fn()
14
+ }
15
+ })
16
+ }
17
+ }));
18
+
19
+ jest.mock('@c-rex/utils', () => ({
20
+ createParams: jest.fn(),
21
+ generateQueryParams: jest.fn()
22
+ }));
23
+
24
+ jest.mock('../transforms/information', () => ({
25
+ transformInformationUnits: jest.fn()
26
+ }));
27
+
28
+ describe('InformationUnitsService', () => {
29
+ let service: InformationUnitsService;
30
+ const sdk = CrexSDK.getInstance();
31
+
32
+ beforeEach(() => {
33
+ jest.clearAllMocks();
34
+ service = new InformationUnitsService();
35
+
36
+ (generateQueryParams as jest.Mock).mockReturnValue("");
37
+ });
38
+ afterEach(() => {
39
+ jest.clearAllMocks();
40
+ });
41
+
42
+ describe('getList', () => {
43
+ const fields = ['field1', 'field2'];
44
+ const mockFieldParams = [{ key: 'Fields', value: 'field1' }];
45
+ const mockResponse = { items: [], pageInfo: {} };
46
+ const language = 'EN-us';
47
+
48
+ beforeEach(() => {
49
+ (createParams as jest.Mock).mockReturnValue(mockFieldParams);
50
+ (transformInformationUnits as jest.Mock).mockReturnValue(mockResponse);
51
+ (generateQueryParams as jest.Mock).mockReturnValue("mockedParams=true");
52
+ (sdk.api.execute as jest.Mock).mockResolvedValue(mockResponse);
53
+ });
54
+
55
+ it('should fetch information units list with correct parameters', async () => {
56
+ const result = await service.getList('test', 2, fields, [language]);
57
+
58
+ expect(createParams).toHaveBeenCalledWith(fields, "Fields");
59
+ expect(transformInformationUnits).toHaveBeenCalledWith(mockResponse);
60
+ expect(generateQueryParams).toHaveBeenCalledWith([
61
+ {
62
+ key: 'pageSize',
63
+ value: '8'
64
+ },
65
+ {
66
+ key: 'PageNumber',
67
+ value: '1'
68
+ },
69
+ {
70
+ key: 'Query',
71
+ value: 'test'
72
+ },
73
+ ...mockFieldParams,
74
+ {
75
+ key: 'Restrict',
76
+ value: 'languages~EN-us'
77
+ },
78
+ ]);
79
+ expect(sdk.api.execute).toHaveBeenCalledWith({
80
+ url: 'InformationUnits/?mockedParams=true',
81
+ method: 'get',
82
+ headers: {},
83
+ });
84
+ expect(result).toEqual(mockResponse);
85
+ });
86
+
87
+ it('should handle null language parameter', async () => {
88
+ const spy = jest.spyOn(service, 'request' as any);
89
+ await service.getList('test', 1, ['field1'], null);
90
+
91
+ expect(spy).toHaveBeenCalledWith(
92
+ expect.not.objectContaining({
93
+ params: expect.arrayContaining([
94
+ expect.objectContaining({ key: 'Restrict' })
95
+ ])
96
+ })
97
+ );
98
+ });
99
+ });
100
+
101
+ describe('getItem', () => {
102
+ it('should fetch a single information unit', async () => {
103
+ const id = '123';
104
+ const mockResponse = { id: '123', title: 'Test' };
105
+
106
+ (sdk.api.execute as jest.Mock).mockResolvedValue({ data: mockResponse });
107
+ (generateQueryParams as jest.Mock).mockReturnValueOnce("mockedParams=true");
108
+
109
+ const result = await service.getItem(id);
110
+
111
+ expect(generateQueryParams).toHaveBeenCalledWith([
112
+ { key: "Fields", value: "renditions" },
113
+ { key: "Fields", value: "directoryNodes" },
114
+ ]);
115
+ expect(sdk.api.execute).toHaveBeenCalledWith({
116
+ url: 'InformationUnits/123?mockedParams=true',
117
+ method: 'get',
118
+ headers: {},
119
+ });
120
+ expect(result).toEqual(mockResponse);
121
+ });
122
+ });
123
+
124
+ describe('getLanguages', () => {
125
+ it('should fetch and transform languages', async () => {
126
+ const mockResponse = [{ value: 'EN', score: 1 }, { value: 'DE', score: 1 }];
127
+ (sdk.api.execute as jest.Mock).mockResolvedValue(mockResponse);
128
+
129
+ const result = await service.getLanguages();
130
+
131
+ expect(sdk.api.execute).toHaveBeenCalledWith({
132
+ url: 'InformationUnits/Languages',
133
+ method: 'get',
134
+ headers: {}
135
+ });
136
+ expect(result).toEqual(['EN', 'DE']);
137
+ });
138
+ });
139
+
140
+ describe('getSuggestions', () => {
141
+ it('should fetch and transform suggestions', async () => {
142
+ const query = 'test';
143
+ const mockResponse = { suggestions: [{ value: 'test1' }, { value: 'test2' }] };
144
+
145
+ (sdk.api.execute as jest.Mock).mockResolvedValue(mockResponse);
146
+ (generateQueryParams as jest.Mock).mockReturnValueOnce("mockedParams=true");
147
+
148
+ const result = await service.getSuggestions(query);
149
+
150
+ expect(generateQueryParams).toHaveBeenCalledWith([
151
+ { key: "prefix", value: "test" },
152
+ ]);
153
+ expect(sdk.api.execute).toHaveBeenCalledWith({
154
+ url: 'InformationUnits/Suggestions?mockedParams=true',
155
+ method: 'get',
156
+ headers: {},
157
+ });
158
+ expect(result).toEqual(['test1', 'test2']);
159
+ });
160
+ });
161
+ });
@@ -0,0 +1,7 @@
1
+ import '../renditions';
2
+
3
+ describe('Force coverage inclusion', () => {
4
+ it('Dummy', () => {
5
+ expect(true).toBe(true);
6
+ });
7
+ });
@@ -0,0 +1,57 @@
1
+ import { CrexSDK, CrexApi } from "@c-rex/core";
2
+ import { LogCategoriesType, LogLevelType, QueryParams } from "@c-rex/types";
3
+ import { generateQueryParams } from "@c-rex/utils";
4
+ import { Method } from "axios";
5
+
6
+ export class BaseService {
7
+ protected api: CrexApi;
8
+ private log: (level: LogLevelType, message: string, category?: LogCategoriesType) => void;
9
+ private endpoint: string;
10
+ private SDK: CrexSDK;
11
+
12
+ constructor(endpoint: string) {
13
+ this.SDK = CrexSDK.getInstance();
14
+ this.api = this.SDK.api;
15
+ this.log = this.SDK.logger.log;
16
+ this.endpoint = endpoint;
17
+ }
18
+
19
+ protected async request<T>(
20
+ {
21
+ path = "",
22
+ method = "get",
23
+ params = [],
24
+ headers = {},
25
+ transformer = (response: any) => response,
26
+ }: {
27
+ path?: string,
28
+ method?: Method,
29
+ params?: QueryParams[],
30
+ headers?: any,
31
+ transformer?: (data: any) => T,
32
+ }
33
+ ): Promise<T> {
34
+ try {
35
+ let url = `${this.endpoint}${path}`;
36
+
37
+ const queryParams = generateQueryParams(params);
38
+ if (queryParams.length > 0) {
39
+ url += `?${queryParams}`;
40
+ }
41
+
42
+ const response = await this.api.execute({
43
+ url,
44
+ method,
45
+ headers,
46
+ })
47
+
48
+ return await transformer(response);
49
+
50
+ } catch (error) {
51
+ this.log("error", `BaseService.request error when request ${path}. Error: ${error}`);
52
+
53
+ throw error;
54
+ }
55
+ }
56
+ }
57
+
@@ -0,0 +1,15 @@
1
+ import { BaseService } from "./baseService";
2
+ import { DirectoryNodes } from "@c-rex/interfaces";
3
+
4
+
5
+ export class DirectoryNodesService extends BaseService {
6
+ constructor() {
7
+ super("DirectoryNodes/");
8
+ }
9
+
10
+ public async getItem(id: string): Promise<DirectoryNodes> {
11
+ return await this.request({
12
+ path: id,
13
+ });
14
+ }
15
+ }
@@ -0,0 +1,23 @@
1
+ import { createParams } from "@c-rex/utils";
2
+ import { transformDocumentTypes } from "./transforms/documentTypes";
3
+ import { BaseService } from "./baseService";
4
+
5
+ export class DocumentTypesService extends BaseService {
6
+ constructor() {
7
+ super("DocumentTypes/");
8
+ }
9
+ public async getLabels(fields: string[]): Promise<string[]> {
10
+ const params = [
11
+ {
12
+ key: "Restrict",
13
+ value: `languages~EN-us`,
14
+ },
15
+ ...createParams(fields, "Fields"),
16
+ ];
17
+
18
+ return await this.request({
19
+ params,
20
+ transformer: transformDocumentTypes,
21
+ });
22
+ }
23
+ }
package/src/index.ts ADDED
@@ -0,0 +1,4 @@
1
+ export * from "./renditions"
2
+ export * from "./directoryNodes"
3
+ export * from "./documentTypes"
4
+ export * from "./informationUnits"
@@ -0,0 +1,75 @@
1
+ import {
2
+ AutocompleteSuggestion,
3
+ informationUnits,
4
+ informationUnitsItems,
5
+ } from "@c-rex/interfaces";
6
+ import { transformInformationUnits } from "./transforms/information";
7
+ import { createParams } from "@c-rex/utils";
8
+ import { BaseService } from "./baseService";
9
+
10
+ export class InformationUnitsService extends BaseService {
11
+ constructor() {
12
+ super("InformationUnits/");
13
+ }
14
+
15
+ public async getList(
16
+ queries: string,
17
+ page: number,
18
+ fields: string[],
19
+ language: string[] | null,
20
+ ): Promise<informationUnits> {
21
+ const remainFields = createParams(fields, "Fields");
22
+
23
+ const params = [
24
+ { key: "pageSize", value: "8" },
25
+ { key: "PageNumber", value: (page - 1).toString() },
26
+ { key: "Query", value: queries },
27
+ ...remainFields,
28
+ ];
29
+
30
+ if (language != null) {
31
+ language.forEach((item) => {
32
+ params.push({
33
+ key: "Restrict",
34
+ value: `languages~${item}`,
35
+ });
36
+ });
37
+ }
38
+
39
+ return await this.request({
40
+ params: params,
41
+ transformer: transformInformationUnits
42
+ });
43
+ }
44
+
45
+ public async getItem(id: string): Promise<informationUnitsItems> {
46
+ const params = [
47
+ { key: "Fields", value: "renditions" },
48
+ { key: "Fields", value: "directoryNodes" },
49
+ ];
50
+
51
+ return await this.request({
52
+ path: id,
53
+ params,
54
+ });
55
+ }
56
+
57
+ public async getLanguages(): Promise<string[]> {
58
+ return await this.request({
59
+ path: `Languages`,
60
+ transformer: (data: { value: string; score: number }[]) => {
61
+ return data.map((item) => item.value);
62
+ },
63
+ });
64
+ }
65
+
66
+ public async getSuggestions(query: string): Promise<string[]> {
67
+ return await this.request({
68
+ path: `Suggestions`,
69
+ params: [{ key: "prefix", value: query }],
70
+ transformer: (data: AutocompleteSuggestion) => {
71
+ return data.suggestions.map((item) => item.value);
72
+ },
73
+ });
74
+ }
75
+ }
@@ -0,0 +1,38 @@
1
+ import { BaseService } from "./baseService";
2
+ import { informationUnitsRenditions } from "@c-rex/interfaces";
3
+
4
+
5
+ export class RenditionsService extends BaseService {
6
+ constructor() {
7
+ super("Renditions/");
8
+ }
9
+
10
+ public async getHTMLRendition(renditions: informationUnitsRenditions[]): Promise<string> {
11
+ const filteredRenditions = renditions.filter(
12
+ (item) => item.format == "application/xhtml+xml",
13
+ );
14
+
15
+ if (filteredRenditions.length == 0) return "";
16
+ if (filteredRenditions[0] == undefined) return "";
17
+
18
+ const item = filteredRenditions[0];
19
+
20
+ const filteredLinks = item.links.filter((item) => item.rel == "view");
21
+
22
+ if (filteredLinks.length == 0) return "";
23
+ if (filteredLinks[0] == undefined) return "";
24
+
25
+
26
+ const url = filteredLinks[0].href;
27
+
28
+ const response = await this.api.execute({
29
+ url,
30
+ method: "get",
31
+ headers: {
32
+ Accept: "application/xhtml+xml",
33
+ },
34
+ })
35
+
36
+ return response;
37
+ }
38
+ }
@@ -0,0 +1,28 @@
1
+ import { DefaultRequest, DocumentTypesItem } from '@c-rex/interfaces';
2
+ import { transformDocumentTypes } from '../documentTypes';
3
+
4
+ describe('transformDocumentTypes', () => {
5
+ it('should transform information units', () => {
6
+ const mockResponse = {
7
+ items: [
8
+ {
9
+ labels: [
10
+ {
11
+ value: "Title 1",
12
+ language: 'en',
13
+ }, {
14
+ value: "Title 2",
15
+ language: 'fr',
16
+ }, {
17
+ value: "Title 3",
18
+ language: 'en',
19
+ }
20
+ ],
21
+ },
22
+ ],
23
+ } as unknown as DefaultRequest<DocumentTypesItem>;
24
+
25
+ const result = transformDocumentTypes(mockResponse);
26
+ expect(result).toEqual(['Title 1', 'Title 3']);
27
+ });
28
+ });
@@ -0,0 +1,34 @@
1
+ import { DefaultRequest } from '@c-rex/interfaces';
2
+ import { transformInformationUnits } from '../information';
3
+ import { informationUnitsItems } from '@c-rex/interfaces';
4
+
5
+ describe('transformInformationUnits', () => {
6
+ it('should transform information units', () => {
7
+ const mockResponse = {
8
+ items: [
9
+ {
10
+ id: '1',
11
+ title: 'Title 1',
12
+ description: 'Description 1',
13
+ }
14
+ ],
15
+ pageInfo: {
16
+ totalCount: 1
17
+ },
18
+ } as unknown as DefaultRequest<informationUnitsItems>;
19
+
20
+ const result = transformInformationUnits(mockResponse);
21
+ expect(result).toEqual({
22
+ items: [
23
+ {
24
+ id: '1',
25
+ title: 'Title 1',
26
+ description: 'Description 1',
27
+ }
28
+ ],
29
+ pageInfo: {
30
+ totalCount: 1
31
+ },
32
+ });
33
+ });
34
+ });
@@ -0,0 +1,16 @@
1
+ import { DefaultRequest } from "@c-rex/interfaces";
2
+ import { DocumentTypesItem } from "@c-rex/interfaces";
3
+
4
+ export const transformDocumentTypes = (data: DefaultRequest<DocumentTypesItem>) => {
5
+ const labels: string[] = [];
6
+
7
+ data.items.forEach((documentItem: DocumentTypesItem) => {
8
+ const aux = documentItem.labels.flatMap((item) => item);
9
+
10
+ aux.forEach((item) => {
11
+ if (item.language == "en") labels.push(item.value);
12
+ });
13
+ });
14
+
15
+ return labels;
16
+ };
@@ -0,0 +1,14 @@
1
+ import { DefaultRequest } from "@c-rex/interfaces";
2
+ import {
3
+ informationUnits,
4
+ informationUnitsItems,
5
+ } from "@c-rex/interfaces";
6
+
7
+ export const transformInformationUnits = (
8
+ data: DefaultRequest<informationUnitsItems>,
9
+ ): informationUnits => {
10
+ return {
11
+ items: data.items.map((item) => item),
12
+ pageInfo: data.pageInfo,
13
+ };
14
+ };
package/dist/index.d.mts DELETED
@@ -1,48 +0,0 @@
1
- import { CrexApi } from '@c-rex/core';
2
- import { Method } from 'axios';
3
- import { informationUnitsRenditions, DirectoryNodes, informationUnits, informationUnitsItems } from '@c-rex/interfaces';
4
-
5
- type QueryParams = {
6
- key: string;
7
- value: string;
8
- };
9
-
10
- declare class BaseService {
11
- protected api: CrexApi;
12
- private log;
13
- private endpoint;
14
- private SDK;
15
- constructor(endpoint: string);
16
- protected request<T>({ path, method, params, headers, transformer, }: {
17
- path?: string;
18
- method?: Method;
19
- params?: QueryParams[];
20
- headers?: any;
21
- transformer?: (data: any) => T;
22
- }): Promise<T>;
23
- }
24
-
25
- declare class RenditionsService extends BaseService {
26
- constructor();
27
- getHTMLRendition(renditions: informationUnitsRenditions[]): Promise<string>;
28
- }
29
-
30
- declare class DirectoryNodesService extends BaseService {
31
- constructor();
32
- getItem(id: string): Promise<DirectoryNodes>;
33
- }
34
-
35
- declare class DocumentTypesService extends BaseService {
36
- constructor();
37
- getLabels(fields: string[]): Promise<string[]>;
38
- }
39
-
40
- declare class InformationUnitsService extends BaseService {
41
- constructor();
42
- getList(queries: string, page: number, fields: string[], language: string[] | null): Promise<informationUnits>;
43
- getItem(id: string): Promise<informationUnitsItems>;
44
- getLanguages(): Promise<string[]>;
45
- getSuggestions(query: string): Promise<string[]>;
46
- }
47
-
48
- export { DirectoryNodesService, DocumentTypesService, InformationUnitsService, RenditionsService };
package/dist/index.d.ts DELETED
@@ -1,48 +0,0 @@
1
- import { CrexApi } from '@c-rex/core';
2
- import { Method } from 'axios';
3
- import { informationUnitsRenditions, DirectoryNodes, informationUnits, informationUnitsItems } from '@c-rex/interfaces';
4
-
5
- type QueryParams = {
6
- key: string;
7
- value: string;
8
- };
9
-
10
- declare class BaseService {
11
- protected api: CrexApi;
12
- private log;
13
- private endpoint;
14
- private SDK;
15
- constructor(endpoint: string);
16
- protected request<T>({ path, method, params, headers, transformer, }: {
17
- path?: string;
18
- method?: Method;
19
- params?: QueryParams[];
20
- headers?: any;
21
- transformer?: (data: any) => T;
22
- }): Promise<T>;
23
- }
24
-
25
- declare class RenditionsService extends BaseService {
26
- constructor();
27
- getHTMLRendition(renditions: informationUnitsRenditions[]): Promise<string>;
28
- }
29
-
30
- declare class DirectoryNodesService extends BaseService {
31
- constructor();
32
- getItem(id: string): Promise<DirectoryNodes>;
33
- }
34
-
35
- declare class DocumentTypesService extends BaseService {
36
- constructor();
37
- getLabels(fields: string[]): Promise<string[]>;
38
- }
39
-
40
- declare class InformationUnitsService extends BaseService {
41
- constructor();
42
- getList(queries: string, page: number, fields: string[], language: string[] | null): Promise<informationUnits>;
43
- getItem(id: string): Promise<informationUnitsItems>;
44
- getLanguages(): Promise<string[]>;
45
- getSuggestions(query: string): Promise<string[]>;
46
- }
47
-
48
- export { DirectoryNodesService, DocumentTypesService, InformationUnitsService, RenditionsService };
package/dist/index.js DELETED
@@ -1,214 +0,0 @@
1
- "use strict";
2
- var __defProp = Object.defineProperty;
3
- var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4
- var __getOwnPropNames = Object.getOwnPropertyNames;
5
- var __hasOwnProp = Object.prototype.hasOwnProperty;
6
- var __export = (target, all) => {
7
- for (var name in all)
8
- __defProp(target, name, { get: all[name], enumerable: true });
9
- };
10
- var __copyProps = (to, from, except, desc) => {
11
- if (from && typeof from === "object" || typeof from === "function") {
12
- for (let key of __getOwnPropNames(from))
13
- if (!__hasOwnProp.call(to, key) && key !== except)
14
- __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15
- }
16
- return to;
17
- };
18
- var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
-
20
- // src/index.ts
21
- var index_exports = {};
22
- __export(index_exports, {
23
- DirectoryNodesService: () => DirectoryNodesService,
24
- DocumentTypesService: () => DocumentTypesService,
25
- InformationUnitsService: () => InformationUnitsService,
26
- RenditionsService: () => RenditionsService
27
- });
28
- module.exports = __toCommonJS(index_exports);
29
-
30
- // src/baseService.ts
31
- var import_core = require("@c-rex/core");
32
- var import_utils = require("@c-rex/utils");
33
- var BaseService = class {
34
- api;
35
- log;
36
- endpoint;
37
- SDK;
38
- constructor(endpoint) {
39
- this.SDK = import_core.CrexSDK.getInstance();
40
- this.api = this.SDK.api;
41
- this.log = this.SDK.logger.log;
42
- this.endpoint = endpoint;
43
- }
44
- async request({
45
- path = "",
46
- method = "get",
47
- params = [],
48
- headers = {},
49
- transformer = (response) => response
50
- }) {
51
- try {
52
- let url = `${this.endpoint}${path}`;
53
- const queryParams = (0, import_utils.generateQueryParams)(params);
54
- if (queryParams.length > 0) {
55
- url += `?${queryParams}`;
56
- }
57
- const response = await this.api.execute({
58
- url,
59
- method,
60
- headers
61
- });
62
- return await transformer(response);
63
- } catch (error) {
64
- this.log("error", `BaseService.request error when request ${path}. Error: ${error}`);
65
- throw error;
66
- }
67
- }
68
- };
69
-
70
- // src/renditions.ts
71
- var RenditionsService = class extends BaseService {
72
- constructor() {
73
- super("Renditions/");
74
- }
75
- async getHTMLRendition(renditions) {
76
- const filteredRenditions = renditions.filter(
77
- (item2) => item2.format == "application/xhtml+xml"
78
- );
79
- if (filteredRenditions.length == 0) return "";
80
- if (filteredRenditions[0] == void 0) return "";
81
- const item = filteredRenditions[0];
82
- const filteredLinks = item.links.filter((item2) => item2.rel == "view");
83
- if (filteredLinks.length == 0) return "";
84
- if (filteredLinks[0] == void 0) return "";
85
- const url = filteredLinks[0].href;
86
- const response = await this.api.execute({
87
- url,
88
- method: "get",
89
- headers: {
90
- Accept: "application/xhtml+xml"
91
- }
92
- });
93
- return response;
94
- }
95
- };
96
-
97
- // src/directoryNodes.ts
98
- var DirectoryNodesService = class extends BaseService {
99
- constructor() {
100
- super("DirectoryNodes/");
101
- }
102
- async getItem(id) {
103
- return await this.request({
104
- path: id
105
- });
106
- }
107
- };
108
-
109
- // src/documentTypes.ts
110
- var import_utils2 = require("@c-rex/utils");
111
-
112
- // src/transforms/documentTypes.ts
113
- var transformDocumentTypes = (data) => {
114
- const labels = [];
115
- data.items.forEach((documentItem) => {
116
- const aux = documentItem.labels.flatMap((item) => item);
117
- aux.forEach((item) => {
118
- if (item.language == "en") labels.push(item.value);
119
- });
120
- });
121
- return labels;
122
- };
123
-
124
- // src/documentTypes.ts
125
- var DocumentTypesService = class extends BaseService {
126
- constructor() {
127
- super("DocumentTypes/");
128
- }
129
- async getLabels(fields) {
130
- const params = [
131
- {
132
- key: "Restrict",
133
- value: `languages~EN-us`
134
- },
135
- ...(0, import_utils2.createParams)(fields, "Fields")
136
- ];
137
- return await this.request({
138
- params,
139
- transformer: transformDocumentTypes
140
- });
141
- }
142
- };
143
-
144
- // src/transforms/information.ts
145
- var transformInformationUnits = (data) => {
146
- return {
147
- items: data.items.map((item) => item),
148
- pageInfo: data.pageInfo
149
- };
150
- };
151
-
152
- // src/informationUnits.ts
153
- var import_utils3 = require("@c-rex/utils");
154
- var InformationUnitsService = class extends BaseService {
155
- constructor() {
156
- super("InformationUnits/");
157
- }
158
- async getList(queries, page, fields, language) {
159
- const remainFields = (0, import_utils3.createParams)(fields, "Fields");
160
- const params = [
161
- { key: "pageSize", value: "8" },
162
- { key: "PageNumber", value: (page - 1).toString() },
163
- { key: "Query", value: queries },
164
- ...remainFields
165
- ];
166
- if (language != null) {
167
- language.forEach((item) => {
168
- params.push({
169
- key: "Restrict",
170
- value: `languages~${item}`
171
- });
172
- });
173
- }
174
- return await this.request({
175
- params,
176
- transformer: transformInformationUnits
177
- });
178
- }
179
- async getItem(id) {
180
- const params = [
181
- { key: "Fields", value: "renditions" },
182
- { key: "Fields", value: "directoryNodes" }
183
- ];
184
- return await this.request({
185
- path: id,
186
- params
187
- });
188
- }
189
- async getLanguages() {
190
- return await this.request({
191
- path: `Languages`,
192
- transformer: (data) => {
193
- return data.map((item) => item.value);
194
- }
195
- });
196
- }
197
- async getSuggestions(query) {
198
- return await this.request({
199
- path: `Suggestions`,
200
- params: [{ key: "prefix", value: query }],
201
- transformer: (data) => {
202
- return data.suggestions.map((item) => item.value);
203
- }
204
- });
205
- }
206
- };
207
- // Annotate the CommonJS export names for ESM import in node:
208
- 0 && (module.exports = {
209
- DirectoryNodesService,
210
- DocumentTypesService,
211
- InformationUnitsService,
212
- RenditionsService
213
- });
214
- //# sourceMappingURL=index.js.map
package/dist/index.js.map DELETED
@@ -1 +0,0 @@
1
- {"version":3,"sources":["../src/index.ts","../src/baseService.ts","../src/renditions.ts","../src/directoryNodes.ts","../src/documentTypes.ts","../src/transforms/documentTypes.ts","../src/transforms/information.ts","../src/informationUnits.ts"],"sourcesContent":["export * from \"./renditions\"\nexport * from \"./directoryNodes\"\nexport * from \"./documentTypes\"\nexport * from \"./informationUnits\"\n","import { CrexSDK, CrexApi } from \"@c-rex/core\";\nimport { LogCategoriesType, LogLevelType, QueryParams } from \"@c-rex/types\";\nimport { generateQueryParams } from \"@c-rex/utils\";\nimport { Method } from \"axios\";\n\nexport class BaseService {\n protected api: CrexApi;\n private log: (level: LogLevelType, message: string, category?: LogCategoriesType) => void;\n private endpoint: string;\n private SDK: CrexSDK;\n\n constructor(endpoint: string) {\n this.SDK = CrexSDK.getInstance();\n this.api = this.SDK.api;\n this.log = this.SDK.logger.log;\n this.endpoint = endpoint;\n }\n\n protected async request<T>(\n {\n path = \"\",\n method = \"get\",\n params = [],\n headers = {},\n transformer = (response: any) => response,\n }: {\n path?: string,\n method?: Method,\n params?: QueryParams[],\n headers?: any,\n transformer?: (data: any) => T,\n }\n ): Promise<T> {\n try {\n let url = `${this.endpoint}${path}`;\n\n const queryParams = generateQueryParams(params);\n if (queryParams.length > 0) {\n url += `?${queryParams}`;\n }\n\n const response = await this.api.execute({\n url,\n method,\n headers,\n })\n\n return await transformer(response);\n\n } catch (error) {\n this.log(\"error\", `BaseService.request error when request ${path}. Error: ${error}`);\n\n throw error;\n }\n }\n}\n\n","import { BaseService } from \"./baseService\";\nimport { informationUnitsRenditions } from \"@c-rex/interfaces\";\n\n\nexport class RenditionsService extends BaseService {\n constructor() {\n super(\"Renditions/\");\n }\n\n public async getHTMLRendition(renditions: informationUnitsRenditions[]): Promise<string> {\n const filteredRenditions = renditions.filter(\n (item) => item.format == \"application/xhtml+xml\",\n );\n\n if (filteredRenditions.length == 0) return \"\";\n if (filteredRenditions[0] == undefined) return \"\";\n\n const item = filteredRenditions[0];\n\n const filteredLinks = item.links.filter((item) => item.rel == \"view\");\n\n if (filteredLinks.length == 0) return \"\";\n if (filteredLinks[0] == undefined) return \"\";\n\n\n const url = filteredLinks[0].href;\n\n const response = await this.api.execute({\n url,\n method: \"get\",\n headers: {\n Accept: \"application/xhtml+xml\",\n },\n })\n\n return response;\n }\n}","import { BaseService } from \"./baseService\";\nimport { DirectoryNodes } from \"@c-rex/interfaces\";\n\n\nexport class DirectoryNodesService extends BaseService {\n constructor() {\n super(\"DirectoryNodes/\");\n }\n\n public async getItem(id: string): Promise<DirectoryNodes> {\n return await this.request({\n path: id,\n });\n }\n}\n","import { createParams } from \"@c-rex/utils\";\nimport { transformDocumentTypes } from \"./transforms/documentTypes\";\nimport { BaseService } from \"./baseService\";\n\nexport class DocumentTypesService extends BaseService {\n constructor() {\n super(\"DocumentTypes/\");\n }\n public async getLabels(fields: string[]): Promise<string[]> {\n const params = [\n {\n key: \"Restrict\",\n value: `languages~EN-us`,\n },\n ...createParams(fields, \"Fields\"),\n ];\n\n return await this.request({\n params,\n transformer: transformDocumentTypes,\n });\n }\n}\n","import { DefaultRequest } from \"@c-rex/interfaces\";\nimport { DocumentTypesItem } from \"@c-rex/interfaces\";\n\nexport const transformDocumentTypes = (data: DefaultRequest<DocumentTypesItem>) => {\n const labels: string[] = [];\n\n data.items.forEach((documentItem: DocumentTypesItem) => {\n const aux = documentItem.labels.flatMap((item) => item);\n\n aux.forEach((item) => {\n if (item.language == \"en\") labels.push(item.value);\n });\n });\n\n return labels;\n};\n","import { DefaultRequest } from \"@c-rex/interfaces\";\nimport {\n informationUnits,\n informationUnitsItems,\n} from \"@c-rex/interfaces\";\n\nexport const transformInformationUnits = (\n data: DefaultRequest<informationUnitsItems>,\n): informationUnits => {\n return {\n items: data.items.map((item) => item),\n pageInfo: data.pageInfo,\n };\n};\n","import {\n AutocompleteSuggestion,\n informationUnits,\n informationUnitsItems,\n} from \"@c-rex/interfaces\";\nimport { transformInformationUnits } from \"./transforms/information\";\nimport { createParams } from \"@c-rex/utils\";\nimport { BaseService } from \"./baseService\";\n\nexport class InformationUnitsService extends BaseService {\n constructor() {\n super(\"InformationUnits/\");\n }\n\n public async getList(\n queries: string,\n page: number,\n fields: string[],\n language: string[] | null,\n ): Promise<informationUnits> {\n const remainFields = createParams(fields, \"Fields\");\n\n const params = [\n { key: \"pageSize\", value: \"8\" },\n { key: \"PageNumber\", value: (page - 1).toString() },\n { key: \"Query\", value: queries },\n ...remainFields,\n ];\n\n if (language != null) {\n language.forEach((item) => {\n params.push({\n key: \"Restrict\",\n value: `languages~${item}`,\n });\n });\n }\n\n return await this.request({\n params: params,\n transformer: transformInformationUnits\n });\n }\n\n public async getItem(id: string): Promise<informationUnitsItems> {\n const params = [\n { key: \"Fields\", value: \"renditions\" },\n { key: \"Fields\", value: \"directoryNodes\" },\n ];\n\n return await this.request({\n path: id,\n params,\n });\n }\n\n public async getLanguages(): Promise<string[]> {\n return await this.request({\n path: `Languages`,\n transformer: (data: { value: string; score: number }[]) => {\n return data.map((item) => item.value);\n },\n });\n }\n\n public async getSuggestions(query: string): Promise<string[]> {\n return await this.request({\n path: `Suggestions`,\n params: [{ key: \"prefix\", value: query }],\n transformer: (data: AutocompleteSuggestion) => {\n return data.suggestions.map((item) => item.value);\n },\n });\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACAA,kBAAiC;AAEjC,mBAAoC;AAG7B,IAAM,cAAN,MAAkB;AAAA,EACX;AAAA,EACF;AAAA,EACA;AAAA,EACA;AAAA,EAER,YAAY,UAAkB;AAC1B,SAAK,MAAM,oBAAQ,YAAY;AAC/B,SAAK,MAAM,KAAK,IAAI;AACpB,SAAK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@
1
- // src/baseService.ts
2
- import { CrexSDK } from "@c-rex/core";
3
- import { generateQueryParams } from "@c-rex/utils";
4
- var BaseService = class {
5
- api;
6
- log;
7
- endpoint;
8
- SDK;
9
- constructor(endpoint) {
10
- this.SDK = CrexSDK.getInstance();
11
- this.api = this.SDK.api;
12
- this.log = this.SDK.logger.log;
13
- this.endpoint = endpoint;
14
- }
15
- async request({
16
- path = "",
17
- method = "get",
18
- params = [],
19
- headers = {},
20
- transformer = (response) => response
21
- }) {
22
- try {
23
- let url = `${this.endpoint}${path}`;
24
- const queryParams = generateQueryParams(params);
25
- if (queryParams.length > 0) {
26
- url += `?${queryParams}`;
27
- }
28
- const response = await this.api.execute({
29
- url,
30
- method,
31
- headers
32
- });
33
- return await transformer(response);
34
- } catch (error) {
35
- this.log("error", `BaseService.request error when request ${path}. Error: ${error}`);
36
- throw error;
37
- }
38
- }
39
- };
40
-
41
- // src/renditions.ts
42
- var RenditionsService = class extends BaseService {
43
- constructor() {
44
- super("Renditions/");
45
- }
46
- async getHTMLRendition(renditions) {
47
- const filteredRenditions = renditions.filter(
48
- (item2) => item2.format == "application/xhtml+xml"
49
- );
50
- if (filteredRenditions.length == 0) return "";
51
- if (filteredRenditions[0] == void 0) return "";
52
- const item = filteredRenditions[0];
53
- const filteredLinks = item.links.filter((item2) => item2.rel == "view");
54
- if (filteredLinks.length == 0) return "";
55
- if (filteredLinks[0] == void 0) return "";
56
- const url = filteredLinks[0].href;
57
- const response = await this.api.execute({
58
- url,
59
- method: "get",
60
- headers: {
61
- Accept: "application/xhtml+xml"
62
- }
63
- });
64
- return response;
65
- }
66
- };
67
-
68
- // src/directoryNodes.ts
69
- var DirectoryNodesService = class extends BaseService {
70
- constructor() {
71
- super("DirectoryNodes/");
72
- }
73
- async getItem(id) {
74
- return await this.request({
75
- path: id
76
- });
77
- }
78
- };
79
-
80
- // src/documentTypes.ts
81
- import { createParams } from "@c-rex/utils";
82
-
83
- // src/transforms/documentTypes.ts
84
- var transformDocumentTypes = (data) => {
85
- const labels = [];
86
- data.items.forEach((documentItem) => {
87
- const aux = documentItem.labels.flatMap((item) => item);
88
- aux.forEach((item) => {
89
- if (item.language == "en") labels.push(item.value);
90
- });
91
- });
92
- return labels;
93
- };
94
-
95
- // src/documentTypes.ts
96
- var DocumentTypesService = class extends BaseService {
97
- constructor() {
98
- super("DocumentTypes/");
99
- }
100
- async getLabels(fields) {
101
- const params = [
102
- {
103
- key: "Restrict",
104
- value: `languages~EN-us`
105
- },
106
- ...createParams(fields, "Fields")
107
- ];
108
- return await this.request({
109
- params,
110
- transformer: transformDocumentTypes
111
- });
112
- }
113
- };
114
-
115
- // src/transforms/information.ts
116
- var transformInformationUnits = (data) => {
117
- return {
118
- items: data.items.map((item) => item),
119
- pageInfo: data.pageInfo
120
- };
121
- };
122
-
123
- // src/informationUnits.ts
124
- import { createParams as createParams2 } from "@c-rex/utils";
125
- var InformationUnitsService = class extends BaseService {
126
- constructor() {
127
- super("InformationUnits/");
128
- }
129
- async getList(queries, page, fields, language) {
130
- const remainFields = createParams2(fields, "Fields");
131
- const params = [
132
- { key: "pageSize", value: "8" },
133
- { key: "PageNumber", value: (page - 1).toString() },
134
- { key: "Query", value: queries },
135
- ...remainFields
136
- ];
137
- if (language != null) {
138
- language.forEach((item) => {
139
- params.push({
140
- key: "Restrict",
141
- value: `languages~${item}`
142
- });
143
- });
144
- }
145
- return await this.request({
146
- params,
147
- transformer: transformInformationUnits
148
- });
149
- }
150
- async getItem(id) {
151
- const params = [
152
- { key: "Fields", value: "renditions" },
153
- { key: "Fields", value: "directoryNodes" }
154
- ];
155
- return await this.request({
156
- path: id,
157
- params
158
- });
159
- }
160
- async getLanguages() {
161
- return await this.request({
162
- path: `Languages`,
163
- transformer: (data) => {
164
- return data.map((item) => item.value);
165
- }
166
- });
167
- }
168
- async getSuggestions(query) {
169
- return await this.request({
170
- path: `Suggestions`,
171
- params: [{ key: "prefix", value: query }],
172
- transformer: (data) => {
173
- return data.suggestions.map((item) => item.value);
174
- }
175
- });
176
- }
177
- };
178
- export {
179
- DirectoryNodesService,
180
- DocumentTypesService,
181
- InformationUnitsService,
182
- RenditionsService
183
- };
184
- //# sourceMappingURL=index.mjs.map
@@ -1 +0,0 @@
1
- {"version":3,"sources":["../src/baseService.ts","../src/renditions.ts","../src/directoryNodes.ts","../src/documentTypes.ts","../src/transforms/documentTypes.ts","../src/transforms/information.ts","../src/informationUnits.ts"],"sourcesContent":["import { CrexSDK, CrexApi } from \"@c-rex/core\";\nimport { LogCategoriesType, LogLevelType, QueryParams } from \"@c-rex/types\";\nimport { generateQueryParams } from \"@c-rex/utils\";\nimport { Method } from \"axios\";\n\nexport class BaseService {\n protected api: CrexApi;\n private log: (level: LogLevelType, message: string, category?: LogCategoriesType) => void;\n private endpoint: string;\n private SDK: CrexSDK;\n\n constructor(endpoint: string) {\n this.SDK = CrexSDK.getInstance();\n this.api = this.SDK.api;\n this.log = this.SDK.logger.log;\n this.endpoint = endpoint;\n }\n\n protected async request<T>(\n {\n path = \"\",\n method = \"get\",\n params = [],\n headers = {},\n transformer = (response: any) => response,\n }: {\n path?: string,\n method?: Method,\n params?: QueryParams[],\n headers?: any,\n transformer?: (data: any) => T,\n }\n ): Promise<T> {\n try {\n let url = `${this.endpoint}${path}`;\n\n const queryParams = generateQueryParams(params);\n if (queryParams.length > 0) {\n url += `?${queryParams}`;\n }\n\n const response = await this.api.execute({\n url,\n method,\n headers,\n })\n\n return await transformer(response);\n\n } catch (error) {\n this.log(\"error\", `BaseService.request error when request ${path}. Error: ${error}`);\n\n throw error;\n }\n }\n}\n\n","import { BaseService } from \"./baseService\";\nimport { informationUnitsRenditions } from \"@c-rex/interfaces\";\n\n\nexport class RenditionsService extends BaseService {\n constructor() {\n super(\"Renditions/\");\n }\n\n public async getHTMLRendition(renditions: informationUnitsRenditions[]): Promise<string> {\n const filteredRenditions = renditions.filter(\n (item) => item.format == \"application/xhtml+xml\",\n );\n\n if (filteredRenditions.length == 0) return \"\";\n if (filteredRenditions[0] == undefined) return \"\";\n\n const item = filteredRenditions[0];\n\n const filteredLinks = item.links.filter((item) => item.rel == \"view\");\n\n if (filteredLinks.length == 0) return \"\";\n if (filteredLinks[0] == undefined) return \"\";\n\n\n const url = filteredLinks[0].href;\n\n const response = await this.api.execute({\n url,\n method: \"get\",\n headers: {\n Accept: \"application/xhtml+xml\",\n },\n })\n\n return response;\n }\n}","import { BaseService } from \"./baseService\";\nimport { DirectoryNodes } from \"@c-rex/interfaces\";\n\n\nexport class DirectoryNodesService extends BaseService {\n constructor() {\n super(\"DirectoryNodes/\");\n }\n\n public async getItem(id: string): Promise<DirectoryNodes> {\n return await this.request({\n path: id,\n });\n }\n}\n","import { createParams } from \"@c-rex/utils\";\nimport { transformDocumentTypes } from \"./transforms/documentTypes\";\nimport { BaseService } from \"./baseService\";\n\nexport class DocumentTypesService extends BaseService {\n constructor() {\n super(\"DocumentTypes/\");\n }\n public async getLabels(fields: string[]): Promise<string[]> {\n const params = [\n {\n key: \"Restrict\",\n value: `languages~EN-us`,\n },\n ...createParams(fields, \"Fields\"),\n ];\n\n return await this.request({\n params,\n transformer: transformDocumentTypes,\n });\n }\n}\n","import { DefaultRequest } from \"@c-rex/interfaces\";\nimport { DocumentTypesItem } from \"@c-rex/interfaces\";\n\nexport const transformDocumentTypes = (data: DefaultRequest<DocumentTypesItem>) => {\n const labels: string[] = [];\n\n data.items.forEach((documentItem: DocumentTypesItem) => {\n const aux = documentItem.labels.flatMap((item) => item);\n\n aux.forEach((item) => {\n if (item.language == \"en\") labels.push(item.value);\n });\n });\n\n return labels;\n};\n","import { DefaultRequest } from \"@c-rex/interfaces\";\nimport {\n informationUnits,\n informationUnitsItems,\n} from \"@c-rex/interfaces\";\n\nexport const transformInformationUnits = (\n data: DefaultRequest<informationUnitsItems>,\n): informationUnits => {\n return {\n items: data.items.map((item) => item),\n pageInfo: data.pageInfo,\n };\n};\n","import {\n AutocompleteSuggestion,\n informationUnits,\n informationUnitsItems,\n} from \"@c-rex/interfaces\";\nimport { transformInformationUnits } from \"./transforms/information\";\nimport { createParams } from \"@c-rex/utils\";\nimport { BaseService } from \"./baseService\";\n\nexport class InformationUnitsService extends BaseService {\n constructor() {\n super(\"InformationUnits/\");\n }\n\n public async getList(\n queries: string,\n page: number,\n fields: string[],\n language: string[] | null,\n ): Promise<informationUnits> {\n const remainFields = createParams(fields, \"Fields\");\n\n const params = [\n { key: \"pageSize\", value: \"8\" },\n { key: \"PageNumber\", value: (page - 1).toString() },\n { key: \"Query\", value: queries },\n ...remainFields,\n ];\n\n if (language != null) {\n language.forEach((item) => {\n params.push({\n key: \"Restrict\",\n value: `languages~${item}`,\n });\n });\n }\n\n return await this.request({\n params: params,\n transformer: transformInformationUnits\n });\n }\n\n public async getItem(id: string): Promise<informationUnitsItems> {\n const params = [\n { key: \"Fields\", value: \"renditions\" },\n { key: \"Fields\", value: \"directoryNodes\" },\n ];\n\n return await this.request({\n path: id,\n params,\n });\n }\n\n public async getLanguages(): Promise<string[]> {\n return await this.request({\n path: `Languages`,\n transformer: (data: { value: string; score: number }[]) => {\n return data.map((item) => item.value);\n },\n });\n }\n\n public async getSuggestions(query: string): Promise<string[]> {\n return await this.request({\n path: `Suggestions`,\n params: [{ key: \"prefix\", value: query }],\n transformer: (data: AutocompleteSuggestion) => {\n return data.suggestions.map((item) => item.value);\n },\n });\n }\n}\n"],"mappings":";AAAA,SAAS,eAAwB;AAEjC,SAAS,2BAA2B;AAG7B,IAAM,cAAN,MAAkB;AAAA,EACX;AAAA,EACF;AAAA,EACA;AAAA,EACA;AAAA,EAER,YAAY,UAAkB;AAC1B,SAAK,MAAM,QAAQ,YAAY;AAC/B,SAAK,MAAM,KAAK,IAAI;AACpB,SAAK,MAAM,KAAK,IAAI,OAAO;AAC3B,SAAK,WAAW;AAAA,EACpB;AAAA,EAEA,MAAgB,QACZ;AAAA,IACI,OAAO;AAAA,IACP,SAAS;AAAA,IACT,SAAS,CAAC;AAAA,IACV,UAAU,CAAC;AAAA,IACX,cA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