Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

[Client types] make args param optional if the input has no required keys #1618

Merged
merged 6 commits into from
Oct 26, 2023
Merged
Show file tree
Hide file tree
Changes from 2 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
12 changes: 8 additions & 4 deletions deno_dist/client/types.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { Hono } from '../hono.ts'
import type { Schema } from '../types.ts'
import type { Schema, HasRequiredKeys } from '../types.ts'
import type { RemoveBlankRecord } from '../utils/types.ts'

type HonoRequest = typeof Hono.prototype['request']
Expand All @@ -16,9 +16,13 @@ export type ClientRequestOptions<T = unknown> = keyof T extends never

type ClientRequest<S extends Schema> = {
[M in keyof S]: S[M] extends { input: infer R; output: infer O }
? RemoveBlankRecord<Required<R>> extends never
? (args?: {}, options?: ClientRequestOptions) => Promise<ClientResponse<O>>
: (args: R, options?: ClientRequestOptions) => Promise<ClientResponse<O>>
? R extends object
? RemoveBlankRecord<Required<R>> extends never
? (args?: {}, options?: ClientRequestOptions) => Promise<ClientResponse<O>>
: HasRequiredKeys<R> extends true
? (args: R, options?: ClientRequestOptions) => Promise<ClientResponse<O>>
: (args?: R, options?: ClientRequestOptions) => Promise<ClientResponse<O>>
: never
: never
} & {
$url: () => URL
Expand Down
11 changes: 11 additions & 0 deletions deno_dist/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -580,6 +580,17 @@ export type ExtractSchema<T> = UnionToIntersection<
T extends Hono<infer _, infer S, any> ? S : never
>

export type RequiredKeysOf<BaseType extends object> = Exclude<
{
[Key in keyof BaseType]: BaseType extends Record<Key, BaseType[Key]> ? Key : never
}[keyof BaseType],
undefined
>

export type HasRequiredKeys<BaseType extends object> = RequiredKeysOf<BaseType> extends never
? false
: true

////////////////////////////////////////
////// //////
////// FetchEvent //////
Expand Down
12 changes: 8 additions & 4 deletions src/client/types.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { Hono } from '../hono'
import type { Schema } from '../types'
import type { Schema, HasRequiredKeys } from '../types'
import type { RemoveBlankRecord } from '../utils/types'

type HonoRequest = typeof Hono.prototype['request']
Expand All @@ -16,9 +16,13 @@ export type ClientRequestOptions<T = unknown> = keyof T extends never

type ClientRequest<S extends Schema> = {
[M in keyof S]: S[M] extends { input: infer R; output: infer O }
? RemoveBlankRecord<Required<R>> extends never
? (args?: {}, options?: ClientRequestOptions) => Promise<ClientResponse<O>>
: (args: R, options?: ClientRequestOptions) => Promise<ClientResponse<O>>
? R extends object
? RemoveBlankRecord<Required<R>> extends never
? (args?: {}, options?: ClientRequestOptions) => Promise<ClientResponse<O>>
: HasRequiredKeys<R> extends true
? (args: R, options?: ClientRequestOptions) => Promise<ClientResponse<O>>
: (args?: R, options?: ClientRequestOptions) => Promise<ClientResponse<O>>
: never
: never
} & {
$url: () => URL
Expand Down
11 changes: 11 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -580,6 +580,17 @@ export type ExtractSchema<T> = UnionToIntersection<
T extends Hono<infer _, infer S, any> ? S : never
>

export type RequiredKeysOf<BaseType extends object> = Exclude<
{
[Key in keyof BaseType]: BaseType extends Record<Key, BaseType[Key]> ? Key : never
}[keyof BaseType],
undefined
>

export type HasRequiredKeys<BaseType extends object> = RequiredKeysOf<BaseType> extends never
? false
: true

////////////////////////////////////////
////// //////
////// FetchEvent //////
Expand Down