fork of hey-api/openapi-ts because I need some additional things
at feat/skip-token 171 lines 4.2 kB view raw
1// This file is auto-generated by @hey-api/openapi-ts 2 3interface SerializeOptions<T> extends SerializePrimitiveOptions, SerializerOptions<T> {} 4 5interface SerializePrimitiveOptions { 6 allowReserved?: boolean; 7 name: string; 8} 9 10export interface SerializerOptions<T> { 11 /** 12 * @default true 13 */ 14 explode: boolean; 15 style: T; 16} 17 18export type ArrayStyle = 'form' | 'spaceDelimited' | 'pipeDelimited'; 19export type ArraySeparatorStyle = ArrayStyle | MatrixStyle; 20type MatrixStyle = 'label' | 'matrix' | 'simple'; 21export type ObjectStyle = 'form' | 'deepObject'; 22type ObjectSeparatorStyle = ObjectStyle | MatrixStyle; 23 24interface SerializePrimitiveParam extends SerializePrimitiveOptions { 25 value: string; 26} 27 28export const separatorArrayExplode = (style: ArraySeparatorStyle) => { 29 switch (style) { 30 case 'label': 31 return '.'; 32 case 'matrix': 33 return ';'; 34 case 'simple': 35 return ','; 36 default: 37 return '&'; 38 } 39}; 40 41export const separatorArrayNoExplode = (style: ArraySeparatorStyle) => { 42 switch (style) { 43 case 'form': 44 return ','; 45 case 'pipeDelimited': 46 return '|'; 47 case 'spaceDelimited': 48 return '%20'; 49 default: 50 return ','; 51 } 52}; 53 54export const separatorObjectExplode = (style: ObjectSeparatorStyle) => { 55 switch (style) { 56 case 'label': 57 return '.'; 58 case 'matrix': 59 return ';'; 60 case 'simple': 61 return ','; 62 default: 63 return '&'; 64 } 65}; 66 67export const serializeArrayParam = ({ 68 allowReserved, 69 explode, 70 name, 71 style, 72 value, 73}: SerializeOptions<ArraySeparatorStyle> & { 74 value: unknown[]; 75}) => { 76 if (!explode) { 77 const joinedValues = ( 78 allowReserved ? value : value.map((v) => encodeURIComponent(v as string)) 79 ).join(separatorArrayNoExplode(style)); 80 switch (style) { 81 case 'label': 82 return `.${joinedValues}`; 83 case 'matrix': 84 return `;${name}=${joinedValues}`; 85 case 'simple': 86 return joinedValues; 87 default: 88 return `${name}=${joinedValues}`; 89 } 90 } 91 92 const separator = separatorArrayExplode(style); 93 const joinedValues = value 94 .map((v) => { 95 if (style === 'label' || style === 'simple') { 96 return allowReserved ? v : encodeURIComponent(v as string); 97 } 98 99 return serializePrimitiveParam({ 100 allowReserved, 101 name, 102 value: v as string, 103 }); 104 }) 105 .join(separator); 106 return style === 'label' || style === 'matrix' ? separator + joinedValues : joinedValues; 107}; 108 109export const serializePrimitiveParam = ({ 110 allowReserved, 111 name, 112 value, 113}: SerializePrimitiveParam) => { 114 if (value === undefined || value === null) { 115 return ''; 116 } 117 118 if (typeof value === 'object') { 119 throw new Error( 120 'Deeply-nested arrays/objects aren’t supported. Provide your own `querySerializer()` to handle these.', 121 ); 122 } 123 124 return `${name}=${allowReserved ? value : encodeURIComponent(value)}`; 125}; 126 127export const serializeObjectParam = ({ 128 allowReserved, 129 explode, 130 name, 131 style, 132 value, 133 valueOnly, 134}: SerializeOptions<ObjectSeparatorStyle> & { 135 value: Record<string, unknown> | Date; 136 valueOnly?: boolean; 137}) => { 138 if (value instanceof Date) { 139 return valueOnly ? value.toISOString() : `${name}=${value.toISOString()}`; 140 } 141 142 if (style !== 'deepObject' && !explode) { 143 let values: string[] = []; 144 Object.entries(value).forEach(([key, v]) => { 145 values = [...values, key, allowReserved ? (v as string) : encodeURIComponent(v as string)]; 146 }); 147 const joinedValues = values.join(','); 148 switch (style) { 149 case 'form': 150 return `${name}=${joinedValues}`; 151 case 'label': 152 return `.${joinedValues}`; 153 case 'matrix': 154 return `;${name}=${joinedValues}`; 155 default: 156 return joinedValues; 157 } 158 } 159 160 const separator = separatorObjectExplode(style); 161 const joinedValues = Object.entries(value) 162 .map(([key, v]) => 163 serializePrimitiveParam({ 164 allowReserved, 165 name: style === 'deepObject' ? `${name}[${key}]` : key, 166 value: v as string, 167 }), 168 ) 169 .join(separator); 170 return style === 'label' || style === 'matrix' ? separator + joinedValues : joinedValues; 171};