Skip to content

Commit

Permalink
refactor: remove axios
Browse files Browse the repository at this point in the history
  • Loading branch information
DiamondYuan committed Oct 18, 2021
1 parent 8ae6291 commit 5566aa3
Showing 1 changed file with 23 additions and 13 deletions.
36 changes: 23 additions & 13 deletions src/common/backend/services/yuque_oauth/service.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { RequestHelper } from './../../../../service/request/common/request';
import { IBasicRequestService } from './../../../../service/common/request';
import { Container } from 'typedi';
import { DocumentService } from './../../index';
import axios, { AxiosInstance } from 'axios';
import { generateUuid } from '@web-clipper/shared/lib/uuid';
import * as qs from 'qs';
import md5 from '@web-clipper/shared/lib/md5';
Expand All @@ -19,19 +21,26 @@ const HOST = 'https://www.yuque.com';
const BASE_URL = `${HOST}/api/v2/`;

export default class YuqueDocumentService implements DocumentService {
private request: AxiosInstance;
private request: RequestHelper;
private userInfo?: YuqueUserInfoResponse;
private config: YuqueBackendServiceConfig;
private repositories: YuqueRepository[];

constructor({ access_token, repositoryType = RepositoryType.all }: YuqueBackendServiceConfig) {
this.config = { access_token, repositoryType };
this.request = axios.create({
// this.request = axios.create({
// baseURL: BASE_URL,
// headers: { 'X-Auth-Token': access_token },
// timeout: 5000,
// transformResponse: [data => JSON.parse(data).data],
// withCredentials: true,
// });
this.request = new RequestHelper({
baseURL: BASE_URL,
headers: { 'X-Auth-Token': access_token },
timeout: 5000,
transformResponse: [data => JSON.parse(data).data],
withCredentials: true,
headers: {
'X-Auth-Token': access_token,
},
request: Container.get(IBasicRequestService),
});
this.repositories = [];
}
Expand Down Expand Up @@ -90,9 +99,11 @@ export default class YuqueDocumentService implements DocumentService {
};
const response = await this.request.post<YuqueCreateDocumentResponse>(
`/repos/${repositoryId}/docs`,
qs.stringify(request)
{
data: request,
}
);
const data = response.data;
const data = response;
return {
href: `${HOST}/${repository.namespace}/${data.slug}`,
repositoryId,
Expand All @@ -104,13 +115,12 @@ export default class YuqueDocumentService implements DocumentService {
if (!this.userInfo) {
this.userInfo = await this.getYuqueUserInfo();
}
return (await this.request.get<YuqueGroupResponse[]>(`users/${this.userInfo.login}/groups`))
.data;
return this.request.get<YuqueGroupResponse[]>(`users/${this.userInfo.login}/groups`);
};

private getYuqueUserInfo = async () => {
const response = await this.request.get<YuqueUserInfoResponse>('user');
return response.data;
return response;
};

private getAllRepositories = async (isGroup: boolean, groupId: number, groupName: string) => {
Expand Down Expand Up @@ -139,7 +149,7 @@ export default class YuqueDocumentService implements DocumentService {
const response = await this.request.get<YuqueRepositoryResponse[]>(
`${isGroup ? 'groups' : 'users'}/${slug}/repos?${qs.stringify(query)}`
);
return response.data;
return response;
} catch (error) {
console.log(error);
return [];
Expand Down

0 comments on commit 5566aa3

Please sign in to comment.