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

@prequest/response-types-generator

v1.5.2

Published

Restful-API 响应的 JSON 数据的 TypeScript 类型生成器

Downloads

18

Readme

@prequest/response-types-generator

Restful-API 响应的 JSON 数据的 TypeScript 类型生成器

安装

npm install @prequest/response-types-generator

使用

最简单使用

下面的例子,将在当前目录下,生成导出 interface 为 Topics 的文件名为 topics 的 TS 文件。

import responseTypesGenerator from '@prequest/response-types-generator'

responseTypesGenerator({
  data: [
    {
      path: 'https://cnodejs.org/api/v1/topics',
    },
  ],
})

全功能使用

import responseTypesGenerator from '@prequest/response-types-generator'

responseTypesGenerator({
  // 全局配置项,data 中的请求会自动与这里进行 merge
  requestOptions: {
    baseURL: 'https://webspiderr.herokuapp.com',
    method: 'GET',
  },
  // 输出文件夹地址
  outPutDir: '/Users/xdoer',
  // 同时请求的接口数量
  requestPoolLimit: 10,
  // 解析响应
  parseResponse: res => res.data,
  // 自定义导出的 interface 名称
  customRootInterfaceName:  (req) => {
    return req.path.replace(/.+\/(\w+)/, (_, name) => name).replace(/^[a-z]/, (c) => c.toUpperCase())
  },
  // 自定义输出的文件名称
  customOutPutFileName: (req) => {
    return req.path.replace(/.+\/(\w+)/, (_, name) => name)
  },
  data: [
    {
      // 接口请求路径
      path: '/crawl/api'
      // 请求配置项项
      requestOptions: {
        params: {
          user: 'xdoer',
          cid: '73b1430d-faa0-44eb-899e-36cf5cbfaec8',
        },
      },
      // 输出文件名
      outPutFileName: 'crawl-types',
      // 导出的 interface 名称
      rootInterfaceName: 'Crawl',
      // 解析响应
      parseResponse: res => JSON.parse(res.data),
      // 自定义中间产物的 interface 名称
      customInterfaceName: (key, value, data) => {
        if (key === 'a') return 'name'
      },
    },
  ],
})

配置

配置分为两部分: 全局配置与局部配置。

全局配置

| 名称 | 类型 | 必填 | 默认值 | 含义 | 备注 | | ----------------------- | ------------------------------------------------------------------------------------- | ---- | ------------------ | --------------------------------------------- | -------------------------------------------------- | | requestOptions | @prequest/node请求配置项 | 否 | 无 | 接口请求配置项 | 全局和局部配置中的字段会进行 merge | | outPutDir | string | 否 | 无 | 类型文件输出目录 | | | requestPoolLimit | number | 否 | 10 | 同时请求的接口数量 | | | parseResponse | (res) => Json string | Json object | 否 | (res) => res.data | 全局解析接口响应 | 全局解析响应,局部响应有差异,可以局部配置中配置 | | customRootInterfaceName | (req) => string | void | 否 | 默认为最后的子路径 | 自定义符合自己预期的 interfaceName 以方便调用 | | | customOutPutFileName | (req) => string | void | 否 | 默认为最后的子路径 | 自定义符合自己预期的文件名称 | 当局部配置项中配置了 outPutPath, 则 | | data | 局部参数[] | 是 | [] | 类型解析列表 | 程序根据配置,自动请求接口,解析响应,生成类型文件 |

局部配置

| 名称 | 类型 | 必填 | 默认值 | 含义 | 备注 | | ------------------- | ------------------------------------------------------------------------------------- | ---- | ------------------ | --------------------------------------------- | ------------------------------------------------------------- | | path | string | 是 | 无 | 接口路径 | | | requestOptions | @prequest/node请求配置项 | 否 | 无 | 接口请求配置项 | requestOptions 也配置了 path 的话,会使用这里的 path 进行请求 | | outPutFileName | string | 否 | 无 | 文件输出名称 | | | rootInterfaceName | string | 否 | 默认为最后的子路径 | 自定义符合自己预期的 interfaceName 以方便调用 | 没配置的话,会使用全局 customRootInterfaceName | | parseResponse | (res) => Json string | Json object | 否 | 无 | 解析接口响应 | | | customInterfaceName | (key, value, data) => string | void | 否 | 无 | 自定义中间产物的 interface | |

其他

项目使用 @prequest/node 发起请求,使用 json-types-generator 进行数据解析。