Skip to content

Commit

Permalink
fix(client/types): make args param optional if the input has no requi…
Browse files Browse the repository at this point in the history
…red keys (#1618)

* feat(types.ts): add RequiredKeysOf and HasRequiredKeys utility types

* fix(client/types.ts): make args param optional if input has no required keys

* chore(types.ts): move RequiredKeysOf and HasRequiredKeys types to utils/types.ts

* chore(types.ts): rewrite ClientRequest type

* chore(types.ts): fix formatting

* chore(types.ts): denoify
  • Loading branch information
MonsterDeveloper authored Oct 26, 2023
1 parent 046767a commit a654842
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 8 deletions.
10 changes: 6 additions & 4 deletions deno_dist/client/types.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { Hono } from '../hono.ts'
import type { Schema } from '../types.ts'
import type { RemoveBlankRecord } from '../utils/types.ts'
import type { HasRequiredKeys } from '../utils/types.ts'

type HonoRequest = typeof Hono.prototype['request']

Expand All @@ -16,9 +16,11 @@ 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
? 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/utils/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,14 @@ export type JSONObject = { [key: string]: JSONPrimitive | JSONArray | JSONObject
export type JSONValue = JSONObject | JSONArray | JSONPrimitive

export type InterfaceToType<T> = T extends Function ? T : { [K in keyof T]: InterfaceToType<T[K]> }

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
10 changes: 6 additions & 4 deletions src/client/types.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { Hono } from '../hono'
import type { Schema } from '../types'
import type { RemoveBlankRecord } from '../utils/types'
import type { HasRequiredKeys } from '../utils/types'

type HonoRequest = typeof Hono.prototype['request']

Expand All @@ -16,9 +16,11 @@ 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
? 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/utils/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,14 @@ export type JSONObject = { [key: string]: JSONPrimitive | JSONArray | JSONObject
export type JSONValue = JSONObject | JSONArray | JSONPrimitive

export type InterfaceToType<T> = T extends Function ? T : { [K in keyof T]: InterfaceToType<T[K]> }

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

0 comments on commit a654842

Please # to comment.