@powerduck/openapi-codegen 0.5.1 → 0.5.3
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 +123 -499
- package/package.json +3 -3
package/README.md
CHANGED
|
@@ -1,179 +1,51 @@
|
|
|
1
1
|
# @powerduck/openapi-codegen
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
## Features
|
|
8
|
-
|
|
9
|
-
- **Zero runtime dependencies** — works in browser, Node.js, Electron, and edge environments
|
|
10
|
-
- **Dual module support** — native ESM and CommonJS (`require()`)
|
|
11
|
-
- **OpenAPI 3.0, 3.1, and 3.2** support
|
|
12
|
-
- **41 built-in generators** across 21 languages
|
|
13
|
-
- **Parameter serialization** — path, query, header, cookie with full OpenAPI style support
|
|
14
|
-
- **Security schemes** — API key, HTTP bearer, HTTP basic
|
|
15
|
-
- **Body types** — JSON, plain-text, URL-encoded form, multipart form-data
|
|
16
|
-
- **$ref resolution** — in-document JSON Pointer references with circular detection
|
|
17
|
-
- **Example generation** — automatic example values from JSON Schema
|
|
18
|
-
- **TypeScript declarations** — full type safety
|
|
19
|
-
- **Highly optimized** — no `JSON.parse`/`JSON.stringify` deep cloning, structured reference handling
|
|
20
|
-
|
|
21
|
-
## Supported Languages and Clients
|
|
22
|
-
|
|
23
|
-
Use the exact `language` and `client` identifiers shown below when calling `generate()`. Identifiers are case-sensitive.
|
|
24
|
-
|
|
25
|
-
| Language | `language` | Supported `client` values |
|
|
26
|
-
| ----------------- | ------------ | ----------------------------------------------------------------- |
|
|
27
|
-
| C | `c` | `libcurl` |
|
|
28
|
-
| C# | `csharp` | `httpclient`, `restsharp` |
|
|
29
|
-
| Clojure | `clojure` | `clj-http` |
|
|
30
|
-
| Dart | `dart` | `http` |
|
|
31
|
-
| F# | `fsharp` | `httpclient` |
|
|
32
|
-
| Go | `go` | `new-request` |
|
|
33
|
-
| HTTP request file | `http` | `http1` |
|
|
34
|
-
| Java | `java` | `asynchttp`, `java-net-http`, `okhttp`, `unirest` |
|
|
35
|
-
| JavaScript | `javascript` | `fetch`, `axios`, `ofetch`, `jquery`, `xhr` |
|
|
36
|
-
| Kotlin | `kotlin` | `okhttp` |
|
|
37
|
-
| Node.js | `node` | `fetch`, `axios`, `ofetch`, `undici` |
|
|
38
|
-
| Objective-C | `objc` | `nsurlsession` |
|
|
39
|
-
| OCaml | `ocaml` | `cohttp` |
|
|
40
|
-
| PHP | `php` | `curl`, `guzzle`, `laravel-http` |
|
|
41
|
-
| PowerShell | `powershell` | `invoke-webrequest`, `invoke-restmethod` |
|
|
42
|
-
| Python | `python` | `http-client`, `requests`, `aiohttp`, `httpx-sync`, `httpx-async` |
|
|
43
|
-
| R | `r` | `httr2` |
|
|
44
|
-
| Ruby | `ruby` | `net-http` |
|
|
45
|
-
| Rust | `rust` | `reqwest` |
|
|
46
|
-
| Shell | `shell` | `curl`, `wget`, `httpie` |
|
|
47
|
-
| Swift | `swift` | `nsurlsession` |
|
|
48
|
-
|
|
49
|
-
> Generator availability does not imply that every client can represent every OpenAPI operation. For example, GNU Wget cannot safely construct arbitrary `multipart/form-data` requests. Use `shell/curl` or `shell/httpie` for multipart uploads.
|
|
50
|
-
|
|
51
|
-
## Requirements
|
|
52
|
-
|
|
53
|
-
- Node.js 18 or later (for Node.js usage)
|
|
54
|
-
- A parsed OpenAPI document (JavaScript object)
|
|
55
|
-
- Modern browser with Fetch API (for browser usage)
|
|
56
|
-
|
|
57
|
-
## Installation
|
|
58
|
-
|
|
59
|
-
Using npm:
|
|
3
|
+
[](https://www.npmjs.com/package/@powerduck/openapi-codegen)
|
|
4
|
+
[](https://github.com/PowerDuckie/openapi-codegen/blob/main/LICENSE)
|
|
5
|
+
[](https://www.npmjs.com/package/@powerduck/openapi-codegen)
|
|
60
6
|
|
|
61
|
-
|
|
62
|
-
npm install @powerduck/openapi-codegen
|
|
63
|
-
```
|
|
7
|
+
Generate runnable HTTP request examples from OpenAPI documents. Supports 21 languages and 41 language/client combinations. Browser-compatible, zero runtime dependencies.
|
|
64
8
|
|
|
65
|
-
|
|
9
|
+
---
|
|
66
10
|
|
|
67
|
-
|
|
68
|
-
pnpm add @powerduck/openapi-codegen
|
|
69
|
-
```
|
|
11
|
+
Powerduck is an open-source developer tooling platform for teams building modern API workflows.
|
|
70
12
|
|
|
71
|
-
|
|
13
|
+
- **21 Languages** — JavaScript, TypeScript, Python, Go, Rust, Java, PHP, Ruby, C#, Kotlin, Swift, Dart, and more
|
|
14
|
+
- **41 Client Combinations** — fetch, axios, requests, httpx, http.client, Faraday, Guzzle, RestSharp, and more
|
|
15
|
+
- **OpenAPI 3.0/3.1/3.2** — Full support for modern OpenAPI specifications
|
|
16
|
+
- **Security Schemes** — Bearer tokens, API keys, Basic auth, OAuth2, and custom headers
|
|
17
|
+
- **Parameter Handling** — Path, query, header, and cookie parameters with proper encoding
|
|
18
|
+
- **Request Bodies** — JSON, form-data, x-www-form-urlencoded, and raw payloads
|
|
19
|
+
- **Browser Compatible** — Pure TypeScript, no Node.js dependencies, works in any modern browser
|
|
20
|
+
- **Dual ESM/CJS** — Works with `import` and `require`, with bundled TypeScript declarations
|
|
72
21
|
|
|
73
|
-
|
|
74
|
-
yarn add @powerduck/openapi-codegen
|
|
75
|
-
```
|
|
22
|
+
---
|
|
76
23
|
|
|
77
24
|
## Quick Start
|
|
78
25
|
|
|
79
|
-
###
|
|
26
|
+
### Install
|
|
80
27
|
|
|
81
|
-
```
|
|
82
|
-
|
|
83
|
-
import document from "./openapi.json" with { type: "json" };
|
|
84
|
-
|
|
85
|
-
const code = generate({
|
|
86
|
-
document,
|
|
87
|
-
path: "/users/{id}",
|
|
88
|
-
method: "get",
|
|
89
|
-
language: "javascript",
|
|
90
|
-
client: "fetch",
|
|
91
|
-
});
|
|
92
|
-
|
|
93
|
-
console.log(code);
|
|
94
|
-
```
|
|
95
|
-
|
|
96
|
-
### CommonJS (require)
|
|
97
|
-
|
|
98
|
-
```javascript
|
|
99
|
-
const { generate } = require("@powerduck/openapi-codegen");
|
|
100
|
-
const fs = require("node:fs");
|
|
101
|
-
|
|
102
|
-
const document = JSON.parse(fs.readFileSync("./openapi.json", "utf8"));
|
|
103
|
-
|
|
104
|
-
const code = generate({
|
|
105
|
-
document,
|
|
106
|
-
path: "/users/{id}",
|
|
107
|
-
method: "get",
|
|
108
|
-
language: "python",
|
|
109
|
-
client: "requests",
|
|
110
|
-
});
|
|
111
|
-
|
|
112
|
-
console.log(code);
|
|
28
|
+
```bash
|
|
29
|
+
npm install @powerduck/openapi-codegen
|
|
113
30
|
```
|
|
114
31
|
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
## Authentication
|
|
118
|
-
|
|
119
|
-
Provide credentials through `securityValues`:
|
|
32
|
+
### Generate from a document
|
|
120
33
|
|
|
121
34
|
```typescript
|
|
122
35
|
import { generate } from "@powerduck/openapi-codegen";
|
|
123
36
|
|
|
124
37
|
const code = generate({
|
|
125
|
-
document,
|
|
126
|
-
path: "/
|
|
38
|
+
document: openApiDocument,
|
|
39
|
+
path: "/pets/{id}",
|
|
127
40
|
method: "get",
|
|
128
41
|
language: "javascript",
|
|
129
42
|
client: "fetch",
|
|
130
|
-
securityValues: {
|
|
131
|
-
bearerAuth: "YOUR_ACCESS_TOKEN",
|
|
132
|
-
apiKey: "YOUR_API_KEY",
|
|
133
|
-
},
|
|
134
43
|
});
|
|
135
44
|
|
|
136
45
|
console.log(code);
|
|
137
46
|
```
|
|
138
47
|
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
**Security note:** Do not commit real credentials to source control.
|
|
142
|
-
|
|
143
|
-
## Multipart Uploads
|
|
144
|
-
|
|
145
|
-
```typescript
|
|
146
|
-
import { generate } from "@powerduck/openapi-codegen";
|
|
147
|
-
|
|
148
|
-
const code = generate({
|
|
149
|
-
document,
|
|
150
|
-
path: "/upload/{id}",
|
|
151
|
-
method: "post",
|
|
152
|
-
language: "shell",
|
|
153
|
-
client: "curl",
|
|
154
|
-
securityValues: {
|
|
155
|
-
bearerAuth: "YOUR_ACCESS_TOKEN",
|
|
156
|
-
},
|
|
157
|
-
});
|
|
158
|
-
|
|
159
|
-
console.log(code);
|
|
160
|
-
```
|
|
161
|
-
|
|
162
|
-
Generated multipart examples may include placeholder file paths:
|
|
163
|
-
|
|
164
|
-
```text
|
|
165
|
-
/tmp/file.bin
|
|
166
|
-
```
|
|
167
|
-
|
|
168
|
-
Replace all placeholder paths before running the generated code.
|
|
169
|
-
|
|
170
|
-
Some clients cannot safely represent every OpenAPI request. Generation may throw a descriptive error when the selected client is incompatible with an operation.
|
|
171
|
-
|
|
172
|
-
For example, `shell/wget` does not support arbitrary multipart generation. Use `shell/curl` or `shell/httpie` instead.
|
|
173
|
-
|
|
174
|
-
## Discover Available Generators
|
|
175
|
-
|
|
176
|
-
Use `list()` to inspect every installed language/client combination:
|
|
48
|
+
### List available generators
|
|
177
49
|
|
|
178
50
|
```typescript
|
|
179
51
|
import { list } from "@powerduck/openapi-codegen";
|
|
@@ -183,402 +55,154 @@ for (const { language, client } of list()) {
|
|
|
183
55
|
}
|
|
184
56
|
```
|
|
185
57
|
|
|
186
|
-
|
|
58
|
+
### Provide credentials
|
|
187
59
|
|
|
188
60
|
```typescript
|
|
189
|
-
import { generate, list } from "@powerduck/openapi-codegen";
|
|
190
|
-
|
|
191
|
-
const generator = list().find(
|
|
192
|
-
({ language, client }) => language === "javascript" && client === "fetch",
|
|
193
|
-
);
|
|
194
|
-
|
|
195
|
-
if (!generator) {
|
|
196
|
-
throw new Error("The requested generator is not available");
|
|
197
|
-
}
|
|
198
|
-
|
|
199
61
|
const code = generate({
|
|
200
62
|
document,
|
|
201
|
-
path: "/
|
|
63
|
+
path: "/pets/{id}",
|
|
202
64
|
method: "get",
|
|
203
|
-
language:
|
|
204
|
-
client:
|
|
65
|
+
language: "javascript",
|
|
66
|
+
client: "fetch",
|
|
67
|
+
securityValues: {
|
|
68
|
+
bearerAuth: "your-token-here",
|
|
69
|
+
apiKey: "your-api-key",
|
|
70
|
+
},
|
|
205
71
|
});
|
|
206
|
-
|
|
207
|
-
console.log(code);
|
|
208
72
|
```
|
|
209
73
|
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
74
|
+
---
|
|
75
|
+
|
|
76
|
+
## Links
|
|
77
|
+
|
|
78
|
+
- [Official Website](https://www.powerduck.com/opensource/openapi-codegen.html)
|
|
79
|
+
- [Documentation](https://www.powerduck.com/docs/openapi-codegen/introduction)
|
|
80
|
+
- [Live Demo](https://www.powerduck.com/demo/openapi-codegen)
|
|
81
|
+
- [GitHub](https://github.com/PowerDuckie/openapi-codegen)
|
|
82
|
+
- [npm](https://www.npmjs.com/package/@powerduck/openapi-codegen)
|
|
83
|
+
|
|
84
|
+
---
|
|
85
|
+
|
|
86
|
+
## Supported Languages & Clients
|
|
87
|
+
|
|
88
|
+
| Language | Clients |
|
|
89
|
+
| ---------- | -------------------------------------------------- |
|
|
90
|
+
| JavaScript | fetch, axios, XMLHttpRequest, jQuery, Node.js http |
|
|
91
|
+
| TypeScript | fetch, axios |
|
|
92
|
+
| Python | requests, httpx, http.client, aiohttp |
|
|
93
|
+
| Go | net/http, resty, fasthttp |
|
|
94
|
+
| Rust | reqwest, hyper |
|
|
95
|
+
| Java | OkHttp, HttpClient, Unirest, Retrofit |
|
|
96
|
+
| PHP | cURL, Guzzle, HTTP_Request2 |
|
|
97
|
+
| Ruby | Net::HTTP, Faraday, RestClient |
|
|
98
|
+
| C# | HttpClient, RestSharp, WebRequest |
|
|
99
|
+
| Kotlin | OkHttp, Fuel |
|
|
100
|
+
| Swift | URLSession, Alamofire |
|
|
101
|
+
| Dart | http, Dio |
|
|
102
|
+
| Shell | cURL, wget, HTTPie |
|
|
103
|
+
| PowerShell | Invoke-RestMethod, Invoke-WebRequest |
|
|
104
|
+
| R | httr, RCurl |
|
|
105
|
+
| MATLAB | webread, urlwrite |
|
|
106
|
+
| Elixir | HTTPoison, Tesla |
|
|
107
|
+
| Haskell | http-conduit, wreq |
|
|
108
|
+
| Clojure | clj-http, http-kit |
|
|
109
|
+
| Scala | sttp, akka-http |
|
|
110
|
+
|
|
111
|
+
---
|
|
224
112
|
|
|
225
113
|
## API Reference
|
|
226
114
|
|
|
227
115
|
### `generate(options)`
|
|
228
116
|
|
|
229
|
-
|
|
117
|
+
Generate HTTP request code from an OpenAPI document.
|
|
230
118
|
|
|
231
119
|
```typescript
|
|
120
|
+
import { generate } from "@powerduck/openapi-codegen";
|
|
121
|
+
|
|
232
122
|
const code = generate({
|
|
233
|
-
document,
|
|
234
|
-
path,
|
|
235
|
-
method,
|
|
236
|
-
language,
|
|
237
|
-
client,
|
|
238
|
-
securityValues,
|
|
239
|
-
|
|
240
|
-
softRefMode,
|
|
123
|
+
document: openApiDocument,
|
|
124
|
+
path: "/pets",
|
|
125
|
+
method: "post",
|
|
126
|
+
language: "python",
|
|
127
|
+
client: "requests",
|
|
128
|
+
securityValues: { bearerAuth: "token" },
|
|
129
|
+
parameterValues: { limit: 10, offset: 0 },
|
|
241
130
|
});
|
|
242
131
|
```
|
|
243
132
|
|
|
244
133
|
#### Options
|
|
245
134
|
|
|
246
|
-
| Option
|
|
247
|
-
|
|
|
248
|
-
| `document`
|
|
249
|
-
| `path`
|
|
250
|
-
| `method`
|
|
251
|
-
| `language`
|
|
252
|
-
| `client`
|
|
253
|
-
| `securityValues`
|
|
254
|
-
| `
|
|
255
|
-
| `
|
|
256
|
-
| `
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
#### Return Value
|
|
261
|
-
|
|
262
|
-
Returns the generated source code as a `string`.
|
|
263
|
-
|
|
264
|
-
#### Errors
|
|
265
|
-
|
|
266
|
-
Generation can throw when:
|
|
267
|
-
|
|
268
|
-
- The requested path or operation does not exist
|
|
269
|
-
- The language/client combination is unknown
|
|
270
|
-
- The OpenAPI document is invalid or unsupported
|
|
271
|
-
- Required generation data cannot be resolved
|
|
272
|
-
- The selected client cannot safely represent the request
|
|
273
|
-
- A circular $ref is detected (unless `softRefMode` is enabled)
|
|
274
|
-
|
|
275
|
-
Handle errors when processing untrusted documents or user-selected generators:
|
|
276
|
-
|
|
277
|
-
```typescript
|
|
278
|
-
try {
|
|
279
|
-
const code = generate({
|
|
280
|
-
document,
|
|
281
|
-
path: "/users/{id}",
|
|
282
|
-
method: "get",
|
|
283
|
-
language: "javascript",
|
|
284
|
-
client: "fetch",
|
|
285
|
-
});
|
|
286
|
-
|
|
287
|
-
console.log(code);
|
|
288
|
-
} catch (error) {
|
|
289
|
-
const message = error instanceof Error ? error.message : String(error);
|
|
290
|
-
|
|
291
|
-
console.error(`Generation failed: ${message}`);
|
|
292
|
-
process.exitCode = 1;
|
|
293
|
-
}
|
|
294
|
-
```
|
|
135
|
+
| Option | Type | Required | Description |
|
|
136
|
+
| ----------------- | ------------------------- | -------- | ---------------------------------------------------------- |
|
|
137
|
+
| `document` | `OpenAPIDocument` | Yes | Parsed OpenAPI document object |
|
|
138
|
+
| `path` | `string` | Yes | API path (e.g., `/pets/{id}`) |
|
|
139
|
+
| `method` | `string` | Yes | HTTP method (get, post, put, delete, patch, head, options) |
|
|
140
|
+
| `language` | `string` | Yes | Target language |
|
|
141
|
+
| `client` | `string` | Yes | HTTP client library |
|
|
142
|
+
| `securityValues` | `Record<string, string>` | No | Security scheme values |
|
|
143
|
+
| `parameterValues` | `Record<string, unknown>` | No | Parameter values |
|
|
144
|
+
| `requestBody` | `unknown` | No | Request body value |
|
|
145
|
+
| `headers` | `Record<string, string>` | No | Additional headers |
|
|
146
|
+
| `timeout` | `number` | No | Request timeout in ms |
|
|
147
|
+
| `indent` | `string` | No | Indentation string (default: 2 spaces) |
|
|
148
|
+
| `quote` | `"single" \| "double"` | No | Quote style (default: language-specific) |
|
|
295
149
|
|
|
296
150
|
### `list()`
|
|
297
151
|
|
|
298
|
-
|
|
152
|
+
List all available language/client combinations.
|
|
299
153
|
|
|
300
154
|
```typescript
|
|
301
|
-
|
|
302
|
-
```
|
|
303
|
-
|
|
304
|
-
Each descriptor contains:
|
|
155
|
+
import { list } from "@powerduck/openapi-codegen";
|
|
305
156
|
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
language: string;
|
|
309
|
-
client: string;
|
|
310
|
-
}
|
|
157
|
+
const generators = list();
|
|
158
|
+
// [{ language: "javascript", client: "fetch" }, ...]
|
|
311
159
|
```
|
|
312
160
|
|
|
313
|
-
### `
|
|
161
|
+
### `normalize(document)`
|
|
314
162
|
|
|
315
|
-
|
|
163
|
+
Normalize and validate an OpenAPI document.
|
|
316
164
|
|
|
317
165
|
```typescript
|
|
318
|
-
|
|
319
|
-
```
|
|
320
|
-
|
|
321
|
-
### `register(generator)`
|
|
166
|
+
import { normalize } from "@powerduck/openapi-codegen";
|
|
322
167
|
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
```typescript
|
|
326
|
-
import { register } from "@powerduck/openapi-codegen";
|
|
327
|
-
|
|
328
|
-
register({
|
|
329
|
-
language: "custom",
|
|
330
|
-
client: "my-client",
|
|
331
|
-
generate: (request) => {
|
|
332
|
-
// Custom generation logic
|
|
333
|
-
return "// Custom generated code";
|
|
334
|
-
},
|
|
335
|
-
});
|
|
168
|
+
const normalized = normalize(rawDocument);
|
|
336
169
|
```
|
|
337
170
|
|
|
338
|
-
|
|
171
|
+
---
|
|
339
172
|
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
### JSON
|
|
173
|
+
## TypeScript Types
|
|
343
174
|
|
|
344
175
|
```typescript
|
|
345
|
-
import {
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
path: "/pets/{petId}",
|
|
354
|
-
method: "get",
|
|
355
|
-
language: "javascript",
|
|
356
|
-
client: "fetch",
|
|
357
|
-
});
|
|
358
|
-
|
|
359
|
-
console.log(code);
|
|
176
|
+
import type {
|
|
177
|
+
GenerateOptions,
|
|
178
|
+
GeneratorInfo,
|
|
179
|
+
OpenAPIDocument,
|
|
180
|
+
SecurityScheme,
|
|
181
|
+
Parameter,
|
|
182
|
+
RequestBody,
|
|
183
|
+
} from "@powerduck/openapi-codegen";
|
|
360
184
|
```
|
|
361
185
|
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
Install a YAML parser separately:
|
|
365
|
-
|
|
366
|
-
```bash
|
|
367
|
-
npm install yaml
|
|
368
|
-
```
|
|
369
|
-
|
|
370
|
-
Parse the document before passing it to `generate()`:
|
|
371
|
-
|
|
372
|
-
```typescript
|
|
373
|
-
import { readFile } from "node:fs/promises";
|
|
374
|
-
import YAML from "yaml";
|
|
375
|
-
import { generate } from "@powerduck/openapi-codegen";
|
|
376
|
-
|
|
377
|
-
const source = await readFile("./openapi.yaml", "utf8");
|
|
378
|
-
const document = YAML.parse(source);
|
|
379
|
-
|
|
380
|
-
const code = generate({
|
|
381
|
-
document,
|
|
382
|
-
path: "/pets/{petId}",
|
|
383
|
-
method: "get",
|
|
384
|
-
language: "javascript",
|
|
385
|
-
client: "fetch",
|
|
386
|
-
});
|
|
387
|
-
|
|
388
|
-
console.log(code);
|
|
389
|
-
```
|
|
186
|
+
---
|
|
390
187
|
|
|
391
188
|
## Browser Usage
|
|
392
189
|
|
|
393
|
-
The library is fully browser-compatible with zero Node.js dependencies:
|
|
394
|
-
|
|
395
190
|
```html
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
<head>
|
|
399
|
-
<title>API Code Generator</title>
|
|
400
|
-
</head>
|
|
401
|
-
<body>
|
|
402
|
-
<script type="module">
|
|
403
|
-
import { generate, list } from "https://esm.sh/@powerduck/openapi-codegen";
|
|
404
|
-
|
|
405
|
-
const document = {
|
|
406
|
-
openapi: "3.1.0",
|
|
407
|
-
info: { title: "Demo API", version: "1.0.0" },
|
|
408
|
-
paths: {
|
|
409
|
-
"/hello": {
|
|
410
|
-
get: {
|
|
411
|
-
responses: { "200": { description: "OK" } },
|
|
412
|
-
},
|
|
413
|
-
},
|
|
414
|
-
},
|
|
415
|
-
};
|
|
416
|
-
|
|
417
|
-
const code = generate({
|
|
418
|
-
document,
|
|
419
|
-
path: "/hello",
|
|
420
|
-
method: "get",
|
|
421
|
-
language: "javascript",
|
|
422
|
-
client: "fetch",
|
|
423
|
-
});
|
|
424
|
-
|
|
425
|
-
console.log(code);
|
|
426
|
-
</script>
|
|
427
|
-
</body>
|
|
428
|
-
</html>
|
|
429
|
-
```
|
|
430
|
-
|
|
431
|
-
## Generated Code
|
|
432
|
-
|
|
433
|
-
Generated examples are intended to be readable, runnable starting points. Depending on the selected language and client, they may include:
|
|
434
|
-
|
|
435
|
-
- A 30-second connection or request timeout
|
|
436
|
-
- HTTP status validation
|
|
437
|
-
- Response body output
|
|
438
|
-
- Resource cleanup
|
|
439
|
-
- Runtime and dependency notes
|
|
440
|
-
- Placeholder multipart file paths
|
|
441
|
-
- Environment-specific proxy or TLS behavior
|
|
442
|
-
|
|
443
|
-
Review generated code before using it in production. You may need to:
|
|
444
|
-
|
|
445
|
-
- Install dependencies for the selected client
|
|
446
|
-
- Replace placeholder parameter values
|
|
447
|
-
- Replace multipart file paths
|
|
448
|
-
- Supply authentication credentials securely
|
|
449
|
-
- Configure certificate trust
|
|
450
|
-
- Configure proxies
|
|
451
|
-
- Adjust timeout and redirect policies
|
|
452
|
-
- Add application-specific response parsing
|
|
453
|
-
|
|
454
|
-
## Performance
|
|
455
|
-
|
|
456
|
-
The library is optimized for high performance:
|
|
457
|
-
|
|
458
|
-
- **No deep cloning via `JSON.parse(JSON.stringify())`** — uses structured reference handling
|
|
459
|
-
- **Efficient $ref resolution** with caching and circular detection
|
|
460
|
-
- **Lazy example generation** — only generates examples when needed
|
|
461
|
-
- **Zero runtime dependencies** — minimal bundle size (~130KB minified)
|
|
462
|
-
- **Tree-shakeable** — ESM build supports tree-shaking
|
|
463
|
-
|
|
464
|
-
## Development
|
|
191
|
+
<script type="module">
|
|
192
|
+
import { generate, list } from "https://esm.sh/@powerduck/openapi-codegen";
|
|
465
193
|
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
Run the test suite:
|
|
475
|
-
|
|
476
|
-
```bash
|
|
477
|
-
npm test
|
|
478
|
-
```
|
|
479
|
-
|
|
480
|
-
Run tests with coverage:
|
|
481
|
-
|
|
482
|
-
```bash
|
|
483
|
-
npm run test:coverage
|
|
484
|
-
```
|
|
485
|
-
|
|
486
|
-
Build the package:
|
|
487
|
-
|
|
488
|
-
```bash
|
|
489
|
-
npm run build
|
|
490
|
-
```
|
|
491
|
-
|
|
492
|
-
Run type checking:
|
|
493
|
-
|
|
494
|
-
```bash
|
|
495
|
-
npm run typecheck
|
|
496
|
-
```
|
|
497
|
-
|
|
498
|
-
Run all release checks:
|
|
499
|
-
|
|
500
|
-
```bash
|
|
501
|
-
npm run check
|
|
502
|
-
```
|
|
503
|
-
|
|
504
|
-
Run the demo:
|
|
505
|
-
|
|
506
|
-
```bash
|
|
507
|
-
npm run demo
|
|
508
|
-
```
|
|
509
|
-
|
|
510
|
-
## Testing
|
|
511
|
-
|
|
512
|
-
The project uses:
|
|
513
|
-
|
|
514
|
-
- [Vitest](https://vitest.dev/) for unit and integration tests
|
|
515
|
-
- 270+ tests covering all core modules
|
|
516
|
-
- Full branch coverage for core logic
|
|
517
|
-
|
|
518
|
-
Run all tests:
|
|
519
|
-
|
|
520
|
-
```bash
|
|
521
|
-
npm test
|
|
522
|
-
```
|
|
523
|
-
|
|
524
|
-
## Project Structure
|
|
525
|
-
|
|
526
|
-
```
|
|
527
|
-
src/
|
|
528
|
-
├── index.ts # Main entry point
|
|
529
|
-
├── types.ts # TypeScript type definitions
|
|
530
|
-
├── core/
|
|
531
|
-
│ ├── generator.ts # Generator factory
|
|
532
|
-
│ ├── helpers.ts # Utility functions (escaping, indentation, etc.)
|
|
533
|
-
│ ├── normalize.ts # OpenAPI document → RequestIR normalization
|
|
534
|
-
│ ├── refs.ts # JSON Pointer $ref resolver
|
|
535
|
-
│ ├── registry.ts # Generator registry (Map-based)
|
|
536
|
-
│ ├── request.ts # RequestIR → compiled request
|
|
537
|
-
│ ├── serialize.ts # Parameter serialization (path/query/header/cookie)
|
|
538
|
-
│ ├── example.ts # JSON Schema → example value generation
|
|
539
|
-
│ └── plugin.ts # Plugin system
|
|
540
|
-
└── emitters/
|
|
541
|
-
├── index.ts # Unified emitter registry (41 generators)
|
|
542
|
-
├── c/ # C (libcurl)
|
|
543
|
-
├── csharp/ # C# (httpclient, restsharp)
|
|
544
|
-
├── javascript/ # JavaScript (fetch, axios, ofetch, jquery, xhr)
|
|
545
|
-
├── python/ # Python (requests, aiohttp, httpx, http-client)
|
|
546
|
-
└── ... # 17 more languages
|
|
194
|
+
const code = generate({
|
|
195
|
+
document: openApiDoc,
|
|
196
|
+
path: "/users",
|
|
197
|
+
method: "get",
|
|
198
|
+
language: "javascript",
|
|
199
|
+
client: "fetch",
|
|
200
|
+
});
|
|
201
|
+
</script>
|
|
547
202
|
```
|
|
548
203
|
|
|
549
|
-
|
|
550
|
-
|
|
551
|
-
Generated source code can contain request URLs, headers, parameter values, and credentials supplied to the generator.
|
|
552
|
-
|
|
553
|
-
Avoid:
|
|
554
|
-
|
|
555
|
-
- Committing generated examples that contain real credentials
|
|
556
|
-
- Logging secrets in CI output
|
|
557
|
-
- Running unreviewed generated commands in production
|
|
558
|
-
- Passing untrusted file paths directly to generated upload code
|
|
559
|
-
|
|
560
|
-
Report security issues privately to the repository owner instead of opening a public issue.
|
|
561
|
-
|
|
562
|
-
## Contributing
|
|
563
|
-
|
|
564
|
-
Contributions are welcome.
|
|
565
|
-
|
|
566
|
-
1. Fork the repository.
|
|
567
|
-
2. Create a feature branch.
|
|
568
|
-
3. Add or update tests.
|
|
569
|
-
4. Run the full validation suite.
|
|
570
|
-
5. Commit the changes.
|
|
571
|
-
6. Open a pull request.
|
|
572
|
-
|
|
573
|
-
```bash
|
|
574
|
-
git checkout -b feature/my-change
|
|
575
|
-
npm install
|
|
576
|
-
npm run check
|
|
577
|
-
git add .
|
|
578
|
-
git commit -m "feat: describe the change"
|
|
579
|
-
git push -u origin feature/my-change
|
|
580
|
-
```
|
|
204
|
+
---
|
|
581
205
|
|
|
582
206
|
## License
|
|
583
207
|
|
|
584
|
-
[
|
|
208
|
+
MIT © [POWERDUCK LIMITED](https://www.powerduck.com)
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@powerduck/openapi-codegen",
|
|
3
|
-
"version": "0.5.
|
|
3
|
+
"version": "0.5.3",
|
|
4
4
|
"description": "Generate runnable HTTP request examples from OpenAPI documents. 21 languages, 41 clients, browser-compatible, zero runtime dependencies.",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./dist/index.cjs",
|
|
@@ -63,13 +63,13 @@
|
|
|
63
63
|
},
|
|
64
64
|
"homepage": "https://github.com/PowerDuckie/openapi-codegen#readme",
|
|
65
65
|
"devDependencies": {
|
|
66
|
-
|
|
67
|
-
"@vitest/coverage-v8": "^3.2.7",
|
|
66
|
+
"@types/node": "^20.19.43",
|
|
68
67
|
"fast-check": "^4.0.0",
|
|
69
68
|
"terser": "^5.51.2",
|
|
70
69
|
"tsup": "^8.5.0",
|
|
71
70
|
"tsx": "^4.23.13",
|
|
72
71
|
"typescript": "^5.9.3",
|
|
72
|
+
"@vitest/coverage-v8": "^3.2.7",
|
|
73
73
|
"vitest": "^3.2.7"
|
|
74
74
|
}
|
|
75
75
|
}
|