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

al-request

v0.1.3

Published

Network request library for labrador and alaska.

Downloads

9

Readme

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

属性 | 类型 | 默认 | 说明 --------- | -------- | -------- | --------- apiRoot | string | '' | 服务器API根路径,如果不设置,调用API必须提供完整URL session | boolean | true | 是否打开自定义Session支持 updateKey | string | '_session'| 当服务器返回的JSON对象有此属性时,更新当前SessionID headerKey | string | 'Session' | HTTP请求Session header名称 getSession| Function | | 获取当前的SessionID,默认获取 wx.app.sessionId setSession| Function | | 设置新的SessionID,默认保存到 wx.app.sessionId defaultHeader| Object | | 默认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