-
Notifications
You must be signed in to change notification settings - Fork 59
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(js-client): add type definitions (#6009)
* feat(js-client): add type definitions Add type definitions for dynamic client methods by defining the NetlifyAPI class as an interface that inherits mapped types. * feat(js-client): treat path params and query params as one object The api client methods will assign these to the correct places but just accept the params as a single object. * fix(js-client): update accessToken type to allow undefined The api response for accessToken can possibly be undefined but we were only allowing string or null before. * feat(js-client): handle snake_case and camelCase params in type definitions Since the API client allows using camelCased params we need to allow these to keep backwards compatibility.
- Loading branch information
1 parent
a6d31ce
commit 9894c6b
Showing
2 changed files
with
61 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import type { operations } from '@netlify/open-api' | ||
|
||
/** | ||
* Converts snake_case to camelCase for TypeScript types. | ||
*/ | ||
type CamelCase<S extends string> = S extends `${infer T}_${infer U}` ? `${T}${Capitalize<CamelCase<U>>}` : S | ||
|
||
/** | ||
* Creates a union of both snake_case and camelCase keys with their respective types. | ||
*/ | ||
type SnakeToCamel<T> = { | ||
[K in keyof T as CamelCase<K & string>]: T[K] | ||
} | ||
|
||
/** | ||
* Combines snake_case and camelCase parameters. | ||
*/ | ||
type CombinedCaseParams<T> = SnakeToCamel<T> | T | ||
|
||
/** | ||
* Combines `path` and `query` parameters into a single type. | ||
*/ | ||
type OperationParams<K extends keyof operations> = 'parameters' extends keyof operations[K] | ||
? 'path' extends keyof operations[K]['parameters'] | ||
? 'query' extends keyof operations[K]['parameters'] | ||
? CombinedCaseParams< | ||
Omit<operations[K]['parameters']['path'], keyof operations[K]['parameters']['query']> & | ||
operations[K]['parameters']['query'] | ||
> | ||
: CombinedCaseParams<operations[K]['parameters']['path']> | ||
: 'query' extends keyof operations[K]['parameters'] | ||
? CombinedCaseParams<operations[K]['parameters']['query']> | ||
: undefined | ||
: undefined | ||
|
||
type SuccessHttpStatusCodes = 200 | 201 | 202 | 203 | 204 | 205 | 206 | 207 | 208 | 226 | ||
/** | ||
* Extracts the response type from the operation. | ||
*/ | ||
type OperationResponse<K extends keyof operations> = 'responses' extends keyof operations[K] | ||
? SuccessHttpStatusCodes extends infer StatusKeys | ||
? StatusKeys extends keyof operations[K]['responses'] | ||
? 'content' extends keyof operations[K]['responses'][StatusKeys] | ||
? 'application/json' extends keyof operations[K]['responses'][StatusKeys]['content'] | ||
? operations[K]['responses'][StatusKeys]['content']['application/json'] | ||
: never | ||
: never | ||
: never | ||
: never | ||
: never | ||
|
||
export type DynamicMethods = { | ||
[K in keyof operations]: (params: OperationParams<K>) => Promise<OperationResponse<K>> | ||
} |