@hey-api/client-fetch
Advanced tools
Comparing version 0.1.9 to 0.1.10
@@ -35,2 +35,5 @@ type ArrayStyle = 'form' | 'spaceDelimited' | 'pipeDelimited'; | ||
}; | ||
declare const urlSearchParamsBodySerializer: { | ||
bodySerializer: <T extends Record<string, any> | Array<Record<string, any>>>(body: T) => URLSearchParams; | ||
}; | ||
@@ -157,2 +160,2 @@ type OmitKeys<T, K> = Pick<T, Exclude<keyof T, K>>; | ||
export { type Client, type Options, type RequestResult, client, createClient, formDataBodySerializer, jsonBodySerializer }; | ||
export { type Client, type Options, type RequestResult, client, createClient, formDataBodySerializer, jsonBodySerializer, urlSearchParamsBodySerializer }; |
{ | ||
"name": "@hey-api/client-fetch", | ||
"version": "0.1.9", | ||
"version": "0.1.10", | ||
"type": "module", | ||
@@ -5,0 +5,0 @@ "description": "Typesafe Fetch API client for your @hey-api/openapi-ts types", |
@@ -72,6 +72,2 @@ import type { Client, Config, RequestOptions } from './types'; | ||
}; | ||
// remove Content-Type if serialized body is FormData; browser will correctly set Content-Type and boundary expression | ||
if (requestInit.body instanceof FormData) { | ||
requestInit.headers.delete('Content-Type'); | ||
} | ||
@@ -78,0 +74,0 @@ let request = new Request(url, requestInit); |
export { client, createClient } from '../'; | ||
export type { Client, Options, RequestResult } from '../types'; | ||
export { formDataBodySerializer, jsonBodySerializer } from '../utils'; | ||
export { | ||
formDataBodySerializer, | ||
jsonBodySerializer, | ||
urlSearchParamsBodySerializer, | ||
} from '../utils'; |
@@ -463,11 +463,7 @@ import type { Config } from './types'; | ||
const serializeFormDataPair = ( | ||
formData: FormData, | ||
key: string, | ||
value: unknown, | ||
) => { | ||
const serializeFormDataPair = (data: FormData, key: string, value: unknown) => { | ||
if (typeof value === 'string' || value instanceof Blob) { | ||
formData.append(key, value); | ||
data.append(key, value); | ||
} else { | ||
formData.append(key, JSON.stringify(value)); | ||
data.append(key, JSON.stringify(value)); | ||
} | ||
@@ -480,3 +476,3 @@ }; | ||
) => { | ||
const formData = new FormData(); | ||
const data = new FormData(); | ||
@@ -488,9 +484,9 @@ Object.entries(body).forEach(([key, value]) => { | ||
if (Array.isArray(value)) { | ||
value.forEach((v) => serializeFormDataPair(formData, key, v)); | ||
value.forEach((v) => serializeFormDataPair(data, key, v)); | ||
} else { | ||
serializeFormDataPair(formData, key, value); | ||
serializeFormDataPair(data, key, value); | ||
} | ||
}); | ||
return formData; | ||
return data; | ||
}, | ||
@@ -503,2 +499,35 @@ }; | ||
const serializeUrlSearchParamsPair = ( | ||
data: URLSearchParams, | ||
key: string, | ||
value: unknown, | ||
) => { | ||
if (typeof value === 'string') { | ||
data.append(key, value); | ||
} else { | ||
data.append(key, JSON.stringify(value)); | ||
} | ||
}; | ||
export const urlSearchParamsBodySerializer = { | ||
bodySerializer: <T extends Record<string, any> | Array<Record<string, any>>>( | ||
body: T, | ||
) => { | ||
const data = new URLSearchParams(); | ||
Object.entries(body).forEach(([key, value]) => { | ||
if (value === undefined || value === null) { | ||
return; | ||
} | ||
if (Array.isArray(value)) { | ||
value.forEach((v) => serializeUrlSearchParamsPair(data, key, v)); | ||
} else { | ||
serializeUrlSearchParamsPair(data, key, value); | ||
} | ||
}); | ||
return data; | ||
}, | ||
}; | ||
const defaultQuerySerializer = createQuerySerializer({ | ||
@@ -505,0 +534,0 @@ allowReserved: false, |
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
44284
1005