Skip to content

Commit

Permalink
Merge pull request #28 from GuoXiCheng/dev-c
Browse files Browse the repository at this point in the history
update
  • Loading branch information
GuoXiCheng authored Dec 5, 2023
2 parents aa10f64 + a5bca44 commit 62c303c
Show file tree
Hide file tree
Showing 9 changed files with 53 additions and 28 deletions.
6 changes: 6 additions & 0 deletions src/__tests__/helper/user-model.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { TinyModel } from "../../storage-lib/interfaces";

export class UserModel extends TinyModel {
name: string;
age: number;
}
4 changes: 3 additions & 1 deletion src/__tests__/helper/user-storage.ts
Original file line number Diff line number Diff line change
@@ -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<UserModel> {
issueNumber = "I8H4X2";
constructor() {
super(new StartTest().getRequest());
}
Expand Down
9 changes: 0 additions & 9 deletions src/__tests__/request-lib.test.ts

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -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);
19 changes: 12 additions & 7 deletions src/request-lib/axios-request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<T>(url: string): Promise<T> {
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) {
Expand Down
2 changes: 1 addition & 1 deletion src/request-lib/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export interface WxRequestOptions {
}

export interface TinyRequest {
get(url: string): Promise<any>;
get<T>(url: string): Promise<T>;
post(url: string): void;
};

Expand Down
4 changes: 2 additions & 2 deletions src/request-lib/wx-request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<T>(url: string): Promise<T> {
return new Promise((resolve, reject) => {
this.wx.request({
url,
Expand All @@ -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);
Expand Down
25 changes: 20 additions & 5 deletions src/storage-lib/gitee-storage.ts
Original file line number Diff line number Diff line change
@@ -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<T> implements TinyStorage<T> {
protected baseUrl = "https://gitee.com";
protected issueNumber!: string;
constructor(private requestInstance: TinyRequest) { }

async findOne(): Promise<void> {
this.requestInstance.get("");
}

async findAll(): Promise<any> {
const url = `${this.baseUrl}/api/v5/repos/guoxicheng/tiny-crud/issues/I8H4X2/comments`;
return this.requestInstance.get(url);
async findAll(): Promise<T[]> {
const url = `${this.baseUrl}/api/v5/repos/guoxicheng/tiny-crud/issues/${this.issueNumber}/comments`;
const response = await this.requestInstance.get<GiteeResponse[]>(url);
return response.map(item => ({
id: item.id,
...JSON.parse(item.body),
created_at: item.created_at,
updated_at: item.updated_at
})) as T[];
}

}
10 changes: 8 additions & 2 deletions src/storage-lib/interfaces.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
export interface TinyStorage {
export interface TinyStorage<T> {
findOne(): void;
findAll(): void;
findAll(): Promise<T[]>;
}

export class TinyModel {
id!: number;
created_at!: string;
updated_at!: string;
}

0 comments on commit 62c303c

Please sign in to comment.