Skip to content

Commit ad7715b

Browse files
authored
fix(js-client): api client types - make passing params optional where 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
1 parent 0413449 commit ad7715b

File tree

1 file changed

+39
-7
lines changed

1 file changed

+39
-7
lines changed

packages/js-client/src/types.ts

Lines changed: 39 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,31 @@
11
import type { operations } from '@netlify/open-api'
22

3+
/**
4+
* Determines whether all keys in T are optional.
5+
*/
6+
type AreAllOptional<T> = keyof T extends never // If T has no keys, it's considered optional
7+
? true
8+
: T extends Record<string, any>
9+
? { [K in keyof T]-?: undefined extends T[K] ? never : K }[keyof T] extends never
10+
? true
11+
: false
12+
: false
13+
14+
/**
15+
* Determines whether `path` and `query` are both optional.
16+
*/
17+
type IsPathAndQueryOptional<K extends keyof operations> = 'parameters' extends keyof operations[K]
18+
? AreAllOptional<
19+
'path' extends keyof operations[K]['parameters'] ? operations[K]['parameters']['path'] : object
20+
> extends true
21+
? AreAllOptional<
22+
'query' extends keyof operations[K]['parameters'] ? operations[K]['parameters']['query'] : object
23+
> extends true
24+
? true
25+
: false
26+
: false
27+
: true
28+
329
/**
430
* Converts snake_case to camelCase for TypeScript types.
531
*/
@@ -13,26 +39,32 @@ type SnakeToCamel<T> = {
1339
}
1440

1541
/**
16-
* Combines snake_case and camelCase parameters.
42+
* Combines snake_case and camelCase parameters into one Params type.
1743
*/
18-
type CombinedCaseParams<T> = SnakeToCamel<T> | T
44+
type Params<T> = SnakeToCamel<T> | T
1945

2046
/**
21-
* Combines `path` and `query` parameters into a single type.
47+
* Extracts and combines `path` and `query` parameters into a single type.
2248
*/
23-
type OperationParams<K extends keyof operations> = 'parameters' extends keyof operations[K]
49+
type ExtractPathAndQueryParameters<K extends keyof operations> = 'parameters' extends keyof operations[K]
2450
? 'path' extends keyof operations[K]['parameters']
2551
? 'query' extends keyof operations[K]['parameters']
26-
? CombinedCaseParams<
52+
? Params<
2753
Omit<operations[K]['parameters']['path'], keyof operations[K]['parameters']['query']> &
2854
operations[K]['parameters']['query']
2955
>
30-
: CombinedCaseParams<operations[K]['parameters']['path']>
56+
: Params<operations[K]['parameters']['path']>
3157
: 'query' extends keyof operations[K]['parameters']
32-
? CombinedCaseParams<operations[K]['parameters']['query']>
58+
? Params<operations[K]['parameters']['query']>
3359
: undefined
3460
: undefined
3561

62+
type OperationParams<K extends keyof operations> = 'parameters' extends keyof operations[K]
63+
? IsPathAndQueryOptional<K> extends true
64+
? ExtractPathAndQueryParameters<K> | void
65+
: ExtractPathAndQueryParameters<K>
66+
: void
67+
3668
type SuccessHttpStatusCodes = 200 | 201 | 202 | 203 | 204 | 205 | 206 | 207 | 208 | 226
3769
/**
3870
* Extracts the response type from the operation.

0 commit comments

Comments
 (0)