diff --git a/package.json b/package.json index cf7b23c..a043a98 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "tiny-crud", - "version": "1.0.11", + "version": "1.0.12", "description": "", "main": "dist/bundle.cjs.js", "module": "dist/bundle.esm.js", diff --git a/src/__tests__/index.test.ts b/src/__tests__/index.test.ts index 6701bc7..a820954 100644 --- a/src/__tests__/index.test.ts +++ b/src/__tests__/index.test.ts @@ -1,21 +1,3 @@ test('example', ()=>{ expect(1).toBe(1); -}); -// import { TinyCRUD } from "../index"; -// import 'dotenv/config'; -// import axios from "axios"; -// test("index gitee", async ()=>{ -// const tinyCRUD = new TinyCRUD({ -// owner: "guoxicheng", -// repo: "tiny-crud", -// issue_number: "I8H4X2", -// base_url: "https://gitee.com", -// request_lib: "axios", -// access_token: process.env.GITEE_TOKEN as string, -// git_platform: "gitee", -// request_object: axios -// }); - -// const detail = await tinyCRUD.createOne("测试"); -// expect(detail).toHaveProperty("body", "测试"); -// }, 30000); \ No newline at end of file +}); \ No newline at end of file diff --git a/src/__tests__/request-lib.test.ts b/src/__tests__/request-lib.test.ts new file mode 100644 index 0000000..6742c17 --- /dev/null +++ b/src/__tests__/request-lib.test.ts @@ -0,0 +1,11 @@ +import 'dotenv/config'; +import { TinyRequestInstance } from '../request-lib'; +import { RequestLib } from '..'; +import axios from 'axios'; + + +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); + expect(detail.length).toBeGreaterThan(0); +}, 30000); \ No newline at end of file diff --git a/src/index.ts b/src/index.ts index 7b96d1f..334b383 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,3 +1,4 @@ +import {TinyRequestInstance} from "./request-lib"; export enum StoragePlatform { gitee = "gitee", github = "github", @@ -11,61 +12,5 @@ export enum RequestLib { export type StoragePlatformType = keyof typeof StoragePlatform; export type RequestLibType = keyof typeof RequestLib; -// export type TinyCRUDConfig = { -// base_url: string; -// owner: string; -// repo: string; -// issue_number: string; -// access_token: string; -// git_platform: "gitee" | "github" | "gitlab"; -// request_lib: "axios" | "wx"; -// request_object?: any; -// } -// export class TinyCRUD { -// constructor(private config: TinyCRUDConfig) { +export {TinyRequestInstance}; -// } - -// async createOne(body: string | Object) { -// switch (this.config.git_platform) { -// case "gitee": -// const url = `${this.config.base_url}/api/v5/repos/${this.config.owner}/${this.config.repo}/issues/${this.config.issue_number}/comments`; -// if (this.config.request_lib === "axios") { - -// const result = await this.config.request_object.post(url, { body }, { headers: { 'Authorization': this.config.access_token } }); -// return result.data; -// } else if (this.config.request_lib === "wx") { -// return new Promise(resolve => { -// this.config.request_object.request({ -// url: url, -// method: 'POST', -// header: { -// 'Authorization': this.config.access_token -// }, -// data: { -// body -// }, -// async success(res: any) { -// resolve(res); -// } -// }); -// }); -// } -// break; -// case "github": -// if (this.config.request_lib === "axios") { - -// } else if (this.config.request_lib === "wx") { } -// break; -// case "gitlab": -// if (this.config.request_lib === "axios") { - -// } else if (this.config.request_lib === "wx") { } -// break; -// } -// } - -// findById(id: number) { - -// } -// } diff --git a/src/request-lib/axios-request.ts b/src/request-lib/axios-request.ts index 74902b7..0d31698 100644 --- a/src/request-lib/axios-request.ts +++ b/src/request-lib/axios-request.ts @@ -2,13 +2,24 @@ import { AxiosInstance } from 'axios'; import { TinyRequest } from './interfaces'; export class AxiosRequest implements TinyRequest { - constructor(private axios: AxiosInstance) { } + constructor(private axios: AxiosInstance, private accessToken: string) { } - get(url: string) { - this.axios.get(url); + async get(url: string) { + const result = await this.axios.get(url, { + headers: { + 'Authorization': this.accessToken, + 'PRIVATE-TOKEN': this.accessToken + } + }); + return result.data; } post(url: string) { - this.axios.post(url); + this.axios.post(url, undefined, { + headers: { + 'Authorization': this.accessToken, + 'PRIVATE-TOKEN': this.accessToken + } + }); } } diff --git a/src/request-lib/index.ts b/src/request-lib/index.ts new file mode 100644 index 0000000..a33cbef --- /dev/null +++ b/src/request-lib/index.ts @@ -0,0 +1,15 @@ +import { AxiosInstance } from "axios"; +import { RequestLib, RequestLibType } from ".."; +import { AxiosRequestFactory, RequestInstance, WxRequestFactory } from "./request-factories"; +import { WxInstance } from "./interfaces"; + +export function TinyRequestInstance(requestLibType: RequestLibType, instance: RequestInstance, accessToken: string) { + switch (requestLibType) { + case RequestLib.axios: + return new AxiosRequestFactory().createRequest(instance as AxiosInstance, accessToken); + case RequestLib.wx: + return new WxRequestFactory().createRequest(instance as WxInstance, accessToken); + default: + throw new Error('invalid request lib type'); + } +} \ No newline at end of file diff --git a/src/request-lib/interfaces.ts b/src/request-lib/interfaces.ts index d96492a..27f0995 100644 --- a/src/request-lib/interfaces.ts +++ b/src/request-lib/interfaces.ts @@ -6,10 +6,14 @@ export interface WxInstance { export interface WxRequestOptions { url: string; method: 'GET' | 'POST'; + header?: object; } export interface TinyRequest { - get(url: string): void; + get(url: string): Promise; post(url: string): void; }; +export interface TinyRequestOptions { + accessToken?: string; +} \ No newline at end of file diff --git a/src/request-lib/request-factories.ts b/src/request-lib/request-factories.ts index 2de70a4..25ceaeb 100644 --- a/src/request-lib/request-factories.ts +++ b/src/request-lib/request-factories.ts @@ -6,17 +6,17 @@ import { WxRequest } from "./wx-request"; export type RequestInstance = WxInstance | AxiosInstance; abstract class TinyRequestFactory { - abstract createRequest(instance: RequestInstance): TinyRequest; + abstract createRequest(instance: RequestInstance, accessToken: string): TinyRequest; } export class AxiosRequestFactory extends TinyRequestFactory { - createRequest(instance: AxiosInstance) { - return new AxiosRequest(instance); + createRequest(instance: AxiosInstance, accessToken: string) { + return new AxiosRequest(instance, accessToken); } } export class WxRequestFactory extends TinyRequestFactory { - createRequest(instance: WxInstance) { - return new WxRequest(instance); + createRequest(instance: WxInstance, accessToken: string) { + return new WxRequest(instance, accessToken); } } diff --git a/src/request-lib/wx-request.ts b/src/request-lib/wx-request.ts index 5fb0f48..63206a9 100644 --- a/src/request-lib/wx-request.ts +++ b/src/request-lib/wx-request.ts @@ -1,19 +1,27 @@ import { WxInstance, TinyRequest } from './interfaces'; export class WxRequest implements TinyRequest { - constructor(private wx: WxInstance) { } + constructor(private wx: WxInstance, private accessToken: string) { } - get(url: string) { - this.wx.request({ + async get(url: string) { + return this.wx.request({ url, - method: 'GET' + method: 'GET', + header: { + 'Authorization': this.accessToken, + 'PRIVATE-TOKEN': this.accessToken + } }); } post(url: string) { this.wx.request({ url, - method: 'POST' + method: 'POST', + header: { + 'Authorization': this.accessToken, + 'PRIVATE-TOKEN': this.accessToken + } }); } } diff --git a/src/tiny-request.ts b/src/tiny-request.ts deleted file mode 100644 index cf6aa9f..0000000 --- a/src/tiny-request.ts +++ /dev/null @@ -1,76 +0,0 @@ -// import { RequestLib, RequestLibType } from "."; -// import { AxiosInstance } from 'axios'; -// interface WxInstance { -// request(options: WxRequestOptions): void; -// [prop: string]: any; -// } - -// interface WxRequestOptions { -// url: string; -// method: 'GET' | 'POST'; -// } - -// type RequestInstance = WxInstance | AxiosInstance; - -// interface TinyRequest { -// get(url: string): void; -// post(url: string): void; -// }; - -// class AxiosRequest implements TinyRequest { -// constructor(private axios: AxiosInstance) { } - -// get(url: string) { -// this.axios.get(url); -// } - -// post(url: string) { -// this.axios.post(url); -// } -// } - -// class WxRequest implements TinyRequest { -// constructor(private wx: WxInstance) { } - -// get(url: string) { -// this.wx.request({ -// url, -// method: 'GET' -// }); -// } - -// post(url: string) { -// this.wx.request({ -// url, -// method: 'POST' -// }); -// } -// } - -// abstract class TinyRequestFactory { -// abstract createRequest(instance: RequestInstance): TinyRequest; -// } - -// class AxiosRequestFactory extends TinyRequestFactory { -// createRequest(instance: AxiosInstance) { -// return new AxiosRequest(instance); -// } -// } - -// class WxRequestFactory extends TinyRequestFactory { -// createRequest(instance: WxInstance) { -// return new WxRequest(instance); -// } -// } - -// function clientNode(requestLibType: RequestLibType, instance: RequestInstance) { -// switch (requestLibType) { -// case RequestLib.axios: -// return new AxiosRequestFactory().createRequest(instance as AxiosInstance); -// case RequestLib.wx: -// return new WxRequestFactory().createRequest(instance as WxInstance); -// default: -// throw new Error("request lib not support"); -// } -// } -