2026-01-13 14:49:19 +01:00

40 lines
1023 B
TypeScript

import type http from 'http'
export function isRecord(value: unknown): value is Record<string, unknown> {
return typeof value === 'object' && value !== null
}
export function readString(value: unknown): string | undefined {
return typeof value === 'string' ? value : undefined
}
export function getFormDataHeaders(formData: import('form-data')): http.OutgoingHttpHeaders {
const raw: unknown = formData.getHeaders()
if (!isRecord(raw)) {
return {}
}
const headers: http.OutgoingHttpHeaders = {}
for (const [key, value] of Object.entries(raw)) {
const str = readString(value)
if (str !== undefined) {
headers[key] = str
}
}
return headers
}
export function getRedirectLocation(headers: unknown): string | undefined {
if (!isRecord(headers)) {
return undefined
}
const { location } = headers
if (typeof location === 'string') {
return location
}
if (Array.isArray(location) && typeof location[0] === 'string') {
return location[0]
}
return undefined
}