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