Skip to content

Commit

Permalink
Merge pull request #18 from GuoXiCheng/dev-c
Browse files Browse the repository at this point in the history
Dev c
  • Loading branch information
GuoXiCheng authored Nov 30, 2023
2 parents 2578282 + d2f88a8 commit 4f9f193
Show file tree
Hide file tree
Showing 8 changed files with 179 additions and 45 deletions.
30 changes: 30 additions & 0 deletions .github/workflows/publish.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
name: Publish npm package

on:
push:
branches:
- publish

jobs:
publish:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v2

- name: Set up Node.js
uses: actions/setup-node@v2
with:
node-version: '14' # 指定 Node.js 版本
registry-url: 'https://registry.npmjs.org'

- name: Install dependencies
run: npm install

- name: Build package
run: npm run build

- name: Publish to npm
run: npm publish
env:
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
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.10",
"version": "1.0.11",
"description": "",
"main": "dist/bundle.cjs.js",
"module": "dist/bundle.esm.js",
Expand Down
3 changes: 3 additions & 0 deletions src/__tests__/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
test('example', ()=>{
expect(1).toBe(1);
});
// import { TinyCRUD } from "../index";
// import 'dotenv/config';
// import axios from "axios";
Expand Down
14 changes: 14 additions & 0 deletions src/request-lib/axios-request.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { AxiosInstance } from 'axios';
import { TinyRequest } from './interfaces';

export class AxiosRequest implements TinyRequest {
constructor(private axios: AxiosInstance) { }

get(url: string) {
this.axios.get(url);
}

post(url: string) {
this.axios.post(url);
}
}
15 changes: 15 additions & 0 deletions src/request-lib/interfaces.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
export interface WxInstance {
request(options: WxRequestOptions): void;
[prop: string]: any;
}

export interface WxRequestOptions {
url: string;
method: 'GET' | 'POST';
}

export interface TinyRequest {
get(url: string): void;
post(url: string): void;
};

22 changes: 22 additions & 0 deletions src/request-lib/request-factories.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { AxiosInstance } from "axios";
import { TinyRequest, WxInstance } from "./interfaces";
import { AxiosRequest } from "./axios-request";
import { WxRequest } from "./wx-request";

export type RequestInstance = WxInstance | AxiosInstance;

abstract class TinyRequestFactory {
abstract createRequest(instance: RequestInstance): TinyRequest;
}

export class AxiosRequestFactory extends TinyRequestFactory {
createRequest(instance: AxiosInstance) {
return new AxiosRequest(instance);
}
}

export class WxRequestFactory extends TinyRequestFactory {
createRequest(instance: WxInstance) {
return new WxRequest(instance);
}
}
19 changes: 19 additions & 0 deletions src/request-lib/wx-request.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { WxInstance, TinyRequest } from './interfaces';

export 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'
});
}
}
119 changes: 75 additions & 44 deletions src/tiny-request.ts
Original file line number Diff line number Diff line change
@@ -1,45 +1,76 @@
import { RequestLib, RequestLibType } from ".";

interface TinyRequest {
get(): void;
post(): void;
};

class AxiosRequest implements TinyRequest {
constructor() {}
get() { }
post() { }
}

class WxRequest implements TinyRequest {
get() { }
post() { }
}

abstract class TinyRequestFactory {
abstract createRequest(): TinyRequest;
}

class AxiosRequestFactory extends TinyRequestFactory {
createRequest() {
return new AxiosRequest();
}
}

class WxRequestFactory extends TinyRequestFactory {
createRequest() {
return new WxRequest();
}
}

function clientNode(requestLibType: RequestLibType, requestInstance: any) {
switch(requestLibType) {
case RequestLib.axios:
return new AxiosRequestFactory().createRequest();
case RequestLib.wx:
return new WxRequestFactory().createRequest();
default:
throw new Error("request lib not support");
}
}
// 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");
// }
// }

0 comments on commit 4f9f193

Please sign in to comment.