( bodyType: HttpBodyType, body: string | null, formData: HttpFormDataEntry[], )
| 146 | } |
| 147 | |
| 148 | export function buildBody( |
| 149 | bodyType: HttpBodyType, |
| 150 | body: string | null, |
| 151 | formData: HttpFormDataEntry[], |
| 152 | ): BuiltBody { |
| 153 | switch (bodyType) { |
| 154 | case 'none': |
| 155 | return { body: undefined } |
| 156 | case 'json': |
| 157 | return { body: body ?? '', contentType: 'application/json' } |
| 158 | case 'text': |
| 159 | return { body: body ?? '', contentType: 'text/plain' } |
| 160 | case 'form-urlencoded': |
| 161 | return { |
| 162 | body: body ?? '', |
| 163 | contentType: 'application/x-www-form-urlencoded', |
| 164 | } |
| 165 | case 'multipart': { |
| 166 | const fd = new FormData() |
| 167 | for (const entry of formData) { |
| 168 | if (!entry.key) |
| 169 | continue |
| 170 | if (entry.type === 'file' && entry.value) { |
| 171 | const buffer = readFileSync(entry.value) |
| 172 | const blob = new Blob([buffer]) |
| 173 | fd.append(entry.key, blob, basename(entry.value)) |
| 174 | } |
| 175 | else { |
| 176 | fd.append(entry.key, entry.value ?? '') |
| 177 | } |
| 178 | } |
| 179 | return { body: fd } |
| 180 | } |
| 181 | } |
| 182 | } |
| 183 | |
| 184 | function detectBodyKind(contentType: string | undefined): HttpResponseBodyKind { |
| 185 | const ct = (contentType ?? '').toLowerCase() |
no test coverage detected