/
server.ts
94 lines (82 loc) · 2.33 KB
/
server.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
import { Container } from 'typedi';
import { PowerpackUserInfo, IPowerpackService } from '@/service/common/powerpack';
import { getLanguage } from './locales/index';
import { LOCAL_USER_PREFERENCE_LOCALE_KEY } from '@/common/modelTypes/userPreference';
import { extend } from 'umi-request';
import config from '@/config';
import { localStorageService } from './chrome/storage';
import { LOCAL_ACCESS_TOKEN_LOCALE_KEY } from './modelTypes/userPreference';
import { IResponse } from './types';
const request = extend({
prefix: `${config.serverHost}/api/`,
timeout: 10000,
headers: {},
});
request.interceptors.request.use(
(url, options) => {
const powerpackService = Container.get(IPowerpackService);
return {
url,
options: {
...options,
headers: {
...options.headers,
token: powerpackService.accessToken || '',
locale: localStorageService.get(LOCAL_USER_PREFERENCE_LOCALE_KEY, getLanguage()),
},
},
};
},
{ global: false }
);
request.interceptors.response.use(
response => {
if (response.clone().status === 401) {
localStorageService.delete(LOCAL_ACCESS_TOKEN_LOCALE_KEY);
}
return response;
},
{ global: false }
);
request.interceptors.response.use(
async response => {
if (response.clone().status !== 200) {
const data: IResponse<unknown> = await response.clone().json();
if (data.message) {
throw new Error(data.message);
}
}
return response;
},
{ global: false }
);
export const getUserInfo = () => {
return request.get<IResponse<PowerpackUserInfo>>('user');
};
export interface PostMailRequestBody {
to: string;
subject: string;
html?: string;
text?: string;
}
export const postMail = (data: PostMailRequestBody) => {
return request.post('service/email', { data });
};
export interface SendToKindleRequestBody {
to: string;
title: string;
content: string;
}
export const sentToKindle = (data: SendToKindleRequestBody) => {
return request.post('service/sendToKindle', { data });
};
export const refresh = () => {
return request.get<IResponse<string>>('refresh');
};
export interface OCRRequestBody {
image: string;
language_type: 'CHN_ENG' | 'ENG' | 'JAP' | 'GER';
}
export const ocr = (data: OCRRequestBody) => {
return request.post<IResponse<string>>('service/ocr', { data });
};