Skip to content
Source
Sign inSign up
TanStack/schemas
Open History to see the last commit

utils.gen.ts

146 lines3.3 KB

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