Skip to content

Commit 8b26ca3

Browse files
committed
Update docs
1 parent 574838f commit 8b26ca3

File tree

4 files changed

+221
-2
lines changed

4 files changed

+221
-2
lines changed

README.md

Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,7 @@ This is a monorepo containing multiple packages:
220220
- **`@devup-api/utils`** - Utility functions for OpenAPI processing
221221
- **`@devup-api/generator`** - TypeScript interface generator from OpenAPI schemas
222222
- **`@devup-api/fetch`** - Type-safe API client
223+
- **`@devup-api/react-query`** - TanStack React Query integration
223224
- **`@devup-api/vite-plugin`** - Vite plugin
224225
- **`@devup-api/next-plugin`** - Next.js plugin
225226
- **`@devup-api/webpack-plugin`** - Webpack plugin
@@ -276,6 +277,151 @@ if (result.data) {
276277
}
277278
```
278279

280+
### **Using DevupObject for Type References**
281+
282+
`DevupObject` allows you to reference generated schema types directly, which is useful for typing variables, function parameters, or component props.
283+
284+
```ts
285+
import { createApi, type DevupObject } from '@devup-api/fetch'
286+
287+
// Access response types from the default OpenAPI schema
288+
type User = DevupObject['User']
289+
type Product = DevupObject['Product']
290+
291+
// Use in your code
292+
const user: User = {
293+
id: '123',
294+
name: 'John Doe',
295+
296+
}
297+
298+
// For request/error types, specify the type category
299+
type CreateUserRequest = DevupObject<'request'>['CreateUserBody']
300+
type ApiError = DevupObject<'error'>['ErrorResponse']
301+
```
302+
303+
---
304+
305+
## 🌐 Multiple API Servers
306+
307+
devup-api supports multiple OpenAPI schemas for working with different API servers.
308+
309+
### **Configuration**
310+
311+
Place multiple OpenAPI files in your project (e.g., `openapi.json`, `openapi2.json`) and the plugin will generate types for each.
312+
313+
### **Usage**
314+
315+
```ts
316+
import { createApi, type DevupObject } from '@devup-api/fetch'
317+
318+
// Default server (uses openapi.json)
319+
const api = createApi({
320+
baseUrl: 'https://api.example.com',
321+
})
322+
323+
// Second server (uses openapi2.json)
324+
const api2 = createApi({
325+
baseUrl: 'https://api.another-service.com',
326+
serverName: 'openapi2.json',
327+
})
328+
329+
// Make requests to different servers
330+
const users = await api.get('getUsers', {})
331+
const products = await api2.get('getProducts', {})
332+
333+
// Access types from different schemas
334+
type User = DevupObject['User'] // From openapi.json
335+
type Product = DevupObject<'response', 'openapi2.json'>['Product'] // From openapi2.json
336+
```
337+
338+
---
339+
340+
## 🔄 React Query Integration
341+
342+
devup-api provides first-class support for TanStack React Query through the `@devup-api/react-query` package.
343+
344+
### **Installation**
345+
346+
```bash
347+
npm install @devup-api/react-query @tanstack/react-query
348+
```
349+
350+
### **Setup**
351+
352+
```ts
353+
import { createApi } from '@devup-api/fetch'
354+
import { createQueryClient } from '@devup-api/react-query'
355+
356+
const api = createApi('https://api.example.com')
357+
const queryClient = createQueryClient(api)
358+
```
359+
360+
### **useQuery**
361+
362+
```ts
363+
function UserProfile({ userId }: { userId: string }) {
364+
const { data, isLoading, error } = queryClient.useQuery(
365+
'get',
366+
'/users/{id}',
367+
{ params: { id: userId } }
368+
)
369+
370+
if (isLoading) return <div>Loading...</div>
371+
if (error) return <div>Error: {error.message}</div>
372+
return <div>{data.name}</div>
373+
}
374+
```
375+
376+
### **useMutation**
377+
378+
```ts
379+
function CreateUser() {
380+
const mutation = queryClient.useMutation('post', 'createUser')
381+
382+
return (
383+
<button onClick={() => mutation.mutate({
384+
body: { name: 'John', email: '[email protected]' }
385+
})}>
386+
Create User
387+
</button>
388+
)
389+
}
390+
```
391+
392+
### **useSuspenseQuery**
393+
394+
```ts
395+
function UserList() {
396+
const { data } = queryClient.useSuspenseQuery('get', 'getUsers', {})
397+
return <ul>{data.map(user => <li key={user.id}>{user.name}</li>)}</ul>
398+
}
399+
```
400+
401+
### **useInfiniteQuery**
402+
403+
```ts
404+
function InfiniteUserList() {
405+
const { data, fetchNextPage, hasNextPage } = queryClient.useInfiniteQuery(
406+
'get',
407+
'getUsers',
408+
{
409+
initialPageParam: 1,
410+
getNextPageParam: (lastPage) => lastPage.nextPage,
411+
}
412+
)
413+
414+
return (
415+
<>
416+
{data?.pages.map(page =>
417+
page.users.map(user => <div key={user.id}>{user.name}</div>)
418+
)}
419+
{hasNextPage && <button onClick={() => fetchNextPage()}>Load More</button>}
420+
</>
421+
)
422+
}
423+
```
424+
279425
---
280426

281427
## ⚙️ Configuration Options

SKILL.md

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ This skill helps you invoke the `devup-api` library to generate and use fully ty
1313
- **Fetch-compatible:** Ergonomic API similar to standard `fetch`.
1414
- **Zero Generics:** No complex generic types to manage manually.
1515
- **Build Tool Integration:** Plugins for Vite, Next.js, Webpack, and Rsbuild.
16+
- **React Query Integration:** First-class support for TanStack React Query with `@devup-api/react-query`.
17+
- **Multiple API Servers:** Support for multiple OpenAPI schemas with `serverName` and `DevupObject` type access.
1618
- **Two-phase Typing:** "Cold Typing" (relaxed types for initial setup) and "Bold Typing" (strict types after generation).
1719

1820
## Usage Instructions
@@ -98,6 +100,76 @@ if (response.data) {
98100
}
99101
```
100102

103+
## Using DevupObject for Type References
104+
105+
`DevupObject` provides direct access to generated schema types:
106+
107+
```ts
108+
import { createApi, type DevupObject } from '@devup-api/fetch'
109+
110+
// Access response types
111+
type User = DevupObject['User']
112+
113+
// Access request/error types
114+
type CreateUserRequest = DevupObject<'request'>['CreateUserBody']
115+
type ApiError = DevupObject<'error'>['ErrorResponse']
116+
117+
// For multiple OpenAPI schemas, specify the server name
118+
type Product = DevupObject<'response', 'openapi2.json'>['Product']
119+
```
120+
121+
## Multiple API Servers
122+
123+
Support multiple OpenAPI schemas with `serverName`:
124+
125+
```ts
126+
import { createApi, type DevupObject } from '@devup-api/fetch'
127+
128+
// Default server
129+
const api = createApi({ baseUrl: 'https://api.example.com' })
130+
131+
// Second server
132+
const api2 = createApi({
133+
baseUrl: 'https://api.another-service.com',
134+
serverName: 'openapi2.json',
135+
})
136+
137+
// Types from different schemas
138+
type User = DevupObject['User'] // openapi.json
139+
type Product = DevupObject<'response', 'openapi2.json'>['Product'] // openapi2.json
140+
```
141+
142+
## React Query Integration
143+
144+
For React applications using TanStack React Query, use `@devup-api/react-query`:
145+
146+
```bash
147+
npm install @devup-api/react-query @tanstack/react-query
148+
```
149+
150+
```ts
151+
import { createApi } from '@devup-api/fetch'
152+
import { createQueryClient } from '@devup-api/react-query'
153+
154+
const api = createApi('https://api.example.com')
155+
const queryClient = createQueryClient(api)
156+
157+
// useQuery
158+
const { data } = queryClient.useQuery('get', '/users/{id}', { params: { id: '123' } })
159+
160+
// useMutation
161+
const mutation = queryClient.useMutation('post', 'createUser')
162+
163+
// useSuspenseQuery
164+
const { data } = queryClient.useSuspenseQuery('get', 'getUsers', {})
165+
166+
// useInfiniteQuery
167+
const { data, fetchNextPage } = queryClient.useInfiniteQuery('get', 'getUsers', {
168+
initialPageParam: 1,
169+
getNextPageParam: (lastPage) => lastPage.nextPage,
170+
})
171+
```
172+
101173
## Guidelines
102174

103175
- **"Cold" vs "Bold" Typing:** When you first start, types might be `any` (Cold Typing). Run your build command (`dev` or `build`) to generate the types and enable strict checking (Bold Typing).

examples/next/app/page.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ export default function Home() {
2323
},
2424
})
2525
const _object: DevupObject['User'] | undefined = data?.[0]
26-
const _object2: DevupObject<'openapi2.json'>['User'] | undefined = data?.[0]
26+
const _object2: DevupObject<'response', 'openapi2.json'>['User'] | undefined =
27+
data?.[0]
2728

2829
console.info(data, isLoading, error)
2930

packages/core/src/api-struct.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@ export interface DevupResponseComponentStruct {}
2929
export interface DevupErrorComponentStruct {}
3030

3131
export type DevupObject<
32-
T extends keyof DevupApiServers | (string & {}) = 'openapi.json',
3332
R extends 'response' | 'request' | 'error' = 'response',
33+
T extends keyof DevupApiServers | (string & {}) = 'openapi.json',
3434
> = ExtractValue<
3535
{
3636
response: ExtractValue<DevupResponseComponentStruct, T>

0 commit comments

Comments
 (0)