diff --git a/src/__tests__/helper/user-model.ts b/src/__tests__/helper/user-model.ts new file mode 100644 index 0000000..36a5d03 --- /dev/null +++ b/src/__tests__/helper/user-model.ts @@ -0,0 +1,6 @@ +import { TinyModel } from "../../storage-lib/interfaces"; + +export class UserModel extends TinyModel { + name: string; + age: number; +} \ No newline at end of file diff --git a/src/__tests__/helper/user-storage.ts b/src/__tests__/helper/user-storage.ts index 8fbff0b..6caba67 100644 --- a/src/__tests__/helper/user-storage.ts +++ b/src/__tests__/helper/user-storage.ts @@ -1,7 +1,9 @@ import { StartTest } from './start-test'; import { GiteeStorage } from '../../storage-lib/gitee-storage'; +import { UserModel } from './user-model'; -export class UserStorage extends GiteeStorage { +export class UserStorage extends GiteeStorage { + issueNumber = "I8H4X2"; constructor() { super(new StartTest().getRequest()); } diff --git a/src/__tests__/request-lib.test.ts b/src/__tests__/request-lib.test.ts deleted file mode 100644 index 8c9f289..0000000 --- a/src/__tests__/request-lib.test.ts +++ /dev/null @@ -1,9 +0,0 @@ -import 'dotenv/config'; -import { StartTest } from './helper/start-test'; - - -test('Test TinyRequestInstance', async () => { - const request = new StartTest().getRequest(); - const detail = await request.get(process.env.GITEE_GET_ALL_URL as string); - expect(detail.length).toBeGreaterThan(0); -}, 30000); \ No newline at end of file diff --git a/src/__tests__/storage-lib.test.ts b/src/__tests__/user-storage.test.ts similarity index 77% rename from src/__tests__/storage-lib.test.ts rename to src/__tests__/user-storage.test.ts index 98c913f..87bfb76 100644 --- a/src/__tests__/storage-lib.test.ts +++ b/src/__tests__/user-storage.test.ts @@ -2,6 +2,6 @@ import { UserStorage } from "./helper/user-storage"; test('Test Storage Lib', async () => { const userStorage = new UserStorage(); - const detail: any[] = await userStorage.findAll(); + const detail = await userStorage.findAll(); expect(detail.length).toBeGreaterThan(0); }, 30000); \ No newline at end of file diff --git a/src/request-lib/axios-request.ts b/src/request-lib/axios-request.ts index 0d31698..6c1ec46 100644 --- a/src/request-lib/axios-request.ts +++ b/src/request-lib/axios-request.ts @@ -4,14 +4,19 @@ import { TinyRequest } from './interfaces'; export class AxiosRequest implements TinyRequest { constructor(private axios: AxiosInstance, private accessToken: string) { } - async get(url: string) { - const result = await this.axios.get(url, { - headers: { - 'Authorization': this.accessToken, - 'PRIVATE-TOKEN': this.accessToken - } + async get(url: string): Promise { + return new Promise((resolve, reject) => { + this.axios.get(url, { + headers: { + 'Authorization': this.accessToken, + 'PRIVATE-TOKEN': this.accessToken + } + }).then((res) => { + resolve(res.data as T); + }).catch(error => { + reject(error); + }); }); - return result.data; } post(url: string) { diff --git a/src/request-lib/interfaces.ts b/src/request-lib/interfaces.ts index 519f373..faaa5d3 100644 --- a/src/request-lib/interfaces.ts +++ b/src/request-lib/interfaces.ts @@ -12,7 +12,7 @@ export interface WxRequestOptions { } export interface TinyRequest { - get(url: string): Promise; + get(url: string): Promise; post(url: string): void; }; diff --git a/src/request-lib/wx-request.ts b/src/request-lib/wx-request.ts index 3f33e36..1a6c4be 100644 --- a/src/request-lib/wx-request.ts +++ b/src/request-lib/wx-request.ts @@ -3,7 +3,7 @@ import { WxInstance, TinyRequest } from './interfaces'; export class WxRequest implements TinyRequest { constructor(private wx: WxInstance, private accessToken: string) { } - async get(url: string) { + async get(url: string): Promise { return new Promise((resolve, reject) => { this.wx.request({ url, @@ -13,7 +13,7 @@ export class WxRequest implements TinyRequest { 'PRIVATE-TOKEN': this.accessToken }, success: (res: {data: string | Object | ArrayBuffer, statusCode: number}) => { - resolve(res); + resolve(res as T); }, fail: (errMsg: string, errNo: number) => { reject(errMsg); diff --git a/src/storage-lib/gitee-storage.ts b/src/storage-lib/gitee-storage.ts index 09d0553..f881505 100644 --- a/src/storage-lib/gitee-storage.ts +++ b/src/storage-lib/gitee-storage.ts @@ -1,17 +1,32 @@ import { TinyRequest } from "../request-lib/interfaces"; -import { TinyStorage } from "./interfaces"; +import { TinyModel, TinyStorage } from "./interfaces"; -export class GiteeStorage implements TinyStorage { +export interface GiteeResponse { + id: number; + body: string; + user: object; + created_at: string; + updated_at: string; +} + +export abstract class GiteeStorage implements TinyStorage { protected baseUrl = "https://gitee.com"; + protected issueNumber!: string; constructor(private requestInstance: TinyRequest) { } async findOne(): Promise { this.requestInstance.get(""); } - async findAll(): Promise { - const url = `${this.baseUrl}/api/v5/repos/guoxicheng/tiny-crud/issues/I8H4X2/comments`; - return this.requestInstance.get(url); + async findAll(): Promise { + const url = `${this.baseUrl}/api/v5/repos/guoxicheng/tiny-crud/issues/${this.issueNumber}/comments`; + const response = await this.requestInstance.get(url); + return response.map(item => ({ + id: item.id, + ...JSON.parse(item.body), + created_at: item.created_at, + updated_at: item.updated_at + })) as T[]; } } \ No newline at end of file diff --git a/src/storage-lib/interfaces.ts b/src/storage-lib/interfaces.ts index 142a493..a695255 100644 --- a/src/storage-lib/interfaces.ts +++ b/src/storage-lib/interfaces.ts @@ -1,4 +1,10 @@ -export interface TinyStorage { +export interface TinyStorage { findOne(): void; - findAll(): void; + findAll(): Promise; +} + +export class TinyModel { + id!: number; + created_at!: string; + updated_at!: string; } \ No newline at end of file