Skip to content

Commit

Permalink
Merge pull request #30 from GuoXiCheng/dev-c
Browse files Browse the repository at this point in the history
add ping
  • Loading branch information
GuoXiCheng authored Dec 7, 2023
2 parents 5a132d8 + f064757 commit d1de1ea
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 18 deletions.
14 changes: 9 additions & 5 deletions src/__tests__/helper/start-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,17 @@ export class StartTest {

}

static getGiteeRequest() {
return createRequest({
requestType: RequestType.axios,
request: axios,
accessToken: process.env.GITEE_TOKEN as string
});
}

static getGiteeOptions(): GiteeStorageOptions {
return {
request: createRequest({
requestType: RequestType.axios,
request: axios,
accessToken: process.env.GITEE_TOKEN as string
}),
request: this.getGiteeRequest(),
owner: process.env.GITEE_OWNER as string,
repo: process.env.GITEE_REPO as string,
number: process.env.GITEE_NUMBER as string,
Expand Down
7 changes: 7 additions & 0 deletions src/__tests__/ping.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { StartTest } from "./helper/start-test";

test('Test Ping Gitee', async () => {
const request = StartTest.getGiteeRequest();
const res = await request.ping();
expect(res).not.toBeNull();
}, 30000);
10 changes: 6 additions & 4 deletions src/request-lib/axios-request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@ export class AxiosRequest implements TinyRequest {
return new Promise((resolve, reject) => {
this.axios.get(url, {
headers: {
'Authorization': this.accessToken,
'PRIVATE-TOKEN': this.accessToken
'Authorization': this.accessToken
}
}).then((res) => {
resolve(res.data as T);
Expand All @@ -22,9 +21,12 @@ export class AxiosRequest implements TinyRequest {
post(url: string) {
this.axios.post(url, undefined, {
headers: {
'Authorization': this.accessToken,
'PRIVATE-TOKEN': this.accessToken
'Authorization': this.accessToken
}
});
}

async ping() {
return this.get('https://gitee.com/api/v5/user');
}
}
25 changes: 21 additions & 4 deletions src/request-lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,31 @@ import { RequestType } from "../enums";
* @returns The created request.
* @throws Error if the request lib type is invalid.
*/
export function createRequest(options: TinyRequestOptions) {
export function createRequest<T extends RequestType>(options: TinyRequestOptions<T>) {
const { requestType, request, accessToken } = options;
switch (requestType) {
case RequestType.axios:
return new AxiosRequestFactory().createRequest(request as AxiosInstance, accessToken);
if (isAxiosInstance(request)) {
return new AxiosRequestFactory().createRequest(request, accessToken);
}
case RequestType.wx:
return new WxRequestFactory().createRequest(request as WxInstance, accessToken);
if (isWxInstance(request)) {
return new WxRequestFactory().createRequest(request, accessToken);
}
default:
throw new Error('invalid request lib type');
}
}
}

function isAxiosInstance(instance: any): instance is AxiosInstance {
return instance
&& typeof instance.get === 'function'
&& typeof instance.post === 'function'
&& typeof instance.put === 'function'
&& typeof instance.delete === 'function'
&& typeof instance.patch === 'function';
}

function isWxInstance(instance: any): instance is WxInstance {
return instance && typeof instance.request === 'function';
}
12 changes: 9 additions & 3 deletions src/request-lib/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,17 @@ export interface WxRequestOptions {
export interface TinyRequest {
get<T>(url: string): Promise<T>;
post(url: string): void;
ping(): Promise<any>;
};

export type RequestInstance = WxInstance | AxiosInstance;
export interface TinyRequestOptions {
requestType: keyof typeof RequestType;
request: RequestInstance;

type RequestTypeMap = {
[RequestType.wx]: WxInstance;
[RequestType.axios]: AxiosInstance;
}
export interface TinyRequestOptions<T extends RequestType> {
requestType: T;
request: RequestTypeMap[T];
accessToken: string;
}
7 changes: 5 additions & 2 deletions src/request-lib/wx-request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@ export class WxRequest implements TinyRequest {
url,
method: 'GET',
header: {
'Authorization': this.accessToken,
'PRIVATE-TOKEN': this.accessToken
'Authorization': this.accessToken
},
success: (res: {data: string | Object | ArrayBuffer, statusCode: number}) => {
resolve(res as T);
Expand All @@ -32,4 +31,8 @@ export class WxRequest implements TinyRequest {
// }
// });
}

async ping() {
return;
}
}

0 comments on commit d1de1ea

Please sign in to comment.