Open History to see the last commit
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
17}
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
27}
28
29export const separatorArrayExplode = (style: ArraySeparatorStyle) => {
30 switch (style) {
31 case 'label':
32 return '.'
33 case 'matrix':
34 return ';'
35 case 'simple':
36 return ','
37 default:
38 return '&'
39 }
40}
41
42export const separatorArrayNoExplode = (style: ArraySeparatorStyle) => {
43 switch (style) {
44 case 'form':
45 return ','
46 case 'pipeDelimited':
47 return '|'
48 case 'spaceDelimited':
49 return '%20'
50 default:
51 return ','
52 }
53}
54
55export const separatorObjectExplode = (style: ObjectSeparatorStyle) => {
56 switch (style) {
57 case 'label':
58 return '.'
59 case 'matrix':
60 return ';'
61 case 'simple':
62 return ','
63 default:
64 return '&'
65 }
66}
67
68export const serializeArrayParam = ({
69 allowReserved,
70 explode,
71 name,
72 style,
73 value,
74}: SerializeOptions<ArraySeparatorStyle> & {
75 value: unknown[]
76}) => {
77 if (!explode) {
78 const joinedValues = (
79 allowReserved ? value : value.map((v) => encodeURIComponent(v as string))
80 ).join(separatorArrayNoExplode(style))
81 switch (style) {
82 case 'label':
83 return `.${joinedValues}`
84 case 'matrix':
85 return `;${name}=${joinedValues}`
86 case 'simple':
87 return joinedValues
88 default:
89 return `${name}=${joinedValues}`
90 }
91 }
92
93 const separator = separatorArrayExplode(style)
94 const joinedValues = value
95 .map((v) => {
96 if (style === 'label' || style === 'simple') {
97 return allowReserved ? v : encodeURIComponent(v as string)
98 }
99
100 return serializePrimitiveParam({
101 allowReserved,
102 name,
103 value: v as string,
104 })
105 })
106 .join(separator)
107 return style === 'label' || style === 'matrix'
108 ? separator + joinedValues
109 : joinedValues
110}
111
112export const serializePrimitiveParam = ({
113 allowReserved,
114 name,
115 value,
116}: SerializePrimitiveParam) => {
117 if (value === undefined || value === null) {
118 return ''
119 }
120
121 if (typeof value === 'object') {
122 throw new Error(
123 'Deeply-nested arrays/objects aren’t supported. Provide your own `querySerializer()` to handle these.',
124 )
125 }
126
127 return `${name}=${allowReserved ? value : encodeURIComponent(value)}`
128}
129
130export const serializeObjectParam = ({
131 allowReserved,
132 explode,
133 name,
134 style,
135 value,
136 valueOnly,
137}: SerializeOptions<ObjectSeparatorStyle> & {
138 value: Record<string, unknown> | Date
139 valueOnly?: boolean
140}) => {
141 if (value instanceof Date) {
142 return valueOnly ? value.toISOString() : `${name}=${value.toISOString()}`
143 }
144
145 if (style !== 'deepObject' && !explode) {
146 let values: string[] = []
147 Object.entries(value).forEach(([key, v]) => {
148 values = [
149 ...values,
150 key,
151 allowReserved ? (v as string) : encodeURIComponent(v as string),
152 ]
153 })
154 const joinedValues = values.join(',')
155 switch (style) {
156 case 'form':
157 return `${name}=${joinedValues}`
158 case 'label':
159 return `.${joinedValues}`
160 case 'matrix':
161 return `;${name}=${joinedValues}`
162 default:
163 return joinedValues
164 }
165 }
166
167 const separator = separatorObjectExplode(style)
168 const joinedValues = Object.entries(value)
169 .map(([key, v]) =>
170 serializePrimitiveParam({
171 allowReserved,
172 name: style === 'deepObject' ? `${name}[${key}]` : key,
173 value: v as string,
174 }),
175 )
176 .join(separator)
177 return style === 'label' || style === 'matrix'
178 ? separator + joinedValues
179 : joinedValues
180}