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

dndc-medusa-wx-request

v1.0.5

Published

微信小程序统一请求功能函数

Downloads

2

Readme

medusa-wx-request

medusa-wx-request 是基于官方 API 进行了二次封装的函数工具,主要功能在于为官方 API 提供 Promise 的能力,本方法在保证了官方 request 方法的功能不受影响的前提下,还提供了以下能力:

  • 请求过程中 loading 弹出层功能
  • 当发生错误时,错误信息提示弹出层功能
  • 预设 API 的通用地址部分和过滤返回数据功能

初始化请求实例

示例

/** services.js */
import Request from 'medusa-wx-request';

const md = new Request({
  baseUrl: 'https://www.miniprogram.com',
  isFilterRes: false,
});

参数

| 属性 | 类型 | 默认值 | 必填 | 说明 | | --- | --- | --- | --- | --- | | baseUrl | String | | 否 | API地址的通用部分 | | isFilterRes | Boolean | true | 否 | 为 false 时,则按照官方API的数据结构进行返回。 为 true 时,则只返回其中的 data 属性。 |

请求功能函数

通过使用实例的 request 方法,就可以正常使用请求功能。出于异常处理实践方式多样并且复杂的考虑,在请求进入 fail 回调时我任然通过 resolve 的方式来更改 Promise 的状态,我将返回的数据结构更改为数组,索引为 0 的元素代表 error 的信息,当请求成功时索引为 0 的元素值为 null,这样通过 error 的值就可以判断请求是成功或是失败。 示例

/** services.js **/
const getLoginInfo = () => md.request({ url: '/api/login' });

/** Page **/

/** async 语法 **/
const [err, res] = await getLoginInfo();
/** Promise 语法 **/
getLoginInfo().then(([err, res]) => {
  ...
});

Loading 与 Toast 功能

该示例的 request 方法可以接收官方API文档中所列的所有参数,除此之外,还提供了加载中提示与错误提示功能。 示例

/**
  * 通过 loadingOps 对象控制加载中提示功能
  * @param isShow {Boolean} 开关功能 默认值为true
  * @param text {String} 提示文案 默认值为'loading...'
  **/

/** 为某一请求关闭加载中提示功能 **/
const getLoginInfo = () => md.request({
  url: '/api/login',
  loadingOps: { isShow: false },
});
/** 为某一请求更改提示文案 **/
const getLoginInfo = () => md.request({
  url: '/api/login',
  loadingOps: { text: '正在加载中...' },
});
/**
  * 通过 toastOps 对象控制错误提示功能
  * @param isShow {Boolean} 开关功能 默认值为 true
  * @param text {String} 提示文案 默认值为 '服务器开小差了哦'
  **/

/** 为某一请求关闭加载中提示功能 **/
const getLoginInfo = () => md.request({
  url: '/api/login',
  toastOps: { isShow: false },
});
/** 为某一请求更改提示文案 **/
const getLoginInfo = () => md.request({
  url: '/api/login',
  toastOps: { text: '请稍后重试' },
});

License

MIT