Rest接口调用方式 CherryRest

码农软件 · 软件分类 · REST/RESTful项目 · 2019-03-10 15:28:58

软件介绍

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 Cherry,{module as CherryModule,config as CherryConfig} from 'cherry-rest'

基于业务Rest接口定义模块

//方式一
CherryModule('name');

//方式二、定义多个模块、模块设置别名
CherryModule('module2|home,moudle3|user');

//方式三、复合继承模式
CherryModule([{
   name:'module3',
   alias:'about',
   children:[{
       name:'info',
   },{
       name:'concat'
   }]
}]);

调用

Cherry.module3.info.query({query:{id:1}}).then(res=>res)
//fetch get /about/info?id=1;
Cherry.module3.info.create({body:{name:1}}).then(res=>res)
//fetch post /about/info {name:1};
Cherry.module3.info.update({query:{id:1}}).then(res=>res)
//fetch put /about/info?id=1;
Cherry.module3.info.remove({path:'1,2'}).then(res=>res)
//fetch delete /about/info/1/2;

Test

import Cherry,{module as CherryModule,config as CherryConfig} from '../dist/index.js'
//http://ip.taobao.com/service/getIpInfo.php?ip=114.114.114.114
jest.setTimeout(1200000);

CherryConfig({
   credentials:'include'
},{
   baseUrl:'http://ip.taobao.com',
   formatter:function(res){
       return res.json()
   }
})

CherryModule('ip|service',{
   formatter:[function(data){
       return data.data;
   }],
   filters:[{
       name:'over',
       options:['city_id|id']
   }]
});

it('async & filters test', () => {
   expect.assertions(1);
   return Cherry.ip.query({path:'getIpInfo.php',query:{ip:'114.114.114.114'}}).then(data => {
       expect(JSON.stringify(data)).toEqual(JSON.stringify({"id":"320100"}))
   });
});

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
import Cherry,{module as CherryModule,config as CherryConfig} from 'cherry-rest'

//基于rest业务接口定义模块
//配置baseUrl
CherryConfig({
   //默认使用fetch请求数据、这里可以配置fetch
   fetch:{},
   common:{
       baseUrl:'http://www.baidu.com'
   }
})

//定义模块
CherryModule([{
   name:'app',
   alias:'mock',
   children:[{
       name:'version',
       alias:'v1',
       children:[{
           name:'user'
       },{
           name:'books'
       }]
   }]
}])

let User=Cherry.app.version.user;
let Book=Cherry.app.version.app;

//GET http://www.baidu.com/mock/v1/user
User.query()

//GET http://www.baidu.com/mock/v1/user?id=123
User.query({query:{id:'123'}})

//POST http://www.baidu.com/mock/v1/user {name:'hello'}
User.create({body:{name:'hello'}})

//PUT http://www.baidu.com/mock/v1/user/123 {name:'world'}
User.update({path:'123',body:{name:'world'}})

//GET http://www.baidu.com/mock/v1/user/${userId}/blogs
User.query({path:['123','blogs'])

//DELETE http://www.baidu.com/mock/v1/user/123
User.remove({path:'123')


本文地址:https://codercto.com/soft/d/1047.html

旷世之战――IBM深蓝夺冠之路

旷世之战――IBM深蓝夺冠之路

纽伯 / 邵谦谦 / 清华大学出版社 / 2004-5 / 35.0

本书作者Monty Neworn是国际计算机象棋协公的主席,作者是用生动活泼的笔触描写了深蓝与卡斯帕罗夫之战这一引起全世界关注的历史事件的前前后后。由于作者的特殊身份和多年来对计算机象棋的关心,使他掌握了许多局外人不能得到的资料,记叙了很多鲜为人知的故事。全书行文流畅、文笔优美,对于棋局的描述更是跌宕起伏、险象环生,让读者好像又一次亲身经历了那场流动人心的战争。 本书作为一本科普读物......一起来看看 《旷世之战――IBM深蓝夺冠之路》 这本书的介绍吧!

JS 压缩/解压工具
JS 压缩/解压工具

在线压缩/解压 JS 代码

JSON 在线解析
JSON 在线解析

在线 JSON 格式化工具

图片转BASE64编码
图片转BASE64编码

在线图片转Base64编码工具