Add API Moudle!

This commit is contained in:
ben.qin 2021-12-29 17:33:27 +08:00
parent 41519b78e4
commit 0140136a0b
4 changed files with 163 additions and 75 deletions

View File

@ -1,7 +1,5 @@
import { Logger } from "./core/Logger";
import { LevelLogLabel, LifeCycleLogLabel } from "./core/PresetLogLabel";
import { API, IParamSetting } from "./core/Api";
import { Storage } from "./core/Storage";
App({
@ -17,56 +15,5 @@ App({
onLaunch() {
Logger.log("小程序启动...",
LevelLogLabel.TraceLabel, LifeCycleLogLabel.OnLaunchLabel);
let s = new Storage("test", {
a: new Date(),
be: 2
});
setTimeout(() => {
s.set("be", 12);
}, 1000)
interface ITestApiInput {
name: string,
id: number,
info: {
data: string
}
}
class TestApi extends API<ITestApiInput, {}> {
public override key:string = "TestApi";
public override params: IParamSetting<ITestApiInput> = {
name: {
tester: "123"
},
id: {
parse: (i) => ++i,
},
info: {}
}
public constructor(data:ITestApiInput) {
super(data);
this.initLabel();
this.initData();
}
}
function testApi() {
let api = new TestApi({
name: "123",
id: 456,
info: {
data: "abc"
}
});
console.log(api);
}
testApi();
}
})

View File

@ -6,6 +6,8 @@ interface IAnyData {
[x:string]: any
}
type IWxRequestOption<O> = WechatMiniprogram.RequestOption<O>;
type DeepReadonly<T> = {
readonly [P in keyof T]: DeepReadonly<T[P]>;
};
@ -22,14 +24,26 @@ type IParamSetting<T extends IAnyData> = {
defaultValue?: T[P],
/**
*
* ###
* 1 \
* 2使 string === string \
* 3使 number === number \
* 4使
*/
tester?: RegExp | ((data:T[P]) => boolean) | string,
tester?: RegExp | ((data:T[P]) => boolean) | string | number,
/**
*
* ###
* 1 \
* 2 undefined \
* 3
*
* @param data
* @param key
* @param all
* @returns
*/
parse?: ((data:T[P], key:string, all:DeepReadonly<Partial<T>>) => T[P]),
parse?: ((data:T[P], key:string, all:DeepReadonly<Partial<T>>) => T[P] | undefined),
/**
*
@ -53,6 +67,10 @@ type IParamSetting<T extends IAnyData> = {
*/
class API<I extends IAnyData, O extends IAnyData> {
public static get baseUrl():string {
return "https://xxx.xxx"
}
public static defaultLogLabel:LogLabel = new LogLabel(
`API:API`, colorRadio(200, 120, 222)
);
@ -67,6 +85,11 @@ class API<I extends IAnyData, O extends IAnyData> {
*/
public key:string = "API";
/**
* API url
*/
public url:string = "/";
/**
* API
*/
@ -75,19 +98,29 @@ class API<I extends IAnyData, O extends IAnyData> {
/**
* API
*/
public data:Partial<I>;
public data?:Partial<I>;
/**
*
*/
public requestData:IAnyData = {} as any;
public requestData?:IWxRequestOption<O>;
/**
* (wx.request)
*/
public timeout?:number;
/**
* (wx.request)
*/
public method:HTTPMethod = HTTPMethod.GET;
/**
*
* data
* @param data API需要的全部数据
*/
public constructor(data? :Partial<I>) {
public constructor(data?: Partial<I>) {
this.data = data ?? {};
}
@ -104,31 +137,38 @@ class API<I extends IAnyData, O extends IAnyData> {
*
*/
public initData() {
for(let key in this.params) {
if (this.data === void 0) {
Logger.log(`数据初始化异常: 没有输入 [data] 数据!`,
LevelLogLabel.FatalLabel, this.LogLabel);
return;
}
for (let key in this.params) {
let data = this.data[key];
let { defaultValue, Optional, tester, parse } = this.params[key];
let { defaultValue, Optional, tester } = this.params[key];
// 默认值赋予
if(data === void 0 && defaultValue !== void 0) {
if (data === void 0 && defaultValue !== void 0) {
this.data[key] = defaultValue;
}
// 数据存在测试
if(data === void 0 && !Optional) {
if (data === void 0 && !Optional) {
Logger.log(`数据校验异常: 数据 [${key}] 是必须的,但是并没有接收到!`,
LevelLogLabel.FatalLabel, this.LogLabel);
}
// 用户自定义测试
if(data !== void 0 && tester !== void 0) {
if (data !== void 0 && tester !== void 0) {
let testRes:boolean = false;
if(tester instanceof RegExp) {
if (tester instanceof RegExp) {
testRes = tester.test(data!);
} else if(typeof tester === "string") {
} else if (typeof tester === "string" || typeof tester === "number") {
testRes = tester === data;
} else if(tester instanceof Function) {
} else if (tester instanceof Function) {
testRes = tester(data!);
} else {
Logger.logMultiple(
@ -137,25 +177,61 @@ class API<I extends IAnyData, O extends IAnyData> {
);
}
if(!testRes) {
if (!testRes) {
Logger.logMultiple(
[LevelLogLabel.FatalLabel, this.LogLabel],
`数据校验异常: [${ key }] 参数数据未通过自定义的 tester:`, data
);
}
}
// 数据预解析
if(parse !== void 0 && data !== void 0) {
this.data[key] = parse(data!, key, this.data as DeepReadonly<Partial<I>>);
}
}
}
/**
*
*/
public collectData() {}
public collectData() {
if (this.data === void 0) {
Logger.log(`收集请求数据异常: 没有输入 [data] 数据!`,
LevelLogLabel.FatalLabel, this.LogLabel);
return;
}
// 重置请求数据
const requestData:IWxRequestOption<O> = this.requestData = {
url: API.baseUrl + this.url,
};
for (let key in this.params) {
let data = this.data[key];
let { isHeader, isTemplate, parse } = this.params[key];
// 数据预解析
if (parse !== void 0) {
data = parse(data!, key, this.data as DeepReadonly<Partial<I>>);
}
// 加载数据
if (!isTemplate) {
if (isHeader) {
wx.request
}
}
}
}
}
enum HTTPMethod {
OPTIONS = "OPTIONS",
GET = "GET",
HEAD = "HEAD",
POST = "POST",
PUT = "PUT",
DELETE = "DELETE",
TRACE = "TRACE",
CONNECT = "CONNECT"
}
export default API;

View File

@ -0,0 +1,63 @@
import { Modular, Manager, ILifetime } from "../../core/Module";
import { API, IParamSetting } from "../../core/Api";
import { Storage } from "../../core/Storage";
/**
*
*/
class TestCore<M extends Manager> extends Modular<M>
implements Partial<ILifetime> {
public onLoad() {
let s = new Storage("test", {
a: new Date(),
be: 2
});
setTimeout(() => {
s.set("be", 12);
}, 1000)
interface ITestApiInput {
name: string,
id: number,
info: {
data: string
}
}
class TestApi extends API<ITestApiInput, {}> {
public override key:string = "TestApi";
public override params: IParamSetting<ITestApiInput> = {
name: {
tester: "123"
},
id: {
parse: (i) => ++i,
},
info: {}
}
public constructor(data:ITestApiInput) {
super(data);
this.initLabel();
this.initData();
}
}
let api = new TestApi({
name: "123",
id: 456,
info: {
data: "abc"
}
});
console.log(api);
}
}
export default TestCore;
export { TestCore };

View File

@ -1,5 +1,6 @@
import { Manager} from "../../core/Module";
import { StatusBar } from "./StatusBar";
import { TestCore } from "./TestCore";
/**
* 使 Manager
@ -7,4 +8,5 @@ import { StatusBar } from "./StatusBar";
*/
Manager.Page((manager)=>{
manager.addModule(StatusBar, "statusBar");
manager.addModule(TestCore, "testCore");
})