Skip to content

Commit

Permalink
fix(js-client): api client types - make passing params optional where…
Browse files Browse the repository at this point in the history
… necessary (#6017)

- add type AreAllOptional to check if every key on a type is optional
- add IsPathAndQueryOptional to check if all params on a method are
optional
- allow passing nothing (void) or undefined for method params when there
are no required params
- simplify OperationParams / move some logic to
ExtractPathAndQueryParameters to avoid repeating
  • Loading branch information
CalebBarnes authored Jan 16, 2025
1 parent 0413449 commit ad7715b
Showing 1 changed file with 39 additions and 7 deletions.
46 changes: 39 additions & 7 deletions packages/js-client/src/types.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,31 @@
import type { operations } from '@netlify/open-api'

/**
* Determines whether all keys in T are optional.
*/
type AreAllOptional<T> = keyof T extends never // If T has no keys, it's considered optional
? true
: T extends Record<string, any>
? { [K in keyof T]-?: undefined extends T[K] ? never : K }[keyof T] extends never
? true
: false
: false

/**
* Determines whether `path` and `query` are both optional.
*/
type IsPathAndQueryOptional<K extends keyof operations> = 'parameters' extends keyof operations[K]
? AreAllOptional<
'path' extends keyof operations[K]['parameters'] ? operations[K]['parameters']['path'] : object
> extends true
? AreAllOptional<
'query' extends keyof operations[K]['parameters'] ? operations[K]['parameters']['query'] : object
> extends true
? true
: false
: false
: true

/**
* Converts snake_case to camelCase for TypeScript types.
*/
Expand All @@ -13,26 +39,32 @@ type SnakeToCamel<T> = {
}

/**
* Combines snake_case and camelCase parameters.
* Combines snake_case and camelCase parameters into one Params type.
*/
type CombinedCaseParams<T> = SnakeToCamel<T> | T
type Params<T> = SnakeToCamel<T> | T

/**
* Combines `path` and `query` parameters into a single type.
* Extracts and combines `path` and `query` parameters into a single type.
*/
type OperationParams<K extends keyof operations> = 'parameters' extends keyof operations[K]
type ExtractPathAndQueryParameters<K extends keyof operations> = 'parameters' extends keyof operations[K]
? 'path' extends keyof operations[K]['parameters']
? 'query' extends keyof operations[K]['parameters']
? CombinedCaseParams<
? Params<
Omit<operations[K]['parameters']['path'], keyof operations[K]['parameters']['query']> &
operations[K]['parameters']['query']
>
: CombinedCaseParams<operations[K]['parameters']['path']>
: Params<operations[K]['parameters']['path']>
: 'query' extends keyof operations[K]['parameters']
? CombinedCaseParams<operations[K]['parameters']['query']>
? Params<operations[K]['parameters']['query']>
: undefined
: undefined

type OperationParams<K extends keyof operations> = 'parameters' extends keyof operations[K]
? IsPathAndQueryOptional<K> extends true
? ExtractPathAndQueryParameters<K> | void
: ExtractPathAndQueryParameters<K>
: void

type SuccessHttpStatusCodes = 200 | 201 | 202 | 203 | 204 | 205 | 206 | 207 | 208 | 226
/**
* Extracts the response type from the operation.
Expand Down

0 comments on commit ad7715b

Please sign in to comment.