@codewithagents/openapi-react-query 0.2.0 → 1.0.0
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/CHANGELOG.md +11 -0
- package/README.md +137 -0
- package/package.json +1 -1
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,16 @@
|
|
|
1
1
|
# Changelog
|
|
2
2
|
|
|
3
|
+
## [1.0.0](https://github.com/codewithagents/glue/compare/openapi-react-query-v0.2.0...openapi-react-query-v1.0.0) (2026-05-24)
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
### ⚠ BREAKING CHANGES
|
|
7
|
+
|
|
8
|
+
* major version bump from 0.x — update your version range from ^0.x to ^1.0.0.
|
|
9
|
+
|
|
10
|
+
### Features
|
|
11
|
+
|
|
12
|
+
* promote api-errors and openapi-react-query to stable 1.0.0 ([#47](https://github.com/codewithagents/glue/issues/47)) ([075be06](https://github.com/codewithagents/glue/commit/075be063b4588080f94dc7f228ca8f30ea8aa5e8))
|
|
13
|
+
|
|
3
14
|
## [0.2.0](https://github.com/codewithagents/glue/compare/openapi-react-query-v0.1.0...openapi-react-query-v0.2.0) (2026-05-24)
|
|
4
15
|
|
|
5
16
|
|
package/README.md
ADDED
|
@@ -0,0 +1,137 @@
|
|
|
1
|
+
# @codewithagents/openapi-react-query
|
|
2
|
+
|
|
3
|
+
[](https://npmjs.com/package/@codewithagents/openapi-react-query)
|
|
4
|
+
[](https://codecov.io/gh/codewithagents/glue)
|
|
5
|
+
|
|
6
|
+
Generate typed [React Query v5](https://tanstack.com/query/v5) hooks from an OpenAPI 3.1 spec. Run once, get a fully typed `useQuery` hook per GET endpoint and a `useMutation` hook per write operation — no hand-written boilerplate.
|
|
7
|
+
|
|
8
|
+
Works alongside [`@codewithagents/openapi-gen`](../openapi-gen) which generates the underlying typed fetch client.
|
|
9
|
+
|
|
10
|
+
## Install
|
|
11
|
+
|
|
12
|
+
```bash
|
|
13
|
+
npm install -D @codewithagents/openapi-react-query @codewithagents/openapi-gen
|
|
14
|
+
npm install @tanstack/react-query
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
## Configure
|
|
18
|
+
|
|
19
|
+
Create `openapi-react-query.config.json` in your project root:
|
|
20
|
+
|
|
21
|
+
```json
|
|
22
|
+
{
|
|
23
|
+
"input_openapi": "./openapi.json",
|
|
24
|
+
"output": "./src/api",
|
|
25
|
+
"stale_time": 30000,
|
|
26
|
+
"gc_time": 300000
|
|
27
|
+
}
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
| Field | Required | Default | Description |
|
|
31
|
+
|---|---|---|---|
|
|
32
|
+
| `input_openapi` | ✅ | — | Path to OpenAPI 3.1 spec (JSON or YAML) |
|
|
33
|
+
| `output` | ✅ | — | Directory to write generated files (same as openapi-gen output) |
|
|
34
|
+
| `stale_time` | — | `0` | `staleTime` in ms applied to all `useQuery` hooks |
|
|
35
|
+
| `gc_time` | — | `300000` | `gcTime` in ms applied to all `useQuery` hooks |
|
|
36
|
+
|
|
37
|
+
## Generate
|
|
38
|
+
|
|
39
|
+
Run both generators — order matters, openapi-gen first:
|
|
40
|
+
|
|
41
|
+
```bash
|
|
42
|
+
npx openapi-gen
|
|
43
|
+
npx openapi-react-query
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
Or add to `package.json`:
|
|
47
|
+
|
|
48
|
+
```json
|
|
49
|
+
{
|
|
50
|
+
"scripts": {
|
|
51
|
+
"generate": "openapi-gen && openapi-react-query"
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
## What gets generated
|
|
57
|
+
|
|
58
|
+
Given a spec with a `/tasks` resource, `hooks.ts` contains:
|
|
59
|
+
|
|
60
|
+
**Key factory** — one per resource, used for cache invalidation:
|
|
61
|
+
|
|
62
|
+
```ts
|
|
63
|
+
export const taskKeys = {
|
|
64
|
+
all: () => ['tasks'] as const,
|
|
65
|
+
list: (params?) => ['tasks', 'list', params] as const,
|
|
66
|
+
detail: (id: string) => ['tasks', id] as const,
|
|
67
|
+
}
|
|
68
|
+
```
|
|
69
|
+
|
|
70
|
+
**Query hooks** — one per GET operation:
|
|
71
|
+
|
|
72
|
+
```ts
|
|
73
|
+
export function useListTasks(params?, options?) {
|
|
74
|
+
return useQuery({
|
|
75
|
+
queryKey: taskKeys.list(params),
|
|
76
|
+
queryFn: () => listTasks(params),
|
|
77
|
+
staleTime: 30000,
|
|
78
|
+
gcTime: 300000,
|
|
79
|
+
...options, // override any option per call site
|
|
80
|
+
})
|
|
81
|
+
}
|
|
82
|
+
```
|
|
83
|
+
|
|
84
|
+
**Mutation hooks** — one per POST/PUT/PATCH/DELETE:
|
|
85
|
+
|
|
86
|
+
```ts
|
|
87
|
+
export function useCreateTask(options?) {
|
|
88
|
+
return useMutation({ mutationFn: (vars) => createTask(vars), ...options })
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
export function useUpdateTask(options?) {
|
|
92
|
+
return useMutation({ mutationFn: ({ id, body }) => updateTask(id, body), ...options })
|
|
93
|
+
}
|
|
94
|
+
```
|
|
95
|
+
|
|
96
|
+
All types are derived from the generated client — no duplication:
|
|
97
|
+
- Data type: `Awaited<ReturnType<typeof listTasks>>`
|
|
98
|
+
- Variables type: `Parameters<typeof createTask>[0]`
|
|
99
|
+
|
|
100
|
+
## Use it
|
|
101
|
+
|
|
102
|
+
```tsx
|
|
103
|
+
import { useListTasks, useCreateTask, taskKeys } from './src/api/hooks'
|
|
104
|
+
import { useQueryClient } from '@tanstack/react-query'
|
|
105
|
+
|
|
106
|
+
function TaskList() {
|
|
107
|
+
const { data, isLoading } = useListTasks({ status: 'open' })
|
|
108
|
+
|
|
109
|
+
const queryClient = useQueryClient()
|
|
110
|
+
const create = useCreateTask({
|
|
111
|
+
onSuccess: () => queryClient.invalidateQueries({ queryKey: taskKeys.all() }),
|
|
112
|
+
})
|
|
113
|
+
|
|
114
|
+
if (isLoading) return <Spinner />
|
|
115
|
+
return (
|
|
116
|
+
<>
|
|
117
|
+
{data?.map(task => <Task key={task.id} task={task} />)}
|
|
118
|
+
<button onClick={() => create.mutate({ title: 'New task' })}>Add</button>
|
|
119
|
+
</>
|
|
120
|
+
)
|
|
121
|
+
}
|
|
122
|
+
```
|
|
123
|
+
|
|
124
|
+
## Multiple specs
|
|
125
|
+
|
|
126
|
+
Use `--config` to point at different config files per vendor:
|
|
127
|
+
|
|
128
|
+
```bash
|
|
129
|
+
npx openapi-gen --config ./config/payments.config.json
|
|
130
|
+
npx openapi-react-query --config ./config/payments.config.json
|
|
131
|
+
```
|
|
132
|
+
|
|
133
|
+
Relative paths in each config resolve from the config file's directory.
|
|
134
|
+
|
|
135
|
+
## License
|
|
136
|
+
|
|
137
|
+
MIT
|