/
ContentLink.ts
76 lines (72 loc) · 2.26 KB
/
ContentLink.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
import { ClientConnection } from 'message-event-channel';
import { CONTENT_LINK } from '../constants/Events';
import { ERRORS_CONTENT_ITEM } from '../constants/Errors';
export interface ContentItemLink {
_meta: {
schema: string;
};
id: string;
contentType: string;
}
export interface ContentLinkOptions {
max?: number | null;
}
export class ContentLink {
/**
* Content Link - Use to open the content browser.
*
* @param connection message-event-channel connection
*/
constructor(private connection: ClientConnection) {}
/**
* This method will trigger a content browser. It returns a promise that will resolve to the chosen Content References.
*
* @param contentTypeIds list of Content Type Ids to filter the Content Browser by.
* @param options.max Max amount of selected contnet links
*
* ### Example
* ```typescript
* const enums = sdk.field.schema.allOf[1].properties.contentType.enum;
* const contentLink = await sdk.contentLink.getMultiple(enums);
*
* console.log(contentLink);
* ```
*/
getMultiple(
contentTypeIds: Array<string>,
options: ContentLinkOptions = { max: null }
): Promise<ContentItemLink[]> {
if (options.max === undefined) {
options.max = null;
}
return this.fetchLinks(contentTypeIds, options.max);
}
/**
* This method will trigger a content browser. It returns a promise that will resolve to the chosen Content Link.
*
* @param contentTypeIds list of Content Type Ids to filter the Content Browser by.
*
* ### Example
* ```typescript
* const enums = sdk.field.schema.allOf[1].properties.contentType.enum;
* const contentLink = await sdk.contentLink.get(enums);
*
* console.log(contentLink);
* ```
*/
async get(contentTypeIds: Array<string>): Promise<ContentItemLink> {
return this.fetchLinks(contentTypeIds);
}
async fetchLinks(contentTypeIds: Array<string>, max?: number | null) {
if (!contentTypeIds || !Array.isArray(contentTypeIds) || !contentTypeIds.length) {
throw new Error(ERRORS_CONTENT_ITEM.NO_IDS);
}
const response = {
contentTypeIds,
...((max !== undefined && { max }) || {}),
};
return this.connection.request(CONTENT_LINK.CONTENT_GET, response, {
timeout: false,
});
}
}