@7nohe/openapi-react-query-codegen 4.0.0-beta.6 → 4.0.0-beta.8
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/AGENTS.md +35 -7
- package/README.md +60 -13
- package/dist/agentsMd.mjs +3 -1
- package/dist/check.mjs +15 -5
- package/dist/cli.d.mts +2 -0
- package/dist/client/emitMocks.d.mts +23 -0
- package/dist/client/emitMocks.mjs +353 -0
- package/dist/client/emitSdk.mjs +8 -3
- package/dist/client/emitTypes.d.mts +6 -1
- package/dist/client/emitTypes.mjs +16 -7
- package/dist/client/index.d.mts +12 -0
- package/dist/client/index.mjs +12 -0
- package/dist/client/mockValue.d.mts +34 -0
- package/dist/client/mockValue.mjs +796 -0
- package/dist/client/msw.d.mts +11 -0
- package/dist/client/msw.mjs +27 -0
- package/dist/client/operations.d.mts +4 -0
- package/dist/client/operations.mjs +25 -0
- package/dist/client/schema.d.mts +25 -3
- package/dist/client/schema.mjs +36 -24
- package/dist/common.d.mts +1 -0
- package/dist/common.mjs +4 -1
- package/dist/constants.d.mts +1 -0
- package/dist/constants.mjs +1 -0
- package/dist/format.d.mts +3 -1
- package/dist/format.mjs +3 -3
- package/dist/generate.mjs +19 -2
- package/dist/hooks/buildCommon.d.mts +6 -1
- package/dist/hooks/buildCommon.mjs +7 -2
- package/dist/runCli.mjs +2 -1
- package/package.json +22 -13
package/AGENTS.md
CHANGED
|
@@ -7,12 +7,13 @@ This file has two parts:
|
|
|
7
7
|
|
|
8
8
|
## Using this package
|
|
9
9
|
|
|
10
|
-
`@7nohe/openapi-react-query-codegen` is a CLI (`openapi-rq`) that reads an OpenAPI spec and generates:
|
|
10
|
+
`@7nohe/openapi-react-query-codegen` is a CLI (`orqc`; `openapi-rq` also works) that reads an OpenAPI spec and generates:
|
|
11
11
|
|
|
12
12
|
1. a TypeScript HTTP client (types, one function per operation and a small fetch or axios runtime) in `<output>/requests/`
|
|
13
13
|
2. TanStack Query v5 hooks and helpers on top of that client in `<output>/queries/`
|
|
14
|
+
3. with `--mock msw`, mock data and MSW request handlers in `<output>/mocks/`
|
|
14
15
|
|
|
15
|
-
Docs: https://
|
|
16
|
+
Docs: https://orqc.7nohe.dev (summary for LLMs: https://orqc.7nohe.dev/llms.txt)
|
|
16
17
|
|
|
17
18
|
### Install
|
|
18
19
|
|
|
@@ -32,7 +33,7 @@ Only the scoped `@7nohe/openapi-react-query-codegen` package belongs to this pro
|
|
|
32
33
|
Add a script and run it whenever the spec changes:
|
|
33
34
|
|
|
34
35
|
```json
|
|
35
|
-
{ "scripts": { "codegen": "
|
|
36
|
+
{ "scripts": { "codegen": "orqc -i ./openapi.yaml -o ./src/openapi" } }
|
|
36
37
|
```
|
|
37
38
|
|
|
38
39
|
| Flag | Default | Notes |
|
|
@@ -40,8 +41,9 @@ Add a script and run it whenever the spec changes:
|
|
|
40
41
|
| `-i, --input <path\|url\|content>` | (required) | OpenAPI 3.0/3.1 spec |
|
|
41
42
|
| `-o, --output <dir>` | `openapi` | `requests/` and `queries/` are created inside |
|
|
42
43
|
| `-c, --client <name>` | `fetch` | or `axios` |
|
|
43
|
-
| `--format <biome\|prettier>` | none | formats `queries/` only |
|
|
44
|
-
| `--lint <biome\|eslint>` | none | lints/fixes `queries/` only |
|
|
44
|
+
| `--format <biome\|prettier>` | none | formats `queries/` (and `mocks/`) only |
|
|
45
|
+
| `--lint <biome\|eslint>` | none | lints/fixes `queries/` (and `mocks/`) only |
|
|
46
|
+
| `--mock <msw>` | off | also generate mock data and MSW handlers into `mocks/` (see below) |
|
|
45
47
|
| `--enums <javascript\|typescript>` | none | emit enum objects |
|
|
46
48
|
| `--useDateType` | off | `Date` for `date`/`date-time`, converted in successful responses |
|
|
47
49
|
| `--noOperationId` | off | derive names from paths instead of `operationId` |
|
|
@@ -78,6 +80,8 @@ Pass the **same flags** every time (keep them in the package.json script). Diffe
|
|
|
78
80
|
└── transformers.gen.ts # date conversion (only with --useDateType)
|
|
79
81
|
```
|
|
80
82
|
|
|
83
|
+
With `--mock msw` there is also `<output>/mocks/`: `data.ts` (mock functions), `handlers.ts` (MSW handlers) and `index.ts`.
|
|
84
|
+
|
|
81
85
|
Never edit generated files; change the spec or the flags and regenerate. Whether to commit the output is the project's choice. If it is committed, add the `--check` step below to CI.
|
|
82
86
|
|
|
83
87
|
### Using the generated code
|
|
@@ -123,6 +127,28 @@ mutate({ body: { name: "Fluffy" } });
|
|
|
123
127
|
|
|
124
128
|
Wrap the app in TanStack Query's `QueryClientProvider` as usual.
|
|
125
129
|
|
|
130
|
+
### Mocks: `--mock msw`
|
|
131
|
+
|
|
132
|
+
```bash
|
|
133
|
+
orqc -i ./openapi.yaml -o ./src/openapi --mock msw
|
|
134
|
+
```
|
|
135
|
+
|
|
136
|
+
Writes `<output>/mocks/` and needs `msw` (2 or 3) in the project. For an operation `findPets` and a schema `Pet` it exports:
|
|
137
|
+
|
|
138
|
+
- `findPetsHandler(override?)`: an MSW handler that answers with the operation's lowest successful status. `override` is a body of the response type, or an MSW resolver for anything else (an error, a body that depends on the request).
|
|
139
|
+
- `handlers`: every handler, ordered so that `/pets/mine` matches before `/pets/{id}`.
|
|
140
|
+
- `findPetsResponseMock()` and `petMock(overrides?)`: the mock data, typed by `requests/types.gen.ts`. `overrides` exists for object schemas only.
|
|
141
|
+
|
|
142
|
+
```ts
|
|
143
|
+
import { setupServer } from "msw/node"; // or setupWorker from "msw/browser"
|
|
144
|
+
import { findPetsHandler, handlers, petMock } from "./openapi/mocks";
|
|
145
|
+
|
|
146
|
+
const server = setupServer(...handlers);
|
|
147
|
+
server.use(findPetsHandler([petMock({ name: "Momo" })])); // own handlers win: MSW takes the first match
|
|
148
|
+
```
|
|
149
|
+
|
|
150
|
+
The values are fixed, not random: the specification's `example`/`default`, else the first `enum` value, else a value made up from the type. To change the data everywhere, add `example`s to the specification and regenerate; do not edit `mocks/`. The handlers keep no state and ignore the request, so pagination, errors and "created items show up in the list" need a handler of your own.
|
|
151
|
+
|
|
126
152
|
### Looking an operation up: `--agentsMd`
|
|
127
153
|
|
|
128
154
|
The generated files can be very large. Do not read them whole: an operation's function, hook and types each start a line, so `grep` finds them.
|
|
@@ -141,10 +167,10 @@ With `--agentsMd` in the codegen script, the generator keeps these instructions,
|
|
|
141
167
|
### CI drift check: `--check`
|
|
142
168
|
|
|
143
169
|
```bash
|
|
144
|
-
|
|
170
|
+
orqc -i ./openapi.yaml -o ./src/openapi --check
|
|
145
171
|
```
|
|
146
172
|
|
|
147
|
-
Generates into a temporary directory (`.openapi-rq-check-*` inside `<output>/queries`, removed afterwards), compares it with `<output>/requests` and `<output>/queries
|
|
173
|
+
Generates into a temporary directory (`.openapi-rq-check-*` inside `<output>/queries`, removed afterwards), compares it with `<output>/requests` and `<output>/queries` (and `<output>/mocks` with `--mock`), and changes no existing file. `--format`/`--lint` rules that target the output directory by path also apply to the temporary copy; a rule that names an exact file or depth from the project root (`src/openapi/queries/*.ts`) does not, so write such rules as `src/openapi/queries/**`. Each file is `added` (would be created), `modified` or `removed` (no longer generated).
|
|
148
174
|
|
|
149
175
|
The `queries/` headers include the package version, so after upgrading this package `--check` fails until you regenerate. Upgrade and regenerate in the same change.
|
|
150
176
|
|
|
@@ -219,10 +245,12 @@ import { check, generate } from "@7nohe/openapi-react-query-codegen";
|
|
|
219
245
|
- A hook builder takes the names it declares and refers to from `hookNames(op)` (`src/hooks/hookNames.mts`), never from the operation's name directly: `settleHookNames` numbers a name that its file already imports or declares, and the files are then built again with those names.
|
|
220
246
|
- A hook builder declares what its statement needs from the generated client: `service` (the SDK function) and `models` (types from `types.gen`). The fixed names (TanStack Query, `Options`, `Common`, `AxiosError`) are imported when the code mentions them, which is only safe because the client generator reserves them. When hook code starts using a new name, add it to `RESERVED_TYPE_NAMES` or `RESERVED_FUNCTION_NAMES` and to the list in `tests/hooks/imports.test.ts`.
|
|
221
247
|
- The generated hooks rely on a small SDK contract: `Options<XData, ThrowOnError>` exported from `sdk.gen`, calls returning `{ data }` and rejecting when `throwOnError` is set, and the `XData` / `XError` type names. Keep it in sync with `src/client/emitSdk.mts` and the runtimes in `src/client/runtime/`.
|
|
248
|
+
- The mocks (`src/client/emitMocks.mts`, `mockValue.mts`) are typed by `types.gen.ts`, so a value has to be one `renderSchema` gives a type for: `mockValue` reads a schema with the same helpers (`componentSchemaView`, `inheritedRequired`, `discriminatorValues`). When the type of a schema changes, change its value with it; `tests/client/mocksCompile.test.ts` compiles the mocks of every specification under `tests/inputs` and fails when the two disagree. An `example` is only used after `exampleValue` has checked it against the schema.
|
|
222
249
|
- The runtimes (`src/client/runtime/*.runtime.ts`) are plain TypeScript that is copied verbatim into users' projects. Keep them dependency-free (axios aside), strict-mode clean, and free of syntax that needs a newer TypeScript than the generated code supports. `scripts/copy-runtime.mjs` ships them next to the compiled generator.
|
|
223
250
|
|
|
224
251
|
### Dependencies
|
|
225
252
|
- Runtime dependencies are deliberately few (`cross-spawn`, `yaml`). Adding one needs a reason; the generator replaced `@hey-api/openapi-ts` to cut the supply-chain surface.
|
|
253
|
+
- `msw` is a devDependency only, for the tests that compile and run the generated handlers. The generated code imports it from the user's project, so it is neither a dependency nor a peer dependency of this package.
|
|
226
254
|
|
|
227
255
|
### Agent-Specific Notes
|
|
228
256
|
- The generators build source text as strings. After changing one, run the tests that compile the generated output (`tests/createSource.test.ts`, `tests/client/`) to confirm it is still valid TypeScript.
|
package/README.md
CHANGED
|
@@ -1,10 +1,18 @@
|
|
|
1
|
-
# OpenAPI React Query Codegen
|
|
1
|
+
# OpenAPI React Query Codegen (orqc)
|
|
2
2
|
|
|
3
|
-
>
|
|
3
|
+
> Generate [TanStack Query (React Query)](https://tanstack.com/query) code from your OpenAPI spec: `queryOptions` factories following the official TanStack Query v5 pattern, ready-to-use hooks, prefetch, ensure, suspense and infinite query helpers, and the typed fetch or axios client they call.
|
|
4
4
|
|
|
5
|
-
[](https://www.npmjs.com/package/@7nohe/openapi-react-query-codegen)
|
|
6
|
+
[](https://www.npmjs.com/package/@7nohe/openapi-react-query-codegen?activeTab=versions)
|
|
7
|
+
[](https://www.npmjs.com/package/@7nohe/openapi-react-query-codegen)
|
|
8
|
+
[](./LICENSE)
|
|
6
9
|
|
|
7
|
-
📖 **[Documentation](https://
|
|
10
|
+
📖 **[Documentation](https://orqc.7nohe.dev)** · [Getting started](https://orqc.7nohe.dev/guides/introduction/) · [Migrating to v4](https://orqc.7nohe.dev/guides/migrating-to-v4/) · [Coming from openapi-react-query](https://orqc.7nohe.dev/guides/migrating-from-openapi-react-query/) · [Benchmarks](https://orqc.7nohe.dev/guides/benchmarks/)
|
|
11
|
+
|
|
12
|
+
<picture>
|
|
13
|
+
<source media="(prefers-color-scheme: dark)" srcset="./.github/assets/how-it-works-dark.svg">
|
|
14
|
+
<img alt="The findPets operation of an OpenAPI document. Running npx @7nohe/orqc@beta -i openapi.yaml generates useFindPets, findPetsOptions, useFindPetsSuspense, prefetchUseFindPets and UseFindPetsKeyFn in openapi/queries, plus a typed client in openapi/requests. An app then calls useFindPets." src="./.github/assets/how-it-works-light.svg" width="880">
|
|
15
|
+
</picture>
|
|
8
16
|
|
|
9
17
|
## Features
|
|
10
18
|
|
|
@@ -13,18 +21,39 @@
|
|
|
13
21
|
- **SSR helpers**: `prefetchQuery`, `prefetchInfiniteQuery`, and `ensureQueryData` functions per operation — ready for Next.js App Router hydration
|
|
14
22
|
- **Hierarchical query keys** with exported key constants and functions: invalidate one exact query, all infinite pages of an operation, or every cache entry of an operation with a single prefix
|
|
15
23
|
- **Pure TypeScript client** (types, SDK functions and a small fetch or axios runtime) generated from the same OpenAPI 3.0/3.1 document — no separate client generator to install
|
|
24
|
+
- **MSW mocks from the same spec** (`--mock msw`): typed mock data and a request handler per operation, with fixed values taken from the spec's examples — build the frontend before the backend exists, and reuse the handlers in tests
|
|
16
25
|
- **CI- and agent-friendly**: `--check` fails CI when committed output is out of date, `--json` prints errors as machine-readable JSON, `--agentsMd` keeps a guide in your `AGENTS.md` that lets a coding agent look one operation up instead of reading the generated files, and the package ships an [`AGENTS.md`](./AGENTS.md) for coding agents
|
|
17
26
|
|
|
18
27
|
## Quick start
|
|
19
28
|
|
|
20
29
|
> [!NOTE]
|
|
21
|
-
> v4 is in beta, and this README describes it. Install it with `@beta`; a plain `npm install` still gets v3, whose README and docs are in the [`v3` branch](https://github.com/7nohe/openapi-react-query-codegen/tree/v3). What changes from v3 is in [Migrating to v4](https://
|
|
30
|
+
> v4 is in beta, and this README describes it. Install it with `@beta`; a plain `npm install` still gets v3, whose README and docs are in the [`v3` branch](https://github.com/7nohe/openapi-react-query-codegen/tree/v3). What changes from v3 is in [Migrating to v4](https://orqc.7nohe.dev/guides/migrating-to-v4/).
|
|
31
|
+
|
|
32
|
+
Try it without installing:
|
|
33
|
+
|
|
34
|
+
```bash
|
|
35
|
+
npx @7nohe/orqc@beta -i ./petstore.yaml
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
Then add it to your project, next to TanStack Query:
|
|
22
39
|
|
|
23
40
|
```bash
|
|
24
41
|
npm install -D @7nohe/openapi-react-query-codegen@beta
|
|
25
|
-
|
|
42
|
+
npm install @tanstack/react-query
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
Keep the command in a `package.json` script. A script only runs the version installed in the project, so the output always matches your lockfile:
|
|
46
|
+
|
|
47
|
+
```json
|
|
48
|
+
{ "scripts": { "codegen": "orqc -i ./petstore.yaml" } }
|
|
26
49
|
```
|
|
27
50
|
|
|
51
|
+
```bash
|
|
52
|
+
npm run codegen
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
This writes the client to `openapi/requests/` and the TanStack Query code to `openapi/queries/` (change the folder with `-o`):
|
|
56
|
+
|
|
28
57
|
```tsx
|
|
29
58
|
import { useQuery } from "@tanstack/react-query";
|
|
30
59
|
import { findPetsOptions } from "./openapi/queries";
|
|
@@ -35,10 +64,19 @@ function Pets() {
|
|
|
35
64
|
}
|
|
36
65
|
```
|
|
37
66
|
|
|
38
|
-
See the [documentation](https://
|
|
67
|
+
See the [documentation](https://orqc.7nohe.dev) for [CLI options](https://orqc.7nohe.dev/guides/cli-options/), [usage](https://orqc.7nohe.dev/guides/usage/) (SSR, infinite queries, mutations), [mocking with MSW](https://orqc.7nohe.dev/guides/mocking/) and [CI and coding agents](https://orqc.7nohe.dev/guides/ci-and-agents/).
|
|
68
|
+
|
|
69
|
+
## Examples
|
|
70
|
+
|
|
71
|
+
| Example | Folder |
|
|
72
|
+
|---|---|
|
|
73
|
+
| [Vite + React](https://orqc.7nohe.dev/examples/vite-react/) | [`examples/react-app`](./examples/react-app) |
|
|
74
|
+
| [Next.js (App Router)](https://orqc.7nohe.dev/examples/nextjs/) | [`examples/nextjs-app`](./examples/nextjs-app) |
|
|
75
|
+
| [TanStack Router](https://orqc.7nohe.dev/examples/tanstack-router/) | [`examples/tanstack-router-app`](./examples/tanstack-router-app) |
|
|
76
|
+
| [React Router](https://orqc.7nohe.dev/examples/react-router/) | [`examples/react-router-app`](./examples/react-router-app) |
|
|
39
77
|
|
|
40
78
|
> [!IMPORTANT]
|
|
41
|
-
>
|
|
79
|
+
> This project publishes only the scoped **`@7nohe/openapi-react-query-codegen`** and its short name **`@7nohe/orqc`**. The unscoped `openapi-react-query-codegen` and `openapi-rq` and the copies under other scopes on npm are published by third parties, not by this project.
|
|
42
80
|
|
|
43
81
|
## How it compares
|
|
44
82
|
|
|
@@ -49,16 +87,21 @@ See the [documentation](https://openapi-react-query-codegen.vercel.app) for CLI
|
|
|
49
87
|
| SSR helpers (`prefetchQuery` / `prefetchInfiniteQuery` / `ensureQueryData`) | ✅ | ❌ | Partial (`usePrefetch`) | Not provided (use `queryOptions`) |
|
|
50
88
|
| Hierarchical query keys for granular invalidation | ✅ | ✅ (tags) | Partial | Not documented |
|
|
51
89
|
| Stable release line | ✅ SemVer | pre-1.0, frequent breaking changes | ✅ | ⚠️ upstream openapi-typescript is in [maintenance mode](https://github.com/openapi-ts/openapi-typescript/issues/2874) |
|
|
52
|
-
| MSW mock generation |
|
|
90
|
+
| MSW mock generation | ✅ (`--mock msw`, fixed values) | ❌ | ✅ (random values with Faker) | ❌ |
|
|
53
91
|
| Vue / Solid / Svelte / Angular | ❌ React-focused | ✅ | ✅ | ❌ |
|
|
92
|
+
| Generation time, GitHub REST API (1,272 operations)¹ | **0.46 s** | 2.98 s | 4.31 s | 2.10 s |
|
|
93
|
+
| Packages added by installing the generator¹ | **9** (2.1 MB) | 51 (10.8 MB) | 89 (101.5 MB) | 35 (20.5 MB) |
|
|
94
|
+
| Type errors in the generated code, Stripe / GitHub APIs¹ | **0 / 0** | 274 / 0 | 219 / 1 | 0 / 0 |
|
|
95
|
+
|
|
96
|
+
¹ Measured on 2026-11-13 on an Apple M2 with Node.js 24 (generation time is the median wall-clock time of the CLI), with 4.0.0-beta.4, `@hey-api/openapi-ts` 0.99.0, Orval 8.46.0 and openapi-typescript 7.13.0 + openapi-react-query 0.5.4. Methods, the other specifications and type-checking times are on the [Benchmarks](https://orqc.7nohe.dev/guides/benchmarks/) page; the scripts are in [`benchmarks/`](./benchmarks).
|
|
54
97
|
|
|
55
|
-
**Scope**: this library is deliberately React-focused
|
|
98
|
+
**Scope**: this library is deliberately React-focused — use the hey-api plugin if you need non-React frameworks. Its [MSW mocks](https://orqc.7nohe.dev/guides/mocking/) answer with fixed values taken from the spec and keep no state; use Orval if you want randomly generated mock data.
|
|
56
99
|
|
|
57
|
-
**Coming from openapi-typescript / openapi-fetch / openapi-react-query?** See the [migration guide](https://
|
|
100
|
+
**Coming from openapi-typescript / openapi-fetch / openapi-react-query?** See the [migration guide](https://orqc.7nohe.dev/guides/migrating-from-openapi-react-query/).
|
|
58
101
|
|
|
59
102
|
## Stability policy
|
|
60
103
|
|
|
61
|
-
The client generator is part of this package, so the generated `requests/` directory and the hooks built on it change together and follow SemVer: breaking output changes only happen in major versions, with a migration guide (see [Migrating to v4](https://
|
|
104
|
+
The client generator is part of this package, so the generated `requests/` directory and the hooks built on it change together and follow SemVer: breaking output changes only happen in major versions, with a migration guide (see [Migrating to v4](https://orqc.7nohe.dev/guides/migrating-to-v4/) if you are coming from v3, where the client was produced by `@hey-api/openapi-ts`).
|
|
62
105
|
|
|
63
106
|
## Requirements
|
|
64
107
|
|
|
@@ -67,6 +110,10 @@ The client generator is part of this package, so the generated `requests/` direc
|
|
|
67
110
|
- `commander` 12–15 (peer dependency)
|
|
68
111
|
- TypeScript 5.x, 6.x or 7.x to compile the generated code. It is not a peer dependency: the generator does not use the `typescript` package.
|
|
69
112
|
|
|
113
|
+
## Contributing
|
|
114
|
+
|
|
115
|
+
Issues and pull requests are welcome. See the [contributing guide](https://orqc.7nohe.dev/contributing/) for the development setup.
|
|
116
|
+
|
|
70
117
|
## License
|
|
71
118
|
|
|
72
|
-
MIT
|
|
119
|
+
[MIT](./LICENSE)
|
package/dist/agentsMd.mjs
CHANGED
|
@@ -16,6 +16,8 @@ function displayPath(output) {
|
|
|
16
16
|
const inside = relative !== "" && !relative.startsWith("..") && !path.isAbsolute(relative);
|
|
17
17
|
return (inside ? relative : output).split(path.sep).join("/");
|
|
18
18
|
}
|
|
19
|
+
// The markers keep the CLI's old name: renaming them would orphan the blocks
|
|
20
|
+
// that earlier versions wrote into projects.
|
|
19
21
|
const begin = (output) => `<!-- openapi-rq:begin ${output} -->`;
|
|
20
22
|
const end = (output) => `<!-- openapi-rq:end ${output} -->`;
|
|
21
23
|
/**
|
|
@@ -34,7 +36,7 @@ export function renderAgentsBlock(output) {
|
|
|
34
36
|
return `${begin(out)}
|
|
35
37
|
## Generated API client (\`${out}\`)
|
|
36
38
|
|
|
37
|
-
\`${out}\` is generated by \`
|
|
39
|
+
\`${out}\` is generated by \`orqc\` (\`@7nohe/openapi-react-query-codegen\`) from the OpenAPI document. Do not edit it: change the document or the flags and generate again. Its files can be very large, so look things up as described here instead of reading them.
|
|
38
40
|
|
|
39
41
|
### Names
|
|
40
42
|
|
package/dist/check.mjs
CHANGED
|
@@ -3,11 +3,21 @@ import { mkdtemp, readdir, readFile, rm } from "node:fs/promises";
|
|
|
3
3
|
import { tmpdir } from "node:os";
|
|
4
4
|
import path from "node:path";
|
|
5
5
|
import { agentsFilePath, checkAgentsBlock } from "./agentsMd.mjs";
|
|
6
|
-
import { queriesOutputPath, requestsOutputPath } from "./constants.mjs";
|
|
6
|
+
import { mocksOutputPath, queriesOutputPath, requestsOutputPath, } from "./constants.mjs";
|
|
7
7
|
import { CodegenError, ErrorCodes, toCodegenError } from "./errors.mjs";
|
|
8
8
|
import { generate } from "./generate.mjs";
|
|
9
|
-
/**
|
|
10
|
-
|
|
9
|
+
/**
|
|
10
|
+
* The generator owns exactly these subdirectories of the output path. The
|
|
11
|
+
* mocks are only its own when it is asked for them: without `--mock` a
|
|
12
|
+
* `mocks/` directory is somebody else's and is left alone.
|
|
13
|
+
*/
|
|
14
|
+
function checkedSubdirectories(options) {
|
|
15
|
+
return [
|
|
16
|
+
requestsOutputPath,
|
|
17
|
+
queriesOutputPath,
|
|
18
|
+
...(options.mock ? [mocksOutputPath] : []),
|
|
19
|
+
];
|
|
20
|
+
}
|
|
11
21
|
/** Name prefix of the throwaway directory `check` generates into. */
|
|
12
22
|
export const scratchPrefix = ".openapi-rq-check-";
|
|
13
23
|
function isDirectory(target) {
|
|
@@ -132,7 +142,7 @@ async function runCheck(options, version) {
|
|
|
132
142
|
// The agent guide is not part of the output directory: it is compared
|
|
133
143
|
// below, against the file the real run would write.
|
|
134
144
|
await generate({ ...options, output: scratchOutput, agentsMd: undefined }, version, { silent: true });
|
|
135
|
-
const changesPerDir = await Promise.all(checkedSubdirectories.map((subdirectory) => diffDirectories({
|
|
145
|
+
const changesPerDir = await Promise.all(checkedSubdirectories(options).map((subdirectory) => diffDirectories({
|
|
136
146
|
expectedRoot: path.join(scratchOutput, subdirectory),
|
|
137
147
|
actualRoot: path.join(options.output, subdirectory),
|
|
138
148
|
// Reported paths are cwd-relative even for an absolute output.
|
|
@@ -162,5 +172,5 @@ export async function check(options, version) {
|
|
|
162
172
|
}
|
|
163
173
|
}
|
|
164
174
|
export function outOfDateError(files) {
|
|
165
|
-
return new CodegenError(ErrorCodes.outputOutOfDate, `Generated output is out of date (${files.length} ${files.length === 1 ? "file differs" : "files differ"}). Re-run
|
|
175
|
+
return new CodegenError(ErrorCodes.outputOutOfDate, `Generated output is out of date (${files.length} ${files.length === 1 ? "file differs" : "files differ"}). Re-run orqc without --check to update it.`, { files });
|
|
166
176
|
}
|
package/dist/cli.d.mts
CHANGED
|
@@ -20,6 +20,8 @@ export type LimitedUserConfig = {
|
|
|
20
20
|
nextPageParam: string;
|
|
21
21
|
initialPageParam: string | number;
|
|
22
22
|
omitInitialPageParam?: boolean;
|
|
23
|
+
/** `--mock`: also generate mock data and request handlers for this library. */
|
|
24
|
+
mock?: "msw";
|
|
23
25
|
/** `--agentsMd`: the file to keep the agent guide in, or `true` for `AGENTS.md`. */
|
|
24
26
|
agentsMd?: string | boolean;
|
|
25
27
|
};
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import type { GeneratedFile } from "../types.mjs";
|
|
2
|
+
import { type EnumsMode } from "./emitTypes.mjs";
|
|
3
|
+
import { type OperationIR } from "./operations.mjs";
|
|
4
|
+
import { type SchemaCtx } from "./schema.mjs";
|
|
5
|
+
/**
|
|
6
|
+
* The major version of MSW the handlers are written for. Version 3 moved
|
|
7
|
+
* `http` and `HttpResponse` from `msw` to `msw/http`; nothing else differs in
|
|
8
|
+
* what is generated.
|
|
9
|
+
*/
|
|
10
|
+
export type MswMajor = 2 | 3;
|
|
11
|
+
export interface MocksOptions {
|
|
12
|
+
mswMajor: MswMajor;
|
|
13
|
+
enums?: EnumsMode;
|
|
14
|
+
/** `.js` for Node16/NodeNext module resolution, otherwise empty. */
|
|
15
|
+
ext: "" | ".js";
|
|
16
|
+
}
|
|
17
|
+
export interface EmittedMocks {
|
|
18
|
+
files: GeneratedFile[];
|
|
19
|
+
/** What could not be mocked as the specification describes it. */
|
|
20
|
+
warnings: string[];
|
|
21
|
+
}
|
|
22
|
+
/** Emit the files of `<output>/mocks`: the mock data and the MSW handlers. */
|
|
23
|
+
export declare function emitMocks(operations: OperationIR[], ctx: SchemaCtx, options: MocksOptions): EmittedMocks;
|