Skip to content

Commit e03d4fb

Browse files
authored
Merge pull request #16 from dev-five-git/fix-typing-and-query
Fix typing and query
2 parents adfcb44 + c30cc1a commit e03d4fb

File tree

17 files changed

+425
-682
lines changed

17 files changed

+425
-682
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"changes":{"packages/generator/package.json":"Patch"},"note":"Fix gen issue","date":"2025-12-04T13:14:14.424512200Z"}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"changes":{"packages/core/package.json":"Patch","packages/react-query/package.json":"Patch","packages/fetch/package.json":"Patch"},"note":"Fix query typing issue","date":"2025-12-04T13:14:26.431881700Z"}

bun.lock

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
"dependencies": {
2121
"@devup-api/fetch": "workspace:*",
2222
"@devup-api/next-plugin": "workspace:*",
23+
"@devup-api/react-query": "workspace:*",
2324
"@devup-ui/react": "^1",
2425
"next": "^16.0.4",
2526
"react": "^19.2.0",
@@ -88,15 +89,15 @@
8889
},
8990
"packages/core": {
9091
"name": "@devup-api/core",
91-
"version": "0.1.5",
92+
"version": "0.1.6",
9293
"devDependencies": {
9394
"@types/node": "^24.10",
9495
"typescript": "^5.9",
9596
},
9697
},
9798
"packages/fetch": {
9899
"name": "@devup-api/fetch",
99-
"version": "0.1.5",
100+
"version": "0.1.7",
100101
"dependencies": {
101102
"@devup-api/core": "workspace:*",
102103
},
@@ -139,7 +140,7 @@
139140
},
140141
"packages/react-query": {
141142
"name": "@devup-api/react-query",
142-
"version": "0.0.0",
143+
"version": "0.1.0",
143144
"dependencies": {
144145
"@devup-api/fetch": "workspace:*",
145146
"@tanstack/react-query": ">=5.90",

examples/next/app/page.tsx

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,46 @@
11
'use client'
22

33
import { createApi } from '@devup-api/fetch'
4+
import { createQueryClient } from '@devup-api/react-query'
45
import { Box, Text } from '@devup-ui/react'
56
import { useEffect } from 'react'
67

7-
const api = createApi('https://api.example.com')
8-
const api2 = createApi('https://api.example2.com', undefined, 'openapi2.json')
8+
const api = createApi({
9+
baseUrl: 'https://api.example.com',
10+
})
11+
const api2 = createApi({
12+
baseUrl: 'https://api.example2.com',
13+
serverName: 'openapi2.json',
14+
})
15+
16+
const queryClient = createQueryClient(api)
917

1018
export default function Home() {
19+
const { data, isLoading, error } = queryClient.useQuery('GET', 'getUsers', {
20+
// params: { id: 1 },
21+
query: {
22+
name: 'John Doe',
23+
},
24+
})
25+
26+
console.info(data, isLoading, error)
27+
28+
const {
29+
data: data2,
30+
error: error2,
31+
mutateAsync,
32+
} = queryClient.useMutation('GET', '/users/{id}', {})
33+
mutateAsync({
34+
params: { id: 1 },
35+
query: {
36+
name: 'John Doe',
37+
},
38+
})
39+
40+
console.info(data2, error2)
41+
1142
useEffect(() => {
12-
api2.get('getUsers2', {}).then((res) => {
43+
api2.get('getUsers2').then((res) => {
1344
console.log(res)
1445
})
1546
api.get('getUsers', {}).then((res) => {

examples/next/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
"react-dom": "^19.2.0",
1515
"@devup-api/next-plugin": "workspace:*",
1616
"@devup-api/fetch": "workspace:*",
17+
"@devup-api/react-query": "workspace:*",
1718
"@devup-ui/react": "^1"
1819
},
1920
"devDependencies": {

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@
1818
"lint": "biome check",
1919
"lint:fix": "biome check --write",
2020
"prepare": "husky",
21-
"build": "bun run -F @devup-api/core build && bun run -F @devup-api/utils build && bun run -F @devup-api/generator build && bun run -F @devup-api/fetch build && bun run -F @devup-api/webpack-plugin build && bun run -F @devup-api/vite-plugin build && bun run -F @devup-api/next-plugin build && bun run -F @devup-api/rsbuild-plugin build",
22-
"publish": "bun publish --cwd packages/core && bun publish --cwd packages/utils && bun publish --cwd packages/generator && bun publish --cwd packages/fetch && bun publish --cwd packages/webpack-plugin && bun publish --cwd packages/vite-plugin && bun publish --cwd packages/next-plugin && bun publish --cwd packages/rsbuild-plugin"
21+
"build": "bun run -F @devup-api/core build && bun run -F @devup-api/utils build && bun run -F @devup-api/generator build && bun run -F @devup-api/fetch build && bun run -F @devup-api/webpack-plugin build && bun run -F @devup-api/vite-plugin build && bun run -F @devup-api/next-plugin build && bun run -F @devup-api/rsbuild-plugin build && bun run -F @devup-api/react-query build",
22+
"publish": "bun publish --cwd packages/core && bun publish --cwd packages/utils && bun publish --cwd packages/generator && bun publish --cwd packages/fetch && bun publish --cwd packages/webpack-plugin && bun publish --cwd packages/vite-plugin && bun publish --cwd packages/next-plugin && bun publish --cwd packages/rsbuild-plugin && bun publish --cwd packages/react-query"
2323
},
2424
"workspaces": [
2525
"packages/*",

packages/core/src/additional.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import type { DevupApiServers } from './api-struct'
12
import type { Middleware } from './middleware'
23

34
export type Additional<
@@ -7,7 +8,14 @@ export type Additional<
78

89
export type RequiredOptions<T extends object> = keyof T extends undefined
910
? never
10-
: T
11+
: 'params' extends keyof T
12+
? T
13+
: 'query' extends keyof T
14+
? T
15+
: 'body' extends keyof T
16+
? T
17+
: never
18+
export type IsCold = keyof DevupApiServers extends never ? true : false
1119
export type DevupApiRequestInit = Omit<RequestInit, 'body'> & {
1220
body?: object | RequestInit['body']
1321
params?: Record<string, string | number | boolean | null | undefined>

packages/core/src/api-struct.ts

Lines changed: 31 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -24,28 +24,48 @@ export interface DevupRequestComponentStruct {}
2424
// biome-ignore lint/suspicious/noEmptyInterface: empty interface
2525
export interface DevupResponseComponentStruct {}
2626

27-
export type DevupApiStruct = DevupGetApiStruct &
28-
DevupPostApiStruct &
29-
DevupPutApiStruct &
30-
DevupDeleteApiStruct &
31-
DevupPatchApiStruct
27+
export type DevupGetApiStructScope<O extends string> = ConditionalScope<
28+
DevupGetApiStruct,
29+
O
30+
>
31+
export type DevupPostApiStructScope<O extends string> = ConditionalScope<
32+
DevupPostApiStruct,
33+
O
34+
>
35+
export type DevupPutApiStructScope<O extends string> = ConditionalScope<
36+
DevupPutApiStruct,
37+
O
38+
>
39+
export type DevupDeleteApiStructScope<O extends string> = ConditionalScope<
40+
DevupDeleteApiStruct,
41+
O
42+
>
43+
export type DevupPatchApiStructScope<O extends string> = ConditionalScope<
44+
DevupPatchApiStruct,
45+
O
46+
>
3247

3348
export type DevupGetApiStructKey<O extends string> = ConditionalKeys<
34-
ConditionalScope<DevupGetApiStruct, O>
49+
DevupGetApiStructScope<O>
3550
>
3651
export type DevupPostApiStructKey<O extends string> = ConditionalKeys<
37-
ConditionalScope<DevupPostApiStruct, O>
52+
DevupPostApiStructScope<O>
3853
>
3954
export type DevupPutApiStructKey<O extends string> = ConditionalKeys<
40-
ConditionalScope<DevupPutApiStruct, O>
55+
DevupPutApiStructScope<O>
4156
>
4257
export type DevupDeleteApiStructKey<O extends string> = ConditionalKeys<
43-
ConditionalScope<DevupDeleteApiStruct, O>
58+
DevupDeleteApiStructScope<O>
4459
>
4560
export type DevupPatchApiStructKey<O extends string> = ConditionalKeys<
46-
ConditionalScope<DevupPatchApiStruct, O>
61+
DevupPatchApiStructScope<O>
4762
>
63+
export type DevupApiStructScope<O extends string> = DevupGetApiStructScope<O> &
64+
DevupPostApiStructScope<O> &
65+
DevupPutApiStructScope<O> &
66+
DevupDeleteApiStructScope<O> &
67+
DevupPatchApiStructScope<O>
4868

4969
export type DevupApiStructKey<O extends string> = ConditionalKeys<
50-
ConditionalScope<DevupApiStruct, O>
70+
DevupApiStructScope<O>
5171
>

packages/core/src/middleware.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,13 @@ export interface MiddlewareCallbackParams {
1111
}
1212

1313
type MiddlewareOnRequest = (
14-
params: MiddlewareCallbackParams,
14+
params: Readonly<MiddlewareCallbackParams>,
1515
) => PromiseOr<undefined | Request | Response>
1616
type MiddlewareOnResponse = (
17-
params: MiddlewareCallbackParams & { response: Response },
17+
params: Readonly<MiddlewareCallbackParams & { response: Response }>,
1818
) => PromiseOr<undefined | Error | Response>
1919
type MiddlewareOnError = (
20-
params: MiddlewareCallbackParams & { error: unknown },
20+
params: Readonly<MiddlewareCallbackParams & { error: unknown }>,
2121
) => PromiseOr<undefined | Error | Response>
2222

2323
export type Middleware =

packages/fetch/src/__tests__/api.test.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,28 @@ test('request does not serialize non-plain object body', async () => {
131131
}
132132
})
133133

134+
test('request serializes plain object body to JSON with custom headers', async () => {
135+
const api = new DevupApi('https://api.example.com', undefined, 'openapi.json')
136+
const mockFetch = globalThis.fetch as unknown as ReturnType<typeof mock>
137+
138+
await api.post(
139+
'/test' as never,
140+
{
141+
body: { name: 'test', value: 123 },
142+
} as never,
143+
)
144+
145+
expect(mockFetch).toHaveBeenCalledTimes(1)
146+
const call = mockFetch.mock.calls[0]
147+
expect(call).toBeDefined()
148+
if (call) {
149+
const request = call[0] as Request
150+
const body = await request.text()
151+
expect(body).toBe(JSON.stringify({ name: 'test', value: 123 }))
152+
expect(request.headers.get('Content-Type')).toBe('application/json')
153+
}
154+
})
155+
134156
test('request merges defaultOptions with request options', async () => {
135157
const api = new DevupApi(
136158
'https://api.example.com',

0 commit comments

Comments
 (0)