@@ -3,14 +3,14 @@ import type {
33 FilterKeys ,
44 HttpMethod ,
55 IsOperationRequestBodyOptional ,
6+ MaybeReadable ,
7+ MaybeWritable ,
68 MediaType ,
79 OperationRequestBodyContent ,
810 PathsWithMethod ,
9- Readable ,
1011 RequiredKeysOf ,
1112 ResponseObjectMap ,
1213 SuccessResponse ,
13- Writable ,
1414} from "openapi-typescript-helpers" ;
1515
1616/** Options for each client instance */
@@ -98,31 +98,37 @@ export type ParamsOption<T> = T extends {
9898 : { params : T [ "parameters" ] }
9999 : DefaultParamsOption ;
100100
101- // Writable <T> strips $Read markers (readOnly properties excluded from request body)
102- export type RequestBodyOption < T > =
103- Writable < OperationRequestBodyContent < T > > extends never
101+ // MaybeWritable <T> strips $Read markers when Markers is true (readOnly properties excluded from request body)
102+ export type RequestBodyOption < T , Markers extends boolean = false > =
103+ MaybeWritable < OperationRequestBodyContent < T > , Markers > extends never
104104 ? { body ?: never }
105105 : IsOperationRequestBodyOptional < T > extends true
106- ? { body ?: Writable < OperationRequestBodyContent < T > > }
107- : { body : Writable < OperationRequestBodyContent < T > > } ;
106+ ? { body ?: MaybeWritable < OperationRequestBodyContent < T > , Markers > }
107+ : { body : MaybeWritable < OperationRequestBodyContent < T > , Markers > } ;
108108
109- export type FetchOptions < T > = RequestOptions < T > & Omit < RequestInit , "body" | "headers" > ;
109+ export type FetchOptions < T , Markers extends boolean = false > = RequestOptions < T , Markers > &
110+ Omit < RequestInit , "body" | "headers" > ;
110111
111- // Readable<T> strips $Write markers (writeOnly properties excluded from response)
112- export type FetchResponse < T extends Record < string | number , any > , Options , Media extends MediaType > =
112+ // MaybeReadable<T> strips $Write markers when Markers is true (writeOnly properties excluded from response)
113+ export type FetchResponse <
114+ T extends Record < string | number , any > ,
115+ Options ,
116+ Media extends MediaType ,
117+ Markers extends boolean = false ,
118+ > =
113119 | {
114- data : ParseAsResponse < Readable < SuccessResponse < ResponseObjectMap < T > , Media > > , Options > ;
120+ data : ParseAsResponse < MaybeReadable < SuccessResponse < ResponseObjectMap < T > , Media > , Markers > , Options > ;
115121 error ?: never ;
116122 response : Response ;
117123 }
118124 | {
119125 data ?: never ;
120- error : Readable < ErrorResponse < ResponseObjectMap < T > , Media > > ;
126+ error : MaybeReadable < ErrorResponse < ResponseObjectMap < T > , Media > , Markers > ;
121127 response : Response ;
122128 } ;
123129
124- export type RequestOptions < T > = ParamsOption < T > &
125- RequestBodyOption < T > & {
130+ export type RequestOptions < T , Markers extends boolean = false > = ParamsOption < T > &
131+ RequestBodyOption < T , Markers > & {
126132 baseUrl ?: string ;
127133 querySerializer ?: QuerySerializer < T > | QuerySerializerOptions ;
128134 bodySerializer ?: BodySerializer < T > ;
@@ -190,10 +196,10 @@ export type Middleware =
190196 } ;
191197
192198/** This type helper makes the 2nd function param required if params/requestBody are required; otherwise, optional */
193- export type MaybeOptionalInit < Params , Location extends keyof Params > =
194- RequiredKeysOf < FetchOptions < FilterKeys < Params , Location > > > extends never
195- ? FetchOptions < FilterKeys < Params , Location > > | undefined
196- : FetchOptions < FilterKeys < Params , Location > > ;
199+ export type MaybeOptionalInit < Params , Location extends keyof Params , Markers extends boolean = false > =
200+ RequiredKeysOf < FetchOptions < FilterKeys < Params , Location > , Markers > > extends never
201+ ? FetchOptions < FilterKeys < Params , Location > , Markers > | undefined
202+ : FetchOptions < FilterKeys < Params , Location > , Markers > ;
197203
198204// The final init param to accept.
199205// - Determines if the param is optional or not.
@@ -206,79 +212,104 @@ export type ClientMethod<
206212 Paths extends Record < string , Record < HttpMethod , { } > > ,
207213 Method extends HttpMethod ,
208214 Media extends MediaType ,
209- > = < Path extends PathsWithMethod < Paths , Method > , Init extends MaybeOptionalInit < Paths [ Path ] , Method > > (
215+ Markers extends boolean = false ,
216+ > = < Path extends PathsWithMethod < Paths , Method > , Init extends MaybeOptionalInit < Paths [ Path ] , Method , Markers > > (
210217 url : Path ,
211218 ...init : InitParam < Init >
212- ) => Promise < FetchResponse < Paths [ Path ] [ Method ] , Init , Media > > ;
219+ ) => Promise < FetchResponse < Paths [ Path ] [ Method ] , Init , Media , Markers > > ;
213220
214- export type ClientRequestMethod < Paths extends Record < string , Record < HttpMethod , { } > > , Media extends MediaType > = <
221+ export type ClientRequestMethod <
222+ Paths extends Record < string , Record < HttpMethod , { } > > ,
223+ Media extends MediaType ,
224+ Markers extends boolean = false ,
225+ > = <
215226 Method extends HttpMethod ,
216227 Path extends PathsWithMethod < Paths , Method > ,
217- Init extends MaybeOptionalInit < Paths [ Path ] , Method > ,
228+ Init extends MaybeOptionalInit < Paths [ Path ] , Method , Markers > ,
218229> (
219230 method : Method ,
220231 url : Path ,
221232 ...init : InitParam < Init >
222- ) => Promise < FetchResponse < Paths [ Path ] [ Method ] , Init , Media > > ;
233+ ) => Promise < FetchResponse < Paths [ Path ] [ Method ] , Init , Media , Markers > > ;
223234
224- export type ClientForPath < PathInfo extends Record < string | number , any > , Media extends MediaType > = {
225- [ Method in keyof PathInfo as Uppercase < string & Method > ] : < Init extends MaybeOptionalInit < PathInfo , Method > > (
235+ export type ClientForPath <
236+ PathInfo extends Record < string | number , any > ,
237+ Media extends MediaType ,
238+ Markers extends boolean = false ,
239+ > = {
240+ [ Method in keyof PathInfo as Uppercase < string & Method > ] : <
241+ Init extends MaybeOptionalInit < PathInfo , Method , Markers > ,
242+ > (
226243 ...init : InitParam < Init >
227- ) => Promise < FetchResponse < PathInfo [ Method ] , Init , Media > > ;
244+ ) => Promise < FetchResponse < PathInfo [ Method ] , Init , Media , Markers > > ;
228245} ;
229246
230- export interface Client < Paths extends { } , Media extends MediaType = MediaType > {
231- request : ClientRequestMethod < Paths , Media > ;
247+ export interface Client < Paths extends { } , Media extends MediaType = MediaType , Markers extends boolean = false > {
248+ request : ClientRequestMethod < Paths , Media , Markers > ;
232249 /** Call a GET endpoint */
233- GET : ClientMethod < Paths , "get" , Media > ;
250+ GET : ClientMethod < Paths , "get" , Media , Markers > ;
234251 /** Call a PUT endpoint */
235- PUT : ClientMethod < Paths , "put" , Media > ;
252+ PUT : ClientMethod < Paths , "put" , Media , Markers > ;
236253 /** Call a POST endpoint */
237- POST : ClientMethod < Paths , "post" , Media > ;
254+ POST : ClientMethod < Paths , "post" , Media , Markers > ;
238255 /** Call a DELETE endpoint */
239- DELETE : ClientMethod < Paths , "delete" , Media > ;
256+ DELETE : ClientMethod < Paths , "delete" , Media , Markers > ;
240257 /** Call a OPTIONS endpoint */
241- OPTIONS : ClientMethod < Paths , "options" , Media > ;
258+ OPTIONS : ClientMethod < Paths , "options" , Media , Markers > ;
242259 /** Call a HEAD endpoint */
243- HEAD : ClientMethod < Paths , "head" , Media > ;
260+ HEAD : ClientMethod < Paths , "head" , Media , Markers > ;
244261 /** Call a PATCH endpoint */
245- PATCH : ClientMethod < Paths , "patch" , Media > ;
262+ PATCH : ClientMethod < Paths , "patch" , Media , Markers > ;
246263 /** Call a TRACE endpoint */
247- TRACE : ClientMethod < Paths , "trace" , Media > ;
264+ TRACE : ClientMethod < Paths , "trace" , Media , Markers > ;
248265 /** Register middleware */
249266 use ( ...middleware : Middleware [ ] ) : void ;
250267 /** Unregister middleware */
251268 eject ( ...middleware : Middleware [ ] ) : void ;
252269}
253270
254- export type ClientPathsWithMethod < CreatedClient extends Client < any , any > , Method extends HttpMethod > =
255- CreatedClient extends Client < infer Paths , infer _Media > ? PathsWithMethod < Paths , Method > : never ;
271+ export type ClientPathsWithMethod < CreatedClient extends Client < any , any , any > , Method extends HttpMethod > =
272+ CreatedClient extends Client < infer Paths , infer _Media , infer _Markers > ? PathsWithMethod < Paths , Method > : never ;
256273
257274export type MethodResponse <
258- CreatedClient extends Client < any , any > ,
275+ CreatedClient extends Client < any , any , any > ,
259276 Method extends HttpMethod ,
260277 Path extends ClientPathsWithMethod < CreatedClient , Method > ,
261278 Options = { } ,
262279> =
263- CreatedClient extends Client < infer Paths extends { [ key : string ] : any } , infer Media extends MediaType >
264- ? NonNullable < FetchResponse < Paths [ Path ] [ Method ] , Options , Media > [ "data" ] >
280+ CreatedClient extends Client <
281+ infer Paths extends { [ key : string ] : any } ,
282+ infer Media extends MediaType ,
283+ infer Markers extends boolean
284+ >
285+ ? NonNullable < FetchResponse < Paths [ Path ] [ Method ] , Options , Media , Markers > [ "data" ] >
265286 : never ;
266287
267- export default function createClient < Paths extends { } , Media extends MediaType = MediaType > (
268- clientOptions ?: ClientOptions ,
269- ) : Client < Paths , Media > ;
270-
271- export type PathBasedClient < Paths extends Record < string | number , any > , Media extends MediaType = MediaType > = {
272- [ Path in keyof Paths ] : ClientForPath < Paths [ Path ] , Media > ;
288+ export default function createClient <
289+ Paths extends { } ,
290+ Media extends MediaType = MediaType ,
291+ Markers extends boolean = false ,
292+ > ( clientOptions ?: ClientOptions ) : Client < Paths , Media , Markers > ;
293+
294+ export type PathBasedClient <
295+ Paths extends Record < string | number , any > ,
296+ Media extends MediaType = MediaType ,
297+ Markers extends boolean = false ,
298+ > = {
299+ [ Path in keyof Paths ] : ClientForPath < Paths [ Path ] , Media , Markers > ;
273300} ;
274301
275- export declare function wrapAsPathBasedClient < Paths extends { } , Media extends MediaType = MediaType > (
276- client : Client < Paths , Media > ,
277- ) : PathBasedClient < Paths , Media > ;
278-
279- export declare function createPathBasedClient < Paths extends { } , Media extends MediaType = MediaType > (
280- clientOptions ?: ClientOptions ,
281- ) : PathBasedClient < Paths , Media > ;
302+ export declare function wrapAsPathBasedClient <
303+ Paths extends { } ,
304+ Media extends MediaType = MediaType ,
305+ Markers extends boolean = false ,
306+ > ( client : Client < Paths , Media , Markers > ) : PathBasedClient < Paths , Media , Markers > ;
307+
308+ export declare function createPathBasedClient <
309+ Paths extends { } ,
310+ Media extends MediaType = MediaType ,
311+ Markers extends boolean = false ,
312+ > ( clientOptions ?: ClientOptions ) : PathBasedClient < Paths , Media , Markers > ;
282313
283314/** Serialize primitive params to string */
284315export declare function serializePrimitiveParam (
0 commit comments