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

pathSerializer.gen.ts

186 lines4.2 KB

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