@wener/console
Version:
Base console UI toolkit
68 lines (67 loc) • 1.71 kB
JavaScript
let ResponseHelper = class ResponseHelper {
json(data, arg, headers) {
return new Response(JSON.stringify(data), {
headers: {
'Content-Type': 'application/json',
...headers
}
});
}
redirect(location, status = 302) {
return this.of(null, {
status,
headers: {
Location: location
}
});
}
of(data, arg, headers) {
if (data instanceof Response) {
return data;
}
let init;
if (typeof arg === 'number') {
init = {
status: arg
};
} else {
init = arg || {};
}
if (headers) {
const hdr = Array.from(new Headers(init.headers).entries());
for (const [k, v] of Object.entries(headers)){
if (Array.isArray(v)) {
v.forEach((v)=>hdr.push([
k,
v
]));
} else {
hdr.push([
k,
v
]);
}
}
init.headers = hdr;
}
return new Response(data, init);
}
};
function merge(a, b) {
const ia = typeof a === 'number' ? {
status: a
} : a || {};
const ib = typeof b === 'number' ? {
status: b
} : b || {};
return {
...ia,
...ib,
headers: {
...ia.headers,
...ib.headers
}
};
}
export const Responses = new ResponseHelper();
//# sourceMappingURL=Responses.js.map