From 1a5edd2d321ed974a0003342fb1f0d7bfd7051c1 Mon Sep 17 00:00:00 2001 From: Xicheng Guo Date: Mon, 4 Dec 2023 15:18:29 +0800 Subject: [PATCH] add storage-lib --- jest.config.js | 1 + src/__tests__/helper/start-test.ts | 14 ++++++++++++++ src/__tests__/helper/user-storage.ts | 8 ++++++++ src/__tests__/index.test.ts | 3 --- src/__tests__/request-lib.test.ts | 8 +++----- src/__tests__/storage-lib.test.ts | 7 +++++++ src/enums.ts | 10 ++++++++++ src/index.ts | 13 ------------- src/request-lib/index.ts | 3 ++- src/storage-lib/gitee-storage.ts | 17 +++++++++++++++++ src/storage-lib/github-storage.ts | 0 src/storage-lib/gitlab-storage.ts | 0 src/storage-lib/interfaces.ts | 4 ++++ src/types.ts | 4 ++++ 14 files changed, 70 insertions(+), 22 deletions(-) create mode 100644 src/__tests__/helper/start-test.ts create mode 100644 src/__tests__/helper/user-storage.ts delete mode 100644 src/__tests__/index.test.ts create mode 100644 src/__tests__/storage-lib.test.ts create mode 100644 src/enums.ts create mode 100644 src/storage-lib/gitee-storage.ts create mode 100644 src/storage-lib/github-storage.ts create mode 100644 src/storage-lib/gitlab-storage.ts create mode 100644 src/storage-lib/interfaces.ts create mode 100644 src/types.ts diff --git a/jest.config.js b/jest.config.js index 8f32653..4dd4e89 100644 --- a/jest.config.js +++ b/jest.config.js @@ -6,4 +6,5 @@ module.exports = { "!src/**/*.d.ts", // 排除 TypeScript 声明文件 "!src/__tests__/**/*.ts" ], + testMatch: ["/src/__tests__/**/*.test.ts"] }; diff --git a/src/__tests__/helper/start-test.ts b/src/__tests__/helper/start-test.ts new file mode 100644 index 0000000..f9e822e --- /dev/null +++ b/src/__tests__/helper/start-test.ts @@ -0,0 +1,14 @@ +import axios from 'axios'; +import 'dotenv/config'; +import { TinyRequestInstance } from '../../request-lib'; +import { RequestLib } from '../../enums'; + +export class StartTest { + constructor() { + + } + + getRequest() { + return TinyRequestInstance(RequestLib.axios, axios, process.env.GITEE_TOKEN as string); + } +} \ No newline at end of file diff --git a/src/__tests__/helper/user-storage.ts b/src/__tests__/helper/user-storage.ts new file mode 100644 index 0000000..8fbff0b --- /dev/null +++ b/src/__tests__/helper/user-storage.ts @@ -0,0 +1,8 @@ +import { StartTest } from './start-test'; +import { GiteeStorage } from '../../storage-lib/gitee-storage'; + +export class UserStorage extends GiteeStorage { + constructor() { + super(new StartTest().getRequest()); + } +} \ No newline at end of file diff --git a/src/__tests__/index.test.ts b/src/__tests__/index.test.ts deleted file mode 100644 index a820954..0000000 --- a/src/__tests__/index.test.ts +++ /dev/null @@ -1,3 +0,0 @@ -test('example', ()=>{ - expect(1).toBe(1); -}); \ No newline at end of file diff --git a/src/__tests__/request-lib.test.ts b/src/__tests__/request-lib.test.ts index 6742c17..8c9f289 100644 --- a/src/__tests__/request-lib.test.ts +++ b/src/__tests__/request-lib.test.ts @@ -1,11 +1,9 @@ import 'dotenv/config'; -import { TinyRequestInstance } from '../request-lib'; -import { RequestLib } from '..'; -import axios from 'axios'; +import { StartTest } from './helper/start-test'; test('Test TinyRequestInstance', async () => { - const instance = TinyRequestInstance(RequestLib.axios, axios, process.env.GITEE_TOKEN as string); - const detail = await instance.get(process.env.GITEE_GET_ALL_URL as string); + 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__/storage-lib.test.ts new file mode 100644 index 0000000..98c913f --- /dev/null +++ b/src/__tests__/storage-lib.test.ts @@ -0,0 +1,7 @@ +import { UserStorage } from "./helper/user-storage"; + +test('Test Storage Lib', async () => { + const userStorage = new UserStorage(); + const detail: any[] = await userStorage.findAll(); + expect(detail.length).toBeGreaterThan(0); +}, 30000); \ No newline at end of file diff --git a/src/enums.ts b/src/enums.ts new file mode 100644 index 0000000..a41ac36 --- /dev/null +++ b/src/enums.ts @@ -0,0 +1,10 @@ +export enum StoragePlatform { + gitee = "gitee", + github = "github", + gitlab = "gitlab" +} + +export enum RequestLib { + axios = "axios", + wx = "wx" +} \ No newline at end of file diff --git a/src/index.ts b/src/index.ts index 334b383..f95a9cd 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,16 +1,3 @@ import {TinyRequestInstance} from "./request-lib"; -export enum StoragePlatform { - gitee = "gitee", - github = "github", - gitlab = "gitlab" -} - -export enum RequestLib { - axios = "axios", - wx = "wx" -} - -export type StoragePlatformType = keyof typeof StoragePlatform; -export type RequestLibType = keyof typeof RequestLib; export {TinyRequestInstance}; diff --git a/src/request-lib/index.ts b/src/request-lib/index.ts index a33cbef..0cb7d8b 100644 --- a/src/request-lib/index.ts +++ b/src/request-lib/index.ts @@ -1,7 +1,8 @@ import { AxiosInstance } from "axios"; -import { RequestLib, RequestLibType } from ".."; import { AxiosRequestFactory, RequestInstance, WxRequestFactory } from "./request-factories"; import { WxInstance } from "./interfaces"; +import { RequestLibType } from "../types"; +import { RequestLib } from "../enums"; export function TinyRequestInstance(requestLibType: RequestLibType, instance: RequestInstance, accessToken: string) { switch (requestLibType) { diff --git a/src/storage-lib/gitee-storage.ts b/src/storage-lib/gitee-storage.ts new file mode 100644 index 0000000..09d0553 --- /dev/null +++ b/src/storage-lib/gitee-storage.ts @@ -0,0 +1,17 @@ +import { TinyRequest } from "../request-lib/interfaces"; +import { TinyStorage } from "./interfaces"; + +export class GiteeStorage implements TinyStorage { + protected baseUrl = "https://gitee.com"; + 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); + } + +} \ No newline at end of file diff --git a/src/storage-lib/github-storage.ts b/src/storage-lib/github-storage.ts new file mode 100644 index 0000000..e69de29 diff --git a/src/storage-lib/gitlab-storage.ts b/src/storage-lib/gitlab-storage.ts new file mode 100644 index 0000000..e69de29 diff --git a/src/storage-lib/interfaces.ts b/src/storage-lib/interfaces.ts new file mode 100644 index 0000000..142a493 --- /dev/null +++ b/src/storage-lib/interfaces.ts @@ -0,0 +1,4 @@ +export interface TinyStorage { + findOne(): void; + findAll(): void; +} \ No newline at end of file diff --git a/src/types.ts b/src/types.ts new file mode 100644 index 0000000..26f0ab7 --- /dev/null +++ b/src/types.ts @@ -0,0 +1,4 @@ +import { RequestLib, StoragePlatform } from "./enums"; + +export type StoragePlatformType = keyof typeof StoragePlatform; +export type RequestLibType = keyof typeof RequestLib; \ No newline at end of file