/
useProjectApiTokensApi.ts
47 lines (37 loc) · 1.19 KB
/
useProjectApiTokensApi.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
import useAPI from '../useApi/useApi';
export interface IApiTokenCreate {
username: string;
type: string;
environment?: string;
projects: string[];
}
const useProjectApiTokensApi = () => {
const { makeRequest, createRequest, errors, loading } = useAPI({
propagateErrors: true,
});
const deleteToken = async (secret: string, project: string) => {
const path = `api/admin/projects/${project}/api-tokens/${secret}`;
const req = createRequest(path, { method: 'DELETE' });
try {
const res = await makeRequest(req.caller, req.id);
return res;
} catch (e) {
throw e;
}
};
const createToken = async (newToken: IApiTokenCreate, project: string) => {
const path = `api/admin/projects/${project}/api-tokens`;
const req = createRequest(path, {
method: 'POST',
body: JSON.stringify(newToken),
});
try {
const res = await makeRequest(req.caller, req.id);
return res;
} catch (e) {
throw e;
}
};
return { deleteToken, createToken, errors, loading };
};
export default useProjectApiTokensApi;