Skip to content

Commit 574838f

Browse files
committed
Add typing
1 parent 6d8aeac commit 574838f

File tree

6 files changed

+122
-54
lines changed

6 files changed

+122
-54
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"changes":{"packages/next-plugin/package.json":"Patch","packages/react-query/package.json":"Patch","packages/fetch/package.json":"Patch","packages/generator/package.json":"Patch","packages/core/package.json":"Patch","packages/utils/package.json":"Patch","packages/rsbuild-plugin/package.json":"Patch","packages/vite-plugin/package.json":"Patch","packages/webpack-plugin/package.json":"Patch"},"note":"Add DevupObject for typing","date":"2025-12-18T10:26:39.561791300Z"}

examples/next/app/page.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
'use client'
22

3-
import { createApi } from '@devup-api/fetch'
3+
import { createApi, type DevupObject } from '@devup-api/fetch'
44
import { createQueryClient } from '@devup-api/react-query'
55
import { Box, Text } from '@devup-ui/react'
66
import { useEffect } from 'react'
@@ -22,6 +22,8 @@ export default function Home() {
2222
name: 'John Doe',
2323
},
2424
})
25+
const _object: DevupObject['User'] | undefined = data?.[0]
26+
const _object2: DevupObject<'openapi2.json'>['User'] | undefined = data?.[0]
2527

2628
console.info(data, isLoading, error)
2729

packages/core/src/api-struct.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import type { ExtractValue } from './additional'
12
import type { ConditionalKeys, ConditionalScope } from './utils'
23

34
// biome-ignore lint/suspicious/noEmptyInterface: empty interface
@@ -24,6 +25,21 @@ export interface DevupRequestComponentStruct {}
2425
// biome-ignore lint/suspicious/noEmptyInterface: empty interface
2526
export interface DevupResponseComponentStruct {}
2627

28+
// biome-ignore lint/suspicious/noEmptyInterface: empty interface
29+
export interface DevupErrorComponentStruct {}
30+
31+
export type DevupObject<
32+
T extends keyof DevupApiServers | (string & {}) = 'openapi.json',
33+
R extends 'response' | 'request' | 'error' = 'response',
34+
> = ExtractValue<
35+
{
36+
response: ExtractValue<DevupResponseComponentStruct, T>
37+
request: ExtractValue<DevupRequestComponentStruct, T>
38+
error: ExtractValue<DevupErrorComponentStruct, T>
39+
},
40+
R
41+
>
42+
2743
export type DevupGetApiStructScope<O extends string> = ConditionalScope<
2844
DevupGetApiStruct,
2945
O

packages/core/src/utils.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1+
import type { ExtractValue } from './additional'
2+
13
export type ConditionalKeys<T, F = string> = keyof T extends undefined
24
? F
35
: keyof T & string
4-
export type ConditionalScope<T, K extends string> = K extends keyof T
5-
? T[K]
6-
: object
6+
export type ConditionalScope<T, K extends string> = ExtractValue<T, K, object>
77

88
export type PromiseOr<T> = Promise<T> | T

0 commit comments

Comments
 (0)