From 13dd9d10907bcd63f993f2bb12ffa8653d417250 Mon Sep 17 00:00:00 2001 From: GuoXiCheng <1377994267@qq.com> Date: Thu, 28 Dec 2023 16:49:31 +0800 Subject: [PATCH] add params --- jest.config.js | 2 +- package-lock.json | 7 +++++++ package.json | 1 + src/__tests__/user-storage.test.ts | 18 ++++++++++++++++-- src/request-lib/axios/axios-request.ts | 7 ++++--- src/request-lib/base/base-request.ts | 6 +++--- src/storage-lib/base/base-params.ts | 3 +++ src/storage-lib/base/base-storage.ts | 5 +++-- src/storage-lib/gitee/gitee-params.ts | 6 ++++++ src/storage-lib/gitee/gitee-storage.ts | 4 ++++ 10 files changed, 48 insertions(+), 11 deletions(-) create mode 100644 src/storage-lib/base/base-params.ts create mode 100644 src/storage-lib/gitee/gitee-params.ts diff --git a/jest.config.js b/jest.config.js index 926fd46..8815745 100644 --- a/jest.config.js +++ b/jest.config.js @@ -1,5 +1,5 @@ module.exports = { - testTimeout: 30000, + testTimeout: 60000, preset: 'ts-jest', testEnvironment: 'node', collectCoverageFrom: [ diff --git a/package-lock.json b/package-lock.json index 8420b31..a51c291 100644 --- a/package-lock.json +++ b/package-lock.json @@ -20,6 +20,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", @@ -3776,6 +3777,12 @@ "integrity": "sha512-KALDyEYgpY+Rlob/iriUtjV6d5Eq+Y191A5g4UqLAi8CyGP9N1+FdVbkc1SxKc2r4YAYqG8JzO2KGL+AizD70Q==", "dev": true }, + "node_modules/dayjs": { + "version": "1.11.10", + "resolved": "https://registry.npmjs.org/dayjs/-/dayjs-1.11.10.tgz", + "integrity": "sha512-vjAczensTgRcqDERK0SR2XMwsF/tSvnvlv6VcF2GIhg6Sx4yOIt/irsr1RDJsKiIyBzJDpCoXiWWq28MqH2cnQ==", + "dev": true + }, "node_modules/debug": { "version": "4.3.4", "resolved": "https://registry.npmjs.org/debug/-/debug-4.3.4.tgz", diff --git a/package.json b/package.json index 73183d4..43ce304 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/src/__tests__/user-storage.test.ts b/src/__tests__/user-storage.test.ts index 2629902..8b37d5c 100644 --- a/src/__tests__/user-storage.test.ts +++ b/src/__tests__/user-storage.test.ts @@ -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', () => { @@ -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 () => { @@ -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(); + }); }); }); \ No newline at end of file diff --git a/src/request-lib/axios/axios-request.ts b/src/request-lib/axios/axios-request.ts index 9893b92..0251cb1 100644 --- a/src/request-lib/axios/axios-request.ts +++ b/src/request-lib/axios/axios-request.ts @@ -12,7 +12,7 @@ export class AxiosRequest extends BaseRequest { this.axios = options.request as AxiosInstance; } - async sendRequest(method: RequestMethods, url: string, body?: string): Promise { + async sendRequest(method: RequestMethods, url: string, body?: string, params?: any): Promise { try { const response = await this.axios.request({ url, @@ -20,9 +20,10 @@ export class AxiosRequest extends BaseRequest { headers: { 'Authorization': `Bearer ${this.accessToken}` }, - ...(body && { data: { body } }) + ...(body && { data: { body } }), + ...(params && { params }) }); - + return response.data; } catch (error: any) { throw error; diff --git a/src/request-lib/base/base-request.ts b/src/request-lib/base/base-request.ts index bcc8bac..880d9c7 100644 --- a/src/request-lib/base/base-request.ts +++ b/src/request-lib/base/base-request.ts @@ -21,10 +21,10 @@ export abstract class BaseRequest { this.decryptFn = options.decryptFn; } - protected abstract sendRequest(method: RequestMethods, url: string, body?: string): Promise; + protected abstract sendRequest(method: RequestMethods, url: string, body?: string, params?: any): Promise; - get(url: string): Promise { - return this.sendRequest('GET', url); + get(url: string, params?: any): Promise { + return this.sendRequest('GET', url, undefined, params); } post(url: string, body: string): Promise { diff --git a/src/storage-lib/base/base-params.ts b/src/storage-lib/base/base-params.ts new file mode 100644 index 0000000..663084e --- /dev/null +++ b/src/storage-lib/base/base-params.ts @@ -0,0 +1,3 @@ +import { GiteeParams } from "../gitee/gitee-params"; + +export type BaseParams = GiteeParams; \ No newline at end of file diff --git a/src/storage-lib/base/base-storage.ts b/src/storage-lib/base/base-storage.ts index 8245a7c..2869cd8 100644 --- a/src/storage-lib/base/base-storage.ts +++ b/src/storage-lib/base/base-storage.ts @@ -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"; @@ -36,9 +37,9 @@ export abstract class BaseStorage { * Retrieves a list of items from the storage. * @returns A promise that resolves to an array of items. */ - async find(): Promise { + async find(params?: BaseParams): Promise { const url = this.getRoute(RouteType.find); - const response = await this.request.get(url); + const response = await this.request.get(url, params); return response.map((item) => this.deserialize(item)); } diff --git a/src/storage-lib/gitee/gitee-params.ts b/src/storage-lib/gitee/gitee-params.ts new file mode 100644 index 0000000..33a51b3 --- /dev/null +++ b/src/storage-lib/gitee/gitee-params.ts @@ -0,0 +1,6 @@ +export type GiteeParams = { + since?: string; + page?: number; + per_page?: number; + order?: 'asc' | 'desc'; +}; \ No newline at end of file diff --git a/src/storage-lib/gitee/gitee-storage.ts b/src/storage-lib/gitee/gitee-storage.ts index c530a39..bac23f3 100644 --- a/src/storage-lib/gitee/gitee-storage.ts +++ b/src/storage-lib/gitee/gitee-storage.ts @@ -1,6 +1,7 @@ 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 extends BaseStorage { @@ -8,4 +9,7 @@ export class GiteeStorage extends BaseStorage { super(request, issueNumber); } + async find(params?: GiteeParams): Promise { + return super.find(params); + } } \ No newline at end of file