@c-rex/services 0.0.6 → 0.0.7
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 +47 -0
- package/dist/index.d.ts +47 -0
- package/dist/index.js +358 -0
- package/dist/index.js.map +1 -0
- package/dist/index.mjs +318 -0
- package/dist/index.mjs.map +1 -0
- package/package.json +7 -13
- package/src/__tests__/baseService.test.ts +0 -98
- package/src/__tests__/directoryNodes.test.ts +0 -84
- package/src/__tests__/documentTypes.test.ts +0 -75
- package/src/__tests__/informationUnits.test.ts +0 -161
- package/src/__tests__/renditions.test.ts +0 -7
- package/src/baseService.ts +0 -57
- package/src/directoryNodes.ts +0 -15
- package/src/documentTypes.ts +0 -23
- package/src/index.ts +0 -4
- package/src/informationUnits.ts +0 -75
- package/src/renditions.ts +0 -38
- package/src/transforms/__tests__/documentTypes.test.ts +0 -28
- package/src/transforms/__tests__/information.test.ts +0 -34
- package/src/transforms/documentTypes.ts +0 -16
- package/src/transforms/information.ts +0 -14
|
@@ -1,161 +0,0 @@
|
|
|
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
|
-
});
|
package/src/baseService.ts
DELETED
|
@@ -1,57 +0,0 @@
|
|
|
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
|
-
|
package/src/directoryNodes.ts
DELETED
|
@@ -1,15 +0,0 @@
|
|
|
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
|
-
}
|
package/src/documentTypes.ts
DELETED
|
@@ -1,23 +0,0 @@
|
|
|
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
DELETED
package/src/informationUnits.ts
DELETED
|
@@ -1,75 +0,0 @@
|
|
|
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
|
-
}
|
package/src/renditions.ts
DELETED
|
@@ -1,38 +0,0 @@
|
|
|
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
|
-
}
|
|
@@ -1,28 +0,0 @@
|
|
|
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
|
-
});
|
|
@@ -1,34 +0,0 @@
|
|
|
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
|
-
});
|
|
@@ -1,16 +0,0 @@
|
|
|
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
|
-
};
|
|
@@ -1,14 +0,0 @@
|
|
|
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
|
-
};
|