@curveball/kernel
Version:
Curveball is a framework writting in Typescript for Node.js
110 lines • 3.1 kB
JavaScript
import { is } from './header-helpers.js';
import { Headers } from './headers.js';
/**
* This interface represents an incoming server request.
*/
export class Response {
constructor(origin) {
this.headers = new Headers();
this.status = 200;
this.origin = origin;
}
/**
* List of HTTP Headers
*/
headers;
/**
* HTTP status code.
*/
status;
/**
* Response Body
*/
body;
/**
* Returns the value of the Content-Type header, with any additional
* parameters such as charset= removed.
*
* If there was no Content-Type header, an empty string will be returned.
*/
get type() {
const type = this.headers.get('content-type');
if (!type) {
return '';
}
return type.split(';')[0];
}
/**
* Shortcut for setting the Content-Type header.
*/
set type(value) {
this.headers.set('content-type', value);
}
/**
* Sends an informational response before the real response.
*
* This can be used to for example send a `100 Continue` or `103 Early Hints`
* response.
*/
async sendInformational(status, headers) {
// No need to do anything
}
/**
* Sends a HTTP/2 push.
*
* The passed middleware will be called with a new Context object specific
* for pushes.
*/
async push(callback) {
// Don't do anything
}
/**
* This method will return true or false if a Request or Response has a
* Content-Type header that matches the argument.
*
* For example, if the Content-Type header has the value: application/hal+json,
* then the arguments will all return true:
*
* * application/hal+json
* * application/json
* * hal+json
* * json
* * application/*
*/
is(type) {
return is(this, type);
}
/**
* redirect redirects the response with an optionally provided HTTP status
* code in the first position to the location provided in address. If no status
* is provided, 303 See Other is used.
*
* @param {(string|number)} addrOrStatus if passed a string, the string will
* be used to set the Location header of the response object and the default status
* of 303 See Other will be used. If a number, an addressed must be passed in the second
* argument.
* @param {string} address If addrOrStatus is passed a status code, this value is
* set as the value of the response's Location header.
*/
redirect(addrOrStatus, address = '') {
let status = 303;
let addr;
if (typeof (addrOrStatus) === 'number') {
status = addrOrStatus;
addr = address;
}
else {
addr = addrOrStatus;
}
this.status = status;
this.headers.set('Location', addr);
}
/**
* Public base URL
*
* This will be used to determine the absoluteUrl
*/
origin;
}
export default Response;
//# sourceMappingURL=response.js.map