fork of hey-api/openapi-ts because I need some additional things
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

at feat/use-query-options 78 lines 2.3 kB view raw
1import type { ArrayStyle, ObjectStyle, SerializerOptions } from './pathSerializer'; 2 3export type QuerySerializer = (query: Record<string, unknown>) => string; 4 5export type BodySerializer = (body: unknown) => unknown; 6 7type QuerySerializerOptionsObject = { 8 allowReserved?: boolean; 9 array?: Partial<SerializerOptions<ArrayStyle>>; 10 object?: Partial<SerializerOptions<ObjectStyle>>; 11}; 12 13export type QuerySerializerOptions = QuerySerializerOptionsObject & { 14 /** 15 * Per-parameter serialization overrides. When provided, these settings 16 * override the global array/object settings for specific parameter names. 17 */ 18 parameters?: Record<string, QuerySerializerOptionsObject>; 19}; 20 21const serializeFormDataPair = (data: FormData, key: string, value: unknown): void => { 22 if (typeof value === 'string' || value instanceof Blob) { 23 data.append(key, value); 24 } else { 25 data.append(key, JSON.stringify(value)); 26 } 27}; 28 29const serializeUrlSearchParamsPair = (data: URLSearchParams, key: string, value: unknown): void => { 30 if (typeof value === 'string') { 31 data.append(key, value); 32 } else { 33 data.append(key, JSON.stringify(value)); 34 } 35}; 36 37export const formDataBodySerializer = { 38 bodySerializer: (body: unknown): FormData => { 39 const data = new FormData(); 40 41 Object.entries(body as Record<string, unknown>).forEach(([key, value]) => { 42 if (value === undefined || value === null) { 43 return; 44 } 45 if (Array.isArray(value)) { 46 value.forEach((v) => serializeFormDataPair(data, key, v)); 47 } else { 48 serializeFormDataPair(data, key, value); 49 } 50 }); 51 52 return data; 53 }, 54}; 55 56export const jsonBodySerializer = { 57 bodySerializer: (body: unknown): string => 58 JSON.stringify(body, (_key, value) => (typeof value === 'bigint' ? value.toString() : value)), 59}; 60 61export const urlSearchParamsBodySerializer = { 62 bodySerializer: (body: unknown): string => { 63 const data = new URLSearchParams(); 64 65 Object.entries(body as Record<string, unknown>).forEach(([key, value]) => { 66 if (value === undefined || value === null) { 67 return; 68 } 69 if (Array.isArray(value)) { 70 value.forEach((v) => serializeUrlSearchParamsPair(data, key, v)); 71 } else { 72 serializeUrlSearchParamsPair(data, key, value); 73 } 74 }); 75 76 return data.toString(); 77 }, 78};