Socket
Socket
Sign inDemoInstall

@medlinker/apitool

Package Overview
Dependencies
Maintainers
4
Versions
17
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@medlinker/apitool

医联前端API工具


Version published
Weekly downloads
35
increased by105.88%
Maintainers
4
Weekly downloads
 
Created
Source

@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

FAQs

Package last updated on 02 Mar 2021

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc