ddv-restful-api

0.4.14 • Public • Published

ddv-restful-api

这是一个基于Promise封装的请求模块

安装

npm:

$ npm install ddv-restful-api

引入:

import * as api from 'ddv-restful-api';

独立版本:

你可以直接使用<script>标签直接引入, ddvRestFulApi 会被注册为一个全局变量。

<script src="https://unpkg.com/ddv-restful-api/dist/api.js"></script>

需要注意的是,为了兼容IE9以下版本浏览器需要预先加载es5-shim

<script src="https://cdnjs.cloudflare.com/ajax/libs/es5-shim/3.4.0/es5-shim.min.js"></script>

引入:

var api = ddvRestFulApi;

配置

//设置默认请求域名
api.setBaseUrl('http://api.testabc.com');
//自定义头前缀
api.setHeadersPrefix('x-hz-');
//是否长期会话
api.setLongStorage(false);
//设置会话初始化最大自动尝试次数,默认3次
api.setSessionInitTrySum(3);
//设置初始化session的path,默认/session/init
api.setSessionInitPath('/session/init');

使用

  • restful-api 支持 GETPOSTPUTDELETE四种请求方式,以及拥有 getpostputdel(或 delete)、api五个方法。
  • 分别使用方法 getpostputdel(或 delete)执行对应的请求。
  • api方法支持模拟GETPOSTPUTDELETE 以及更多请求方式。

例子:

1、GET请求
  • send 发送的数据会自动转换为 query 服务器GET参数
  • headers() :改变请求头:api.get('/path').headers({})
  • path():可以改变请求的path:api.get('/path').path('/path2')
  • query() :发送地址栏 后面的参数
  • sendData()send方法是 sendData一个别名
  • req()res(): 如果在node服务器建立http服务,需要设置reqres来确定会话
  • context(): 这个是res和req的集合context({'req','res'})
import api from 'ddv-restful-api';

//支持多维数组
api.get('/v1_0/pc/raiders/news/type').send({
    'array':[
        '1',
        '2',
        '3',
        {
            'test':'ddv',
            'ddvjs':['a','b','c']
        }
    ]
}).then(function(res){
    console.log('结果:',res);
}).catch(function(e){
    console.log('e',e.statusCode);
    console.log('e',e.message);
});
  • 小提示如果您仅仅是使用get请求你可以选择
import {get as getApi} from 'ddv-restful-api';

//支持多维数组
getApi('/v1_0/pc/raiders/news/type').send({
    'array':[
        '1',
        '2',
        '3',
        {
            'test':'ddv',
            'ddvjs':['a','b','c']
        }
    ]
}).then(function(res){
    console.log('结果:',res);
}).catch(function(e){
    console.log('e',e.statusCode);
    console.log('e',e.message);
});
  • 温馨提示:getApi是别名
2、POST请求
  • 类似GET,但是 POSTPUTDELETE 支持 query() 和 send()
  • query 是服务器GET参数
  • send 是服务器POST参数,也就是请求体
import {post as postApi} from 'ddv-restful-api';

//支持多维数组
postApi('/v1_0/pc/raiders/news/type').send({
    'array':[
        '1',
        '2',
        '3',
        {
            'test':'ddv',
            'ddvjs':['a','b','c']
        }
    ]
}).then(function(res){
    console.log('结果:',res);
}).catch(function(e){
    console.log('e',e.statusCode);
    console.log('e',e.message);
});
3、使用api方法请求
  • 与上述例子类似
  • 可使用 method() 方法更改请示方式,默认是 GET
import api from 'ddv-restful-api';

//支持多维数组
api('/v1_0/pc/raiders/news/type').method('PUT').send({
    'array':[
        '1',
        '2',
        '3',
        {
            'test':'ddv',
            'ddvjs':['a','b','c']
        }
    ]
}).then(function(res){
    console.log('结果:',res);
}).catch(function(e){
    console.log('e',e.statusCode);
    console.log('e',e.message);
});
import api from 'ddv-restful-api';

//支持多维数组
api.get('/v1_0/pc/raiders/news/type').send({
    'array':[
        '1',
        '2',
        '3',
        {
            'test':'ddv',
            'ddvjs':['a','b','c']
        }
    ]
}).then(function(res){
    console.log('结果:',res);
}).catch(function(e){
    console.log('e',e.statusCode);
    console.log('e',e.message);
});
  • 小提示如果您仅仅是使用get请求你可以选择
import {get as getApi} from 'ddv-restful-api';

//支持多维数组
getApi('/v1_0/pc/raiders/news/type').send({
    'array':[
        '1',
        '2',
        '3',
        {
            'test':'ddv',
            'ddvjs':['a','b','c']
        }
    ]
}).then(function(res){
    console.log('结果:',res);
}).catch(function(e){
    console.log('e',e.statusCode);
    console.log('e',e.message);
});
  • 温馨提示:getApi是别名
2、POST请求
  • 类似GET,但是 POSTPUTDELETE 支持 query() 和 send()
  • query 是服务器GET参数
  • send 是服务器POST参数,也就是请求体
import {post as postApi} from 'ddv-restful-api';

//支持多维数组
postApi('/v1_0/pc/raiders/news/type').send({
    'array':[
        '1',
        '2',
        '3',
        {
            'test':'ddv',
            'ddvjs':['a','b','c']
        }
    ]
}).then(function(res){
    console.log('结果:',res);
}).catch(function(e){
    console.log('e',e.statusCode);
    console.log('e',e.message);
});
3、使用api方法请求
  • 与上述例子类似
  • 可使用 method() 方法更改请示方式,默认是 GET
import api from 'ddv-restful-api';

//支持多维数组
api('/v1_0/pc/raiders/news/type').method('PUT').send({
    'array':[
        '1',
        '2',
        '3',
        {
            'test':'ddv',
            'ddvjs':['a','b','c']
        }
    ]
}).then(function(res){
    console.log('结果:',res);
}).catch(function(e){
    console.log('e',e.statusCode);
    console.log('e',e.message);
});

Readme

Keywords

Package Sidebar

Install

npm i ddv-restful-api

Weekly Downloads

21

Version

0.4.14

License

ISC

Unpacked Size

996 kB

Total Files

23

Last publish

Collaborators

  • coderpreacher
  • huadichan
  • yuchonghua