Skip to content

Commit

Permalink
Merge pull request #55 from GuoXiCheng/dev-h
Browse files Browse the repository at this point in the history
add params
  • Loading branch information
GuoXiCheng authored Dec 28, 2023
2 parents 715e81f + 13dd9d1 commit a9bbe0b
Show file tree
Hide file tree
Showing 10 changed files with 48 additions and 11 deletions.
2 changes: 1 addition & 1 deletion jest.config.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module.exports = {
testTimeout: 30000,
testTimeout: 60000,
preset: 'ts-jest',
testEnvironment: 'node',
collectCoverageFrom: [
Expand Down
7 changes: 7 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
"@types/jest": "^29.5.9",
"axios": "^1.6.2",
"crypto-js": "^4.2.0",
"dayjs": "^1.11.10",
"dotenv": "^16.3.1",
"jest": "^29.7.0",
"nyc": "^15.1.0",
Expand Down
18 changes: 16 additions & 2 deletions src/__tests__/user-storage.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
import { PlainObject } from "../storage-lib/base/plain-object";
import { UserModel } from "./helper/user-model";
import { User } from "./helper/user-storage";
import dayjs from 'dayjs';
import utc from 'dayjs/plugin/utc';
import timezone from 'dayjs/plugin/timezone';

dayjs.extend(utc);
dayjs.extend(timezone);

describe('Test User Storage', () => {

Expand Down Expand Up @@ -113,7 +119,7 @@ describe('Test User Storage', () => {
expect(result.length).toEqual(10);

// 因为是并行创建,所以批量新增的数据是无序的
expect((await User.find()).map(item=>item.name)).not.toEqual(userList.map(item=>item.name));
expect((await User.find()).map(item => item.name)).not.toEqual(userList.map(item => item.name));
});

test('Test createAll User orderly', async () => {
Expand All @@ -122,6 +128,14 @@ describe('Test User Storage', () => {
expect(result.length).toEqual(10);

// 因为是顺序创建,所以批量新增的数据是有序的
expect((await User.find()).map(item=>item.name)).toEqual(userList.map(item=>item.name));
expect((await User.find()).map(item => item.name)).toEqual(userList.map(item => item.name));
});

test('Test find user with params since', async () => {
const time = dayjs().tz('Asia/Shanghai').subtract(1, 'second').format();
const result = await User.find({ since: time });
result.forEach(item => {
expect(dayjs(item.created_at).isAfter(time)).toBeTruthy();
});
});
});
7 changes: 4 additions & 3 deletions src/request-lib/axios/axios-request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,18 @@ export class AxiosRequest extends BaseRequest {
this.axios = options.request as AxiosInstance;
}

async sendRequest<T>(method: RequestMethods, url: string, body?: string): Promise<T> {
async sendRequest<T>(method: RequestMethods, url: string, body?: string, params?: any): Promise<T> {
try {
const response = await this.axios.request<T>({
url,
method,
headers: {
'Authorization': `Bearer ${this.accessToken}`
},
...(body && { data: { body } })
...(body && { data: { body } }),
...(params && { params })
});

return response.data;
} catch (error: any) {
throw error;
Expand Down
6 changes: 3 additions & 3 deletions src/request-lib/base/base-request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@ export abstract class BaseRequest {
this.decryptFn = options.decryptFn;
}

protected abstract sendRequest<T>(method: RequestMethods, url: string, body?: string): Promise<T>;
protected abstract sendRequest<T>(method: RequestMethods, url: string, body?: string, params?: any): Promise<T>;

get<T>(url: string): Promise<T> {
return this.sendRequest<T>('GET', url);
get<T>(url: string, params?: any): Promise<T> {
return this.sendRequest<T>('GET', url, undefined, params);
}

post<T>(url: string, body: string): Promise<T> {
Expand Down
3 changes: 3 additions & 0 deletions src/storage-lib/base/base-params.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { GiteeParams } from "../gitee/gitee-params";

export type BaseParams = GiteeParams;
5 changes: 3 additions & 2 deletions src/storage-lib/base/base-storage.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { BaseRequest } from "../../request-lib";
import { BaseComment } from "./base-comment";
import { BaseModel } from "./base-model";
import { BaseParams } from "./base-params";
import { PlainObject } from "./plain-object";
import { RouteType } from "./route-type";

Expand Down Expand Up @@ -36,9 +37,9 @@ export abstract class BaseStorage<T extends BaseModel> {
* Retrieves a list of items from the storage.
* @returns A promise that resolves to an array of items.
*/
async find(): Promise<T[]> {
async find(params?: BaseParams): Promise<T[]> {
const url = this.getRoute(RouteType.find);
const response = await this.request.get<BaseComment[]>(url);
const response = await this.request.get<BaseComment[]>(url, params);
return response.map((item) => this.deserialize<T>(item));
}

Expand Down
6 changes: 6 additions & 0 deletions src/storage-lib/gitee/gitee-params.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export type GiteeParams = {
since?: string;
page?: number;
per_page?: number;
order?: 'asc' | 'desc';
};
4 changes: 4 additions & 0 deletions src/storage-lib/gitee/gitee-storage.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
import { BaseRequest } from "../../request-lib";
import { BaseModel } from "../base/base-model";
import { BaseStorage } from "../base/base-storage";
import { GiteeParams } from "./gitee-params";

export class GiteeStorage<T extends BaseModel> extends BaseStorage<T> {

constructor(protected request: BaseRequest, protected issueNumber: string) {
super(request, issueNumber);
}

async find(params?: GiteeParams): Promise<T[]> {
return super.find(params);
}
}

0 comments on commit a9bbe0b

Please sign in to comment.