-
Notifications
You must be signed in to change notification settings - Fork 2
/
apiClient.ts
66 lines (51 loc) · 1.83 KB
/
apiClient.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
import path from 'path'
import { createIntegreSQLApiClientError } from './apiClientError'
import { GetTestDatabaseResponse, InitializeTemplateResponse } from './interfaces'
export interface IntegreSQLApiClientOptions {
url: string
}
export class IntegreSQLApiClient {
baseUrl: string
constructor(options: IntegreSQLApiClientOptions) {
const url = new URL(options.url)
this.baseUrl = url.toString() + 'api/v1'
}
async request<T>(
method: 'GET' | 'POST' | 'PUT' | 'DELETE',
endpoint: string,
body?: Record<string, unknown>
): Promise<T> {
const url = path.join(this.baseUrl, endpoint)
const response = await fetch(url, {
method,
body: body ? JSON.stringify(body) : undefined,
headers: { 'Content-Type': 'application/json' },
})
const text = await response.text()
if (response.status >= 400) {
throw createIntegreSQLApiClientError({ responseStatus: response.status, responseText: text })
}
return text ? JSON.parse(text) : null
}
async initializeTemplate(hash: string) {
return this.request<InitializeTemplateResponse>('POST', '/templates', { hash })
}
async finalizeTemplate(hash: string) {
return this.request<null>('PUT', `/templates/${hash}`)
}
async discardTemplate(hash: string) {
return this.request<null>('DELETE', `/templates/${hash}`)
}
async discardAllTemplates() {
return this.request<null>('DELETE', `/admin/templates`)
}
async getTestDatabase(hash: string) {
return this.request<GetTestDatabaseResponse>('GET', `/templates/${hash}/tests`)
}
async reuseTestDatabase(hash: string, id: number) {
return this.request<null>('DELETE', `/templates/${hash}/tests/${id}`)
}
async recreateTestDatabase(hash: string, id: number) {
return this.request<null>('POST', `/templates/${hash}/tests/${id}/recreate`)
}
}