Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

fix(js-client): handle type definitions for request body params #6018

Merged
merged 4 commits into from
Jan 16, 2025
Merged
Show file tree
Hide file tree
Changes from all 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
11 changes: 11 additions & 0 deletions packages/js-client/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,17 @@ type APIOptions = {
globalParams?: Record<string, unknown>
}

/**
* The Netlify API client.
* @see {@link https://open-api.netlify.com | Open API Reference}
* @see {@link https://docs.netlify.com/api/get-started | Online Documentation}
*
* @example
* ```ts
* const client = new NetlifyAPI('YOUR_ACCESS_TOKEN')
* const sites = await client.listSites()
* ```
*/
// eslint-disable-next-line @typescript-eslint/no-empty-interface -- NetlifyAPI is a class and the interface just inherits mapped types
export interface NetlifyAPI extends DynamicMethods {}

Expand Down
128 changes: 124 additions & 4 deletions packages/js-client/src/types.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import type { ReadStream } from 'node:fs'

import type { operations } from '@netlify/open-api'

/**
Expand Down Expand Up @@ -43,6 +45,117 @@ type SnakeToCamel<T> = {
*/
type Params<T> = SnakeToCamel<T> | T

type HasRequestBody<K extends keyof operations> = 'requestBody' extends keyof operations[K]
? operations[K]['requestBody'] extends { content: any }
? 'application/json' extends keyof operations[K]['requestBody']['content']
? true
: 'application/octet-stream' extends keyof operations[K]['requestBody']['content']
? true
: false
: false
: false

/**
* Extracts the request body type from the operation.
*/
type RequestBody<K extends keyof operations> = 'requestBody' extends keyof operations[K]
? operations[K]['requestBody'] extends { content: any }
? 'application/json' extends keyof operations[K]['requestBody']['content']
? operations[K]['requestBody']['content']['application/json']
: 'application/octet-stream' extends keyof operations[K]['requestBody']['content']
? ReadStream | (() => ReadStream)
: never
: never
: never

type IsRequestBodyJson<K extends keyof operations> = 'requestBody' extends keyof operations[K]
? operations[K]['requestBody'] extends { content: any }
? 'application/json' extends keyof operations[K]['requestBody']['content']
? true
: false
: false
: false

type IsRequestBodyOctetStream<K extends keyof operations> = 'requestBody' extends keyof operations[K]
? operations[K]['requestBody'] extends { content: any }
? 'application/octet-stream' extends keyof operations[K]['requestBody']['content']
? true
: false
: false
: false

type RequestBodyParam<K extends keyof operations> =
HasRequestBody<K> extends true
? IsRequestBodyOptional<K> extends true
? DetailedRequestBodyOptional<K>
: DetailedRequestBody<K>
: never

type DetailedRequestBody<K extends keyof operations> =
IsRequestBodyJson<K> extends true
? {
/**
* The request body for `application/json`.
* Automatically serialized to JSON based on the operation.
* Can be a JSON object or a function returning one.
*/
body: RequestBody<K> | (() => RequestBody<K>)
}
: IsRequestBodyOctetStream<K> extends true
? {
/**
* The request body for `application/octet-stream`.
* Can be a Node.js readable stream or a function returning one
* @example
* fs.createReadStream('./file')
* @example
* () => fs.createReadStream('./file')
*/
body: ReadStream | (() => ReadStream)
}
: never

type DetailedRequestBodyOptional<K extends keyof operations> =
IsRequestBodyJson<K> extends true
? {
/**
* The request body for `application/json`.
* Automatically serialized to JSON based on the operation.
* Can be a JSON object or a function returning one.
*/
body?: RequestBody<K> | (() => RequestBody<K>)
}
: IsRequestBodyOctetStream<K> extends true
? {
/**
* The request body for `application/octet-stream`.
* Can be a Node.js readable stream or a function returning one
* @example
* fs.createReadStream('./file')
* @example
* () => fs.createReadStream('./file')
*/
body?: ReadStream | (() => ReadStream)
}
: never

/**
* Determines whether all properties in the request body are optional.
*/
type IsRequestBodyOptional<K extends keyof operations> =
HasRequestBody<K> extends true ? (AreAllOptional<RequestBody<K>> extends true ? true : false) : true

/**
* Determines whether any parameters or request body are required.
*/
type IsParamsOrRequestBodyRequired<K extends keyof operations> = 'parameters' extends keyof operations[K]
? IsPathAndQueryOptional<K> extends true
? IsRequestBodyOptional<K> extends true
? false
: true
: true
: false

/**
* Extracts and combines `path` and `query` parameters into a single type.
*/
Expand All @@ -59,11 +172,18 @@ type ExtractPathAndQueryParameters<K extends keyof operations> = 'parameters' ex
: undefined
: undefined

type OperationParams<K extends keyof operations> = 'parameters' extends keyof operations[K]
? IsPathAndQueryOptional<K> extends true
? ExtractPathAndQueryParameters<K> | void
/**
* Combines path, query, and request body parameters into a single type.
*/
type CombinedParamsAndRequestBody<K extends keyof operations> =
HasRequestBody<K> extends true
? ExtractPathAndQueryParameters<K> & RequestBodyParam<K>
: ExtractPathAndQueryParameters<K>
: void

type OperationParams<K extends keyof operations> =
IsParamsOrRequestBodyRequired<K> extends false
? CombinedParamsAndRequestBody<K> | void
: CombinedParamsAndRequestBody<K>

type SuccessHttpStatusCodes = 200 | 201 | 202 | 203 | 204 | 205 | 206 | 207 | 208 | 226
/**
Expand Down
Loading