Skip to content

Commit 02af82a

Browse files
authored
fix(js-client): add type definitions for operation method opts argument (#6021)
* fix(js-client): handle type definitions for requestBody Handling type definitions for requestBody in API schema while still taking into consideration optional / void params if all path, query, and requestBody keys are optional. * fix(js-client): add types for request body json and octet stream - combined the request body type with the Params type - handle cases where request body properties are optional - handle cases where all params and request body are optional - allow 'application/json' body to be a JSON object or a function returning one - allow 'application/octet-stream' body to be a Node.js ReadStream or a function returning one - simplify by extracting repetitive logic to helper types like `IsParamsOrRequestBodyRequired` and `IsRequestBodyOptional` etc. * refactor(js-client): rename RequestBodyDecorator to DetailedRequestBody for clarity These types are responsible for adding detailed annotations to the `body` parameter of API methods, such as describing its usage and examples for `application/json` and `application/octet-stream`. The new names make their purpose more explicit and improve readability for future maintenance and usage. * fix(js-client): add method opts type definition All dynamic operation methods can have a second arg `opts` to pass any additional properties to `node-fetch` RequestInit. This adds the type definition for the `opts` argument. * fix(js-client): remove export for type OperationParams added this export by accident, just removing it
1 parent e2e9114 commit 02af82a

File tree

1 file changed

+29
-2
lines changed

1 file changed

+29
-2
lines changed

Diff for: packages/js-client/src/types.ts

+29-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import type { ReadStream } from 'node:fs'
22

33
import type { operations } from '@netlify/open-api'
4+
import type { RequestInit } from 'node-fetch'
45

56
/**
67
* Determines whether all keys in T are optional.
@@ -182,7 +183,7 @@ type CombinedParamsAndRequestBody<K extends keyof operations> =
182183

183184
type OperationParams<K extends keyof operations> =
184185
IsParamsOrRequestBodyRequired<K> extends false
185-
? CombinedParamsAndRequestBody<K> | void
186+
? CombinedParamsAndRequestBody<K> | void | undefined
186187
: CombinedParamsAndRequestBody<K>
187188

188189
type SuccessHttpStatusCodes = 200 | 201 | 202 | 203 | 204 | 205 | 206 | 207 | 208 | 226
@@ -202,5 +203,31 @@ type OperationResponse<K extends keyof operations> = 'responses' extends keyof o
202203
: never
203204

204205
export type DynamicMethods = {
205-
[K in keyof operations]: (params: OperationParams<K>) => Promise<OperationResponse<K>>
206+
[K in keyof operations]: (
207+
params: OperationParams<K>,
208+
/**
209+
* Any properties you want passed to `node-fetch`.
210+
*
211+
* The `headers` property is merged with some `defaultHeaders`:
212+
* ```ts
213+
* {
214+
* 'User-agent': 'netlify-js-client',
215+
* 'accept': 'application/json',
216+
* }
217+
* ```
218+
*
219+
* @example
220+
* ```ts
221+
* const site = await client.getSite(
222+
* { site_id: 'YOUR_SITE_ID' },
223+
* {
224+
* headers: {
225+
* 'X-Example-Header': 'Example Value',
226+
* },
227+
* },
228+
* )
229+
* ```
230+
*/
231+
opts?: RequestInit | void | undefined,
232+
) => Promise<OperationResponse<K>>
206233
}

0 commit comments

Comments
 (0)