UNPKG

kequapp

Version:

A minimal, zero-magic Node web framework built on native APIs

27 lines (26 loc) 900 B
import createParseBody, { parseJson } from "./create-parse-body.js"; import streamReader from "./stream-reader.js"; const parseBody = createParseBody({ 'text/': ({ data }) => data.toString(), 'application/json': parseJson, }, ({ data }) => data); export default function createGetResponse(res) { let _body; return async (options = {}) => { if (_body === undefined) { // ensures application has responded const data = await streamReader(res); _body = { headers: { 'content-type': String(res.getHeader('Content-Type') ?? ''), 'content-disposition': String(res.getHeader('Content-Disposition') ?? ''), }, data, }; } if (options.raw === true) { return _body.data; } return parseBody(_body); }; }