-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathprezly.ts
72 lines (57 loc) · 2.02 KB
/
prezly.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
import prezlySDK from '@prezly/sdk';
import Category from '@prezly/sdk/dist/types/Category';
export class Prezly {
private readonly sdk: prezlySDK;
public constructor(accessToken: string) {
this.sdk = new prezlySDK({ accessToken });
}
public async getCategories(newsroomId: number): Promise<Category[]> {
const data = await this.sdk.newsroomCategories.list(newsroomId);
return Array.isArray(data) ? data : Object.values(data);
}
public async getStory(id: number) {
return this.sdk.stories.get(id);
}
public async fetchStoryBySlug(slug: string) {
const jsonQuery = {
"$and": [
{ "slug": { '$eq': slug}},
{ "$and": [{ "lifecycle_status": { '$in': ['published']}}]},
{ "$and": [{ "visibility": { '$in': ['public']}}]}
],
};
const { stories } = await this.searchStories({
limit:1,
jsonQuery: JSON.stringify(jsonQuery)
} );
if (stories.length === 1) {
return this.getStory(stories[0].id);
}
}
public async getHomepageStories(limit = 6) {
const jsonQuery = {
"$and": [
//{ "slug": { '$in': ['public']}},
{ "$and": [{ "lifecycle_status": { '$in': ['published']}}]},
{ "$and": [{ "visibility": { '$in': ['public']}}]}
],
};
const { stories } = await this.searchStories({
limit,
sortOrder: '-published_at',
jsonQuery: JSON.stringify(jsonQuery)
} );
const extendedStories = [];
for (const story of stories) {
const extendedStory = await this.getStory(story.id);
extendedStories.push(extendedStory);
}
return extendedStories;
}
public async getStories(options = { limit: 1000 }) {
return this.sdk.stories.list(options);
}
public async searchStories(options) {
return this.sdk.stories.search(options);
}
}