Skip to content

Commit 9d0a36f

Browse files
authored
Merge pull request #133 from FrontEndDev-org/feat/v0.x
feat: oas-gen-ts -> openapi-axios
2 parents c967bb2 + 8fef68d commit 9d0a36f

File tree

7 files changed

+183
-215
lines changed

7 files changed

+183
-215
lines changed

CHANGELOG.md

Lines changed: 111 additions & 152 deletions
Large diffs are not rendered by default.

README.md

Lines changed: 43 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,42 @@
1-
# oas-gen-ts
1+
# openapi-axios
22

3-
[![code-review](https://github.com/cloudcome/oas-gen-ts/actions/workflows/code-review.yml/badge.svg)](https://github.com/cloudcome/oas-gen-ts/actions/workflows/code-review.yml)
4-
[![dependency-review](https://github.com/cloudcome/oas-gen-ts/actions/workflows/dependency-review.yml/badge.svg)](https://github.com/cloudcome/oas-gen-ts/actions/workflows/dependency-review.yml)
5-
[![Codacy Badge](https://app.codacy.com/project/badge/Grade/e788387e5e27472ba3b5003bf19aeea7)](https://app.codacy.com/gh/cloudcome/oas-gen-ts/dashboard?utm_source=gh&utm_medium=referral&utm_content=&utm_campaign=Badge_grade)
6-
[![Codacy Badge](https://app.codacy.com/project/badge/Coverage/e788387e5e27472ba3b5003bf19aeea7)](https://app.codacy.com/gh/cloudcome/oas-gen-ts/dashboard?utm_source=gh&utm_medium=referral&utm_content=&utm_campaign=Badge_coverage)
7-
![npm](https://img.shields.io/npm/v/oas-gen-ts)
8-
![release](https://img.shields.io/github/v/release/cloudcome/oas-gen-ts)
9-
![license](https://img.shields.io/github/license/cloudcome/oas-gen-ts)
3+
[![code-review](https://github.com/FrontEndDev-org/openapi-axios/actions/workflows/code-review.yml/badge.svg)](https://github.com/FrontEndDev-org/openapi-axios/actions/workflows/code-review.yml)
4+
[![dependency-review](https://github.com/FrontEndDev-org/openapi-axios/actions/workflows/dependency-review.yml/badge.svg)](https://github.com/FrontEndDev-org/openapi-axios/actions/workflows/dependency-review.yml)
5+
[![Codacy Badge](https://app.codacy.com/project/badge/Grade/e788387e5e27472ba3b5003bf19aeea7)](https://app.codacy.com/gh/FrontEndDev-org/openapi-axios/dashboard?utm_source=gh&utm_medium=referral&utm_content=&utm_campaign=Badge_grade)
6+
[![Codacy Badge](https://app.codacy.com/project/badge/Coverage/e788387e5e27472ba3b5003bf19aeea7)](https://app.codacy.com/gh/FrontEndDev-org/openapi-axios/dashboard?utm_source=gh&utm_medium=referral&utm_content=&utm_campaign=Badge_coverage)
7+
![npm](https://img.shields.io/npm/v/openapi-axios)
8+
![release](https://img.shields.io/github/v/release/FrontEndDev-org/openapi-axios)
9+
![license](https://img.shields.io/github/license/FrontEndDev-org/openapi-axios)
1010

11-
OpenAPI Specification ➡️ TypeScript
11+
OpenAPI ➡️ Axios
1212

13-
Convert OpenAPI Specification declaration files into type declarations and executable functions. Compared with other similar tools, it has the following characteristics
13+
OpenAPI 规范声明文件转换为类型声明和可执行函数(基于 Axios)。与其他同类工具相比,具有以下特点
1414

15-
- Each API is a function for easy tree shaking at build time
16-
- Easily integrate with local request clients, such as Axios instances created in local projects
17-
- Easy to use, easy to learn, type safe
15+
- 每个 API 都是一个函数,用于在构建时轻松进行 tree shaking
16+
- 轻松与本地请求客户端集成,例如在本地项目中创建的 Axios 实例
17+
- 易于使用,易于学习,类型安全
1818

1919
# Install
2020

2121
```shell
22-
npm i -D oas-gen-ts
22+
npm i -D openapi-axios
2323
```
2424

2525
or
2626

2727
```shell
28-
yarn add --dev oas-gen-ts
28+
yarn add --dev openapi-axios
2929
```
3030

3131
# Usage
3232

3333
## CLI
3434

35-
Create oas.config.js or oas.json in the root directory of the project. The search order for configuration files is `oas.config.cjs`, `oas.config.js`, `oas.json`.
35+
在项目根目录下创建配置文件,配置文件的搜索顺序是 `openapi.config.cjs``openapi.config.js``openapi.json`
3636

3737
```js
38-
// oas.config.cjs
39-
const { defineConfig } = require('oas-gen-ts');
38+
// openapi.config.cjs
39+
const { defineConfig } = require('openapi-axios');
4040

4141
module.exports = defineConfig({
4242
list: [
@@ -49,13 +49,13 @@ module.exports = defineConfig({
4949
```
5050

5151
```shell
52-
# Generate typescript files based on configuration files
53-
npx oas-gen-ts
52+
# 根据配置文件生成typescript文件
53+
npx openapi-axios
5454

55-
# The `src/apis/swagger/pet.ts` file will be generated
55+
# 将生成 `src/apis/swagger/pet.ts` 文件
5656
```
5757

58-
The generated file will be exported as one function and one operation, like this:
58+
生成的文件将导出为一个函数和一个操作,如下所示:
5959

6060
```ts
6161
// src/apis/swagger/pet.ts
@@ -109,7 +109,7 @@ export async function findPetsByStatus(
109109
// ...
110110
```
111111

112-
Then you can directly import a function and use it. Calling an interface is as simple as calling a local function, is it similar to RPC (remote procedure call).
112+
然后你可以直接导入一个函数并使用它。 调用接口就像调用本地函数一样简单,是不是类似于 RPCremote procedure call)。
113113

114114
```ts
115115
import { findPetsByStatus } from '@/apis/swagger/pet';
@@ -123,7 +123,7 @@ const pets = await findPetsByStatus({
123123
## API
124124

125125
```ts
126-
import { generate } from 'oas-gen-ts';
126+
import { generate } from 'openapi-axios';
127127

128128
generate({
129129
// ...config
@@ -132,21 +132,22 @@ generate({
132132

133133
# Config
134134

135-
| Name | Type | Required | Description | Default |
136-
| -------------------- | ----------- | -------- | ------------------------------------------ | ----------------------------------------------- |
137-
| `cwd` | `string` | `false` | current working directory | `process.cwd()` |
138-
| `dest` | `string` | `false` | Destination directory for generated files | `src/apis` |
139-
| `axiosImport` | `string` | `false` | axios import string | Import from the official and create an instance |
140-
| `unwrapResponseData` | `boolean` | `false` | unwrap the data item from the response | `false` |
141-
| `list` | `OasItem[]` | `false` | List of OpenAPI Specification declarations | `[]` |
142-
143-
`OasItem`:
144-
145-
| Name | Type | Required | Description | Default |
146-
| ------------- | -------- | -------- | ----------------------------------------------- | ----------------------------------------------- |
147-
| `name` | `string` | `true` | filename, which can be a path | `process.cwd()` |
148-
| `axiosImport` | `string` | `false` | axios import string, highest priority | Import from the official and create an instance |
149-
| `url` | `string` | `false` | The remote address of the OpenAPI Specification | `undefined` |
150-
| `spec` | `Spec` | `false` | The local Objects of the OpenAPI Specification | `undefined` |
151-
152-
**At least one of `url` and `spec` exists**
135+
| 参数名 | 类型 | 可选性 | 描述 | 默认值 |
136+
| -------------------- | --------------- | ------- | --------------------------------------------------------------------------------- | ----------------------------------------------- |
137+
| `cwd` | `string` | `false` | 当前工作路径 | `process.cwd()` |
138+
| `dest` | `string` | `false` | 目标目录 | `src/apis` |
139+
| `axiosImport` | `string` | `false` | axios 导入内容 | 默认从官方 Axios 导入,可以使用自己实现的客户端 |
140+
| `unwrapResponseData` | `boolean` | `false` | 是否取消对 axios response 的包裹(即直接返回 ResponseData,而不是 AxiosResponse) | `false` |
141+
| `list` | `OpenApiSpec[]` | `false` | OpenAPI 规范声明列表 | |
142+
| `[]` | | | | |
143+
144+
`OpenApiSpec` 签名:
145+
146+
| 名称 | 类型 | 可选项 | 描述 | 默认值 |
147+
| ------------- | -------- | ------- | -------------------------------------- | --------------- |
148+
| `name` | `string` | `true` | 文件名,可以包含路径,相当于 dest 配置 | `process.cwd()` |
149+
| `axiosImport` | `string` | `false` | axios 导入内容,优先级更高 ||
150+
| `url` | `string` | `false` | 远程 openApi 描述地址 | `undefined` |
151+
| `spec` | `Spec` | `false` | 本地 OpenApi 描述文件 | `undefined` |
152+
153+
备注:`url` 属性和 `spec` 属性,至少有一个必须。

package-lock.json

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
2-
"name": "oas-gen-ts",
2+
"name": "openapi-axios",
33
"version": "0.6.27",
4-
"description": "OpenAPI Specification ➡️ TypeScript",
4+
"description": "OpenAPI ➡️ Axios",
55
"scripts": {
66
"prepare": "husky install",
77
"lint": "eslint src/**/*.ts && tsc --project tsconfig.json --noEmit",
@@ -33,7 +33,7 @@
3333
"./package.json": "./package.json"
3434
},
3535
"bin": {
36-
"oas-gen-ts": "bin/index.cjs"
36+
"openapi-axios": "bin/index.cjs"
3737
},
3838
"files": [
3939
"dist-cjs",
@@ -44,14 +44,15 @@
4444
"keywords": [
4545
"cloudcome",
4646
"ydr.me",
47-
"OpenAPI Specification",
47+
"OpenAPI",
48+
"swagger",
4849
"TypeScript",
4950
"OAS",
5051
"axios"
5152
],
5253
"author": "publish-node-package-action",
53-
"homepage": "https://github.com/cloudcome/oas-gen-ts",
54-
"repository": "https://github.com/cloudcome/oas-gen-ts",
54+
"homepage": "https://github.com/FrontEndDev-org/openapi-axios",
55+
"repository": "https://github.com/FrontEndDev-org/openapi-axios",
5556
"license": "MIT",
5657
"dependencies": {
5758
"chalk": "^4.1.2",

src/const.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,4 @@ const dirname = __dirname;
55
export const templatesDir = path.join(dirname, '../templates');
66
export const axiosImportDefault = `import { Axios } from 'axios';
77
const axios = new Axios();`;
8-
export const helpersImport = `import { formatHeaders, formatBody } from 'oas-gen-ts/helpers';`;
8+
export const helpersImport = `import { formatHeaders, formatBody } from 'openapi-axios/helpers';`;

src/generator.ts

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,23 @@ import fs from 'fs/promises';
22
import path from 'path';
33
import { generateApi } from 'swagger-typescript-api';
44
import { axiosImportDefault, helpersImport, templatesDir } from './const';
5-
import { Generated, GeneratedCallback, OasItem, OasItemAsSpec, OasItemAsUrl, StrictConfig } from './types';
5+
import {
6+
Generated,
7+
GeneratedCallback,
8+
OpenApiSpec,
9+
OpenApiSpecAsLocal,
10+
OpenApiSpecAsRemote,
11+
StrictConfig,
12+
} from './types';
613

7-
export async function generateItem(oasItem: OasItem, config: StrictConfig): Promise<Generated> {
14+
export async function generateItem(oasItem: OpenApiSpec, config: StrictConfig): Promise<Generated> {
815
const { name, axiosImport: axiosImportScope } = oasItem;
916
const { cwd, dest, axiosImport: axiosImportGlobal, unwrapResponseData } = config;
1017
const axiosImport = axiosImportScope || axiosImportGlobal || axiosImportDefault;
1118
const { files } = await generateApi({
1219
name,
13-
url: (oasItem as OasItemAsUrl).url,
14-
spec: (oasItem as OasItemAsSpec).spec,
20+
url: (oasItem as OpenApiSpecAsRemote).url,
21+
spec: (oasItem as OpenApiSpecAsLocal).spec,
1522
output: false,
1623
httpClientType: 'axios',
1724
templates: templatesDir,

src/types.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
interface OasItemBase {
1+
interface OpenApiSpecBase {
22
name: string;
33

44
/**
@@ -10,17 +10,17 @@ interface OasItemBase {
1010
axiosImport?: string;
1111
}
1212

13-
export interface OasItemAsUrl extends OasItemBase {
13+
export interface OpenApiSpecAsRemote extends OpenApiSpecBase {
1414
url: string;
1515
}
1616

1717
export type Spec = import('swagger-schema-official').Spec;
1818

19-
export interface OasItemAsSpec extends OasItemBase {
19+
export interface OpenApiSpecAsLocal extends OpenApiSpecBase {
2020
spec: Spec;
2121
}
2222

23-
export type OasItem = OasItemAsUrl | OasItemAsSpec;
23+
export type OpenApiSpec = OpenApiSpecAsRemote | OpenApiSpecAsLocal;
2424

2525
export interface UserConfig {
2626
/**
@@ -56,9 +56,9 @@ export interface UserConfig {
5656
onGenerated?: (generated: Generated) => any;
5757

5858
/**
59-
* oas 列表
59+
* OpenApiSpec 列表
6060
*/
61-
list: OasItem[];
61+
list: OpenApiSpec[];
6262
}
6363

6464
export type StrictConfig = Required<UserConfig>;
@@ -73,7 +73,7 @@ export enum ContentKind {
7373

7474
export interface Generated {
7575
files: string[];
76-
oasItem: OasItem;
76+
oasItem: OpenApiSpec;
7777
config: StrictConfig;
7878
}
7979

0 commit comments

Comments
 (0)