@redocly/client-generator 0.0.0-snapshot.1784554531 → 0.0.0-snapshot.1784620262

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.
Files changed (64) hide show
  1. package/README.md +87 -415
  2. package/lib/config.d.ts +4 -0
  3. package/lib/config.d.ts.map +1 -1
  4. package/lib/emitters/client-assembly.js +4 -1
  5. package/lib/emitters/client-assembly.js.map +1 -1
  6. package/lib/emitters/descriptor.d.ts.map +1 -1
  7. package/lib/emitters/descriptor.js +1 -1
  8. package/lib/emitters/descriptor.js.map +1 -1
  9. package/lib/emitters/emit-options.d.ts +7 -0
  10. package/lib/emitters/emit-options.d.ts.map +1 -1
  11. package/lib/emitters/emit-options.js.map +1 -1
  12. package/lib/emitters/operation-aliases.d.ts.map +1 -1
  13. package/lib/emitters/operation-aliases.js +6 -0
  14. package/lib/emitters/operation-aliases.js.map +1 -1
  15. package/lib/emitters/operation-signature.d.ts +1 -0
  16. package/lib/emitters/operation-signature.d.ts.map +1 -1
  17. package/lib/emitters/operation-signature.js +5 -2
  18. package/lib/emitters/operation-signature.js.map +1 -1
  19. package/lib/emitters/operations.d.ts +2 -2
  20. package/lib/emitters/operations.d.ts.map +1 -1
  21. package/lib/emitters/operations.js +4 -2
  22. package/lib/emitters/operations.js.map +1 -1
  23. package/lib/emitters/pagination.d.ts +6 -0
  24. package/lib/emitters/pagination.d.ts.map +1 -1
  25. package/lib/emitters/pagination.js +16 -1
  26. package/lib/emitters/pagination.js.map +1 -1
  27. package/lib/emitters/runtime-sources.d.ts +3 -3
  28. package/lib/emitters/runtime-sources.js +3 -3
  29. package/lib/emitters/runtime-sources.js.map +1 -1
  30. package/lib/emitters/wrapper-support.d.ts.map +1 -1
  31. package/lib/emitters/wrapper-support.js +2 -0
  32. package/lib/emitters/wrapper-support.js.map +1 -1
  33. package/lib/generators/mock.js +1 -1
  34. package/lib/generators/mock.js.map +1 -1
  35. package/lib/generators/swr.js +1 -1
  36. package/lib/generators/swr.js.map +1 -1
  37. package/lib/generators/tanstack-query.js +1 -1
  38. package/lib/generators/tanstack-query.js.map +1 -1
  39. package/lib/generators/transformers.d.ts.map +1 -1
  40. package/lib/generators/transformers.js +4 -2
  41. package/lib/generators/transformers.js.map +1 -1
  42. package/lib/index.d.ts.map +1 -1
  43. package/lib/index.js +1 -0
  44. package/lib/index.js.map +1 -1
  45. package/lib/intermediate-representation/build.js +3 -7
  46. package/lib/intermediate-representation/build.js.map +1 -1
  47. package/lib/intermediate-representation/model.d.ts +2 -5
  48. package/lib/intermediate-representation/model.d.ts.map +1 -1
  49. package/lib/intermediate-representation/model.js.map +1 -1
  50. package/lib/intermediate-representation/sanitize-identifiers.js +2 -0
  51. package/lib/intermediate-representation/sanitize-identifiers.js.map +1 -1
  52. package/lib/runtime/create-client.d.ts +2 -1
  53. package/lib/runtime/create-client.d.ts.map +1 -1
  54. package/lib/runtime/create-client.js +23 -4
  55. package/lib/runtime/create-client.js.map +1 -1
  56. package/lib/runtime/paginate.d.ts +3 -2
  57. package/lib/runtime/paginate.d.ts.map +1 -1
  58. package/lib/runtime/paginate.js +8 -2
  59. package/lib/runtime/paginate.js.map +1 -1
  60. package/lib/runtime/types.d.ts +3 -1
  61. package/lib/runtime/types.d.ts.map +1 -1
  62. package/lib/types.d.ts +4 -0
  63. package/lib/types.d.ts.map +1 -1
  64. package/package.json +2 -2
package/README.md CHANGED
@@ -1,390 +1,59 @@
1
1
  # @redocly/client-generator
2
2
 
3
- > ⚠️ **Experimental.** This package and the client generator are released as **experimental**: the generated output, options, and the plugin API may change in any minor release until it is declared stable (see [ADR-0013](./docs/adr/0013-experimental-status.md)).
3
+ Generate a typed TypeScript client from an OpenAPI description.
4
+ See https://github.com/Redocly/redocly-cli for the full project.
5
+
6
+ > [!WARNING]
7
+ > This package is experimental: the generated output, options, and the plugin API may change in any minor release until it is declared stable (see [ADR-0013](./docs/adr/0013-experimental-status.md)).
4
8
  > Pin your version if you depend on the output, and expect to regenerate when you upgrade.
5
9
  > Feedback is very welcome while we stabilize it.
6
10
 
7
- Generate a typed TypeScript client (inline types + `fetch` runtime) from an OpenAPI description.
8
- The emitted client has **zero runtime dependencies** by default — it uses only web-standard APIs (`fetch`, `AbortController`, `URLSearchParams`, …), so it runs in browsers, Node ≥ 18, Bun, Deno, and edge runtimes.
9
- Opt in to `runtime: 'package'` to import the runtime from this package instead, so engine fixes arrive via `npm update` without regenerating.
10
- Code is produced through the TypeScript compiler AST (not string templates), so output is correct by construction; `typescript` is the only (peer) dependency.
11
-
12
- This package is the engine behind the **`redocly generate-client`** command.
13
- To run it from the command line, install [`@redocly/cli`](https://github.com/Redocly/redocly-cli) and see the [`generate-client` command reference](https://redocly.com/docs/cli/commands/generate-client).
14
- The rest of this README covers using the package **programmatically**.
15
-
16
- ## Features
17
-
18
- - **Broad input** — OpenAPI **3.0, 3.1, and 3.2.0**, plus **Swagger 2.0** (normalized to 3.x before generation)
19
- `api` is a file path or URL.
20
- - **Zero-dependency client** (the default `inline` runtime) built via the TS AST, with `typescript` as the only peer dep
21
- - **Output modes** — `single` or `split` (schema types in a sibling `<stem>.schemas.ts`) (`outputMode`)
22
- - **Runtime distribution** (`runtime`) — `inline` (default, self-contained) or `package`: the client imports its runtime from `@redocly/client-generator` (pure-data operation descriptors + a build-time version-skew guard), so engine fixes arrive via `npm update` — no regeneration
23
- - **Both call styles, always** — a typed `client` instance (grouped-args methods, **per-instance configuration and credentials** via `createClient(OPERATIONS, { auth, serverUrl, … })`) plus free-function operations bound to it
24
- - **Argument styles** — `flat` positional args or a `grouped` `vars` object (`argsStyle`)
25
- - **Rich types** — inline types for every schema:
26
- - string enums as unions or runtime `const` objects (`enumStyle`)
27
- - **discriminated-union `is<Member>()` type guards** `<Op>Result` / `Error` / `Params` / `Body` / `Headers` / `Variables` aliases (collision-suppressed)
28
- - validation keywords surfaced as JSDoc
29
- - `dateType: 'Date'`
30
- - **typed `multipart/form-data` bodies** (object fields, binary → `Blob`) auto-serialized to `FormData`
31
- - **Runtime** — a typed `ClientConfig` (`serverUrl`, headers, `fetch` swap, hooks) applied via `configure()`:
32
- - **composable middleware** (`onRequest`/`onResponse`/`onError`) whose `ctx.operation.id`/`path`/`tags` are the spec's **literal unions** — a misspelled operation id fails compilation
33
- - **opt-in, abort-aware retries** with backoff, jitter, `Retry-After`, and a custom `retryOn`
34
- - per-call response decoding (`parseAs`)
35
- - OpenAPI **query-serialization styles**
36
- - `errorMode: 'result'` for a discriminated `{ data, error, response }`
37
- - a minification-safe `OPERATIONS` metadata map
38
- - **Auth** — Basic / Bearer / apiKey (header, query, cookie) setters from `securitySchemes`, async token providers, and per-instance credentials via `ClientConfig.auth`
39
- - **Server-Sent Events** — `text/event-stream` operations as typed async iterators with auto-reconnect; payloads typed from OpenAPI 3.2 `itemSchema`
40
- - **Auto-pagination** — declared via the `pagination` option (a statically verified convention rule + per-operation overrides) or the spec's `x-pagination` extension (`cursor`/`offset`/`page` styles); paginated operations keep their one-shot call and gain `.pages()`/`.items()` async iterators, item types resolved from the response schema at generate time
41
- - **Generators** (`generators`) — `sdk` (default), `zod`, `tanstack-query` (React/Vue/Svelte/Solid), `swr`, `transformers`, `mock` (MSW handlers + baked or `faker` data), and a [custom-generator plugin API](#custom-generators)
42
- - **Hardened** — document-derived names coerced to safe unique identifiers, comment text escaped, and a bounded SSE reader
43
-
44
- No add-on generator adds a dependency to the emitted client; its peer library is needed only in your app.
45
-
46
- ## Use programmatically
47
-
48
- `generateClient(options)` is the API behind the CLI command — it loads the spec, builds the client, and writes the files:
11
+ The generated client uses only web-standard APIs (`fetch`, `AbortController`, `URLSearchParams`), so by default it is a single self-contained file with zero runtime dependencies that runs in browsers, Node ≥ 18, Bun, Deno, and edge runtimes.
12
+ (Running the generator itself requires the Node version in this package's `engines` field.)
13
+ Code is produced through the TypeScript compiler AST, not string templates; `typescript` is the only peer dependency.
49
14
 
50
- ```ts
51
- import { generateClient } from '@redocly/client-generator';
15
+ This package is the engine behind the [`generate-client` command](https://redocly.com/docs/cli/commands/generate-client) — install [`@redocly/cli`](https://www.npmjs.com/package/@redocly/cli) to run it from the command line or `redocly.yaml`.
16
+ How to use the generated client — auth, middleware, retries, pagination, Server-Sent Events, and the add-on generators (`zod`, `tanstack-query`, `swr`, `mock`, `transformers`) — is documented in [Use the generated client](https://redocly.com/docs/cli/guides/use-generated-client).
17
+ This README covers using the package programmatically.
52
18
 
53
- const result = await generateClient({
54
- api: 'openapi.yaml', // file path or URL
55
- output: 'src/client.ts', // entry file; the .schemas.ts sibling derives from it in split mode
56
- outputMode: 'single', // 'single' | 'split'
57
- runtime: 'inline', // 'inline' | 'package' (import the runtime from this package)
58
- argsStyle: 'flat', // 'flat' | 'grouped'
59
- errorMode: 'throw', // 'throw' | 'result'
60
- dateType: 'string', // 'string' | 'Date' (pair 'Date' with the 'transformers' generator)
61
- enumStyle: 'const-object', // 'const-object' | 'union'
62
- generators: ['sdk'], // see "Generators" below
63
- // serverUrl, queryFramework, mockData, mockSeed, pagination, customGenerators are also accepted
64
- });
19
+ ## Basic usage
65
20
 
66
- console.log(`Wrote ${result.files.length} file(s), ${result.bytes} bytes.`);
67
- ```
68
-
69
- For type-safe authoring of a standalone options object, annotate it with `satisfies Config`:
21
+ ### Generate a client
70
22
 
71
23
  ```ts
72
- import { generateClient, type Config } from '@redocly/client-generator';
24
+ import { generateClient } from '@redocly/client-generator';
73
25
 
74
- const options = {
75
- api: './openapi.yaml',
26
+ const result = await generateClient({
27
+ api: './openapi.yaml', // file path or URL; OpenAPI 3.0/3.1/3.2 or Swagger 2.0
76
28
  output: './src/api/client.ts',
77
29
  generators: ['sdk', 'zod'],
78
- } satisfies Config;
79
-
80
- await generateClient(options);
81
- ```
82
-
83
- To inspect the output without writing to disk, the lower-level `collectGeneratedFiles(model, opts)` returns the files in memory (see `src/index.ts`).
84
-
85
- ## Using the generated client
86
-
87
- ```ts
88
- import { configure, use, getOrderById, listMenuItems, setBearer } from './client.ts';
89
-
90
- // Optional: global config (base URL, headers, fetch swap, hooks, retry).
91
- configure({ retry: { retries: 3 } });
92
-
93
- // Composable middleware for cross-cutting concerns (logging, tracing, auth refresh).
94
- use({
95
- onRequest: (ctx) => {
96
- ctx.headers['X-Request-Id'] = crypto.randomUUID();
97
- },
98
30
  });
99
31
 
100
- // Auth helpers are generated from the spec's `securitySchemes`.
101
- setBearer(token);
102
-
103
- const menu = await listMenuItems({ limit: 10 });
104
- const order = await getOrderById('ord_01khr487f7qm7p44xn427m43vb');
105
-
106
- // Per-call options (AbortSignal, retry override) go in the trailing `init` arg.
107
- const live = await listMenuItems({ limit: 10 }, { signal: controller.signal });
108
- ```
109
-
110
- Each operation's trailing `init` argument is `RequestOptions` (`RequestInit` plus an optional per-call `retry` override and a `parseAs` reader).
111
- Retry is opt-in and abort-aware.
112
- A custom `retryOn(ctx)` predicate can branch on `ctx.error` (transport failure) or `ctx.response` (HTTP status / body) and opt a `POST` in.
113
-
114
- By default a response body is decoded by negotiating from its `Content-Type`.
115
- Pass `parseAs` in `init` to force a reader — `'json'`, `'text'`, `'blob'`, `'arrayBuffer'`, `'formData'`, `'stream'` (the raw `ReadableStream`), or `'auto'` (the default):
116
-
117
- ```ts
118
- const stream = await getMenuItemPhoto('prd_…', { parseAs: 'stream' }); // ReadableStream
119
- ```
120
-
121
- `parseAs` is a **runtime override only** — it does not change the operation's static return type.
122
- Forcing a reader that disagrees with the schema is the caller's responsibility.
123
-
124
- Query parameters honor their OpenAPI serialization style.
125
- The default (`form` + `explode: true`) repeats array values.
126
- Declare `style` / `explode` / `allowReserved` on a parameter to get `form`+`explode:false` (`key=a,b`), `spaceDelimited` (`key=a%20b`), `pipeDelimited` (`key=a|b`), `deepObject`, or reserved-char passthrough.
127
-
128
- A setter is generated for each injectable scheme — `setBearer` (HTTP `bearer` / OAuth2), `setBasicAuth(username, password)` (HTTP `basic`), and `setApiKey…` for `apiKey` schemes in header, query, or cookie — and each operation sends the credentials its `security` requires.
129
- Each setter is instance-bound sugar over the exported client (`export const setBearer = client.auth.bearer;`); credentials are **per instance** (`ClientConfig.auth`), so independent instances built with `createClient(OPERATIONS, { auth })` carry independent credentials — the generated module exports `createClient` in both runtimes (see the [`multi-instance` example](./examples/multi-instance)).
130
- Bearer/apiKey credentials accept a `TokenProvider` (a string or a possibly-async function resolved per request) for refresh-token flows:
131
-
132
- ```ts
133
- import { setBearer, setBasicAuth, setApiKey } from './client.ts';
134
-
135
- setBearer(async () => await getFreshAccessToken()); // resolved before each authed call
136
- setBasicAuth('alice', 's3cr3t'); // `Authorization: Basic <base64>`
137
- setApiKey('my-api-key'); // header / query / cookie, per the scheme's `in`
138
- ```
139
-
140
- With `argsStyle: 'grouped'`, inputs are bundled into a single `vars` object (typed as the operation's `<Op>Variables`) instead of positional arguments, while `init` stays the trailing argument:
141
-
142
- ```ts
143
- const order = await getOrderById({ orderId: 'ord_01khr487f7qm7p44xn427m43vb' });
32
+ console.log(`Wrote ${result.files.length} file(s), ${result.bytes} bytes.`);
144
33
  ```
145
34
 
146
- With `errorMode: 'result'`, operations don't throw on non-2xx.
147
- Each operation returns a discriminated `{ data, error, response }` whose `error` is typed from the spec's 4xx/5xx bodies (the `<Op>Error` union).
148
- On success `error` is `undefined`; the HTTP status is always on `response.status`:
35
+ Every `redocly.yaml` `client` option is accepted with the same name and default — see the [`client` configuration reference](https://redocly.com/docs/cli/configuration/reference/client).
36
+ For type-safe authoring of a standalone options object, annotate it with `satisfies Config`.
149
37
 
150
- ```ts
151
- const { data, error, response } = await getOrderById('ord_01khr487f7qm7p44xn427m43vb');
152
- if (error)
153
- console.error(response.status, error); // `error` is the typed body
154
- else console.log(data.id); // `data` is the success body
155
- ```
38
+ ### Build extra client instances
156
39
 
157
- Transport/abort failures still throw in both modes.
158
-
159
- The client also exports an `OPERATIONS` map (operationId → `{ method, path, tags }`) plus the `OperationId` / `OperationPath` / `OperationTag` / `OperationMetadata` types.
160
- The string-literal keys, path templates, and tags survive minification, so they're the stable handle for cache/query keys, tracing span names, and request logging:
40
+ The generated module exports its operation descriptors, so an app can build additional instances with independent configuration and credentials over the same generated code:
161
41
 
162
42
  ```ts
163
- import { OPERATIONS, getOrderById } from './client.ts';
164
-
165
- const queryKey = [OPERATIONS.getOrderById.path, orderId]; // "/orders/{orderId}"
166
- ```
167
-
168
- ### Customizing requests and responses
43
+ import { createClient } from '@redocly/client-generator';
44
+ import { OPERATIONS, type Ops } from './client.ts';
169
45
 
170
- You shape requests and responses from your own code — never by editing the generated client, so changes survive regeneration.
171
- Middleware (`use(...)` — sugar for `client.use(...)`, so it registers on that instance) hooks the request lifecycle, and each `RequestContext` carries the operation's identity so you can target by operationId or tag instead of brittle URL matching.
172
- `onRequest` may mutate `ctx.url` / `ctx.method` / `ctx.headers` **and `ctx.body`**; `onResponse` may observe or replace the `Response`:
173
-
174
- ```ts
175
- import { configure, use, listMenuItems } from './client.ts';
176
-
177
- // A custom transport — proxy, instrument, or (here) swap fetch entirely.
178
- configure({ fetch: myFetch });
179
-
180
- use({
181
- onRequest: (ctx) => {
182
- // Target specific operations by identity, not URL shape.
183
- if (ctx.operation.id === 'createOrder' || ctx.operation.tags.includes('Orders')) {
184
- ctx.headers['X-Idempotency-Key'] = crypto.randomUUID();
185
- (ctx.body as { source?: string }).source = 'web'; // body edits are sent
186
- }
187
- },
188
- onResponse: (response, ctx) => {
189
- console.debug(ctx.operation.id, response.status);
190
- },
46
+ const internal = createClient<Ops>(OPERATIONS, {
47
+ serverUrl: 'https://api.example.com',
48
+ auth: { basic: { username: 'svc', password: 's3cr3t' } },
191
49
  });
192
-
193
- // A header for one call only goes in the trailing RequestOptions.
194
- await listMenuItems({}, { headers: { 'X-Request-Id': '42' } });
195
- ```
196
-
197
- `ctx.operation` is `{ id, path, tags }` — the operationId, the path template (`{param}` placeholders intact), and the operation's tags. All three are **typed literal unions** (`OperationId` / `OperationPath` / `OperationTag`, exported alongside the `OPERATIONS` map), so `ctx.operation.id === '…'` and `ctx.operation.tags.includes('…')` autocomplete and reject typos at compile time.
198
- See the [`configure-and-middleware` example](../../tests/e2e/generate-client/examples/configure-and-middleware) for a runnable end-to-end version, and [ADR-0014](./docs/adr/0014-request-response-customization.md) for the rationale.
199
-
200
- ### Baking defaults into a published SDK
201
-
202
- The customization above is composed by the **consumer**. If you instead **publish an SDK** and want those defaults already active for _your_ users, bake them in at generation time with `--setup <file>`. The setup module imports its contract from `@redocly/client-generator` (so it resolves and is unit-testable before the client is generated) and returns a `defineClientSetup({ config, middleware })`:
203
-
204
- ```ts
205
- // client-setup.ts
206
- import { defineClientSetup, type RequestContext } from '@redocly/client-generator';
207
-
208
- export default defineClientSetup({
209
- config: { serverUrl: 'https://api.acme.com', retry: { retries: 3 } },
210
- middleware: [
211
- {
212
- onRequest: (ctx: RequestContext) => {
213
- ctx.headers['X-Acme-SDK'] = '1.4.0';
214
- if (ctx.operation.tags.includes('Orders'))
215
- ctx.headers['X-Idempotency-Key'] = crypto.randomUUID();
216
- },
217
- },
218
- ],
219
- });
220
- ```
221
-
222
- ```sh
223
- redocly generate-client openapi.yaml --output src/api/client.ts --setup ./client-setup.ts
224
- ```
225
-
226
- The generator bakes the `config`/`middleware` into the generated client, so the published package applies them on import — your users call operations with no setup of their own, and can still override (their `configure`/`use` run after the baked ones; config layers spec defaults → baked setup → app `configure()`, middleware composes). Works across both output modes and both runtimes. A setup file may import **only** from `@redocly/client-generator`, so it never adds a dependency to the client.
227
-
228
- See the [`baked-setup` example](./examples/baked-setup) and [ADR-0015](./docs/adr/0015-publisher-setup-bake-in.md).
229
-
230
- ## Testing the generated client
231
-
232
- The client is plain `fetch` code, so you test it like any HTTP code — there is no special harness.
233
-
234
- **In Node** (scripts, Vitest/Jest) there is **no CORS** — point it at any reachable API and call it:
235
-
236
- ```ts
237
- import { configure, listMenuItems } from './client.ts';
238
-
239
- configure({ serverUrl: 'https://api.example.com' });
240
- const items = await listMenuItems(); // resolves or throws ApiError
241
- ```
242
-
243
- **In the browser, CORS applies** — and it's the **target API's** policy, not the client's. Two things
244
- to watch:
245
-
246
- - The API must allow your origin (`Access-Control-Allow-Origin`).
247
- - Any **custom request header** you add (e.g. `X-Request-Id` via `use({ onRequest })` middleware)
248
- triggers a **CORS preflight**, so the API must also list that header in `Access-Control-Allow-Headers`
249
- — otherwise the browser blocks the request and `fetch` throws `TypeError: Failed to fetch`. If the
250
- API doesn't allow it, drop the header, use a dev proxy (e.g. Vite's `server.proxy`), or mock the API.
251
-
252
- **Without a backend (recommended for tests and local dev), use MSW mocks.** Add `mock` to
253
- `generators` to emit an MSW handler module; requests are intercepted in-process, so there's **no real
254
- network and no CORS** — the same client runs unchanged against the mocks:
255
-
256
- ```ts
257
- import { setupServer } from 'msw/node'; // or 'msw/browser' for the browser
258
- import { handlers } from './client.mocks.ts';
259
-
260
- const server = setupServer(...handlers);
261
- beforeAll(() => server.listen());
262
- afterAll(() => server.close());
263
-
264
- // now calls to the generated client resolve from the mocks
265
- ```
266
-
267
- The repo's [`examples/`](./examples) are runnable end-to-end: `npm install && npm run dev` in an
268
- example directory serves it on `http://localhost:5173` against the live demo API (a CORS-enabled
269
- `GET`), and `examples/mock` shows the MSW-backed flow.
270
-
271
- ## Generators
272
-
273
- `generators` (default `['sdk']`) selects which files to emit.
274
- The `sdk` client is dependency-free with the default `inline` runtime; each add-on lands in its own sibling file and needs its peer library only in your app.
275
- Every generator works with both runtimes and both output modes.
276
-
277
- ### Runtime validation with Zod
278
-
279
- `generators: ['sdk', 'zod']` emits a standalone `<output>.zod.ts` module of [Zod](https://zod.dev) schemas (one `export const <Name>Schema` per schema).
280
- The generated client never imports Zod; only the `*.zod.ts` module does.
281
- Install Zod in your app as a peer — any `zod` `^3.23 || ^4`:
282
-
283
- ```ts
284
- import { z } from 'zod';
285
- import type { Pet } from './client.ts';
286
- import { PetSchema } from './client.zod.ts';
287
-
288
- const pet: Pet = PetSchema.parse(await res.json()); // z.infer<typeof PetSchema> === Pet
289
- ```
290
-
291
- Schemas carry the validation refinements stable across Zod 3.23 and 4 (`.min`/`.max`, `.gt`/`.lt`, `.int`, `.regex`).
292
- Refs become `z.lazy(() => …)` so recursive schemas work.
293
- Format helpers (`.email`/`.uuid`/`.url`) are not emitted, since they diverge between Zod 3 and 4.
294
-
295
- The module also emits `operationSchemas` (request/response validators keyed by operationId) and the `zodValidation` middleware:
296
-
297
- ```ts
298
- import { use } from './client.ts';
299
- import { zodValidation } from './client.zod.ts';
300
-
301
- use(zodValidation()); // request bodies validated before the wire; JSON responses after it
302
- ```
303
-
304
- A mismatch throws `ZodValidationError` (`operationId`, `direction`, zod `issues`); payloads are never mutated.
305
-
306
- ### TanStack Query
307
-
308
- `generators: ['sdk', 'tanstack-query']` emits a standalone `<output>.tanstack.ts` module of [TanStack Query](https://tanstack.com/query) v5 factories wrapping the sdk operations.
309
- Each query op (`GET`/`HEAD`) gets a `<op>QueryKey`/`<op>Options` factory (returning `queryOptions`).
310
- Each mutation gets a `<op>Mutation` factory (returning `mutationKey`/`mutationFn`):
311
-
312
- ```ts
313
- import { useQuery, useMutation } from '@tanstack/react-query';
314
- import { getPetOptions, createPetMutation } from './client.tanstack.ts';
315
-
316
- const { data } = useQuery(getPetOptions({ id }));
317
- const { mutate } = useMutation(createPetMutation());
318
- ```
319
-
320
- Only the `*.tanstack.ts` module imports TanStack Query; install it as a peer — any `@tanstack/react-query` `^5`.
321
- The factories wrap the **throw-mode** sdk (the default), since TanStack's `queryFn` is expected to throw on error.
322
-
323
- **Framework** — TanStack's `queryOptions`/`mutationOptions` API is identical across adapters, so the emitted module is byte-identical across frameworks; only the import specifier differs.
324
- Set `queryFramework` to `react` (default), `vue`, `svelte`, or `solid` and install the matching adapter (`@tanstack/<framework>-query`, any `^5`).
325
-
326
- ### SWR
327
-
328
- `generators: ['sdk', 'swr']` emits a standalone `<output>.swr.ts` module of [SWR](https://swr.vercel.app) hooks.
329
- Each query op (`GET`/`HEAD`) gets a `<op>Key` tuple factory + a `use<Op>(vars, init?)` hook over `useSWR`; each mutation gets a `use<Op>()` hook over `useSWRMutation`:
330
-
331
- ```ts
332
- import { useGetPetById, useCreatePet } from './client.swr.ts';
333
-
334
- const { data } = useGetPetById({ id });
335
- const { trigger } = useCreatePet();
336
- await trigger({ body: { name: 'Rex' } });
337
- ```
338
-
339
- Only the `*.swr.ts` module imports SWR (`swr` for queries, `swr/mutation` for mutations); install it as a peer — any `swr` `^2`.
340
- The hooks wrap the **throw-mode** sdk functions.
341
-
342
- ### Date transformers
343
-
344
- By default `date-time`/`date` fields are typed `string`.
345
- Set `dateType: 'Date'` to type them as `Date`, and add `'transformers'` to `generators` to emit a `<output>.transformers.ts` module of `transform<Name>` functions that convert wire ISO strings to `Date` at runtime:
346
-
347
- ```ts
348
- import { getPet } from './client.ts';
349
- import { transformPet } from './client.transformers.ts';
350
-
351
- const pet = transformPet(await getPet(id)); // pet.createdAt is now a Date
352
50
  ```
353
51
 
354
- The transformers import only the schema **types**, so they add no dependency to the client (`Date` is a web standard).
355
- `int64` → `bigint` is deferred; without `dateType: 'Date'` the date fields stay `string`.
52
+ With `runtime: 'package'` the generated client also imports its whole engine from this package (instead of embedding it), so engine fixes arrive via `npm update` — install this package as a regular dependency of the consuming app.
356
53
 
357
- ### MSW mocks
54
+ ### Write a custom generator
358
55
 
359
- `generators: ['sdk', 'mock']` emits a standalone `<output>.mocks.ts` module of [MSW](https://mswjs.io) v2 request handlers and `create<Schema>(overrides?)` data factories.
360
- Each handler intercepts its operation's method + path and responds with a fixture baked from the spec (prefers `example`/`default`; `format: binary` → `new Blob([])`.
361
- Recursive schemas terminate at the cycle with an empty array/record).
362
- Each `create<Schema>` factory builds the same default object and merges `overrides`, so factories double as test builders.
363
- Install MSW as a dev dependency — `msw` `^2`:
364
-
365
- ```ts
366
- // test setup (Node)
367
- import { setupServer } from 'msw/node';
368
- import { handlers } from './client.mocks';
369
-
370
- const server = setupServer(...handlers);
371
- beforeAll(() => server.listen());
372
- afterEach(() => server.resetHandlers());
373
- afterAll(() => server.close());
374
-
375
- // override a single factory for one case
376
- import { createMenuItem } from './client.mocks';
377
- const special = createMenuItem({ name: 'Cold Brew', price: 499 });
378
- ```
379
-
380
- **Realistic data with faker** — mock data is **baked** by default (deterministic literals, no extra dependency).
381
- Set `mockData: 'faker'` to emit [`@faker-js/faker`](https://fakerjs.dev) calls for realistic data, and `mockSeed: <n>` to pin faker's PRNG so the data is reproducible.
382
- Factory signatures are identical in both modes; faker mode makes `@faker-js/faker` (`^9`) a dev dependency of your app (the client itself gains no dependency).
383
-
384
- ## Custom generators
385
-
386
- Need an output the built-ins don't ship — validators in another library, a UI permissions map, mocks in your test runner's format, an SDK in your house style?
387
- Write a **custom generator**: it reads the same OpenAPI-derived model the built-ins do, runs in the same pass, and its output never drifts from the spec.
56
+ A custom generator reads the same spec-derived model the built-ins consume, runs in the same pass, and returns files:
388
57
 
389
58
  ```ts
390
59
  // route-map-generator.ts
@@ -408,89 +77,92 @@ export default defineGenerator({
408
77
  });
409
78
  ```
410
79
 
411
- Select it in `generators` by import specifier (a path or package), or register it inline and select it by `name`:
80
+ Select it in `generators` by import specifier (a path or a package name), or register it inline via `customGenerators` and select it by `name`.
81
+ A custom generator never adds dependencies to the generated client.
412
82
 
413
- ```ts
414
- import { generateClient } from '@redocly/client-generator';
415
- import routeMap from './tools/route-map-generator.ts';
83
+ ### Bake defaults into a published SDK
416
84
 
417
- await generateClient({
418
- api: './openapi.yaml',
419
- output: './src/api/client.ts',
420
- customGenerators: [routeMap], // register…
421
- generators: ['sdk', 'route-map'], // …then select by name (or pass './tools/route-map-generator.ts')
422
- });
423
- ```
85
+ The `setup` option takes a module that default-exports `defineClientSetup({ config, middleware })`; its defaults are baked into the generated client so a published SDK ships them built in, and consumers can still override.
86
+ See [Publisher defaults](https://redocly.com/docs/cli/guides/use-generated-client#publisher-defaults).
424
87
 
425
- `@redocly/client-generator` also exports the IR types and the codegen toolkit the built-ins use (`ts`, `printStatements`, `operationSignature`, `schemaToTypeNode`, `pascalCase`, …).
426
- A generator declares the same `requires`/`errorModes`/`dateTypes`/`runtimes` contract, validated up front.
427
- A custom generator never adds dependencies to the generated client.
428
- See `examples/custom-generator` for a runnable example.
88
+ ## API
429
89
 
430
- ## Server-Sent Events (streaming)
90
+ ### `generateClient`
431
91
 
432
- An operation whose `2xx` response declares `text/event-stream` is generated — with no option — as a typed **async-generator** client method plus the matching free function.
433
- Each event's `data` is typed from the OpenAPI 3.2 `itemSchema` (falling back to the media `schema`, then `string`):
92
+ Loads the spec, builds the client, and writes the files.
434
93
 
435
94
  ```ts
436
- import { streamMessages } from './client.ts';
95
+ async function generateClient(options: GenerateClientOptions): Promise<GenerateClientResult>;
437
96
 
438
- for await (const ev of streamMessages()) {
439
- console.log(ev.id, ev.data.text); // ev: ServerSentEvent<Message>
440
- }
97
+ type GenerateClientResult = {
98
+ outputPath: string; // the `output` anchor path (the entry file in multi-file modes)
99
+ bytes: number; // total bytes written
100
+ files: Array<{ path: string; bytes: number }>; // every file written to disk
101
+ };
441
102
  ```
442
103
 
443
- The stream **auto-reconnects** on a dropped connection, resuming via the `Last-Event-ID` header (backoff: server `retry:` → `reconnectDelay` → 1s, exponential with jitter, capped at 30s).
444
- Tune or opt out per call (`{ reconnect: false }` / `{ reconnectDelay: 500 }`), and `break` or pass an `AbortSignal` to stop early — both end the iterator cleanly (no throw).
445
- SSE operations always throw `ApiError` on an initial non-2xx and never return the `result`-mode shape.
104
+ `GenerateClientOptions` is the [`Config` type](./src/config.ts) (`api` and `output` required; `outputMode`, `runtime`, `argsStyle`, `errorMode`, `dateType`, `enumStyle`, `serverUrl`, `queryFramework`, `mockData`, `mockSeed`, `generators`, `customGenerators`, `setup`, `pagination` optional) plus an optional resolved Redocly `config` used for spec loading.
446
105
 
447
- ## Auto-pagination
106
+ ### `collectGeneratedFiles`
448
107
 
449
- Pagination is **declared, never guessed** — in the `pagination` option (or `redocly.yaml` `client.pagination`; the spec's `x-pagination` operation extension takes the same fields).
450
- A convention rule applies to every operation it **structurally fits** (the advance param is a declared query parameter of the right type; the JSON pointers resolve in the JSON success-response schema, `items` landing on an array); an explicit rule (an `operations` override or `x-pagination`) that doesn't fit fails generation.
451
- Precedence per operation: `operations[id]` > `x-pagination` > convention; `exclude` wins over all.
108
+ Runs the configured generators against a built model and returns the files in memory, without writing to disk:
452
109
 
453
110
  ```ts
454
- await generateClient({
455
- api: './openapi.yaml',
456
- output: './src/api/client.ts',
457
- pagination: {
458
- style: 'cursor', // 'cursor' | 'offset' | 'page'
459
- cursorParam: 'cursor', // the query param that receives the cursor
460
- nextCursor: '/nextCursor', // JSON pointer to the next cursor in the response
461
- items: '/orders', // JSON pointer to the page's item array
462
- },
463
- });
111
+ function collectGeneratedFiles(
112
+ model: ApiModel,
113
+ options: {
114
+ outputPath: string;
115
+ outputMode: OutputMode;
116
+ emit: EmitOptions;
117
+ generators: string[];
118
+ registry?: Map<string, GeneratorDescriptor>; // defaults to the built-ins
119
+ }
120
+ ): GeneratedFile[];
464
121
  ```
465
122
 
466
- Each paginated operation keeps its one-shot call and gains `.pages(args?, init?)` / `.items(args?, init?)` async iterators (the flat function too — note its iterators take the **grouped** args shape while the function itself stays positional).
467
- Item types are resolved from the response schema at generate time — no runtime reflection:
123
+ ### `defineGenerator`
124
+
125
+ Authors a custom generator (`{ name, run }` plus optional `requires`/`errorModes`/`dateTypes`/`runtimes` compatibility metadata, validated up front):
468
126
 
469
127
  ```ts
470
- import { client, listOrders } from './client.ts';
128
+ function defineGenerator(generator: CustomGenerator): CustomGenerator;
129
+ ```
471
130
 
472
- for await (const order of client.listOrders.items({ params: { limit: 20 } })) {
473
- console.log(order.id); // `order` is typed `Order`
474
- }
131
+ The package index also exports the codegen toolkit the built-ins use (`ts`, `printStatements`, `parseStatements`, `operationSignature`, `schemaToTypeNode`, `pascalCase`, …) and the IR types, so a custom generator emits TypeScript exactly as the first-party ones do.
475
132
 
476
- await listOrders({ limit: 20 }); // flat one-shot: positional
477
- for await (const page of listOrders.pages({ params: { cursor: 'c2' } })) {
478
- // resume from a saved cursor; pass `{ signal }` as `init` to abort mid-iteration
479
- }
133
+ ### `defineClientSetup`
134
+
135
+ Authors a publisher setup module for the `setup` option:
136
+
137
+ ```ts
138
+ function defineClientSetup(setup: {
139
+ config?: ClientConfig;
140
+ middleware?: Middleware[];
141
+ }): ClientSetup;
480
142
  ```
481
143
 
482
- `cursor` style stops when `nextCursor` is absent/`null`/empty (and throws if the cursor doesn't advance); `offset`/`page` styles stop on an empty page.
483
- Iteration is error-mode-agnostic: a failed page throws `ApiError` even on `errorMode: 'result'` clients, where `.pages()` yields raw pages (not `{ data, error, response }` envelopes).
484
- Inline output embeds the pagination module only when some operation paginates; `runtime: 'package'` clients receive pagination improvements via `npm update`.
144
+ A setup module may import only from `@redocly/client-generator`, so it never adds a dependency to the client.
145
+
146
+ ### `createClient`
147
+
148
+ The runtime factory that `runtime: 'package'` clients import, also usable directly to build extra instances over generated descriptors (see [Basic usage](#build-extra-client-instances)):
149
+
150
+ ```ts
151
+ function createClient<Ops>(
152
+ operations: Record<string, OperationDescriptor>,
153
+ config?: ClientConfig
154
+ ): Client<Ops>;
155
+ ```
485
156
 
486
157
  ## Examples
487
158
 
488
- Runnable examples live in [`examples/`](../../tests/e2e/generate-client/examples): `zero-install-quickstart`, `fetch-functions`, `configure-and-middleware`, `baked-setup`, `sse-streaming`, `zod`, `tanstack-query`, `mock`, `custom-generator`, `programmatic`, `vendored-edge`, `package-runtime` (`runtime: 'package'` — engine fixes via `npm update`), `multi-instance` (per-tenant `createClient` instances over one generated module), `pagination` and `custom-pagination` (the declared convention vs. a hand-written helper), and `nested-facade` (a custom generator grouping operations by tag).
159
+ Runnable examples — from a zero-install quickstart to middleware, baked setup, SSE streaming, pagination, custom generators, and the package runtime — live in [`tests/e2e/generate-client/examples`](https://github.com/Redocly/redocly-cli/tree/main/tests/e2e/generate-client/examples).
489
160
  Each is a standalone Vite app with a checked-in, drift-checked generated client.
490
161
 
491
162
  ## Documentation
492
163
 
493
- - [`generate-client` command reference](https://redocly.com/docs/cli/commands/generate-client) — CLI usage, every flag, and `redocly.yaml` configuration.
164
+ - [`generate-client` command reference](https://redocly.com/docs/cli/commands/generate-client) — CLI usage, flags, and `redocly.yaml` configuration.
165
+ - [Use the generated client](https://redocly.com/docs/cli/guides/use-generated-client) — the runtime API and the add-on generators.
494
166
  - [`ARCHITECTURE.md`](./ARCHITECTURE.md) and the [ADRs](./docs/adr/) — how the package is built and why.
495
167
 
496
168
  ## Development
@@ -503,4 +175,4 @@ npm run unit # unit tests (this package is held at 100% cover
503
175
  VITEST_SUITE=e2e npx vitest run tests/e2e/generate-client/ # behavioral e2e
504
176
  ```
505
177
 
506
- The client runtime lives in `src/runtime/` (real, unit-testable modules; package mode imports them, inline mode embeds them via the generated `src/emitters/runtime-sources.ts` snapshot and the `src/emitters/inline-runtime.ts` assembler), the structural emitters in `src/emitters/`, the IR in `src/intermediate-representation/`, the generators in `src/generators/`, and the file-layout writers in `src/writers/`.
178
+ The client runtime lives in `src/runtime/` (real, unit-testable modules; package mode imports them, inline mode embeds them), the structural emitters in `src/emitters/`, the IR in `src/intermediate-representation/`, the generators in `src/generators/`, and the file-layout writers in `src/writers/`.
package/lib/config.d.ts CHANGED
@@ -55,6 +55,10 @@ export type Config = {
55
55
  setup?: string;
56
56
  /** Runtime distribution: 'inline' (default, self-contained) | 'package' (imports @redocly/client-generator). */
57
57
  runtime?: 'inline' | 'package';
58
+ /** Extension in generated relative imports. `'js'` (default) suits tsc and bundlers;
59
+ * `'ts'` suits runtimes that resolve specifiers literally, like Node's built-in
60
+ * type stripping (`node client.ts`). */
61
+ importExt?: 'js' | 'ts';
58
62
  /** Auto-pagination rules (convention + per-operation overrides + `exclude`), resolved
59
63
  * together with each operation's `x-pagination` extension. Explicit rules that don't
60
64
  * fit their operation fail generation. */
@@ -1 +1 @@
1
- {"version":3,"file":"config.d.ts","sourceRoot":"","sources":["../src/config.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,4BAA4B,CAAC;AAE5D,OAAO,KAAK,EAAE,gBAAgB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
  hasSchemas
129
- ? schemaLinks(body + '\n' + sugar, ctx.schemaNames, `./${splitStem}.schemas.js`)
129
+ ? schemaLinks(body + '\n' + sugar, ctx.schemaNames, `./${splitStem}.schemas.${options.importExt ?? 'js'}`)
130
130
  : '',
131
131
  ...(embed ? [] : [runtimeSection]),
132
132
  body,
@@ -271,6 +271,9 @@ function flatSugarStatement(op, ident, ctx) {
271
271
  if (op.headerParams.length > 0) {
272
272
  props.push(factory.createShorthandPropertyAssignment('headers'));
273
273
  }
274
+ if (op.cookieParams.length > 0) {
275
+ props.push(factory.createShorthandPropertyAssignment('cookies'));
276
+ }
274
277
  const call = factory.createCallExpression(factory.createPropertyAccessExpression(factory.createIdentifier('client'), ident), undefined, [factory.createObjectLiteralExpression(props, false), factory.createIdentifier('init')]);
275
278
  const fn = arrow(params, call);
276
279
  if (!ctx.pagination?.has(op.name))