npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

ddv-restful-ws-api

v0.2.2

Published

ddv-restful-ws-api

Downloads

28

Readme

ddv-restful-ws-api

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

安装

npm:

$ npm install ddv-restful-ws-api

引入:

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

独立版本:

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

<script src="https://unpkg.com/ddv-restful-ws-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 = ddvRestfulWsApi;

配置

//设置默认请求域名
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-ws-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-ws-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-ws-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-ws-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-ws-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-ws-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-ws-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-ws-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);
});