-
Notifications
You must be signed in to change notification settings - Fork 5
/
api.ts
209 lines (169 loc) · 5.24 KB
/
api.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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
import { userAsync } from '@/stores/user';
import { API_HOST, API_BASEURL } from '@/config';
import type {
User as UserDTO,
Engine as EngineDTO,
Collection as CollectionDTO,
CollectionType as CollectionTypeDTO,
} from '@/types/dto';
export interface HttpError extends Error {
response?: Response;
}
export type HttpHeaders = Record<string, string>;
export type QueryParams = Record<string, unknown>;
export async function getFilters(query?: string): Promise<void> {
return getJSON(`/filters${query || ''}`);
}
export async function getDataSource(id: number): Promise<void> {
return getJSON(`/datasources/${id}`);
}
export async function getDataSources(query?: string): Promise<void> {
return getJSON(`/datasources${query || ''}`);
}
export async function setDataSources(content: string | string[]): Promise<void> {
return postJSON('/datasources', { content });
}
export async function patchDataSourceCollections(
id: number,
collectionIds: number[]
): Promise<unknown> {
return patchJSON(`/datasources/${id}/filters`, collectionIds);
}
export async function delDataSource({ id, query }: { id: number, query?: string }): Promise<void> {
return delJSON(`/datasources/${id}${query || ''}`);
}
export async function getCalculations(query?: string): Promise<void> {
return getJSON(`/calculations${query || ''}`);
}
export async function getCalculationEngines(): Promise<void> {
return getJSON(`/calculations/engines`);
}
export async function getCalculationEngine(engine = 'dummy'): Promise<EngineDTO> {
return fetchJSON(
`${API_HOST}/calculations/template?engine=${engine}`,
{ credentials: 'omit' }
);
}
export async function setCalculation({ dataId, engine = 'dummy', input, workflow = 'workflow', query = '' }:
{
dataId: number,
engine?: string,
input?: string,
workflow?: string,
query?: string
}): Promise<void> {
postJSON(`/webhooks/calc_update${query}`, {})
return postJSON(`/calculations${query}`, { dataId, engine, input, workflow });
}
export async function delCalculation({ id, query }: { id: number, query?: string }): Promise<void> {
return delJSON(`/calculations/${id}${query || ''}`);
}
export async function login(email: string, password: string): Promise<void> {
return postJSON('/auth', { email, password });
}
export async function logout(): Promise<void> {
return delJSON('/auth');
}
export async function me(): Promise<UserDTO> {
return getJSON<UserDTO>('/auth');
}
export async function setMe(data: UserDTO): Promise<UserDTO> {
return putJSON<UserDTO, UserDTO>('/auth', data);
}
export async function getCollectionTypes(): Promise<CollectionTypeDTO[]> {
return getJSON('/collections/types');
}
export async function getCollectionDataSources(): Promise<void> {
return getJSON(`/collections/datasources`);
}
export async function getCollections(query?: string): Promise<void> {
return getJSON(`/collections${query || ''}`);
}
export async function setCollection(collection: CollectionDTO): Promise<void> {
return putJSON('/collections', collection);
}
export async function delCollection(collectionId: number): Promise<void> {
return delJSON(`/collections/${collectionId}`);
}
export async function searchUsers(search: string, limit = 10): Promise<UserDTO[]> {
if (!search) return [];
return getJSON('/users', { search, limit });
}
export async function getUsers(ids: number[] = []): Promise<UserDTO[]> {
if (!ids.length) return [];
return getJSON('/users', { ids: ids.join(',') });
}
export async function getJSON<T>(
path: string,
params?: QueryParams,
headers?: HttpHeaders
): Promise<T> {
const url = new URL(API_BASEURL + path);
if (params) {
Object.entries(params).forEach(([key, value]) =>
url.searchParams.append(key, value as string)
);
}
return fetchJSON(url.toString(), { headers });
}
export async function postJSON<T, U>(path: string, data: T, headers?: HttpHeaders): Promise<U> {
const url = new URL(API_BASEURL + path);
return fetchJSON<U>(url.toString(), {
method: 'POST',
body: JSON.stringify(data),
headers,
});
}
export async function putJSON<T, U>(path: string, data: T, headers?: HttpHeaders): Promise<U> {
const url = new URL(API_BASEURL + path);
return fetchJSON<U>(url.toString(), {
method: 'PUT',
body: JSON.stringify(data),
headers,
});
}
export async function patchJSON<T, U>(path: string, data: T, headers?: HttpHeaders): Promise<U> {
const url = new URL(API_BASEURL + path);
return fetchJSON<U>(url.toString(), {
method: 'PATCH',
body: JSON.stringify(data),
headers,
});
}
export async function delJSON<T, U>(path: string, headers?: HttpHeaders): Promise<U> {
const url = new URL(API_BASEURL + path);
return fetchJSON<U>(url.toString(), {
method: 'DELETE',
headers,
});
}
// low-level transport
export default async function fetchJSON<T>(
url: string,
{ headers, ...options }: RequestInit = {}
): Promise<T> {
const req = new Request(url, {
headers: {
'Content-Type': 'application/json',
...headers,
},
credentials: 'include',
mode: 'cors',
...options,
});
const res = await fetch(req);
if (!res.ok) {
if (res.status === 401) {
userAsync.set(null);
return null;
}
const json = await res.json();
const err: HttpError = new Error(json.error);
err.response = res;
throw err;
}
if (res.status === 204) {
return null;
}
return await res.json();
}