-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathBase.ts
More file actions
81 lines (61 loc) · 2.18 KB
/
Base.ts
File metadata and controls
81 lines (61 loc) · 2.18 KB
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
import 'core-js/full/array/from-async';
import { HTTPClient } from 'koajax';
import { githubClient, RepositoryModel, Issue as GitHubIssue } from 'mobx-github';
import { TableCellAttachment, TableCellMedia, TableCellValue } from 'mobx-lark';
import { DataObject } from 'mobx-restful';
import { isEmpty } from 'web-utility';
import { API_Host, GithubToken, isServer, LARK_API_HOST, ProxyBaseURL } from './configuration';
export const ownClient = new HTTPClient({
baseURI: `${API_Host}/api/`,
responseType: 'json',
});
export const larkClient = new HTTPClient({
baseURI: LARK_API_HOST,
responseType: 'json',
});
if (!isServer()) githubClient.baseURI = `${API_Host}/api/GitHub/`;
githubClient.use(({ request }, next) => {
if (GithubToken)
request.headers = {
Authorization: `Bearer ${GithubToken}`,
...request.headers,
};
return next();
});
export { githubClient, RepositoryModel };
export type { GitHubIssue as Issue };
export const githubRawClient = new HTTPClient({
baseURI: `${ProxyBaseURL}/raw.githubusercontent.com/`,
responseType: 'arraybuffer',
});
export interface GithubSearchData<T> {
total_count: number;
incomplete_results: boolean;
items: T[];
}
export const makeGithubSearchCondition = (queryMap: DataObject) =>
Object.entries(queryMap)
.filter(([, value]) => !isEmpty(value))
.map(([key, value]) => `${key}:${value}`)
.join(' ');
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 larkClient.post<UploadedFile>(
'https://api.escuelajs.co/api/v1/files/upload',
form,
);
return body!.location;
}
export function fileURLOf(field: TableCellValue, cache = false) {
if (!(field instanceof Array) || !field[0]) return field + '';
const file = field[0] as TableCellMedia | TableCellAttachment;
let URI = `/api/Lark/file/${'file_token' in file ? file.file_token : file.attachmentToken}/${file.name}`;
if (cache) URI += '?cache=1';
return URI;
}