-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.ts
139 lines (126 loc) · 3.11 KB
/
client.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
import fetch from "cross-fetch";
import {
ACCOUNTS_ENDPOINT,
API_BASE_URL,
API_PREFIX,
EXPORTS_ENDPOINT,
ISSUES_ENDPOINT,
ITEMS_ENDPOINT,
LISTS_ENDPOINT,
SUBSCRIBERS_ENDPOINT,
} from "./constants";
import {
AddSubscriberInput,
RevueAddItemInput,
RevueExport,
RevueExportFromList,
RevueExportID,
RevueIssue,
RevueIssueID,
RevueItem,
RevueList,
RevueListID,
RevueProfileUrl,
RevueSubscriber,
UnsubscribeInput,
UpdateSubscriberInput,
} from "./types";
interface RequestOptions extends RequestInit {
body?: any;
}
interface ClientOptions {
token: string;
}
export default class RevueClient {
token: string;
constructor(options: ClientOptions) {
if (!options.token) {
throw new Error("Missing Revue API token");
}
this.token = options.token;
}
async request<Result = any>(
url: string,
{ headers, body, ...init }: RequestOptions = {}
): Promise<Result> {
const res = await fetch(`${API_BASE_URL}${API_PREFIX}${url}`, {
...init,
headers: {
"Content-Type": "application/json",
Authorization: `Token ${this.token}`,
...headers,
},
body: JSON.stringify(body),
});
const data = await res.json();
return data;
}
// Lists
getAllLists() {
return this.request<RevueList[]>(LISTS_ENDPOINT);
}
getSingleList(listId: RevueListID) {
return this.request<RevueList>(`${LISTS_ENDPOINT}/${listId}`);
}
// Issues
getAllSentIssues() {
return this.request<RevueIssue[]>(ISSUES_ENDPOINT);
}
getCurrentIssue() {
return this.request<RevueIssue>(`${ISSUES_ENDPOINT}/current`);
}
getLastSentIssue() {
return this.request<RevueIssue>(`${ISSUES_ENDPOINT}/latest`);
}
// Items
getInboxItems() {
return this.request<RevueItem[]>(ITEMS_ENDPOINT);
}
addItemToIssue(issueId: RevueIssueID, body: RevueAddItemInput) {
return this.request<RevueItem>(`${ISSUES_ENDPOINT}/${issueId}/items`, {
method: "POST",
body,
});
}
// Subscribers
getAllSubscribers() {
return this.request<RevueItem[]>(SUBSCRIBERS_ENDPOINT);
}
getAllUnsubscribed() {
return this.request<RevueItem[]>(`${SUBSCRIBERS_ENDPOINT}/unsubscribed`);
}
addSubscriber(body: AddSubscriberInput) {
return this.request<RevueSubscriber>(SUBSCRIBERS_ENDPOINT, {
method: "POST",
body,
});
}
updateSubscriber(body: UpdateSubscriberInput) {
return this.request<RevueSubscriber>(SUBSCRIBERS_ENDPOINT, {
method: "PATCH",
body,
});
}
unsubscribe(body: UnsubscribeInput) {
return this.request<RevueSubscriber>(SUBSCRIBERS_ENDPOINT, {
method: "POST",
body,
});
}
// Exports
getAllExports() {
return this.request<RevueExportFromList[]>(EXPORTS_ENDPOINT);
}
getSingleExport(exportId: RevueExportID) {
return this.request<RevueExport>(`${EXPORTS_ENDPOINT}/${exportId}`);
}
startExport(listId: RevueListID) {
return this.request<RevueExport>(`${EXPORTS_ENDPOINT}/lists/${listId}`, {
method: "POST",
});
}
// Profile
getProfileUrl() {
return this.request<RevueProfileUrl>(`${ACCOUNTS_ENDPOINT}/me`);
}
}