UNPKG

@nodescript/stdlib

Version:
112 lines (111 loc) 3.83 kB
// All typed array share the same prototype const TypedArray = Object.getPrototypeOf(Uint8Array); export var FetchResponseType; (function (FetchResponseType) { FetchResponseType["AUTO"] = "auto"; FetchResponseType["JSON"] = "json"; FetchResponseType["URL_ENCODED"] = "urlencoded"; FetchResponseType["TEXT"] = "text"; FetchResponseType["BINARY"] = "binary"; })(FetchResponseType || (FetchResponseType = {})); export var FetchMethod; (function (FetchMethod) { FetchMethod["GET"] = "GET"; FetchMethod["POST"] = "POST"; FetchMethod["PUT"] = "PUT"; FetchMethod["DELETE"] = "DELETE"; FetchMethod["PATCH"] = "PATCH"; })(FetchMethod || (FetchMethod = {})); export function parseQueryString(qs) { const result = {}; const searchParams = new URLSearchParams(qs); for (const key of searchParams.keys()) { const values = searchParams.getAll(key); result[key] = values; } return result; } export function mergeUrlQuery(url, query) { const parsedUrl = new URL(url); for (const [key, value] of Object.entries(query)) { if (value === undefined) { continue; } const arr = Array.isArray(value) ? value : [value]; for (const value of arr) { parsedUrl.searchParams.append(key, value); } } return parsedUrl.toString(); } export function determineRequestBody(method, body) { switch (true) { case method === FetchMethod.GET: return [undefined, undefined]; case (body instanceof URLSearchParams): return [body.toString(), 'application/x-www-form-urlencoded']; case (body instanceof ArrayBuffer || body instanceof TypedArray): { return [body, undefined]; } case typeof body === 'object': return [JSON.stringify(body), 'application/json']; case typeof body === 'string': return [body, 'text/plain']; default: return [undefined, undefined]; } } export async function readResponse(response, type, contentType) { switch (type) { case FetchResponseType.AUTO: { if (contentType.includes('application/json')) { return readResponse(response, FetchResponseType.JSON, contentType); } if (contentType.includes('application/x-www-form-urlencoded')) { return readResponse(response, FetchResponseType.URL_ENCODED, contentType); } return readResponse(response, FetchResponseType.TEXT, contentType); } case FetchResponseType.JSON: { return await response.json(); } case FetchResponseType.URL_ENCODED: { const text = await response.text(); return new URLSearchParams(text); } case FetchResponseType.TEXT: { return await response.text(); } case FetchResponseType.BINARY: { return await response.arrayBuffer(); } default: { throw new Error(`Unsupported response type: ${type}`); } } } export function headersToObject(headers) { const result = {}; for (const [key, value] of Object.entries(headers)) { result[key] = Array.isArray(value) ? value : [value]; } return result; } export class HttpRequestFailed extends Error { constructor(status, method, url, details) { super(`${status} - ${method} ${url}`); this.name = this.constructor.name; this.details = {}; this.status = 500; this.status = status; this.details = details; } } export class FetchError extends Error { constructor(status, message) { super(message); this.name = this.constructor.name; this.status = 500; this.status = status; } }