New Research: Supply Chain Attack on Axios Pulls Malicious Dependency from npm.Details →
Socket
Book a DemoSign in
Socket

al-request

Package Overview
Dependencies
Maintainers
1
Versions
4
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

al-request

Network request library for labrador and alaska.

latest
Source
npmnpm
Version
0.1.3
Version published
Maintainers
1
Created
Source

al-request

Network request library for labrador and alaska.

Labrador 网络库,适用于 Alaska Restful 格式的服务器接口。

如果服务器发生错误,返回的对象中 error 属性应当为字符串类型的错误信息,如果没有发生错误,error 属性留空。

本库对微信小程序 wx.request 进行了二次封装,尤其是内置了对自定义Session的支持。

简单应用


import request from 'al-request';

async function test(){
  let res=request('my/api');
  console.log('res',res);
}

要让上述代码正确运行,我们需要在Labrador配置文件.labrador中增加常量 API_ROOT 定义所有请求默认的API根路径,否则调用 request 方法时必须提供完整URL地址。

{
  "define":{
    "API_ROOT": "https://maichong.it/"
  }
}

在上边的代码中,我们使用了 al-request 导出的默认网络客户端,有些时候我们可能需要自定义网络客户端,例如我们需要调用多个域名下的接口。使用 create 函数创建一个自定义客户端:

import { create } from 'al-request';

const request = create({ apiRoot:'https://your.domain/' });

request.post('user/login',{ username:'liang' });

如果我们需要更新默认客户端的配置,只需要调用其 setOptions 方法:

import request from 'al-request';
request.setOptions({ apiRoot:'https://your.domain/' });

setOptions 方法的参数和 create 方法的参数一致。 通过 create 方法创建的自定义网络客户端也有 setOptions 方法。

API

Options

属性类型默认说明
apiRootstring''服务器API根路径,如果不设置,调用API必须提供完整URL
sessionbooleantrue是否打开自定义Session支持
updateKeystring'_session'当服务器返回的JSON对象有此属性时,更新当前SessionID
headerKeystring'Session'HTTP请求Session header名称
getSessionFunction获取当前的SessionID,默认获取 wx.app.sessionId
setSessionFunction设置新的SessionID,默认保存到 wx.app.sessionId
defaultHeaderObject默认Header

方法

create(options): request
request([method], apiName, [data], [header]): Promise
request.get(apiName, [data], [header]): Promise
request.post(apiName, [data], [header]): Promise
request.put(apiName, [data], [header]): Promise
request.delete(apiName, [data], [header]): Promise
request.head(apiName, [data], [header]): Promise
request.options(apiName, [data], [header]): Promise
request.trace(apiName, [data], [header]): Promise
request.connect(apiName, [data], [header]): Promise
request.setOptions(options)
request.updateOptions(options)
request.getOptions()

request 的第一个参数指定请求的方法,有效值:OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT,可以省略,默认为 GET

request('my/api') 等价于 request('GET', 'my/api') 等价于 request.get('my/api')

request.post('my/api') 等价于 request('POST', 'my/api')

data 参数为提交的KV数据对象,可以省略,如果请求方法为 POSTPUT 时,数据作为HTTP请求BODY,否则将转换为URL Query。

例如 request('my/api',{foo:'bar'}) 将请求 GET http://your.domain/my/api?foo=bar

Keywords

labrador

FAQs

Package last updated on 19 Jan 2017

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts