-
Notifications
You must be signed in to change notification settings - Fork 178
/
Copy pathapis.ts
61 lines (50 loc) · 1.9 KB
/
apis.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
import axios, { AxiosResponse } from 'axios';
import { githubClientId, githubClientSecret } from 'config/environments';
const instance = axios.create();
instance.interceptors.request.use(request => {
request.params = {client_id: githubClientId, client_secret: githubClientSecret, ...request.params};
return request;
});
const request = (url: string, process: (mappedUrl: string, args: any[]) => Promise<AxiosResponse<any>>) => {
const tokens = url.split('/');
const baseURL = /^https?:\/\//i.test(url) ? '' : 'https://api.github.com';
return (...args: any[]) => {
const mappedUrl = baseURL + tokens.map(token => token.startsWith(':') ? args.shift() : token).join('/');
return process(mappedUrl, args);
};
};
const GET = (url: string) => {
return request(url, (mappedUrl: string, args: any[]) => {
const [params] = args;
return instance.get(mappedUrl, {params});
});
};
const DELETE = (url: string) => {
return request(url, (mappedUrl: string, args: any[]) => {
const [params] = args;
return instance.delete(mappedUrl, {params});
});
};
const POST = (url: string) => {
return request(url, (mappedUrl: string, args: any[]) => {
const [body, params] = args;
return instance.post(mappedUrl, body, {params});
});
};
const PUT = (url: string) => {
return request(url, (mappedUrl: string, args: any[]) => {
const [body, params] = args;
return instance.put(mappedUrl, body, {params});
});
};
const PATCH = (url: string) => {
return request(url, (mappedUrl: string, args: any[]) => {
const [body, params] = args;
return instance.patch(mappedUrl, body, {params});
});
};
export const GitHubApi = {
listCommits: GET('/repos/:owner/:repo/commits'),
getAccessToken: (code: string) => instance.post('https://github.com/login/oauth/access_token', {code}, {headers: {Accept: 'application/json'}}),
getLatestRelease: GET('/repos/:owner/:repo/releases/latest'),
};