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

box-cat

v3.0.8

Published

对暴露get,post之类接口的实例(例如axios、fly.io)二度封装

Downloads

28

Readme

Features

Api factory

Scene

  请求数据这部分,贯穿着整个前端生涯,不知你又是怎样去管理这部分模块。接口一对一写成函数然后export出去?直接this.$fetch('userInfo/1')?前者冗余,后者碎片化不好维护。
  那些业务场景简单的还好,如果像后台管理那种,动不动上百个接口,如果不用一个足够简单的结构去维护这些接口,越到后期,成本越昂贵。
  所以,box-cat就是为了解决这个痛点而生,通过足够简单的结构object来维护接口,并自动生成接口函数
  

Introduction

box-cat只是解决接口的集中管理。到最后调用的本体还是我们传的http请求库

通过接口对象和HTTP请求库(例如axios、fly.js)进行二度封装,实现api集中管理。

// script引入
<script src="https://unpkg.com/box-cat/dist/boxCat.js"></script>
BoxCat.createProxy
// a.js
import { createApis, createProxy } from 'box-cat'
// server其中的key只要匹配到其中的method(不区分大小写)就会生成对应的以key为名的接口函数
const server = {
  // 接口函数: 接口
  postFile: 'wap/file',
  deleteFile: 'wap/files/:imgId',
  getUserUpFile: 'wap/file/:userId/:imgId
}
export default createApis(server, axios)
// createApiProxy返回一个proxy
// export default createProxy(server, axios)
// a1.js
import http from './a.js'
// 方法使用和对应的HTTP请求库一样
http.postFile({id: 1}).then().catch()
/*
* 如果路径带的有param,则第一个参数是params,http请求库的参数往后顺延
* 当你有多段param的时候,传的是对象
*/ 
await http.deleteFile(1)
await http.getUserUpFile({
  userId: 1,
  imgId: 10
})

method

createApi:返回所有接口方法

createApiProxy:返回Proxy实例,惰性生成接口方法。当判断不支持proxy时,内部会优雅降级为createApi

Options

// createApi和createApiProxy默认参数配置
createApis(server, response, {
  methods: {
    'get': ['get'],
    'post': ['post'],
    'put': ['put'],
    'delete': ['delete'],
    'options': ['options'],
    'head': ['head'],
    'trace': ['trace'],
    'connect': ['connect'],
    'patch': ['patch']
  },
  mergeMethods: {},
  rule: ':param',
  methodsRule: 'startsWith'
  config: {}
})

server

api的管理文件,key不区分大小写

response

HTTP 请求库(axios,fly.io之类)

options

methods

自定义methods,用于匹配接口函数名 例如:'post': ['ADD', 'post', 'submit']

mergeMethods

合并methods

rule

param的匹配规则

methodsRule

默认是:startsWith 匹配方法 'startsWith' | 'endsWith' | 'includes'

config

其他配置,相当于axios的config