效果示例
原始版
演示 getOrders
方法
class OrderMapper{
baseUrl = "/proxy/service/know/order";
async getOrders(order: Order): Promise<any> {
let answer = {RESULT: false, DATA: {}}
const response = await axios.get( this.baseUrl + "/list", {
params: order
});
return response.data;
if (response.status === HTTP_STATUS.OK){
answer.RESULT = true;
answer.DATA = response.data;
}else{
answer.RESULT = false;
}
return answer;
}
}
注解版
@RequestMapping("/proxy/service/know/order")
class OrderMapper{
@GetMapping("/list")
async getOrders(order: Order) {}
}
自动装配
定义接口
RESTFul API 的特别支持,继承RestFulMapper即可得到自动生成如下方法: findById, findAll, save, update, delete, findByXxx
import Shop from "@/types/ShopCard";
import { RequestMapping, RestFulMapper } from "@thatcompany/axios";
@RequestMapping('/proxy/service/know/shop')
export class ShopMapper extends RestFulMapper<Shop, number> {}
export default new ShopMapper();
使用接口
import shopMapper from "@/api/mapper/ShopMapper";
async function test() {
const firstShop = await shopMapper.findById(1);
firstShop.name = 'axios';
await shopMapper.update(firstShop);
const priceIsHundredShopsA = await shopMapper.findAll({ price: 100 });
priceIsHundredShopsA.forEach(async (shop) => {
await shopMapper.delete(shop.id);
});
}
手动装配
@RequestMapping("/proxy/service/know/order")
class OrderMapper{
// 可定义返回类型辅助ts
@GetMapping("/list")
async getOrders(order: Order): Promise<ThatAxiosResponse> {
return Promise.resolve({} as ThatAxiosResponse);
}
// 也可以不定义返回类型返回 Promise<any>
// {orderId} 会填充为orderId参数
@GetMapping("/{orderId}")
async getOrderById(orderId: Number) {}
// {payNo} 会填充为payNo参数
// orderId和payNo参数会被构建为data和params
// GET DELETE 变成/permit/{payNo}?orderId={orderId}&payNo={payNo}
@GetMapping("/permit/{payNo}")
@RequestLog // 日志记录
@RequestSecurityPermit // 安全放行
async getOrderByPayNo(payNo: string, orderId: Number) {}
// 单个对象参数,会被解构为data和params
@PostMapping("/permit")
@RequestSecurityPermit // 安全放行
async createOrder(orderThesis: OrderThesis) {}
}
export default new OrderMapper();