Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .changepacks/changepack_log_maCvVFhSXg-WSKYfKPVeo.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"changes":{"packages/zod/package.json":"Patch","packages/core/package.json":"Patch","packages/react-query/package.json":"Patch","packages/hookform/package.json":"Patch","packages/generator/package.json":"Patch","packages/fetch/package.json":"Patch"},"note":"Impl pregen, Fix wrong path issue","date":"2026-03-25T12:54:53.475679Z"}
5 changes: 5 additions & 0 deletions examples/next/app/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,11 @@ export default function Home() {
const _object2: DevupObject<'response', 'openapi2.json'>['User'] | undefined =
data?.[0]

const _results = queryClient.useQueries([
['GET', 'getUsers', { query: { name: 'John Doe' } }],
['GET', '/users/{id}', { params: { id: 1 }, query: { name: 'John Doe' } }],
])

console.info(data, isLoading, error)

const {
Expand Down
18 changes: 7 additions & 11 deletions packages/core/src/additional.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,10 @@ export type Additional<
Target extends object,
> = T extends keyof Target ? Target[T] & object : object

export type RequiredOptions<T extends object> = keyof T extends undefined
export type RequiredOptions<T extends object> = ('params' | 'query' | 'body') &
keyof T extends never
? never
: 'params' extends keyof T
? T
: 'query' extends keyof T
? T
: 'body' extends keyof T
? T
: never
: T
export type IsCold = keyof DevupApiServers extends never ? true : false
export type DevupApiRequestInit = Omit<RequestInit, 'body'> & {
body?: object | RequestInit['body']
Expand All @@ -40,9 +35,10 @@ export type ExtractValue<T, V extends string, F = any> = V extends keyof T

export type BoildApiOption<O> = Omit<DevupApiRequestInit, 'params'> &
Omit<O, 'response' | 'error'>
export type ConditionalApiOption<O> = IsCold extends true
? DevupApiRequestInit
: BoildApiOption<O>
export type ConditionalApiOption<
O,
_Cold extends boolean = IsCold,
> = _Cold extends true ? DevupApiRequestInit : BoildApiOption<O>

export type ApiOption<O extends object, R extends unknown[] = []> = [
RequiredOptions<O>,
Expand Down
66 changes: 43 additions & 23 deletions packages/core/src/api-struct.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,17 +28,24 @@ export interface DevupResponseComponentStruct {}
// biome-ignore lint/suspicious/noEmptyInterface: empty interface
export interface DevupErrorComponentStruct {}

// biome-ignore lint/suspicious/noEmptyInterface: empty interface for augmentation by generator
export interface DevupPrecomputedMethodKeys {}

// biome-ignore lint/suspicious/noEmptyInterface: empty interface for augmentation by generator
export interface DevupPrecomputedScopes {}

type LowercaseMethod = 'get' | 'post' | 'put' | 'delete' | 'patch'

export type DevupObject<
R extends 'response' | 'request' | 'error' = 'response',
T extends keyof DevupApiServers | (string & {}) = 'openapi.json',
> = ExtractValue<
{
response: ExtractValue<DevupResponseComponentStruct, T>
request: ExtractValue<DevupRequestComponentStruct, T>
error: ExtractValue<DevupErrorComponentStruct, T>
},
R
>
> = R extends 'response'
? ExtractValue<DevupResponseComponentStruct, T>
: R extends 'request'
? ExtractValue<DevupRequestComponentStruct, T>
: R extends 'error'
? ExtractValue<DevupErrorComponentStruct, T>
: never

export type DevupGetApiStructScope<O extends string> = ConditionalScope<
DevupGetApiStruct,
Expand Down Expand Up @@ -91,25 +98,38 @@ export type DevupApiMethodKeys =
export type DevupApiMethodScope<
S extends string,
M extends DevupApiMethodKeys,
> = {
get: DevupGetApiStructScope<S>
post: DevupPostApiStructScope<S>
put: DevupPutApiStructScope<S>
delete: DevupDeleteApiStructScope<S>
patch: DevupPatchApiStructScope<S>
GET: DevupGetApiStructScope<S>
POST: DevupPostApiStructScope<S>
PUT: DevupPutApiStructScope<S>
DELETE: DevupDeleteApiStructScope<S>
PATCH: DevupPatchApiStructScope<S>
}[M]
> = M extends 'get' | 'GET'
? DevupGetApiStructScope<S>
: M extends 'post' | 'POST'
? DevupPostApiStructScope<S>
: M extends 'put' | 'PUT'
? DevupPutApiStructScope<S>
: M extends 'delete' | 'DELETE'
? DevupDeleteApiStructScope<S>
: M extends 'patch' | 'PATCH'
? DevupPatchApiStructScope<S>
: never

export type DevupApiMethodKey<
S extends string,
M extends DevupApiMethodKeys,
> = S extends keyof DevupPrecomputedMethodKeys
? ExtractValue<
ExtractValue<DevupPrecomputedMethodKeys, S>,
Lowercase<M> & LowercaseMethod,
never
>
: ConditionalKeys<DevupApiMethodScope<S, M>>

export type DevupApiStructScope<O extends string> = DevupGetApiStructScope<O> &
DevupPostApiStructScope<O> &
DevupPutApiStructScope<O> &
DevupDeleteApiStructScope<O> &
DevupPatchApiStructScope<O>

export type DevupApiStructKey<O extends string> = ConditionalKeys<
DevupApiStructScope<O>
>
export type DevupApiStructKey<O extends string> =
| DevupGetApiStructKey<O>
| DevupPostApiStructKey<O>
| DevupPutApiStructKey<O>
| DevupDeleteApiStructKey<O>
| DevupPatchApiStructKey<O>
3 changes: 2 additions & 1 deletion packages/core/src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type { ExtractValue } from './additional'
import type { DevupApiServers } from './api-struct'

export type ConditionalKeys<T, F = string> = keyof T extends undefined
export type ConditionalKeys<T, F = string> = keyof DevupApiServers extends never
? F
: keyof T & string
export type ConditionalScope<T, K extends string> = ExtractValue<T, K, object>
Expand Down
26 changes: 11 additions & 15 deletions packages/fetch/src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,15 @@ import type {
ApiOption,
BoildApiOption,
ConditionalKeys,
DevupApiMethodKey,
DevupApiRequestInit,
DevupApiServers,
DevupApiStructKey,
DevupApiStructScope,
DevupDeleteApiStructKey,
DevupDeleteApiStructScope,
DevupGetApiStructKey,
DevupGetApiStructScope,
DevupPatchApiStructKey,
DevupPatchApiStructScope,
DevupPostApiStructKey,
DevupPostApiStructScope,
DevupPutApiStructKey,
DevupPutApiStructScope,
ExtractValue,
Middleware,
Expand Down Expand Up @@ -65,7 +61,7 @@ export class DevupApi<S extends ConditionalKeys<DevupApiServers>> {
}

get<
T extends DevupGetApiStructKey<S>,
T extends DevupApiMethodKey<S, 'get'>,
O extends Additional<T, DevupGetApiStructScope<S>>,
>(
path: T,
Expand All @@ -80,7 +76,7 @@ export class DevupApi<S extends ConditionalKeys<DevupApiServers>> {
}

GET<
T extends DevupGetApiStructKey<S>,
T extends DevupApiMethodKey<S, 'get'>,
O extends Additional<T, DevupGetApiStructScope<S>>,
>(
path: T,
Expand All @@ -95,7 +91,7 @@ export class DevupApi<S extends ConditionalKeys<DevupApiServers>> {
}

post<
T extends DevupPostApiStructKey<S>,
T extends DevupApiMethodKey<S, 'post'>,
O extends Additional<T, DevupPostApiStructScope<S>>,
>(
path: T,
Expand All @@ -110,7 +106,7 @@ export class DevupApi<S extends ConditionalKeys<DevupApiServers>> {
}

POST<
T extends DevupPostApiStructKey<S>,
T extends DevupApiMethodKey<S, 'post'>,
O extends Additional<T, DevupPostApiStructScope<S>>,
>(
path: T,
Expand All @@ -125,7 +121,7 @@ export class DevupApi<S extends ConditionalKeys<DevupApiServers>> {
}

put<
T extends DevupPutApiStructKey<S>,
T extends DevupApiMethodKey<S, 'put'>,
O extends Additional<T, DevupPutApiStructScope<S>>,
>(
path: T,
Expand All @@ -140,7 +136,7 @@ export class DevupApi<S extends ConditionalKeys<DevupApiServers>> {
}

PUT<
T extends DevupPutApiStructKey<S>,
T extends DevupApiMethodKey<S, 'put'>,
O extends Additional<T, DevupPutApiStructScope<S>>,
>(
path: T,
Expand All @@ -155,7 +151,7 @@ export class DevupApi<S extends ConditionalKeys<DevupApiServers>> {
}

delete<
T extends DevupDeleteApiStructKey<S>,
T extends DevupApiMethodKey<S, 'delete'>,
O extends Additional<T, DevupDeleteApiStructScope<S>>,
>(
path: T,
Expand All @@ -170,7 +166,7 @@ export class DevupApi<S extends ConditionalKeys<DevupApiServers>> {
}

DELETE<
T extends DevupDeleteApiStructKey<S>,
T extends DevupApiMethodKey<S, 'delete'>,
O extends Additional<T, DevupDeleteApiStructScope<S>>,
>(
path: T,
Expand All @@ -185,7 +181,7 @@ export class DevupApi<S extends ConditionalKeys<DevupApiServers>> {
}

patch<
T extends DevupPatchApiStructKey<S>,
T extends DevupApiMethodKey<S, 'patch'>,
O extends Additional<T, DevupPatchApiStructScope<S>>,
>(
path: T,
Expand All @@ -200,7 +196,7 @@ export class DevupApi<S extends ConditionalKeys<DevupApiServers>> {
}

PATCH<
T extends DevupPatchApiStructKey<S>,
T extends DevupApiMethodKey<S, 'patch'>,
O extends Additional<T, DevupPatchApiStructScope<S>>,
>(
path: T,
Expand Down
Loading