CherryRest
一种非常优雅Rest接口调用方式
Features
- 基于Restful风格接口设计的模块化接口url生成器
- 集中管理接口url
- 默认使用fetch作为接口加载器
- 可以给模块单独设置baseUrl
- 支持response formatter配置、支持单个module formatter
- 内置默认filter,当前只有一个over,重载response data filed
Install
npm install cherry-rest --save
Usage
import Cherrymodule as CherryModuleconfig as CherryConfig from 'cherry-rest'
基于业务Rest接口定义模块
//方式一; //方式二、定义多个模块、模块设置别名; //方式三、复合继承模式;
调用
Cherrymodule3info//fetch get /about/info?id=1;Cherrymodule3info//fetch post /about/info {name:1};Cherrymodule3info//fetch put /about/info?id=1;Cherrymodule3info//fetch delete /about/info/1/2;
Test
//http://ip.taobao.com/service/getIpInfo.php?ip=114.114.114.114jest; ; ;
Demo
Base
API Example:
get http://www.baidu.com/mock/v1/user
get http://www.baidu.com/mock/v1/user?id=${userId}
get http://www.baidu.com/mock/v1/user/${userId}/blogs
get http://www.baidu.com/mock/v1/books
post http://www.baidu.com/mock/v1/user {name:'hello'}
put http://www.baidu.com/mock/v1/user/{userId} {name:'world'}
//import //基于rest业务接口定义模块//配置baseUrl //定义模块 let User=Cherryappversionuser;let Book=Cherryappversionapp; //GET http://www.baidu.com/mock/v1/userUser //GET http://www.baidu.com/mock/v1/user?id=123User //POST http://www.baidu.com/mock/v1/user {name:'hello'}User //PUT http://www.baidu.com/mock/v1/user/123 {name:'world'}User //GET http://www.baidu.com/mock/v1/user/${userId}/blogsUser