axigen 0.1.0 → 0.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +157 -27
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1,109 +1,239 @@
|
|
|
1
1
|
# axigen
|
|
2
2
|
|
|
3
|
-
> Generate typed Axios client functions from OpenAPI / Swagger
|
|
3
|
+
> Generate typed Axios client functions from your OpenAPI / Swagger spec — in one command.
|
|
4
4
|
|
|
5
5
|
[](https://www.npmjs.com/package/axigen)
|
|
6
6
|
[](./LICENSE)
|
|
7
|
+
[](https://nodejs.org)
|
|
7
8
|
|
|
8
|
-
##
|
|
9
|
+
## Overview
|
|
10
|
+
|
|
11
|
+
**axigen** reads your OpenAPI (or Swagger) spec and generates fully-typed Axios wrapper functions, so you never have to write boilerplate API calls by hand again.
|
|
12
|
+
|
|
13
|
+
- ✅ Supports OpenAPI 3.x and Swagger 2.x (YAML or JSON)
|
|
14
|
+
- ✅ Generates typed request/response interfaces
|
|
15
|
+
- ✅ Works with your own Axios instance
|
|
16
|
+
- ✅ TypeScript and JavaScript output
|
|
17
|
+
- ✅ JSDoc comments from your spec's summaries
|
|
18
|
+
- ✅ Filter endpoints by tag
|
|
19
|
+
- ✅ Zero runtime dependencies in generated code
|
|
20
|
+
|
|
21
|
+
## Installation
|
|
9
22
|
|
|
10
23
|
```bash
|
|
11
24
|
npm install -D axigen
|
|
12
|
-
#
|
|
25
|
+
# or
|
|
13
26
|
pnpm add -D axigen
|
|
27
|
+
# or
|
|
28
|
+
yarn add -D axigen
|
|
14
29
|
```
|
|
15
30
|
|
|
16
|
-
##
|
|
31
|
+
## Quick Start
|
|
17
32
|
|
|
18
|
-
|
|
33
|
+
**1. Create a config file:**
|
|
19
34
|
|
|
20
35
|
```bash
|
|
21
36
|
npx axigen init
|
|
22
37
|
```
|
|
23
38
|
|
|
24
|
-
|
|
39
|
+
**2. Edit `axigen.config.js`:**
|
|
25
40
|
|
|
26
41
|
```js
|
|
27
42
|
/** @type {import('axigen').AxigenConfig} */
|
|
28
43
|
module.exports = {
|
|
44
|
+
// Path to your OpenAPI spec (YAML or JSON)
|
|
29
45
|
input: "./openapi.yaml",
|
|
46
|
+
|
|
30
47
|
output: {
|
|
48
|
+
// Generated Axios client functions
|
|
31
49
|
client: "./src/api/client.ts",
|
|
50
|
+
// Generated TypeScript types
|
|
32
51
|
types: "./src/api/types.ts",
|
|
33
52
|
},
|
|
53
|
+
|
|
54
|
+
// Import path to your Axios instance inside your project
|
|
34
55
|
axiosInstancePath: "../lib/axios",
|
|
56
|
+
|
|
35
57
|
language: "ts",
|
|
36
58
|
jsdoc: true,
|
|
37
59
|
};
|
|
38
60
|
```
|
|
39
61
|
|
|
40
|
-
|
|
62
|
+
**3. Run:**
|
|
41
63
|
|
|
42
64
|
```bash
|
|
43
65
|
npx axigen generate
|
|
44
|
-
#
|
|
66
|
+
# or simply:
|
|
45
67
|
npx axigen
|
|
46
68
|
```
|
|
47
69
|
|
|
48
|
-
|
|
70
|
+
---
|
|
49
71
|
|
|
50
|
-
|
|
72
|
+
## Example
|
|
73
|
+
|
|
74
|
+
Given this OpenAPI spec:
|
|
51
75
|
|
|
52
76
|
```yaml
|
|
53
77
|
paths:
|
|
78
|
+
/users:
|
|
79
|
+
get:
|
|
80
|
+
operationId: getUsers
|
|
81
|
+
summary: List all users
|
|
82
|
+
parameters:
|
|
83
|
+
- name: page
|
|
84
|
+
in: query
|
|
85
|
+
schema:
|
|
86
|
+
type: integer
|
|
87
|
+
- name: limit
|
|
88
|
+
in: query
|
|
89
|
+
schema:
|
|
90
|
+
type: integer
|
|
91
|
+
|
|
54
92
|
/users/{userId}:
|
|
55
93
|
get:
|
|
56
94
|
operationId: getUserById
|
|
95
|
+
summary: Get a user by ID
|
|
96
|
+
parameters:
|
|
97
|
+
- name: userId
|
|
98
|
+
in: path
|
|
99
|
+
required: true
|
|
100
|
+
schema:
|
|
101
|
+
type: string
|
|
102
|
+
|
|
103
|
+
put:
|
|
104
|
+
operationId: updateUser
|
|
105
|
+
summary: Update a user
|
|
57
106
|
parameters:
|
|
58
107
|
- name: userId
|
|
59
108
|
in: path
|
|
60
109
|
required: true
|
|
110
|
+
schema:
|
|
111
|
+
type: string
|
|
112
|
+
requestBody:
|
|
113
|
+
required: true
|
|
114
|
+
content:
|
|
115
|
+
application/json:
|
|
116
|
+
schema:
|
|
117
|
+
$ref: "#/components/schemas/UpdateUserBody"
|
|
61
118
|
```
|
|
62
119
|
|
|
63
|
-
|
|
120
|
+
axigen generates:
|
|
64
121
|
|
|
65
122
|
```ts
|
|
123
|
+
// src/api/client.ts — auto-generated by axigen, do not edit
|
|
124
|
+
|
|
125
|
+
import type { AxiosResponse } from "axios";
|
|
126
|
+
import { axiosInstance } from "../lib/axios";
|
|
127
|
+
import type {
|
|
128
|
+
GetUsersQueryParams,
|
|
129
|
+
GetUsersResponse,
|
|
130
|
+
GetUserByIdPathParams,
|
|
131
|
+
GetUserByIdResponse,
|
|
132
|
+
UpdateUserPathParams,
|
|
133
|
+
UpdateUserBody,
|
|
134
|
+
UpdateUserResponse,
|
|
135
|
+
} from "./types";
|
|
136
|
+
|
|
66
137
|
/**
|
|
67
|
-
*
|
|
138
|
+
* List all users
|
|
139
|
+
* `GET /users`
|
|
140
|
+
*/
|
|
141
|
+
export async function getUsers(params?: GetUsersQueryParams): Promise<AxiosResponse<GetUsersResponse>> {
|
|
142
|
+
return axiosInstance.get("/users", { params });
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
/**
|
|
146
|
+
* Get a user by ID
|
|
68
147
|
* `GET /users/{userId}`
|
|
69
148
|
*/
|
|
70
149
|
export async function getUserById(userId: GetUserByIdPathParams["userId"]): Promise<AxiosResponse<GetUserByIdResponse>> {
|
|
71
150
|
return axiosInstance.get(`/users/${userId}`);
|
|
72
151
|
}
|
|
152
|
+
|
|
153
|
+
/**
|
|
154
|
+
* Update a user
|
|
155
|
+
* `PUT /users/{userId}`
|
|
156
|
+
*/
|
|
157
|
+
export async function updateUser(userId: UpdateUserPathParams["userId"], data: UpdateUserBody): Promise<AxiosResponse<UpdateUserResponse>> {
|
|
158
|
+
return axiosInstance.put(`/users/${userId}`, data);
|
|
159
|
+
}
|
|
160
|
+
```
|
|
161
|
+
|
|
162
|
+
---
|
|
163
|
+
|
|
164
|
+
## Configuration
|
|
165
|
+
|
|
166
|
+
| Option | Type | Default | Description |
|
|
167
|
+
| --------------------- | -------------- | --------------- | ----------------------------------------------------- |
|
|
168
|
+
| `input` | `string` | — | Path to your OpenAPI spec file (YAML or JSON) |
|
|
169
|
+
| `output.client` | `string` | — | Output path for the generated client functions |
|
|
170
|
+
| `output.types` | `string` | — | Output path for generated TypeScript types (optional) |
|
|
171
|
+
| `axiosInstancePath` | `string` | — | Import path to your Axios instance |
|
|
172
|
+
| `axiosInstanceExport` | `string` | `axiosInstance` | Named export of your Axios instance |
|
|
173
|
+
| `language` | `'ts' \| 'js'` | `'ts'` | Output language |
|
|
174
|
+
| `jsdoc` | `boolean` | `true` | Add JSDoc comments to generated functions |
|
|
175
|
+
| `tags` | `string[]` | — | Only generate endpoints matching these tags |
|
|
176
|
+
|
|
177
|
+
### Axios Instance
|
|
178
|
+
|
|
179
|
+
axigen does not create an Axios instance for you — it imports the one you already have in your project. For example:
|
|
180
|
+
|
|
181
|
+
```ts
|
|
182
|
+
// src/lib/axios.ts
|
|
183
|
+
import axios from "axios";
|
|
184
|
+
|
|
185
|
+
export const axiosInstance = axios.create({
|
|
186
|
+
baseURL: "https://api.example.com/v1",
|
|
187
|
+
headers: {
|
|
188
|
+
"Content-Type": "application/json",
|
|
189
|
+
},
|
|
190
|
+
});
|
|
73
191
|
```
|
|
74
192
|
|
|
75
|
-
|
|
193
|
+
Then in your config:
|
|
194
|
+
|
|
195
|
+
```js
|
|
196
|
+
axiosInstancePath: '../lib/axios',
|
|
197
|
+
axiosInstanceExport: 'axiosInstance', // default, can be omitted
|
|
198
|
+
```
|
|
76
199
|
|
|
77
|
-
|
|
78
|
-
| --------------------- | -------------- | --------------- | ----------------------------------- |
|
|
79
|
-
| `input` | `string` | — | مسیر فایل OpenAPI (yaml یا json) |
|
|
80
|
-
| `output.client` | `string` | — | مسیر خروجی فایل توابع |
|
|
81
|
-
| `output.types` | `string` | — | مسیر خروجی فایل types (اختیاری) |
|
|
82
|
-
| `axiosInstancePath` | `string` | — | مسیر import مربوط به axios instance |
|
|
83
|
-
| `axiosInstanceExport` | `string` | `axiosInstance` | نام export |
|
|
84
|
-
| `language` | `'ts' \| 'js'` | `'ts'` | زبان خروجی |
|
|
85
|
-
| `jsdoc` | `boolean` | `true` | اضافه کردن JSDoc |
|
|
86
|
-
| `tags` | `string[]` | — | فیلتر بر اساس تگ |
|
|
200
|
+
---
|
|
87
201
|
|
|
88
|
-
##
|
|
202
|
+
## CLI
|
|
89
203
|
|
|
90
204
|
```bash
|
|
205
|
+
# Generate client from config (default command)
|
|
91
206
|
axigen generate [--config <path>] [--cwd <path>]
|
|
207
|
+
|
|
208
|
+
# Create a starter config file
|
|
92
209
|
axigen init
|
|
210
|
+
|
|
211
|
+
# Show version
|
|
93
212
|
axigen --version
|
|
213
|
+
|
|
214
|
+
# Show help
|
|
94
215
|
axigen --help
|
|
95
216
|
```
|
|
96
217
|
|
|
97
|
-
|
|
218
|
+
---
|
|
219
|
+
|
|
220
|
+
## Programmatic Usage
|
|
221
|
+
|
|
222
|
+
You can also use axigen directly in Node.js scripts:
|
|
98
223
|
|
|
99
224
|
```ts
|
|
100
225
|
import { generate, loadConfig } from "axigen";
|
|
101
226
|
|
|
102
227
|
const config = await loadConfig(process.cwd());
|
|
103
228
|
const result = await generate(config, process.cwd());
|
|
104
|
-
|
|
229
|
+
|
|
230
|
+
console.log(`✓ Generated ${result.endpointCount} endpoints`);
|
|
231
|
+
console.log(` client → ${result.clientPath}`);
|
|
232
|
+
console.log(` types → ${result.typesPath}`);
|
|
105
233
|
```
|
|
106
234
|
|
|
107
|
-
|
|
235
|
+
---
|
|
236
|
+
|
|
237
|
+
## License
|
|
108
238
|
|
|
109
239
|
MIT
|