Options
All
  • Public
  • Public/Protected
  • All
Menu

YApi to TypeScript Build Status codecov

根据 YApi 的接口定义生成 TypeScript 的接口类型及其请求函数代码。

预览

特性

  • 支持多服务器、多项目、多分类
  • 支持预处理接口信息
  • 可自定义类型或函数名称
  • 完整的注释
  • 支持生成 React Hooks 的请求代码
  • 支持参数路径
  • 支持上传文件

环境要求

  • Node >= 8
  • YApi >= 1.5.12

安装

# yarn
yarn add yapi-to-typescript

# 或者,npm
npm i yapi-to-typescript --save

使用

yapi-to-typescript 基于当前目录下的 ytt.config.ts 配置文件进行相关操作。

生成配置文件

使用命令 ytt init 可在当前目录自动创建配置文件 ytt.config.ts,如果配置文件已存在,将会询问你是否覆盖:

# yarn
yarn ytt init

# 或者,npm
npx ytt init

修改配置文件

打开当前目录下的 ytt.config.ts 配置文件,直接修改即可。查看配置说明

生成代码

直接执行命令 ytt 即可抓取 YApi 的接口定义并生成相应的 TypeScript 代码:

# yarn
yarn ytt

# 或者,npm
npx ytt

配置

概论

从实质上而言,配置就是一个服务器列表,各个服务器又包含一个项目列表,各个项目下都有一个分类列表,其类型大致如此:

type Servers = Array<{
  projects: Array<{
    categories: Array<{
      // ...
    }>
  }>
}>

// 配置实质是一个服务器列表
type Config = Servers

因此,你可分别在 服务器级别项目级别分类级别 进行相关配置,如果不同级别存在相同的配置项,低级别的配置项会覆盖高级别的配置项,也就是说:

  • 如果存在相同的配置项,分类级别 的配置会覆盖 项目级别服务器级别 的配置项;
  • 如果存在相同的配置项,项目级别 的配置会覆盖 服务器级别 的配置项。

配置项

具体配置项见:API 文档

许可

MIT @ Jay Fong

Index

Type aliases

CategoryList

CategoryList: Category[]

分类列表,对应数据导出的 json 内容

Config

配置。

InterfaceList

InterfaceList: Interface[]

接口列表

PropDefinitions

PropDefinitions: PropDefinition[]

属性定义列表

RequestFunctionRestArgs

RequestFunctionRestArgs: RequestFunctionRestArgs<T>

请求函数的额外参数

SyntheticalConfig

SyntheticalConfig: Partial<ServerConfig & ProjectConfig & CategoryConfig & object>

混合的配置。

Legend

  • Module
  • Object literal
  • Variable
  • Function
  • Function with type parameter
  • Index signature
  • Type alias
  • Enumeration
  • Enumeration member
  • Property
  • Method
  • Interface
  • Interface with type parameter
  • Constructor
  • Property
  • Method
  • Index signature
  • Class
  • Class with type parameter
  • Constructor
  • Property
  • Method
  • Accessor
  • Index signature
  • Inherited constructor
  • Inherited property
  • Inherited method
  • Inherited accessor
  • Protected property
  • Protected method
  • Protected accessor
  • Private property
  • Private method
  • Private accessor
  • Static property
  • Static method

Generated using TypeDoc