79 lines
1.9 KiB
JavaScript
79 lines
1.9 KiB
JavaScript
let HttpError = class HttpError2 {
|
|
/**
|
|
* @param {number} status
|
|
* @param {{message: string} extends App.Error ? (App.Error | string | undefined) : App.Error} body
|
|
*/
|
|
constructor(status, body) {
|
|
this.status = status;
|
|
if (typeof body === "string") {
|
|
this.body = { message: body };
|
|
} else if (body) {
|
|
this.body = body;
|
|
} else {
|
|
this.body = { message: `Error: ${status}` };
|
|
}
|
|
}
|
|
toString() {
|
|
return JSON.stringify(this.body);
|
|
}
|
|
};
|
|
let Redirect = class Redirect2 {
|
|
/**
|
|
* @param {300 | 301 | 302 | 303 | 304 | 305 | 306 | 307 | 308} status
|
|
* @param {string} location
|
|
*/
|
|
constructor(status, location) {
|
|
this.status = status;
|
|
this.location = location;
|
|
}
|
|
};
|
|
let ActionFailure = class ActionFailure2 {
|
|
/**
|
|
* @param {number} status
|
|
* @param {T} [data]
|
|
*/
|
|
constructor(status, data) {
|
|
this.status = status;
|
|
this.data = data;
|
|
}
|
|
};
|
|
function error(status, message) {
|
|
if (isNaN(status) || status < 400 || status > 599) {
|
|
throw new Error(`HTTP error status codes must be between 400 and 599 — ${status} is invalid`);
|
|
}
|
|
return new HttpError(status, message);
|
|
}
|
|
function json(data, init) {
|
|
const body = JSON.stringify(data);
|
|
const headers = new Headers(init?.headers);
|
|
if (!headers.has("content-length")) {
|
|
headers.set("content-length", encoder.encode(body).byteLength.toString());
|
|
}
|
|
if (!headers.has("content-type")) {
|
|
headers.set("content-type", "application/json");
|
|
}
|
|
return new Response(body, {
|
|
...init,
|
|
headers
|
|
});
|
|
}
|
|
const encoder = new TextEncoder();
|
|
function text(body, init) {
|
|
const headers = new Headers(init?.headers);
|
|
if (!headers.has("content-length")) {
|
|
headers.set("content-length", encoder.encode(body).byteLength.toString());
|
|
}
|
|
return new Response(body, {
|
|
...init,
|
|
headers
|
|
});
|
|
}
|
|
export {
|
|
ActionFailure as A,
|
|
HttpError as H,
|
|
Redirect as R,
|
|
error as e,
|
|
json as j,
|
|
text as t
|
|
};
|