fork of hey-api/openapi-ts because I need some additional things
at main 3.4 kB view raw
1// This file is auto-generated by @hey-api/openapi-ts 2 3import type { BodySerializer, QuerySerializer } from './bodySerializer.gen'; 4import { 5 type ArraySeparatorStyle, 6 serializeArrayParam, 7 serializeObjectParam, 8 serializePrimitiveParam, 9} from './pathSerializer.gen'; 10 11export interface PathSerializer { 12 path: Record<string, unknown>; 13 url: string; 14} 15 16export const PATH_PARAM_RE = /\{[^{}]+\}/g; 17 18export const defaultPathSerializer = ({ path, url: _url }: PathSerializer) => { 19 let url = _url; 20 const matches = _url.match(PATH_PARAM_RE); 21 if (matches) { 22 for (const match of matches) { 23 let explode = false; 24 let name = match.substring(1, match.length - 1); 25 let style: ArraySeparatorStyle = 'simple'; 26 27 if (name.endsWith('*')) { 28 explode = true; 29 name = name.substring(0, name.length - 1); 30 } 31 32 if (name.startsWith('.')) { 33 name = name.substring(1); 34 style = 'label'; 35 } else if (name.startsWith(';')) { 36 name = name.substring(1); 37 style = 'matrix'; 38 } 39 40 const value = path[name]; 41 42 if (value === undefined || value === null) { 43 continue; 44 } 45 46 if (Array.isArray(value)) { 47 url = url.replace( 48 match, 49 serializeArrayParam({ explode, name, style, value }), 50 ); 51 continue; 52 } 53 54 if (typeof value === 'object') { 55 url = url.replace( 56 match, 57 serializeObjectParam({ 58 explode, 59 name, 60 style, 61 value: value as Record<string, unknown>, 62 valueOnly: true, 63 }), 64 ); 65 continue; 66 } 67 68 if (style === 'matrix') { 69 url = url.replace( 70 match, 71 `;${serializePrimitiveParam({ 72 name, 73 value: value as string, 74 })}`, 75 ); 76 continue; 77 } 78 79 const replaceValue = encodeURIComponent( 80 style === 'label' ? `.${value as string}` : (value as string), 81 ); 82 url = url.replace(match, replaceValue); 83 } 84 } 85 return url; 86}; 87 88export const getUrl = ({ 89 baseUrl, 90 path, 91 query, 92 querySerializer, 93 url: _url, 94}: { 95 baseUrl?: string; 96 path?: Record<string, unknown>; 97 query?: Record<string, unknown>; 98 querySerializer: QuerySerializer; 99 url: string; 100}) => { 101 const pathUrl = _url.startsWith('/') ? _url : `/${_url}`; 102 let url = (baseUrl ?? '') + pathUrl; 103 if (path) { 104 url = defaultPathSerializer({ path, url }); 105 } 106 let search = query ? querySerializer(query) : ''; 107 if (search.startsWith('?')) { 108 search = search.substring(1); 109 } 110 if (search) { 111 url += `?${search}`; 112 } 113 return url; 114}; 115 116export function getValidRequestBody(options: { 117 body?: unknown; 118 bodySerializer?: BodySerializer | null; 119 serializedBody?: unknown; 120}) { 121 const hasBody = options.body !== undefined; 122 const isSerializedBody = hasBody && options.bodySerializer; 123 124 if (isSerializedBody) { 125 if ('serializedBody' in options) { 126 const hasSerializedBody = 127 options.serializedBody !== undefined && options.serializedBody !== ''; 128 129 return hasSerializedBody ? options.serializedBody : null; 130 } 131 132 // not all clients implement a serializedBody property (i.e. client-axios) 133 return options.body !== '' ? options.body : null; 134 } 135 136 // plain/text body 137 if (hasBody) { 138 return options.body; 139 } 140 141 // no body was provided 142 return undefined; 143}