From f064757af51e9c380061b7db4d28bb10b9e34067 Mon Sep 17 00:00:00 2001 From: Xicheng Guo Date: Thu, 7 Dec 2023 14:01:44 +0800 Subject: [PATCH] add ping --- src/__tests__/helper/start-test.ts | 14 +++++++++----- src/__tests__/ping.test.ts | 7 +++++++ src/request-lib/axios-request.ts | 10 ++++++---- src/request-lib/index.ts | 25 +++++++++++++++++++++---- src/request-lib/interfaces.ts | 12 +++++++++--- src/request-lib/wx-request.ts | 7 +++++-- 6 files changed, 57 insertions(+), 18 deletions(-) create mode 100644 src/__tests__/ping.test.ts diff --git a/src/__tests__/helper/start-test.ts b/src/__tests__/helper/start-test.ts index 8ac6547..1ab473b 100644 --- a/src/__tests__/helper/start-test.ts +++ b/src/__tests__/helper/start-test.ts @@ -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, diff --git a/src/__tests__/ping.test.ts b/src/__tests__/ping.test.ts new file mode 100644 index 0000000..59fc070 --- /dev/null +++ b/src/__tests__/ping.test.ts @@ -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); \ No newline at end of file diff --git a/src/request-lib/axios-request.ts b/src/request-lib/axios-request.ts index 6c1ec46..e21cc3c 100644 --- a/src/request-lib/axios-request.ts +++ b/src/request-lib/axios-request.ts @@ -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); @@ -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'); + } } diff --git a/src/request-lib/index.ts b/src/request-lib/index.ts index 7f88906..4a85e80 100644 --- a/src/request-lib/index.ts +++ b/src/request-lib/index.ts @@ -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(options: TinyRequestOptions) { 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'); } -} \ No newline at end of file +} + +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'; +} diff --git a/src/request-lib/interfaces.ts b/src/request-lib/interfaces.ts index 3d41c9e..21465d3 100644 --- a/src/request-lib/interfaces.ts +++ b/src/request-lib/interfaces.ts @@ -17,11 +17,17 @@ export interface WxRequestOptions { export interface TinyRequest { get(url: string): Promise; post(url: string): void; + ping(): Promise; }; 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 { + requestType: T; + request: RequestTypeMap[T]; accessToken: string; } \ No newline at end of file diff --git a/src/request-lib/wx-request.ts b/src/request-lib/wx-request.ts index 1a6c4be..b2e7d33 100644 --- a/src/request-lib/wx-request.ts +++ b/src/request-lib/wx-request.ts @@ -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); @@ -32,4 +31,8 @@ export class WxRequest implements TinyRequest { // } // }); } + + async ping() { + return; + } }