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

request-miniprogram

v0.1.19

Published

基于wx.request二次封装的请求器

Downloads

16

Readme

request-miniprogram

基于wx.request二次封装的请求器

安装

npm i request-miniprogram

然后选择微信开发者工具的菜单栏 "工具" --- "构建npm" 再引用就可以使用

简单使用

Application

import {Application} from "request-miniprogram"

// 该方法接收一个对象,用于该单例的配置初始化
Application.configure({});

// 获取请求api地址
Application.getHost();

// 重定向至授权页
Application.redirectToAuthPage();

// 重定向页面
Application.toRedirectionPage();

配置

app.js

import {Application} from "request-miniprogram"

App({
    onLaunch: function () {
        // 应用启动时初始化配置
        Application.configure({
            host: 'http://laravel.test/api', // 全局请求api
            authPage: '/pages/auth/index', // 授权页
            redirectPage: '/pages/home/index', // 重定向页面
            globalData:{}, // 全局数据
        });
    },
});

Auth 类

import {Auth} from "request-miniprogram";

// 登录
Auth.login(scope, expiration);

// 获取令牌
Auth.token();

// 清除会话
Auth.logout();

// 监听器,当令牌失效时,将自动删除令牌,并退出当前会话
Auth.listener();

// 判断当前用户是否为访客
Auth.guest();

// 判断当前用户是否登录
Auth.check();

// 获取Application实例
Auth.getApp();

存储token例子


auth().then((response) => {
    const {token_type, access_token, expiration} = response.data;

    Auth.login(access_token, expiration);
});

拦截器

  • 前置拦截器

请求前会进行api域名检查,如果未配置或配置错误,将被触发

  • 后置拦截器

当后端返回 401,403,404,422,500状态码时,该拦截器将被触发

当状态码为401时,需要验证的页面将会跳转至授权页

当状态码为404时,会跳转至首页

Request方法集

import {RESTFul} from "request-miniprogram";

获取资源

请求器会自动为您处理请求接口,可以根据你的个人习惯书写为: /usersusers

请求方式: GET

RESTFul.get('users').then((response) => {
    // DoSomething...
})

// 查询/获取资源
const query = {name: '张三', phone: 15689324465,};

RESTFul.get('users', query).then((response) => {
    // DoSomething...
})

资源详情

请求方式: POST


const uid = 22;

RESTFul.getDetails('users', uid).then((response) => {
    // DoSomething...
})

创建资源

请求方式: POST

const user = {
    name: 'jack',
    age: 20,
    sex: 1,
};

RESTFul.post('users', user).then((response) => {
    // DoSomething...
})

更新资源

请求方式: PATCH

const user = {
    id: 1,
    name: 'jack',
    age: 20,
    sex: 1,
};

RESTFul.update('users', user).then((response) => {
    // DoSomething...
})

删除资源

请求方式: DELETE

const id = 20;

RESTFul.delete('users', {id}).then((response) => {
    // DoSomething...
})

// 批量删除
const ids = [1, 2, 3, 4, 5];

RESTFul.delete('users', ids).then((response) => {
    // DoSomething...
})

上传资源

请求方式: POST

const file = e.detail.files[0];
const directory = '/var/html/images'

RESTFul.upload('users/upload', {filePath: file, fileName: 'img', uploadDirectory: directory}).then((response) => {
    // Dosomething
})

开源协议

MIT