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

@codesmiths/mp-requests

v1.0.15

Published

Codesmiths Shanghai helper functions for WeChat Mini Program requests

Downloads

20

Readme

Brainchild Digital Shanghai WeChat Mini Program Requests Helpers - User Guide

Helper Functions

HTTP Requests

  • get(path, data = {})
  • post(path, data = {})
  • put(path, data = {})
  • del(path, data = {})

Other

  • getHost()
  • getHeader()

Installation

Using npm:

npm i @codesmiths/mp-requests

Configuration

Before using the request functions, make sure your /env/server.js is configured the correct way.

  • The environment (env) is defined based on Mini Program envVersion.
  • getHost function constructs the host url based on structure defined in server.js. Origin is one of the root urls based on your defined env. Other elements you pass in the structure array corresponds with the key you export.
  • Request functions uses getHost function and append it with the path you pass as param.
// const dev = true;
let env;

const { envVersion } = wx.getAccountInfoSync().miniProgram;
if (envVersion === 'release') env = 'prod';
if (envVersion === 'trial') env = 'stag';
if (envVersion === 'develop') env = dev ? 'dev' : 'stag';

module.exports = {
  lang: 'en',
  api: 'api/v1',
  env,
  root: {
    dev: 'http://localhost:3000',
    stag: 'https://www.stagingurl.com',
    prod: 'https://wwww.productionurl.com',
  },
  structure: ['origin', 'api', 'lang'],
};

// getHost() in prod env will return the following url:
// https://wwww.productionurl.com/api/v1/en/

Headers

Headers need to be stored in Mini Program Storage.

wx.setStorage({ key: 'header', data: 'headerData' })

All request functions use the header in the Mini Program storage.

You can also get the header directly fron the storage with

getHeader()

Example

Request Functions

All request functions accept 2 params (path, {someData: {}})

path is the

import http from '@codesmiths/mp-requests';

http.get('restaurants')
  .then((res) => {
    console.log(res)
  });

http.post('contact-us', { email: '[email protected]' })
  .then((res) => {
    console.log(res)
  });

http.put('address', { address: '上海市巨鹿路' })
  .then((res) => {
    console.log(res)
  });

http.del('restaurant/1')
  .then((res) => {
    console.log(res)
  });