@hey-api/client-fetch
Advanced tools
Comparing version 0.1.14 to 0.2.0
@@ -38,2 +38,3 @@ type ArrayStyle = 'form' | 'spaceDelimited' | 'pipeDelimited'; | ||
}; | ||
declare const createConfig: (override?: Config) => Config; | ||
@@ -65,8 +66,2 @@ type OmitKeys<T, K> = Pick<T, Exclude<keyof T, K>>; | ||
/** | ||
* By default, options passed to this call will be applied to the global | ||
* client instance. Set to false to create a local client instance. | ||
* @default true | ||
*/ | ||
global?: boolean; | ||
/** | ||
* An object containing any HTTP headers that you want to pre-populate your | ||
@@ -106,4 +101,9 @@ * `Headers` object with. | ||
responseTransformer?: (data: unknown) => Promise<unknown>; | ||
/** | ||
* Throw an error instead of returning it in the response? | ||
* @default false | ||
*/ | ||
throwOnError?: boolean; | ||
} | ||
interface RequestOptionsBase extends Omit<Config, 'global'> { | ||
interface RequestOptionsBase extends Config { | ||
path?: Record<string, unknown>; | ||
@@ -123,5 +123,5 @@ query?: Record<string, unknown>; | ||
}>; | ||
type MethodFn = <Data = unknown, Error = unknown>(options: RequestOptionsBase) => RequestResult<Data, Error>; | ||
type RequestFn = <Data = unknown, Error = unknown>(options: RequestOptionsBase & Pick<Required<RequestOptionsBase>, 'method'>) => RequestResult<Data, Error>; | ||
interface ClientBase<Request = unknown, Response = unknown, Options = unknown> { | ||
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>; | ||
interface Client<Req = Request, Res = Response, Options = RequestOptions> { | ||
connect: MethodFn; | ||
@@ -132,3 +132,3 @@ delete: MethodFn; | ||
head: MethodFn; | ||
interceptors: Middleware<Request, Response, Options>; | ||
interceptors: Middleware<Req, Res, Options>; | ||
options: MethodFn; | ||
@@ -139,2 +139,3 @@ patch: MethodFn; | ||
request: RequestFn; | ||
setConfig: (config: Config) => Config; | ||
trace: MethodFn; | ||
@@ -145,3 +146,2 @@ } | ||
}; | ||
type Client = ClientBase<Request, Response, RequestOptions>; | ||
type OptionsBase = Omit<RequestOptionsBase, 'url'> & { | ||
@@ -163,5 +163,4 @@ /** | ||
declare const createClient: (config: Config) => Client; | ||
declare const client: Client; | ||
declare const createClient: (config?: Config) => Client; | ||
export { type Client, type Config, type Options, type RequestResult, client, createClient, formDataBodySerializer, jsonBodySerializer, urlSearchParamsBodySerializer }; | ||
export { type Client, type Config, type Options, type RequestResult, createClient, createConfig, formDataBodySerializer, jsonBodySerializer, urlSearchParamsBodySerializer }; |
{ | ||
"name": "@hey-api/client-fetch", | ||
"version": "0.1.14", | ||
"version": "0.2.0", | ||
"type": "module", | ||
@@ -5,0 +5,0 @@ "description": "Typesafe Fetch API client for your @hey-api/openapi-ts types", |
import type { Client, Config, RequestOptions } from './types'; | ||
import { | ||
createDefaultConfig, | ||
createConfig, | ||
createInterceptors, | ||
@@ -8,2 +8,3 @@ createQuerySerializer, | ||
getUrl, | ||
mergeConfigs, | ||
mergeHeaders, | ||
@@ -17,38 +18,20 @@ } from './utils'; | ||
let globalConfig = createDefaultConfig(); | ||
export const createClient = (config: Config = {}): Client => { | ||
let _config = mergeConfigs(createConfig(), config); | ||
const globalInterceptors = createInterceptors< | ||
Request, | ||
Response, | ||
RequestOptions | ||
>(); | ||
const getConfig = (): Config => ({ ..._config }); | ||
export const createClient = (config: Config): Client => { | ||
const defaultConfig = createDefaultConfig(); | ||
const _config = { ...defaultConfig, ...config }; | ||
const setConfig = (config: Config): Config => { | ||
_config = mergeConfigs(_config, config); | ||
return getConfig(); | ||
}; | ||
if (_config.baseUrl?.endsWith('/')) { | ||
_config.baseUrl = _config.baseUrl.substring(0, _config.baseUrl.length - 1); | ||
} | ||
_config.headers = mergeHeaders(defaultConfig.headers, _config.headers); | ||
const interceptors = createInterceptors<Request, Response, RequestOptions>(); | ||
if (_config.global) { | ||
globalConfig = { ..._config }; | ||
} | ||
// @ts-ignore | ||
const getConfig = () => (_config.root ? globalConfig : _config); | ||
const interceptors = _config.global | ||
? globalInterceptors | ||
: createInterceptors<Request, Response, RequestOptions>(); | ||
// @ts-ignore | ||
const request: Client['request'] = async (options) => { | ||
const config = getConfig(); | ||
const opts: RequestOptions = { | ||
...config, | ||
..._config, | ||
...options, | ||
headers: mergeHeaders(config.headers, options.headers), | ||
headers: mergeHeaders(_config.headers, options.headers), | ||
}; | ||
@@ -98,8 +81,7 @@ if (opts.body && opts.bodySerializer) { | ||
// return empty objects so truthy checks for data/error succeed | ||
if ( | ||
response.status === 204 || | ||
response.headers.get('Content-Length') === '0' | ||
) { | ||
if (response.ok) { | ||
if (response.ok) { | ||
if ( | ||
response.status === 204 || | ||
response.headers.get('Content-Length') === '0' | ||
) { | ||
return { | ||
@@ -110,9 +92,3 @@ data: {}, | ||
} | ||
return { | ||
error: {}, | ||
...result, | ||
}; | ||
} | ||
if (response.ok) { | ||
if (opts.parseAs === 'stream') { | ||
@@ -124,2 +100,3 @@ return { | ||
} | ||
const parseAs = | ||
@@ -142,2 +119,7 @@ (opts.parseAs === 'auto' | ||
let error = await response.text(); | ||
if (opts.throwOnError) { | ||
throw new Error(error); | ||
} | ||
try { | ||
@@ -149,3 +131,3 @@ error = JSON.parse(error); | ||
return { | ||
error, | ||
error: error || {}, | ||
...result, | ||
@@ -167,10 +149,5 @@ }; | ||
request, | ||
setConfig, | ||
trace: (options) => request({ ...options, method: 'TRACE' }), | ||
}; | ||
}; | ||
export const client = createClient({ | ||
...globalConfig, | ||
// @ts-ignore | ||
root: true, | ||
}); |
@@ -1,4 +0,5 @@ | ||
export { client, createClient } from '../'; | ||
export { createClient } from '../'; | ||
export type { Client, Config, Options, RequestResult } from '../types'; | ||
export { | ||
createConfig, | ||
formDataBodySerializer, | ||
@@ -5,0 +6,0 @@ jsonBodySerializer, |
@@ -40,8 +40,2 @@ import type { | ||
/** | ||
* By default, options passed to this call will be applied to the global | ||
* client instance. Set to false to create a local client instance. | ||
* @default true | ||
*/ | ||
global?: boolean; | ||
/** | ||
* An object containing any HTTP headers that you want to pre-populate your | ||
@@ -101,5 +95,10 @@ * `Headers` object with. | ||
responseTransformer?: (data: unknown) => Promise<unknown>; | ||
/** | ||
* Throw an error instead of returning it in the response? | ||
* @default false | ||
*/ | ||
throwOnError?: boolean; | ||
} | ||
interface RequestOptionsBase extends Omit<Config, 'global'> { | ||
interface RequestOptionsBase extends Config { | ||
path?: Record<string, unknown>; | ||
@@ -118,9 +117,15 @@ query?: Record<string, unknown>; | ||
type MethodFn = <Data = unknown, Error = unknown>( | ||
options: RequestOptionsBase, | ||
options: Omit<RequestOptionsBase, 'method'>, | ||
) => RequestResult<Data, Error>; | ||
type RequestFn = <Data = unknown, Error = unknown>( | ||
options: RequestOptionsBase & Pick<Required<RequestOptionsBase>, 'method'>, | ||
options: Omit<RequestOptionsBase, 'method'> & | ||
Pick<Required<RequestOptionsBase>, 'method'>, | ||
) => RequestResult<Data, Error>; | ||
interface ClientBase<Request = unknown, Response = unknown, Options = unknown> { | ||
export interface Client< | ||
Req = Request, | ||
Res = Response, | ||
Options = RequestOptions, | ||
> { | ||
connect: MethodFn; | ||
@@ -131,3 +136,3 @@ delete: MethodFn; | ||
head: MethodFn; | ||
interceptors: Middleware<Request, Response, Options>; | ||
interceptors: Middleware<Req, Res, Options>; | ||
options: MethodFn; | ||
@@ -138,2 +143,3 @@ patch: MethodFn; | ||
request: RequestFn; | ||
setConfig: (config: Config) => Config; | ||
trace: MethodFn; | ||
@@ -147,4 +153,2 @@ } | ||
export type Client = ClientBase<Request, Response, RequestOptions>; | ||
type OptionsBase = Omit<RequestOptionsBase, 'url'> & { | ||
@@ -151,0 +155,0 @@ /** |
@@ -385,2 +385,11 @@ import type { Config } from './types'; | ||
export const mergeConfigs = (a: Config, b: Config): Config => { | ||
const config = { ...a, ...b }; | ||
if (config.baseUrl?.endsWith('/')) { | ||
config.baseUrl = config.baseUrl.substring(0, config.baseUrl.length - 1); | ||
} | ||
config.headers = mergeHeaders(a.headers, b.headers); | ||
return config; | ||
}; | ||
export const mergeHeaders = ( | ||
@@ -546,10 +555,10 @@ ...headers: Array<Required<Config>['headers'] | undefined> | ||
export const createDefaultConfig = (): Config => ({ | ||
export const createConfig = (override: Config = {}): Config => ({ | ||
...jsonBodySerializer, | ||
baseUrl: '', | ||
fetch: globalThis.fetch, | ||
global: true, | ||
headers: defaultHeaders, | ||
parseAs: 'auto', | ||
querySerializer: defaultQuerySerializer, | ||
...override, | ||
}); |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
124646
44234
1002
2
8
1