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

miniapp-axios

v1.1.2

Published

An encapsulation of miniapp request and using the similar api as axios

Downloads

8

Readme

miniapp-axios

An encapsulation of miniapp request and using the similar api as axios

Build Status Codecov branch NPM code style: prettier GitHub

Install

npm install --save miniapp-axios

Usage

import mpAxios from 'miniapp-axios'

Example

Performing a GET request

// Make a request for a user with a given ID
mpAxios
  .get('/user?ID=12345')
  .then(function(response) {
    console.log(response)
  })
  .catch(function(error) {
    console.log(error)
  })

// Optionally the request above could also be done as
mpAxios
  .get('/user', {
    params: {
      ID: 12345,
    },
  })
  .then(function(response) {
    console.log(response)
  })
  .catch(function(error) {
    console.log(error)
  })

Performing a POST request

mpAxios
  .post('/user', {
    firstName: 'Fred',
    lastName: 'Flintstone',
  })
  .then(function(response) {
    console.log(response)
  })
  .catch(function(error) {
    console.log(error)
  })

mpAxios API

Requests can be made by passing the relevant config to mpAxios.

mpAxios(config)
// Send a POST request
mpAxios({
  method: 'post',
  url: '/user/12345',
  data: {
    firstName: 'Fred',
    lastName: 'Flintstone',
  },
})
// GET request for remote image
mpAxios({
  method: 'get',
  url: 'http://bit.ly/2mTM3nY',
  dataType: 'json',
}).then(function(response) {})
mpAxios(url[, config])
// Send a GET request (default method)
mpAxios('/user/12345')

Request method aliases

For convenience aliases have been provided for all supported request methods.

mpAxios.get(url[, config])
mpAxios.delete(url[, config])
mpAxios.head(url[, config])
mpAxios.options(url[, config])
mpAxios.post(url[, data[, config]])
mpAxios.put(url[, data[, config]])
NOTE

When using the alias methods url, method, and data properties don't need to be specified in config.

Request Config

These are the available config options for making requests. Only the url is required. Requests will default to GET if method is not specified.

{
  // `url` is the server URL that will be used for the request
  url: '/user',

  // `method` is the request method to be used when making the request
  method: 'get', // default

  // `baseURL` will be prepended to `url` unless `url` is absolute.
  // It can be convenient to set `baseURL` for an instance of mpAxios to pass relative URLs
  // to methods of that instance.
  baseURL: 'https://some-domain.com/api/',

  // `transformRequest` allows changes to the request data before it is sent to the server
  // This is only applicable for request methods 'PUT', 'POST', and 'PATCH'
  transformRequest: [function (data, headers) {
    // Do whatever you want to transform the data
    // Returned value is optional
    // return data;
  }],

  // `transformResponse` allows changes to the response data to be made before
  // it is passed to then/catch
  transformResponse: [function (data) {
    // Do whatever you want to transform the data
    // Returned value is optional
    return data;
  }],

  // `headers` are custom headers to be sent
  headers: {'X-Requested-With': 'XMLHttpRequest'},

  // `params` are the URL parameters to be sent with the request
  // Must be a plain object or a URLSearchParams object
  params: {
    ID: 12345
  },

  // `paramsSerializer` is an optional function in charge of serializing `params`
  // (e.g. https://www.npmjs.com/package/qs, http://api.jquery.com/jquery.param/)
  paramsSerializer: function(params) {
    return Qs.stringify(params, {arrayFormat: 'brackets'})
  },

  // `data` is the data to be sent as the request body
  // Only applicable for request methods 'PUT', 'POST', and 'PATCH'
  data: {
    firstName: 'Fred'
  },

  // `timeout` specifies the number of milliseconds before the request times out.
  // If the request takes longer than `timeout`, the request will be aborted.
  // Keep the same as wx.request defaults.
  timeout: 60000,

  // How many times to retry request when timeout
  retry: 0, // default

  // About the two options, see:
  // https://developers.weixin.qq.com/miniprogram/dev/api/wx.request.html
  responseType: 'text',
  dataType: 'json'
}

Response Schema

The original returned by wx.request

Config Defaults

You can specify config defaults that will be applied to every request.

Global mpAxios defaults

mpAxios.defaults.baseURL = 'https://api.example.com'
mpAxios.defaults.headers = {
  'Content-Type': 'application/x-www-form-urlencoded',
}

Interceptors

You can intercept requests or responses before they are handled by then or catch.

// Add a request interceptor
mpAxios.interceptors.request.use(
  function(config) {
    // Do something before request is sent
    return config
  },
  function(error) {
    // Do something with request error
    return Promise.reject(error)
  }
)

// Add a response interceptor
mpAxios.interceptors.response.use(
  function(response) {
    // Do something with response data
    return response
  },
  function(error) {
    // Do something with response error
    return Promise.reject(error)
  }
)

Cookie

If you have some requirements about cookies?you can combine the module with weapp-cookie.

License

MIT © TOC-TEAM