-
Notifications
You must be signed in to change notification settings - Fork 0
/
api.tsx
64 lines (60 loc) · 1.55 KB
/
api.tsx
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
import axios, { AxiosResponse } from 'axios';
import Debug from 'debug';
const debug = Debug('deepcase:hasura');
export interface HasuraAxiosResponse extends AxiosResponse {
error?: string;
}
export interface HasuraApiOptions {
path: string;
ssl: boolean;
secret: string;
}
export interface HasuraApiQueryOptions {
route?: string;
}
export class HasuraApi {
options: HasuraApiOptions;
constructor(options: HasuraApiOptions) {
this.options = options;
}
validateStatus() { return true };
getError(result: AxiosResponse): null | string {
const { status } = result;
const error = status >= 200 && status < 300 ? null : result.statusText;
return error;
}
sql(sql: string) {
return this.query({
type: 'run_sql',
args: {
sql,
},
}, {
route: '/v1/query',
});
}
explain(query: string) {
return this.query({
query: {
query
},
}, {
route: '/v1/graphql/explain',
});
}
async query(data: any, options: HasuraApiQueryOptions = { route: '/v1/query' }) {
debug('query', data?.type);
const result: HasuraAxiosResponse = await axios({
method: 'post',
url: `http${this.options.ssl ? 's' : ''}://${this.options.path}${options?.route}`,
headers: {
...(this.options.secret ? { 'x-hasura-admin-secret': this.options.secret } : {}),
},
data,
validateStatus: this.validateStatus,
});
result.error = this.getError(result);
if (result.error) debug('error', result?.error, result?.data);
return result;
}
}