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

api-typing

v1.14.0

Published

Axios based HTTP client with type hint

Downloads

103

Readme

api-typing

npm version

api-typing


English | 简体中文

一个强类型提示的 http 客户端(基于 axios)。

亲爱的前端同事们,你是否受够了和后端每天不断地对接口,调整接口路径和传参格式?是否疲于查看后端的回参文档?是否疲于人肉查看同步每次更新后的接口字段变更?是时候让这些工作更加简单轻松一点了。

通过 api-typing 只要一些简单的配置,就可以让你的接口及时同步后端更新,并且拥有丝滑的传参和回参提示,甚至还有后端的字段注释也可以展示。还等什么呢,让我们开始吧。

快速上手

安装 api-typing

pnpm i api-typing

设置更新类型的脚本

package.jsonscripts字段中添加如下内容

"get-types": "get-types \"https://raw.githubusercontent.com/OAI/OpenAPI-Specification/main/examples/v3.0/petstore-expanded.json\" \"./api-typing-meta.d.ts\""

你的package.json整体预览解析

{
  "name": "api-typing",
  "version": "0.1.1",
  "description": "Axios based HTTP client with type hint",
  "scripts": {
    "get-types": "get-types @arg1 @arg2"
  },
  "license": "MIT",
  "dependencies": {},
  "devDependencies": {}
}

| 脚本参数 | 类型 | 必须 | 默认值 | | ---------------------------------------- | --------------------------- | ---- | -------------------- | | @arg1(你项目的 openapi 的 json 导出地址) | string (http 或 https 链接) | 是 | 无 | | @arg2(生成的 ts 类型文件名) | string | 否 | api-typing-meta.d.ts |

注意这里的@arg1@arg2 都要使用""包裹起来,两个参数中间是有空格的。

这里提供两种常用的 openapi json(@arg1) 文档的地址获取方式

  1. 如果后端项目用了 swagger 可以在 swagger-ui 的页面中找到

  2. 如果项目用的 apifox 则 openapi 的导出路径打开方式为项目设置 > 导出数据 > 打开URL

    apifox export openapi

执行 get-types

在终端中运行以下命令

pnpm run get-types

然后检查项目的根目录有没有生成 api-typing-meta.d.ts,如果没有请检查上一步是否出错。

配置 tsconfig.json

确保 api-typing-meta.d.ts 在根目录成功生成后,将该文件添加到 tsconfig.json 的 include 字段中

tsconfig.json

{
  "include": ["api-typing-meta.d.ts"]
}

开始使用吧

import { createHTTPClient } from "api-typing"
// 这里createHTTPClient生成的是继承自 axios instance 的实例,你可以像使用axios一样添加你自己的interceptor
createHTTPClient({ baseURL: "your baseURL" })
  // post 的 url无需手动填写,会根据你的项目自动提示可用的url,其他的请求方式同理
  .post("choose/url/with/hint", {
    /**
     * 入参也会自动提示
     */
  })
  .then((d) => {
    /**
     * 这里的d同样继承自axiosresponse,d.data为接口返回的数据
     */
  })