2023-12-08 02:05:23 +00:00
|
|
|
import MimeTypeMap from './MimeTypeMap';
|
|
|
|
|
|
|
|
|
|
export default class LayResponse {
|
2024-03-28 07:07:35 +00:00
|
|
|
#routing = undefined;
|
|
|
|
|
#body = 'Not Implemented';
|
|
|
|
|
#file = undefined;
|
|
|
|
|
#redirectUrl = undefined;
|
|
|
|
|
#statusCode = 502;
|
|
|
|
|
#headerList = new Map();
|
|
|
|
|
#mimeType = 'text/plain';
|
|
|
|
|
#options = {
|
2023-12-08 02:05:23 +00:00
|
|
|
disableFileCheck: false,
|
|
|
|
|
};
|
|
|
|
|
|
2024-03-20 03:43:27 +00:00
|
|
|
constructor(routing) {
|
2024-03-28 07:07:35 +00:00
|
|
|
this.#routing = routing;
|
2023-12-08 02:05:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type(key) {
|
|
|
|
|
if (!MimeTypeMap.has(key)) {
|
2024-03-28 07:07:35 +00:00
|
|
|
if(this.#mimeType != 'text/plain') this.#mimeType = MimeTypeMap.get('text');
|
2023-12-08 02:05:23 +00:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
this.mimeType = MimeTypeMap.get(key);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
status(value = null) {
|
2024-03-28 07:07:35 +00:00
|
|
|
if (isNaN(value)) throw new TypeError('status has to be a number');
|
|
|
|
|
this.#statusCode = value || 502;
|
2023-12-08 02:05:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
redirect(value = null, status = 302) {
|
|
|
|
|
if (value === null) {
|
2024-03-28 07:07:35 +00:00
|
|
|
return this.#redirectUrl;
|
2023-12-08 02:05:23 +00:00
|
|
|
}
|
|
|
|
|
|
2024-03-28 07:07:35 +00:00
|
|
|
this.#redirectUrl = value;
|
2023-12-08 02:05:23 +00:00
|
|
|
this.status(status);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
headers() {
|
2024-03-28 07:07:35 +00:00
|
|
|
if (!this.#headerList.get('Content-Type')) {
|
|
|
|
|
this.#headerList.set('Content-Type', `${this.#mimeType}`);
|
2023-12-08 02:05:23 +00:00
|
|
|
}
|
2024-03-28 07:07:35 +00:00
|
|
|
return this.#headerList;
|
2023-12-08 02:05:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
header(key, value) {
|
2024-03-28 07:07:35 +00:00
|
|
|
this.#headerList.set(key, value);
|
2023-12-08 02:05:23 +00:00
|
|
|
}
|
|
|
|
|
|
2024-03-28 07:07:35 +00:00
|
|
|
async build(options = {}) {
|
|
|
|
|
if (this.#redirectUrl) {
|
|
|
|
|
return Response.redirect(this.#redirectUrl, this.status());
|
2023-12-08 02:05:23 +00:00
|
|
|
}
|
2024-03-28 07:07:35 +00:00
|
|
|
|
|
|
|
|
if(this.#file) return new Response(this.#file);
|
|
|
|
|
|
|
|
|
|
if(['text/plain'].includes(this.#mimeType)) {
|
|
|
|
|
await this.handleLayout(this.#body, options);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return new Response(this.#body, {
|
2023-12-08 02:05:23 +00:00
|
|
|
status: this.status(),
|
|
|
|
|
headers: this.headers(),
|
|
|
|
|
});
|
|
|
|
|
}
|
2024-01-20 04:59:20 +00:00
|
|
|
|
2024-03-28 07:07:35 +00:00
|
|
|
async send() {
|
|
|
|
|
await this.text(...arguments);
|
|
|
|
|
|
|
|
|
|
if (typeof(arguments[0]) != 'string' || !arguments[0]) return;
|
|
|
|
|
if (this.#options.disableFileCheck) return;
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
const temp = Bun.file(arguments[0]);
|
|
|
|
|
|
|
|
|
|
if (await temp.exists()) {
|
|
|
|
|
this.#file = temp;
|
|
|
|
|
}
|
|
|
|
|
} catch (err) {}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async text() {
|
|
|
|
|
let [value, ...rest] = arguments;
|
|
|
|
|
if (typeof value != 'string') {
|
|
|
|
|
value = JSON.stringify(value);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
this.type('text');
|
|
|
|
|
await this.handleLayout(value, ...rest);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
json(data, options = {}) {
|
|
|
|
|
let { replacer, space } = options;
|
|
|
|
|
if (!Array.isArray(replacer) && typeof replacer != 'function') {
|
|
|
|
|
replacer = null;
|
|
|
|
|
}
|
|
|
|
|
if (isNaN(space) || Math.floor(space) < 0) {
|
|
|
|
|
space = 0;
|
|
|
|
|
}
|
|
|
|
|
this.type('json');
|
|
|
|
|
this.#body = JSON.stringify(data, replacer, space);
|
|
|
|
|
}
|
|
|
|
|
|
2024-02-03 01:12:29 +00:00
|
|
|
async render(path, data = {}) {
|
2024-03-28 07:07:35 +00:00
|
|
|
const loader = await this.#routing.getServer().load(path);
|
|
|
|
|
const content = await loader.parse(data);
|
|
|
|
|
|
2024-02-03 01:12:29 +00:00
|
|
|
return content;
|
2024-01-20 04:59:20 +00:00
|
|
|
}
|
2024-03-28 07:07:35 +00:00
|
|
|
|
|
|
|
|
async handleLayout(content, options = {}) {
|
|
|
|
|
this.#body = String(content);
|
|
|
|
|
if(!options.hasOwnProperty('layout') || typeof options.layout !== 'string') {
|
|
|
|
|
if(this.#routing.getServer().hasLayout()) {
|
|
|
|
|
options.layout = this.#routing.getServer().getLayout();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if(!options.layout) return
|
|
|
|
|
|
|
|
|
|
const layout = await this.#routing.getServer().load(options.layout);
|
|
|
|
|
if(layout) {
|
|
|
|
|
this.#body = await layout.parse({ ...(options?.data || {}), content: String(content) }, options);
|
|
|
|
|
this.#mimeType = layout.mimeType;
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-12-08 02:05:23 +00:00
|
|
|
}
|