-
Notifications
You must be signed in to change notification settings - Fork 3
/
Base.ts
44 lines (36 loc) · 1.11 KB
/
Base.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
import { HTTPClient } from 'koajax';
import { githubClient, RepositoryModel } from 'mobx-github';
export const isServer = () => typeof window === 'undefined';
const VercelHost = process.env.VERCEL_URL,
GithubToken = process.env.GITHUB_TOKEN;
const API_Host = isServer()
? VercelHost
? `https://${VercelHost}`
: 'http://localhost:3000'
: globalThis.location.origin;
export const ownClient = new HTTPClient({
baseURI: `${API_Host}/api/`,
responseType: 'json',
});
githubClient.use(({ request }, next) => {
if (GithubToken)
request.headers = {
...request.headers,
Authorization: `Bearer ${GithubToken}`,
};
return next();
});
export const repositoryStore = new RepositoryModel('idea2app');
type UploadedFile = Record<'originalname' | 'filename' | 'location', string>;
/**
* @see {@link https://fakeapi.platzi.com/en/rest/files/}
*/
export async function upload(file: Blob) {
const form = new FormData();
form.append('file', file);
const { body } = await ownClient.post<UploadedFile>(
'https://api.escuelajs.co/api/v1/files/upload',
form,
);
return body!.location;
}