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

@medlinker/apitool

v0.2.0

Published

医联前端API工具

Downloads

5

Readme

@medlinker/apitool

医联前端 api 工具

安装

npm i @medlinker/apitool

功能特性

  1. 基于axios二次封装的request模块
  2. 通过 swagger 接口文档自动生成接口代码工具-createAPI。建议和上述封装的request模块结合使用。
  3. 支持 typescript

request

基于axios封装,但是为了兼容 typescript 的声明,将 API 做了较大的改动。

import request from '@medlinker/apitool/request'

async function fetch() {
  const res = await request.get('/user/list').setParams({ start: 0, limit: 20 })

  console.log(res)
}

请求方法

request.method(url, config)

method 支持getpostputdelete

  • url - 请求地址
  • config - 继承于 axios 配置,可以传入其他的配置,在回调时可能有用

同时每一个请求网络的方法都支持传入声明,只是不同请求类型的声明参数有所区别。

P 是 URL 请求参数,D 是请求正文,R 是返回值

  • request.get<P={},R={}>(url, config)
  • request.post<D={},R={}>(url, config)
  • request.put<D={},R={}>(url, config)
  • request.delete<P={},R={}>(url, config)

Request实例

上述的每一个方法都会返回一个 Request 对象实例,每一个实例有以下属性。

.appendParams(params):this

给请求追加参数

.setParams(params):this

给请求设置请求参数,会整体覆盖之前的参数

.appendData(params):this

给请求追加正文数据

.setParams(params):this

给请求设置正文数据,会整体覆盖之前的正文

.setConfig(config):this

给请求设置配置参数。

.setHeaders(headers):this

给请求设置请求头配置。

then

promise then 方法,会触发发送请求,如果上一次请求没完成则会中断上一次请求。支持await

catch

promise catch 方法,会触发发送请求,如果上一次请求没完成则会中断上一次请求。支持await

.cancel()

如果有正在进行中的请求,则中断当前请求,并抛出一个CancelError的异常。

.create(config)

创建一个新的 request 实例,并会将传入的 config 作为默认配置。

  • config - 继承于 axios 配置,并支持以下额外回调
  • config.onSuccess - 请求成功后触发
  • config.onError - 请求失败后触发
  • config.beforeSend - 请求发送前触发,通常在此添加公共参数,请求头等
export type RequestInstanceConfig = RequestBaseConfig & {
  onSuccess?: (data: AxiosResponse, config: RequestInstanceConfig) => Promise<any> | any
  onError?: (error: AxiosError, config: RequestInstanceConfig) => Promise<any> | any
  beforeSend?: (config: RequestBaseConfig) => RequestBaseConfig | Promise<RequestBaseConfig>
}

.isCancelError(err)

传入 err 是否是一个中断错误

createAPI

根据 swagger 接口配置生成 api 代码,支持命令和命令行调用

import createAPI from '@medlinker/apitool/createAPI'

createAPI.genCode(swaggerInfo, config)

  • swaggerInfo - swagger 接口配置
  • config - 配置
  • config.dist - 代码输出文件
  • config.generator - createAPI 默认只会生成数据声明代码,需要通过钩子来生成方法和头部。参考下文
  • config.requestModule - request 模块引用,默认使用@medlinker/apitool/request。如果你有自定义 request,可以配置'./request.ts'这样的相对路径
createAPI.genCode(swaggerConfig, {
  dist: path.resolve('./src/api/api.ts'),
  generator: createAPI.generators.get('nextjs'),
  requestModule: './request',
})

generators

通过createAPI.generators.get(name:string)来获取 generator.

目前支持以下类型

  • nextjs nextjs 服务端渲染
  • default(默认)

cli

模块安装后支持createAPI命令来生成代码,如:

{
  "scripts": {
    "api": "DEBUG=CreateAPI createAPI -u=http://patient-medication-dev.medlinker.com/swagger/doc.json -g=nextjs -r='./request' -d=./client/apis/api.ts"
  }
}

通过 createAPI -h 来获取完整的命令信息。

如果你想查看调试信息,请在命令前添加DEBUG=CreateAPI是内置的 debug 模块生效。

TODO