Files
mayfly-go/mayfly_go_web/src/common/Api.ts

82 lines
1.6 KiB
TypeScript
Raw Normal View History

import request from './request';
/**
* api请求
*/
class Api {
/**
* url
*/
url: string;
/**
*
*/
method: string;
constructor(url: string, method: string) {
this.url = url;
this.method = method;
}
/**
* url
*/
getUrl() {
return request.getApiUrl(this.url);
}
/**
* api
* @param {Object} param api的参数
*/
request(param: any = null, options: any = null, headers: any = null): Promise<any> {
return request.request(this.method, this.url, param, headers, options);
}
/** 静态方法 **/
/**
* Api对象url与method属性
* @param url url
* @param method (get,post,put,delete...)
*/
static create(url: string, method: string): Api {
return new Api(url, method);
}
/**
* get api
* @param url url
*/
static newGet(url: string): Api {
return Api.create(url, 'get');
}
/**
* new post api
* @param url url
*/
static newPost(url: string): Api {
return Api.create(url, 'post');
}
/**
* new put api
* @param url url
*/
static newPut(url: string): Api {
return Api.create(url, 'put');
}
/**
* new delete api
* @param url url
*/
static newDelete(url: string): Api {
return Api.create(url, 'delete');
}
}
export default Api;