Skip to content

Commit

Permalink
Merge pull request #20 from GuoXiCheng/dev-c
Browse files Browse the repository at this point in the history
update request-lib
  • Loading branch information
GuoXiCheng authored Dec 1, 2023
2 parents bba4ee3 + d159eae commit 73d01c9
Show file tree
Hide file tree
Showing 10 changed files with 68 additions and 168 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
20 changes: 1 addition & 19 deletions src/__tests__/index.test.ts
Original file line number Diff line number Diff line change
@@ -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);
});
11 changes: 11 additions & 0 deletions src/__tests__/request-lib.test.ts
Original file line number Diff line number Diff line change
@@ -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);
59 changes: 2 additions & 57 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import {TinyRequestInstance} from "./request-lib";
export enum StoragePlatform {
gitee = "gitee",
github = "github",
Expand All @@ -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) {

// }
// }
19 changes: 15 additions & 4 deletions src/request-lib/axios-request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
});
}
}
15 changes: 15 additions & 0 deletions src/request-lib/index.ts
Original file line number Diff line number Diff line change
@@ -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');
}
}
6 changes: 5 additions & 1 deletion src/request-lib/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<any>;
post(url: string): void;
};

export interface TinyRequestOptions {
accessToken?: string;
}
10 changes: 5 additions & 5 deletions src/request-lib/request-factories.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
18 changes: 13 additions & 5 deletions src/request-lib/wx-request.ts
Original file line number Diff line number Diff line change
@@ -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
}
});
}
}
76 changes: 0 additions & 76 deletions src/tiny-request.ts

This file was deleted.

0 comments on commit 73d01c9

Please sign in to comment.