fork of hey-api/openapi-ts because I need some additional things
at main 140 lines 3.3 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(match, serializeArrayParam({ explode, name, style, value })) 48 continue 49 } 50 51 if (typeof value === 'object') { 52 url = url.replace( 53 match, 54 serializeObjectParam({ 55 explode, 56 name, 57 style, 58 value: value as Record<string, unknown>, 59 valueOnly: true 60 }) 61 ) 62 continue 63 } 64 65 if (style === 'matrix') { 66 url = url.replace( 67 match, 68 `;${serializePrimitiveParam({ 69 name, 70 value: value as string 71 })}` 72 ) 73 continue 74 } 75 76 const replaceValue = encodeURIComponent( 77 style === 'label' ? `.${value as string}` : (value as string) 78 ) 79 url = url.replace(match, replaceValue) 80 } 81 } 82 return url 83} 84 85export const getUrl = ({ 86 baseUrl, 87 path, 88 query, 89 querySerializer, 90 url: _url 91}: { 92 baseUrl?: string 93 path?: Record<string, unknown> 94 query?: Record<string, unknown> 95 querySerializer: QuerySerializer 96 url: string 97}) => { 98 const pathUrl = _url.startsWith('/') ? _url : `/${_url}` 99 let url = (baseUrl ?? '') + pathUrl 100 if (path) { 101 url = defaultPathSerializer({ path, url }) 102 } 103 let search = query ? querySerializer(query) : '' 104 if (search.startsWith('?')) { 105 search = search.substring(1) 106 } 107 if (search) { 108 url += `?${search}` 109 } 110 return url 111} 112 113export function getValidRequestBody(options: { 114 body?: unknown 115 bodySerializer?: BodySerializer | null 116 serializedBody?: unknown 117}) { 118 const hasBody = options.body !== undefined 119 const isSerializedBody = hasBody && options.bodySerializer 120 121 if (isSerializedBody) { 122 if ('serializedBody' in options) { 123 const hasSerializedBody = 124 options.serializedBody !== undefined && options.serializedBody !== '' 125 126 return hasSerializedBody ? options.serializedBody : null 127 } 128 129 // not all clients implement a serializedBody property (i.e. client-axios) 130 return options.body !== '' ? options.body : null 131 } 132 133 // plain/text body 134 if (hasBody) { 135 return options.body 136 } 137 138 // no body was provided 139 return undefined 140}