@qualisero/openapi-endpoint 0.12.3 → 0.13.2
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 +32 -48
- package/dist/cli.js +740 -25
- package/dist/index.d.ts +15 -192
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +30 -115
- package/dist/openapi-helpers.d.ts.map +1 -1
- package/dist/openapi-helpers.js +70 -38
- package/dist/openapi-mutation.d.ts +76 -108
- package/dist/openapi-mutation.d.ts.map +1 -1
- package/dist/openapi-mutation.js +39 -52
- package/dist/openapi-query.d.ts +73 -208
- package/dist/openapi-query.d.ts.map +1 -1
- package/dist/openapi-query.js +66 -71
- package/dist/openapi-utils.d.ts +30 -4
- package/dist/openapi-utils.d.ts.map +1 -1
- package/dist/openapi-utils.js +45 -56
- package/dist/types.d.ts +411 -197
- package/dist/types.d.ts.map +1 -1
- package/dist/types.js +36 -0
- package/package.json +1 -1
- package/dist/openapi-endpoint.d.ts +0 -18
- package/dist/openapi-endpoint.d.ts.map +0 -1
- package/dist/openapi-endpoint.js +0 -24
- package/dist/types-documentation.d.ts +0 -158
- package/dist/types-documentation.d.ts.map +0 -1
- package/dist/types-documentation.js +0 -9
package/README.md
CHANGED
|
@@ -13,9 +13,9 @@ Turns your `openapi.json` into typesafe API composables using Vue Query (TanStac
|
|
|
13
13
|
Let's you get TanStack Vue Query composables that enforce consistency (name of endpoints, typing) with your API's `openapi.json` file:
|
|
14
14
|
|
|
15
15
|
```typescript
|
|
16
|
-
const { data, isLoading } = api.useQuery(
|
|
16
|
+
const { data, isLoading } = api.useQuery(QueryOperationId.getPet, { petId: '123' })
|
|
17
17
|
|
|
18
|
-
const createPetMutation = api.useMutation(
|
|
18
|
+
const createPetMutation = api.useMutation(MutationOperationId.createPet)
|
|
19
19
|
createPetMutation.mutate({ data: { name: 'Fluffy', species: 'cat' } })
|
|
20
20
|
```
|
|
21
21
|
|
|
@@ -52,7 +52,12 @@ import { useOpenApi } from '@qualisero/openapi-endpoint'
|
|
|
52
52
|
import axios from 'axios'
|
|
53
53
|
|
|
54
54
|
// Import your auto-generated operations (includes both metadata and types)
|
|
55
|
-
import {
|
|
55
|
+
import {
|
|
56
|
+
QueryOperationId,
|
|
57
|
+
MutationOperationId,
|
|
58
|
+
openApiOperations,
|
|
59
|
+
type OpenApiOperations,
|
|
60
|
+
} from './generated/api-operations'
|
|
56
61
|
|
|
57
62
|
// Create axios instance
|
|
58
63
|
const axiosInstance = axios.create({
|
|
@@ -66,21 +71,21 @@ const api = useOpenApi<OpenApiOperations>({
|
|
|
66
71
|
})
|
|
67
72
|
|
|
68
73
|
// Export for use in other parts of your application
|
|
69
|
-
export { api, OperationId }
|
|
74
|
+
export { api, QueryOperationId, MutationOperationId, OperationId }
|
|
70
75
|
```
|
|
71
76
|
|
|
72
77
|
### 2. Use the API in your components
|
|
73
78
|
|
|
74
79
|
```typescript
|
|
75
80
|
// In your Vue components
|
|
76
|
-
import { api,
|
|
81
|
+
import { api, QueryOperationId, MutationOperationId } from './api/init'
|
|
77
82
|
|
|
78
83
|
// Use queries for GET operations
|
|
79
|
-
const { data: pets, isLoading } = api.useQuery(
|
|
80
|
-
const { data: pet } = api.useQuery(
|
|
84
|
+
const { data: pets, isLoading } = api.useQuery(QueryOperationId.listPets)
|
|
85
|
+
const { data: pet } = api.useQuery(QueryOperationId.getPet, { petId: '123' })
|
|
81
86
|
|
|
82
87
|
// Use mutations for POST/PUT/PATCH/DELETE operations
|
|
83
|
-
const createPetMutation = api.useMutation(
|
|
88
|
+
const createPetMutation = api.useMutation(MutationOperationId.createPet)
|
|
84
89
|
|
|
85
90
|
// Execute mutations
|
|
86
91
|
await createPetMutation.mutateAsync({
|
|
@@ -96,10 +101,10 @@ The library supports type-safe, reactive query parameters that automatically tri
|
|
|
96
101
|
|
|
97
102
|
```typescript
|
|
98
103
|
import { ref, computed } from 'vue'
|
|
99
|
-
import { api,
|
|
104
|
+
import { api, QueryOperationId } from './api/init'
|
|
100
105
|
|
|
101
106
|
// Static query parameters
|
|
102
|
-
const { data: pets } = api.useQuery(
|
|
107
|
+
const { data: pets } = api.useQuery(QueryOperationId.listPets, {
|
|
103
108
|
queryParams: { limit: 10 },
|
|
104
109
|
})
|
|
105
110
|
// Results in: GET /pets?limit=10
|
|
@@ -108,7 +113,7 @@ const { data: pets } = api.useQuery(OperationId.listPets, {
|
|
|
108
113
|
const limit = ref(10)
|
|
109
114
|
const status = ref<'available' | 'pending' | 'sold'>('available')
|
|
110
115
|
|
|
111
|
-
const petsQuery = api.useQuery(
|
|
116
|
+
const petsQuery = api.useQuery(QueryOperationId.listPets, {
|
|
112
117
|
queryParams: computed(() => ({
|
|
113
118
|
limit: limit.value,
|
|
114
119
|
status: status.value,
|
|
@@ -122,7 +127,7 @@ status.value = 'pending'
|
|
|
122
127
|
|
|
123
128
|
// Combine with path parameters
|
|
124
129
|
const userPetsQuery = api.useQuery(
|
|
125
|
-
|
|
130
|
+
QueryOperationId.listUserPets,
|
|
126
131
|
computed(() => ({ userId: userId.value })),
|
|
127
132
|
{
|
|
128
133
|
queryParams: computed(() => ({
|
|
@@ -140,27 +145,6 @@ const userPetsQuery = api.useQuery(
|
|
|
140
145
|
- **Automatic refetch**: Changes to query params trigger automatic refetch via TanStack Query's key mechanism
|
|
141
146
|
- **Backward compatible**: Works alongside existing `axiosOptions.params`
|
|
142
147
|
|
|
143
|
-
### Automatic Operation Type Detection with `api.useEndpoint`
|
|
144
|
-
|
|
145
|
-
The `api.useEndpoint` method automatically detects whether an operation is a query (GET/HEAD/OPTIONS) or mutation (POST/PUT/PATCH/DELETE) based on the HTTP method defined in your OpenAPI specification:
|
|
146
|
-
|
|
147
|
-
```typescript
|
|
148
|
-
import { ref, computed } from 'vue'
|
|
149
|
-
import { api, OperationId } from './api/init'
|
|
150
|
-
|
|
151
|
-
// Automatically becomes a query for GET operations
|
|
152
|
-
const listEndpoint = api.useEndpoint(OperationId.listPets)
|
|
153
|
-
// TypeScript knows this has query properties like .data, .isLoading, .refetch()
|
|
154
|
-
|
|
155
|
-
// Automatically becomes a mutation for POST operations
|
|
156
|
-
const createEndpoint = api.useEndpoint(OperationId.createPet)
|
|
157
|
-
// TypeScript knows this has mutation properties like .mutate(), .mutateAsync()
|
|
158
|
-
|
|
159
|
-
// Use the endpoints according to their detected type
|
|
160
|
-
const petData = listEndpoint.data // Query data
|
|
161
|
-
await createEndpoint.mutateAsync({ data: { name: 'Fluffy' } }) // Mutation execution
|
|
162
|
-
```
|
|
163
|
-
|
|
164
148
|
### Automatic Cache Management and Refetching
|
|
165
149
|
|
|
166
150
|
By default, mutations automatically:
|
|
@@ -171,19 +155,19 @@ By default, mutations automatically:
|
|
|
171
155
|
|
|
172
156
|
```typescript
|
|
173
157
|
// Default behavior: automatic cache management
|
|
174
|
-
const createPet = api.useMutation(
|
|
158
|
+
const createPet = api.useMutation(MutationOperationId.createPet)
|
|
175
159
|
// No additional configuration needed - cache management is automatic
|
|
176
160
|
|
|
177
161
|
// Manual control over cache invalidation
|
|
178
|
-
const updatePet = api.useMutation(
|
|
162
|
+
const updatePet = api.useMutation(MutationOperationId.updatePet, {
|
|
179
163
|
dontInvalidate: true, // Disable automatic invalidation
|
|
180
164
|
dontUpdateCache: true, // Disable automatic cache updates
|
|
181
|
-
invalidateOperations: [
|
|
165
|
+
invalidateOperations: [QueryOperationId.listPets], // Manually specify operations to invalidate
|
|
182
166
|
})
|
|
183
167
|
|
|
184
168
|
// Refetch specific endpoints after mutation
|
|
185
|
-
const petListQuery = api.useQuery(
|
|
186
|
-
const createPetWithRefetch = api.useMutation(
|
|
169
|
+
const petListQuery = api.useQuery(QueryOperationId.listPets)
|
|
170
|
+
const createPetWithRefetch = api.useMutation(MutationOperationId.createPet, {
|
|
187
171
|
refetchEndpoints: [petListQuery], // Manually refetch these endpoints
|
|
188
172
|
})
|
|
189
173
|
```
|
|
@@ -198,7 +182,7 @@ async function uploadPetPicture(petId: string, file: File) {
|
|
|
198
182
|
const formData = new FormData()
|
|
199
183
|
formData.append('file', file)
|
|
200
184
|
|
|
201
|
-
const uploadMutation = api.useMutation(
|
|
185
|
+
const uploadMutation = api.useMutation(MutationOperationId.uploadPetPic, { petId })
|
|
202
186
|
|
|
203
187
|
return uploadMutation.mutateAsync({
|
|
204
188
|
data: formData,
|
|
@@ -212,7 +196,7 @@ async function uploadPetPicture(petId: string, file: File) {
|
|
|
212
196
|
|
|
213
197
|
// Alternative: using the object structure (if your API supports binary strings)
|
|
214
198
|
async function uploadPetPictureAsString(petId: string, binaryData: string) {
|
|
215
|
-
const uploadMutation = api.useMutation(
|
|
199
|
+
const uploadMutation = api.useMutation(MutationOperationId.uploadPetPic, { petId })
|
|
216
200
|
|
|
217
201
|
return uploadMutation.mutateAsync({
|
|
218
202
|
data: {
|
|
@@ -231,10 +215,10 @@ async function handleFileUpload(event: Event, petId: string) {
|
|
|
231
215
|
formData.append('file', file)
|
|
232
216
|
|
|
233
217
|
const uploadMutation = api.useMutation(
|
|
234
|
-
|
|
218
|
+
MutationOperationId.uploadPetPic,
|
|
235
219
|
{ petId },
|
|
236
220
|
{
|
|
237
|
-
invalidateOperations: [
|
|
221
|
+
invalidateOperations: [QueryOperationId.getPet, QueryOperationId.listPets],
|
|
238
222
|
onSuccess: (data) => {
|
|
239
223
|
console.log('Upload successful:', data)
|
|
240
224
|
},
|
|
@@ -254,17 +238,17 @@ async function handleFileUpload(event: Event, petId: string) {
|
|
|
254
238
|
|
|
255
239
|
### Reactive Enabling/Disabling Based on Path Parameters
|
|
256
240
|
|
|
257
|
-
|
|
241
|
+
You can chain queries where one query provides the parameters for another:
|
|
258
242
|
|
|
259
243
|
```typescript
|
|
260
244
|
import { ref, computed } from 'vue'
|
|
261
245
|
|
|
262
246
|
// First query to get user information
|
|
263
|
-
const userQuery = api.useQuery(
|
|
247
|
+
const userQuery = api.useQuery(QueryOperationId.getUser, { userId: 123 })
|
|
264
248
|
|
|
265
249
|
// Second query that depends on the first query's result
|
|
266
250
|
const userPetsQuery = api.useQuery(
|
|
267
|
-
|
|
251
|
+
QueryOperationId.listUserPets,
|
|
268
252
|
computed(() => ({
|
|
269
253
|
userId: userQuery.data.value?.id, // Chain: use ID from first query
|
|
270
254
|
})),
|
|
@@ -275,7 +259,7 @@ const selectedPetId = ref<string | undefined>(undefined)
|
|
|
275
259
|
|
|
276
260
|
// Query automatically enables/disables based on parameter availability
|
|
277
261
|
const petQuery = api.useQuery(
|
|
278
|
-
|
|
262
|
+
QueryOperationId.getPet,
|
|
279
263
|
computed(() => ({ petId: selectedPetId.value })),
|
|
280
264
|
)
|
|
281
265
|
|
|
@@ -291,7 +275,7 @@ const userId = ref<string>('user1')
|
|
|
291
275
|
const shouldFetchPets = ref(true)
|
|
292
276
|
|
|
293
277
|
const userPetsQuery = api.useQuery(
|
|
294
|
-
|
|
278
|
+
QueryOperationId.listUserPets,
|
|
295
279
|
computed(() => ({ userId: userId.value })),
|
|
296
280
|
{
|
|
297
281
|
enabled: computed(
|
|
@@ -308,7 +292,7 @@ import { ref } from 'vue'
|
|
|
308
292
|
|
|
309
293
|
// Use reactive query parameters
|
|
310
294
|
const limit = ref(10)
|
|
311
|
-
const petsQuery = api.useQuery(
|
|
295
|
+
const petsQuery = api.useQuery(QueryOperationId.listPets, {
|
|
312
296
|
queryParams: { limit: limit.value },
|
|
313
297
|
})
|
|
314
298
|
|