@hey-api/client-fetch
Advanced tools
Comparing version 0.2.0 to 0.2.1
@@ -41,3 +41,3 @@ type ArrayStyle = 'form' | 'spaceDelimited' | 'pipeDelimited'; | ||
type OmitKeys<T, K> = Pick<T, Exclude<keyof T, K>>; | ||
interface Config extends Omit<RequestInit, 'body' | 'headers' | 'method'> { | ||
interface Config<ThrowOnError extends boolean = false> extends Omit<RequestInit, 'body' | 'headers' | 'method'> { | ||
/** | ||
@@ -104,5 +104,5 @@ * Base URL for all requests made by this client. | ||
*/ | ||
throwOnError?: boolean; | ||
throwOnError?: ThrowOnError; | ||
} | ||
interface RequestOptionsBase extends Config { | ||
interface RequestOptionsBase<ThrowOnError extends boolean> extends Config<ThrowOnError> { | ||
path?: Record<string, unknown>; | ||
@@ -112,8 +112,12 @@ query?: Record<string, unknown>; | ||
} | ||
type RequestResult<Data = unknown, Error = unknown> = Promise<({ | ||
type RequestResult<ThrowOnError extends boolean, Data = unknown, TError = unknown> = ThrowOnError extends true ? Promise<{ | ||
data: Data; | ||
request: Request; | ||
response: Response; | ||
}> : Promise<({ | ||
data: Data; | ||
error: undefined; | ||
} | { | ||
data: undefined; | ||
error: Error; | ||
error: TError; | ||
}) & { | ||
@@ -123,4 +127,4 @@ request: Request; | ||
}>; | ||
type MethodFn = <Data = unknown, Error = unknown>(options: Omit<RequestOptionsBase, 'method'>) => RequestResult<Data, Error>; | ||
type RequestFn = <Data = unknown, Error = unknown>(options: Omit<RequestOptionsBase, 'method'> & Pick<Required<RequestOptionsBase>, 'method'>) => RequestResult<Data, Error>; | ||
type MethodFn = <ThrowOnError extends boolean, Data = unknown, TError = unknown>(options: Omit<RequestOptionsBase<ThrowOnError>, 'method'>) => RequestResult<ThrowOnError, Data, TError>; | ||
type RequestFn = <ThrowOnError extends boolean, Data = unknown, TError = unknown>(options: Omit<RequestOptionsBase<ThrowOnError>, 'method'> & Pick<Required<RequestOptionsBase<ThrowOnError>>, 'method'>) => RequestResult<ThrowOnError, Data, TError>; | ||
interface Client<Req = Request, Res = Response, Options = RequestOptions> { | ||
@@ -130,3 +134,3 @@ connect: MethodFn; | ||
get: MethodFn; | ||
getConfig: () => Config; | ||
getConfig: () => Config<false>; | ||
head: MethodFn; | ||
@@ -139,9 +143,9 @@ interceptors: Middleware<Req, Res, Options>; | ||
request: RequestFn; | ||
setConfig: (config: Config) => Config; | ||
setConfig: (config: Config<false>) => Config<false>; | ||
trace: MethodFn; | ||
} | ||
type RequestOptions = RequestOptionsBase & Config & { | ||
type RequestOptions = RequestOptionsBase<false> & Config<false> & { | ||
headers: Headers; | ||
}; | ||
type OptionsBase = Omit<RequestOptionsBase, 'url'> & { | ||
type OptionsBase<ThrowOnError extends boolean> = Omit<RequestOptionsBase<ThrowOnError>, 'url'> & { | ||
/** | ||
@@ -154,9 +158,9 @@ * You can provide a client instance returned by `createClient()` instead of | ||
}; | ||
type Options<T = unknown> = T extends { | ||
type Options<T = unknown, ThrowOnError extends boolean = false> = T extends { | ||
body?: any; | ||
} ? T extends { | ||
headers?: any; | ||
} ? OmitKeys<OptionsBase, 'body' | 'headers' | 'responseTransformer'> & T : OmitKeys<OptionsBase, 'body' | 'responseTransformer'> & T & Pick<OptionsBase, 'headers'> : T extends { | ||
} ? OmitKeys<OptionsBase<ThrowOnError>, 'body' | 'headers' | 'responseTransformer'> & T : OmitKeys<OptionsBase<ThrowOnError>, 'body' | 'responseTransformer'> & T & Pick<OptionsBase<ThrowOnError>, 'headers'> : T extends { | ||
headers?: any; | ||
} ? OmitKeys<OptionsBase, 'headers' | 'responseTransformer'> & T & Pick<OptionsBase, 'body'> : OptionsBase & T; | ||
} ? OmitKeys<OptionsBase<ThrowOnError>, 'headers' | 'responseTransformer'> & T & Pick<OptionsBase<ThrowOnError>, 'body'> : OptionsBase<ThrowOnError> & T; | ||
@@ -163,0 +167,0 @@ declare const createClient: (config?: Config) => Client; |
{ | ||
"name": "@hey-api/client-fetch", | ||
"version": "0.2.0", | ||
"version": "0.2.1", | ||
"type": "module", | ||
@@ -5,0 +5,0 @@ "description": "Typesafe Fetch API client for your @hey-api/openapi-ts types", |
@@ -31,2 +31,3 @@ import type { Client, Config, RequestOptions } from './types'; | ||
const request: Client['request'] = async (options) => { | ||
// @ts-ignore | ||
const opts: RequestOptions = { | ||
@@ -33,0 +34,0 @@ ..._config, |
@@ -10,3 +10,3 @@ import type { | ||
export interface Config | ||
export interface Config<ThrowOnError extends boolean = false> | ||
extends Omit<RequestInit, 'body' | 'headers' | 'method'> { | ||
@@ -99,6 +99,7 @@ /** | ||
*/ | ||
throwOnError?: boolean; | ||
throwOnError?: ThrowOnError; | ||
} | ||
interface RequestOptionsBase extends Config { | ||
export interface RequestOptionsBase<ThrowOnError extends boolean> | ||
extends Config<ThrowOnError> { | ||
path?: Record<string, unknown>; | ||
@@ -109,17 +110,38 @@ query?: Record<string, unknown>; | ||
export type RequestResult<Data = unknown, Error = unknown> = Promise< | ||
({ data: Data; error: undefined } | { data: undefined; error: Error }) & { | ||
request: Request; | ||
response: Response; | ||
} | ||
>; | ||
export type RequestResult< | ||
ThrowOnError extends boolean, | ||
Data = unknown, | ||
TError = unknown, | ||
> = ThrowOnError extends true | ||
? Promise<{ | ||
data: Data; | ||
request: Request; | ||
response: Response; | ||
}> | ||
: Promise< | ||
( | ||
| { data: Data; error: undefined } | ||
| { data: undefined; error: TError } | ||
) & { | ||
request: Request; | ||
response: Response; | ||
} | ||
>; | ||
type MethodFn = <Data = unknown, Error = unknown>( | ||
options: Omit<RequestOptionsBase, 'method'>, | ||
) => RequestResult<Data, Error>; | ||
type MethodFn = < | ||
ThrowOnError extends boolean, | ||
Data = unknown, | ||
TError = unknown, | ||
>( | ||
options: Omit<RequestOptionsBase<ThrowOnError>, 'method'>, | ||
) => RequestResult<ThrowOnError, Data, TError>; | ||
type RequestFn = <Data = unknown, Error = unknown>( | ||
options: Omit<RequestOptionsBase, 'method'> & | ||
Pick<Required<RequestOptionsBase>, 'method'>, | ||
) => RequestResult<Data, Error>; | ||
type RequestFn = < | ||
ThrowOnError extends boolean, | ||
Data = unknown, | ||
TError = unknown, | ||
>( | ||
options: Omit<RequestOptionsBase<ThrowOnError>, 'method'> & | ||
Pick<Required<RequestOptionsBase<ThrowOnError>>, 'method'>, | ||
) => RequestResult<ThrowOnError, Data, TError>; | ||
@@ -134,3 +156,3 @@ export interface Client< | ||
get: MethodFn; | ||
getConfig: () => Config; | ||
getConfig: () => Config<false>; | ||
head: MethodFn; | ||
@@ -143,12 +165,15 @@ interceptors: Middleware<Req, Res, Options>; | ||
request: RequestFn; | ||
setConfig: (config: Config) => Config; | ||
setConfig: (config: Config<false>) => Config<false>; | ||
trace: MethodFn; | ||
} | ||
export type RequestOptions = RequestOptionsBase & | ||
Config & { | ||
export type RequestOptions = RequestOptionsBase<false> & | ||
Config<false> & { | ||
headers: Headers; | ||
}; | ||
type OptionsBase = Omit<RequestOptionsBase, 'url'> & { | ||
type OptionsBase<ThrowOnError extends boolean> = Omit< | ||
RequestOptionsBase<ThrowOnError>, | ||
'url' | ||
> & { | ||
/** | ||
@@ -162,12 +187,19 @@ * You can provide a client instance returned by `createClient()` instead of | ||
export type Options<T = unknown> = T extends { body?: any } | ||
export type Options< | ||
T = unknown, | ||
ThrowOnError extends boolean = false, | ||
> = T extends { body?: any } | ||
? T extends { headers?: any } | ||
? OmitKeys<OptionsBase, 'body' | 'headers' | 'responseTransformer'> & T | ||
: OmitKeys<OptionsBase, 'body' | 'responseTransformer'> & | ||
? OmitKeys< | ||
OptionsBase<ThrowOnError>, | ||
'body' | 'headers' | 'responseTransformer' | ||
> & | ||
T | ||
: OmitKeys<OptionsBase<ThrowOnError>, 'body' | 'responseTransformer'> & | ||
T & | ||
Pick<OptionsBase, 'headers'> | ||
Pick<OptionsBase<ThrowOnError>, 'headers'> | ||
: T extends { headers?: any } | ||
? OmitKeys<OptionsBase, 'headers' | 'responseTransformer'> & | ||
? OmitKeys<OptionsBase<ThrowOnError>, 'headers' | 'responseTransformer'> & | ||
T & | ||
Pick<OptionsBase, 'body'> | ||
: OptionsBase & T; | ||
Pick<OptionsBase<ThrowOnError>, 'body'> | ||
: OptionsBase<ThrowOnError> & T; |
@@ -153,4 +153,8 @@ import type { Config } from './types'; | ||
}: SerializeOptions<ObjectSeparatorStyle> & { | ||
value: Record<string, unknown>; | ||
value: Record<string, unknown> | Date; | ||
}) => { | ||
if (value instanceof Date) { | ||
return value.toISOString(); | ||
} | ||
if (style !== 'deepObject' && !explode) { | ||
@@ -341,10 +345,5 @@ let values: string[] = []; | ||
if ( | ||
[ | ||
'application/octet-stream', | ||
'application/pdf', | ||
'application/zip', | ||
'audio/', | ||
'image/', | ||
'video/', | ||
].some((type) => content.includes(type)) | ||
['application/', 'audio/', 'image/', 'video/'].some((type) => | ||
content.startsWith(type), | ||
) | ||
) { | ||
@@ -354,3 +353,3 @@ return 'blob'; | ||
if (content.includes('text/')) { | ||
if (content.startsWith('text/')) { | ||
return 'text'; | ||
@@ -357,0 +356,0 @@ } |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
45509
1037