@foal/core
Version:
Full-featured Node.js framework, with no complexity
942 lines (941 loc) • 32.8 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.HttpResponseNotImplemented = exports.HttpResponseInternalServerError = exports.HttpResponseServerError = exports.HttpResponseTooManyRequests = exports.HttpResponseConflict = exports.HttpResponseMethodNotAllowed = exports.HttpResponseNotFound = exports.HttpResponseForbidden = exports.HttpResponseUnauthorized = exports.HttpResponseBadRequest = exports.HttpResponseClientError = exports.HttpResponseRedirect = exports.HttpResponseMovedPermanently = exports.HttpResponseRedirection = exports.HttpResponseNoContent = exports.HttpResponseCreated = exports.HttpResponseOK = exports.HttpResponseSuccess = exports.HttpResponse = void 0;
exports.isHttpResponse = isHttpResponse;
exports.isHttpResponseSuccess = isHttpResponseSuccess;
exports.isHttpResponseOK = isHttpResponseOK;
exports.isHttpResponseCreated = isHttpResponseCreated;
exports.isHttpResponseNoContent = isHttpResponseNoContent;
exports.isHttpResponseRedirection = isHttpResponseRedirection;
exports.isHttpResponseMovedPermanently = isHttpResponseMovedPermanently;
exports.isHttpResponseRedirect = isHttpResponseRedirect;
exports.isHttpResponseClientError = isHttpResponseClientError;
exports.isHttpResponseBadRequest = isHttpResponseBadRequest;
exports.isHttpResponseUnauthorized = isHttpResponseUnauthorized;
exports.isHttpResponseForbidden = isHttpResponseForbidden;
exports.isHttpResponseNotFound = isHttpResponseNotFound;
exports.isHttpResponseMethodNotAllowed = isHttpResponseMethodNotAllowed;
exports.isHttpResponseConflict = isHttpResponseConflict;
exports.isHttpResponseTooManyRequests = isHttpResponseTooManyRequests;
exports.isHttpResponseServerError = isHttpResponseServerError;
exports.isHttpResponseInternalServerError = isHttpResponseInternalServerError;
exports.isHttpResponseNotImplemented = isHttpResponseNotImplemented;
/**
* Represent an HTTP response. This class must be extended.
* Instances of HttpResponse are returned in hooks and controller
* methods.
*
* @export
* @abstract
* @class HttpResponse
*/
class HttpResponse {
/**
* Property used internally by isHttpResponse.
*
* @memberof HttpResponse
*/
isHttpResponse = true;
/**
* Specify if the body property is a stream.
*
* @type {boolean}
* @memberof HttpResponse
*/
stream = false;
body;
cookies = {};
headers = {};
/**
* Create an instance of HttpResponse.
* @param {*} [body] - Optional body of the response.
* @memberof HttpResponse
*/
constructor(body, options = {}) {
if (typeof body !== 'undefined') {
this.body = body;
}
this.stream = options.stream || false;
}
/**
* Add or replace a header in the response.
*
* @param {string} name - The header name.
* @param {string} value - The value name.
* @returns {this}
* @memberof HttpResponse
*/
setHeader(name, value) {
this.headers[name] = value;
return this;
}
/**
* Read the value of a header added with setHeader.
*
* @param {string} name - The header name.
* @returns {(string|undefined)} The header value or undefined if it
* does not exist.
* @memberof HttpResponse
*/
getHeader(name) {
return this.headers[name];
}
/**
* Read all the headers added with setHeader.
*
* @returns {{ [key: string]: string }} - The headers.
* @memberof HttpResponse
*/
getHeaders() {
return { ...this.headers };
}
/**
* Add or replace a cookie in the response.
*
* @param {string} name - The cookie name.
* @param {string} value - The cookie value.
* @param {CookieOptions} [options={}] - The cookie directives if any.
* @returns {this}
* @memberof HttpResponse
*/
setCookie(name, value, options = {}) {
this.cookies[name] = { value, options };
return this;
}
/**
* Read the value and directives of a cookie added with setCookie.
*
* @param {string} name - The cookie name.
* @returns {({ value: string|undefined, options: CookieOptions })} The cookie value and directives
* or undefined and an empty object if the cookie does not exist.
* @memberof HttpResponse
*/
getCookie(name) {
if (!this.cookies[name]) {
return { value: undefined, options: {} };
}
const { value, options } = this.cookies[name];
return { value, options: { ...options } };
}
/**
* Read all the cookies added with setCookie.
*
* @returns {({ [key: string]: { value: string|undefined, options: CookieOptions } })}
* The name, value and directives of the cookies.
* @memberof HttpResponse
*/
getCookies() {
const cookies = {};
for (const cookieName in this.cookies) {
const { value, options } = this.cookies[cookieName];
cookies[cookieName] = { value, options: { ...options } };
}
return cookies;
}
}
exports.HttpResponse = HttpResponse;
/**
* Check if an object is an instance of HttpResponse.
*
* This function is a help when you have several packages using @foal/core.
* Npm can install the package several times, which leads to duplicate class
* definitions. If this is the case, the keyword `instanceof` may return false
* while the object is an instance of the class. This function fixes this
* problem.
*
* @export
* @param {*} obj - The object to check.
* @returns {obj is HttpResponse} - True if the error is an instance of HttpResponse. False otherwise.
*/
function isHttpResponse(obj) {
return obj instanceof HttpResponse ||
(typeof obj === 'object' && obj !== null && obj.isHttpResponse === true);
}
/* 2xx Success */
/**
* Represent an HTTP response with a success status 2xx.
*
* @export
* @abstract
* @class HttpResponseSuccess
* @extends {HttpResponse}
*/
class HttpResponseSuccess extends HttpResponse {
/**
* Property used internally by isHttpResponseSuccess.
*
* @memberof HttpResponseSuccess
*/
isHttpResponseSuccess = true;
/**
* Create an instance of HttpResponseSuccess.
* @param {*} [body] - Optional body of the response.
* @memberof HttpResponseSuccess
*/
constructor(body, options = {}) {
super(body, options);
}
}
exports.HttpResponseSuccess = HttpResponseSuccess;
/**
* Check if an object is an instance of HttpResponseSuccess.
*
* This function is a help when you have several packages using @foal/core.
* Npm can install the package several times, which leads to duplicate class
* definitions. If this is the case, the keyword `instanceof` may return false
* while the object is an instance of the class. This function fixes this
* problem.
*
* @export
* @param {*} obj - The object to check.
* @returns {obj is HttpResponseSuccess} - True if the error is an instance of HttpResponseSuccess. False otherwise.
*/
function isHttpResponseSuccess(obj) {
return obj instanceof HttpResponseSuccess ||
(typeof obj === 'object' && obj !== null && obj.isHttpResponseSuccess === true);
}
/**
* Represent an HTTP response with the status 200 - OK.
*
* @export
* @class HttpResponseOK
* @extends {HttpResponseSuccess}
*/
class HttpResponseOK extends HttpResponseSuccess {
/**
* Property used internally by isHttpResponseOK.
*
* @memberof HttpResponseOK
*/
isHttpResponseOK = true;
statusCode = 200;
statusMessage = 'OK';
/**
* Create an instance of HttpResponseOK.
* @param {*} [body] - Optional body of the response.
* @memberof HttpResponseOK
*/
constructor(body, options = {}) {
super(body, options);
}
}
exports.HttpResponseOK = HttpResponseOK;
/**
* Check if an object is an instance of HttpResponseOK.
*
* This function is a help when you have several packages using @foal/core.
* Npm can install the package several times, which leads to duplicate class
* definitions. If this is the case, the keyword `instanceof` may return false
* while the object is an instance of the class. This function fixes this
* problem.
*
* @export
* @param {*} obj - The object to check.
* @returns {obj is HttpResponseOK} - True if the error is an instance of HttpResponseOK. False otherwise.
*/
function isHttpResponseOK(obj) {
return obj instanceof HttpResponseOK ||
(typeof obj === 'object' && obj !== null && obj.isHttpResponseOK === true);
}
/**
* Represent an HTTP response with the status 201 - CREATED.
*
* @export
* @class HttpResponseCreated
* @extends {HttpResponseSuccess}
*/
class HttpResponseCreated extends HttpResponseSuccess {
/**
* Property used internally by isHttpResponseCreated.
*
* @memberof HttpResponseCreated
*/
isHttpResponseCreated = true;
statusCode = 201;
statusMessage = 'CREATED';
/**
* Create an instance of HttpResponseCreated.
* @param {*} [body] - Optional body of the response.
* @memberof HttpResponseCreated
*/
constructor(body, options = {}) {
super(body, options);
}
}
exports.HttpResponseCreated = HttpResponseCreated;
/**
* Check if an object is an instance of HttpResponseCreated.
*
* This function is a help when you have several packages using @foal/core.
* Npm can install the package several times, which leads to duplicate class
* definitions. If this is the case, the keyword `instanceof` may return false
* while the object is an instance of the class. This function fixes this
* problem.
*
* @export
* @param {*} obj - The object to check.
* @returns {obj is HttpResponseCreated} - True if the error is an instance of HttpResponseCreated. False otherwise.
*/
function isHttpResponseCreated(obj) {
return obj instanceof HttpResponseCreated ||
(typeof obj === 'object' && obj !== null && obj.isHttpResponseCreated === true);
}
/**
* Represent an HTTP response with the status 204 - NO CONTENT.
*
* @export
* @class HttpResponseNoContent
* @extends {HttpResponseSuccess}
*/
class HttpResponseNoContent extends HttpResponseSuccess {
/**
* Property used internally by is HttpResponseNoContent.
*
* @memberof HttpResponseNoContent
*/
isHttpResponseNoContent = true;
statusCode = 204;
statusMessage = 'NO CONTENT';
/**
* Create an instance of HttpResponseNoContent.
* @memberof HttpResponseNoContent
*/
constructor() {
super();
}
}
exports.HttpResponseNoContent = HttpResponseNoContent;
/**
* Check if an object is an instance of HttpResponseNoContent.
*
* This function is a help when you have several packages using @foal/core.
* Npm can install the package several times, which leads to duplicate class
* definitions. If this is the case, the keyword `instanceof` may return false
* while the object is an instance of the class. This function fixes this
* problem.
*
* @export
* @param {*} obj - The object to check.
* @returns {obj is HttpResponseNoContent} - True if the error is an instance of HttpResponseNoContent. False otherwise.
*/
function isHttpResponseNoContent(obj) {
return obj instanceof HttpResponseNoContent ||
(typeof obj === 'object' && obj !== null && obj.isHttpResponseNoContent === true);
}
/* 3xx Redirection */
/**
* Represent an HTTP response with a redirection status 3xx.
*
* @export
* @abstract
* @class HttpResponseRedirection
* @extends {HttpResponse}
*/
class HttpResponseRedirection extends HttpResponse {
/**
* Property used internally by isHttpResponseRedirection.
*
* @memberof HttpResponseRedirection
*/
isHttpResponseRedirection = true;
/**
* Create an instance of HttpResponseRedirection.
* @param {*} [body] - Optional body of the response.
* @memberof HttpResponseRedirection
*/
constructor(body, options = {}) {
super(body, options);
}
}
exports.HttpResponseRedirection = HttpResponseRedirection;
/**
* Check if an object is an instance of HttpResponseRedirection.
*
* This function is a help when you have several packages using @foal/core.
* Npm can install the package several times, which leads to duplicate class
* definitions. If this is the case, the keyword `instanceof` may return false
* while the object is an instance of the class. This function fixes this
* problem.
*
* @export
* @param {*} obj - The object to check.
* @returns {obj is HttpResponseRedirection} - True if the error is an instance of HttpResponseRedirection.
* False otherwise.
*/
function isHttpResponseRedirection(obj) {
return obj instanceof HttpResponseRedirection ||
(typeof obj === 'object' && obj !== null && obj.isHttpResponseRedirection === true);
}
/**
* Represent an HTTP response with the status 301 - MOVED PERMANENTLY.
*
* @export
* @class HttpResponseMovedPermanently
* @extends {HttpResponseRedirection}
*/
class HttpResponseMovedPermanently extends HttpResponseRedirection {
path;
/**
* Property used internally by isHttpResponseMovedPermanently.
*
* @memberof isHttpResponseMovedPermanently
*/
isHttpResponseMovedPermanently = true;
statusCode = 301;
statusMessage = 'MOVED PERMANENTLY';
/**
* Create an instance of HttpResponseMovedPermanently.
* @param {string} path - The redirection path.
* @memberof HttpResponseMovedPermanently
*/
constructor(path) {
super();
this.path = path;
}
}
exports.HttpResponseMovedPermanently = HttpResponseMovedPermanently;
/**
* Check if an object is an instance of HttpResponseMovedPermanently.
*
* This function is a help when you have several packages using @foal/core.
* Npm can install the package several times, which leads to duplicate class
* definitions. If this is the case, the keyword `instanceof` may return false
* while the object is an instance of the class. This function fixes this
* problem.
*
* @export
* @param {*} obj - The object to check.
* @returns {obj is HttpResponseMovedPermanently} - True if the error is an
* instance of HttpResponseMovedPermanently. False otherwise.
*/
function isHttpResponseMovedPermanently(obj) {
return obj instanceof HttpResponseMovedPermanently ||
(typeof obj === 'object' && obj !== null && obj.isHttpResponseMovedPermanently === true);
}
/**
* Represent an HTTP response with the status 302 - FOUND.
*
* @export
* @class HttpResponseRedirect
* @extends {HttpResponseRedirection}
*/
class HttpResponseRedirect extends HttpResponseRedirection {
path;
/**
* Property used internally by isHttpResponseRedirect.
*
* @memberof HttpResponseRedirect
*/
isHttpResponseRedirect = true;
statusCode = 302;
statusMessage = 'FOUND';
/**
* Create an instance of HttpResponseRedirect.
* @param {string} path - The redirection path.
* @param {*} [body] - Optional body of the response.
* @memberof HttpResponseRedirect
*/
constructor(path, body, options = {}) {
super(body, options);
this.path = path;
}
}
exports.HttpResponseRedirect = HttpResponseRedirect;
/**
* Check if an object is an instance of HttpResponseRedirect.
*
* This function is a help when you have several packages using @foal/core.
* Npm can install the package several times, which leads to duplicate class
* definitions. If this is the case, the keyword `instanceof` may return false
* while the object is an instance of the class. This function fixes this
* problem.
*
* @export
* @param {*} obj - The object to check.
* @returns {obj is HttpResponseRedirect} - True if the error is an instance of HttpResponseRedirect. False otherwise.
*/
function isHttpResponseRedirect(obj) {
return obj instanceof HttpResponseRedirect ||
(typeof obj === 'object' && obj !== null && obj.isHttpResponseRedirect === true);
}
/* 4xx Client Error */
/**
* Represent an HTTP response with a client error status 4xx.
*
* @export
* @abstract
* @class HttpResponseClientError
* @extends {HttpResponse}
*/
class HttpResponseClientError extends HttpResponse {
/**
* Property used internally by isHttpResponseClientError.
*
* @memberof HttpResponseClientError
*/
isHttpResponseClientError = true;
/**
* Create an instance of HttpResponseClientError.
* @param {*} [body] - Optional body of the response.
* @memberof HttpResponseClientError
*/
constructor(body, options = {}) {
super(body, options);
}
}
exports.HttpResponseClientError = HttpResponseClientError;
/**
* Check if an object is an instance of HttpResponseClientError.
*
* This function is a help when you have several packages using @foal/core.
* Npm can install the package several times, which leads to duplicate class
* definitions. If this is the case, the keyword `instanceof` may return false
* while the object is an instance of the class. This function fixes this
* problem.
*
* @export
* @param {*} obj - The object to check.
* @returns {obj is HttpResponseClientError} - True if the error is an instance of HttpResponseClientError.
* False otherwise.
*/
function isHttpResponseClientError(obj) {
return obj instanceof HttpResponseClientError ||
(typeof obj === 'object' && obj !== null && obj.isHttpResponseClientError === true);
}
/**
* Represent an HTTP response with the status 400 - BAD REQUEST.
*
* @export
* @class HttpResponseBadRequest
* @extends {HttpResponseClientError}
*/
class HttpResponseBadRequest extends HttpResponseClientError {
/**
* Property used internally by isHttpResponseBadRequest.
*
* @memberof HttpResponseBadRequest
*/
isHttpResponseBadRequest = true;
statusCode = 400;
statusMessage = 'BAD REQUEST';
/**
* Create an instance of HttpResponseBadRequest.
* @param {*} [body] - Optional body of the response.
* @memberof HttpResponseBadRequest
*/
constructor(body, options = {}) {
super(body, options);
}
}
exports.HttpResponseBadRequest = HttpResponseBadRequest;
/**
* Check if an object is an instance of HttpResponseBadRequest.
*
* This function is a help when you have several packages using @foal/core.
* Npm can install the package several times, which leads to duplicate class
* definitions. If this is the case, the keyword `instanceof` may return false
* while the object is an instance of the class. This function fixes this
* problem.
*
* @export
* @param {*} obj - The object to check.
* @returns {obj is HttpResponseBadRequest} - True if the error is an instance of HttpResponseBadRequest.
* False otherwise.
*/
function isHttpResponseBadRequest(obj) {
return obj instanceof HttpResponseBadRequest ||
(typeof obj === 'object' && obj !== null && obj.isHttpResponseBadRequest === true);
}
/**
* Represent an HTTP response with the status 401 - UNAUTHORIZED.
*
* @export
* @class HttpResponseUnauthorized
* @extends {HttpResponseClientError}
*/
class HttpResponseUnauthorized extends HttpResponseClientError {
/**
* Property used internally by isHttpResponseUnauthorized.
*
* @memberof HttpResponseUnauthorized
*/
isHttpResponseUnauthorized = true;
statusCode = 401;
statusMessage = 'UNAUTHORIZED';
/**
* Create an instance of HttpResponseUnauthorized.
* @param {*} [body] - Optional body of the response.
* @memberof HttpResponseUnauthorized
*/
constructor(body, options = {}) {
super(body, options);
this.setHeader('WWW-Authenticate', '');
}
}
exports.HttpResponseUnauthorized = HttpResponseUnauthorized;
/**
* Check if an object is an instance of HttpResponseUnauthorized.
*
* This function is a help when you have several packages using @foal/core.
* Npm can install the package several times, which leads to duplicate class
* definitions. If this is the case, the keyword `instanceof` may return false
* while the object is an instance of the class. This function fixes this
* problem.
*
* @export
* @param {*} obj - The object to check.
* @returns {obj is HttpResponseUnauthorized} - True if the error is an instance of HttpResponseUnauthorized.
* False otherwise.
*/
function isHttpResponseUnauthorized(obj) {
return obj instanceof HttpResponseUnauthorized ||
(typeof obj === 'object' && obj !== null && obj.isHttpResponseUnauthorized === true);
}
/**
* Represent an HTTP response with the status 403 - FORBIDDEN.
*
* @export
* @class HttpResponseForbidden
* @extends {HttpResponseClientError}
*/
class HttpResponseForbidden extends HttpResponseClientError {
/**
* Property used internally by isHttpResponseForbidden.
*
* @memberof HttpResponseForbidden
*/
isHttpResponseForbidden = true;
statusCode = 403;
statusMessage = 'FORBIDDEN';
/**
* Create an instance of HttpResponseForbidden.
* @param {*} [body] - Optional body of the response.
* @memberof HttpResponseForbidden
*/
constructor(body, options = {}) {
super(body, options);
}
}
exports.HttpResponseForbidden = HttpResponseForbidden;
/**
* Check if an object is an instance of HttpResponseForbidden.
*
* This function is a help when you have several packages using @foal/core.
* Npm can install the package several times, which leads to duplicate class
* definitions. If this is the case, the keyword `instanceof` may return false
* while the object is an instance of the class. This function fixes this
* problem.
*
* @export
* @param {*} obj - The object to check.
* @returns {obj is HttpResponseForbidden} - True if the error is an instance of HttpResponseForbidden. False otherwise.
*/
function isHttpResponseForbidden(obj) {
return obj instanceof HttpResponseForbidden ||
(typeof obj === 'object' && obj !== null && obj.isHttpResponseForbidden === true);
}
/**
* Represent an HTTP response with the status 404 - NOT FOUND.
*
* @export
* @class HttpResponseNotFound
* @extends {HttpResponseClientError}
*/
class HttpResponseNotFound extends HttpResponseClientError {
/**
* Property used internally by isHttpResponseNotFound.
*
* @memberof HttpResponseNotFound
*/
isHttpResponseNotFound = true;
statusCode = 404;
statusMessage = 'NOT FOUND';
/**
* Create an instance of HttpResponseNotFound.
* @param {*} [body] - Optional body of the response.
* @memberof HttpResponseNotFound
*/
constructor(body, options = {}) {
super(body, options);
}
}
exports.HttpResponseNotFound = HttpResponseNotFound;
/**
* Check if an object is an instance of HttpResponseNotFound.
*
* This function is a help when you have several packages using @foal/core.
* Npm can install the package several times, which leads to duplicate class
* definitions. If this is the case, the keyword `instanceof` may return false
* while the object is an instance of the class. This function fixes this
* problem.
*
* @export
* @param {*} obj - The object to check.
* @returns {obj is HttpResponseNotFound} - True if the error is an instance of HttpResponseNotFound. False otherwise.
*/
function isHttpResponseNotFound(obj) {
return obj instanceof HttpResponseNotFound ||
(typeof obj === 'object' && obj !== null && obj.isHttpResponseNotFound === true);
}
/**
* Represent an HTTP response with the status 405 - METHOD NOT ALLOWED.
*
* @export
* @class HttpResponseMethodNotAllowed
* @extends {HttpResponseClientError}
*/
class HttpResponseMethodNotAllowed extends HttpResponseClientError {
/**
* Property used internally by isHttpResponseMethodNotAllowed.
*
* @memberof HttpResponseMethodNotAllowed
*/
isHttpResponseMethodNotAllowed = true;
statusCode = 405;
statusMessage = 'METHOD NOT ALLOWED';
/**
* Create an instance of HttpResponseMethodNotAllowed.
* @param {*} [body] - Optional body of the response.
* @memberof HttpResponseMethodNotAllowed
*/
constructor(body, options = {}) {
super(body, options);
}
}
exports.HttpResponseMethodNotAllowed = HttpResponseMethodNotAllowed;
/**
* Check if an object is an instance of HttpResponseMethodNotAllowed.
*
* This function is a help when you have several packages using @foal/core.
* Npm can install the package several times, which leads to duplicate class
* definitions. If this is the case, the keyword `instanceof` may return false
* while the object is an instance of the class. This function fixes this
* problem.
*
* @export
* @param {*} obj - The object to check.
* @returns {obj is HttpResponseMethodNotAllowed} - True if the error is an instance of HttpResponseMethodNotAllowed.
* False otherwise.
*/
function isHttpResponseMethodNotAllowed(obj) {
return obj instanceof HttpResponseMethodNotAllowed ||
(typeof obj === 'object' && obj !== null && obj.isHttpResponseMethodNotAllowed === true);
}
/**
* Represent an HTTP response with the status 409 - CONFLICT.
*
* @export
* @class HttpResponseConflict
* @extends {HttpResponseClientError}
*/
class HttpResponseConflict extends HttpResponseClientError {
/**
* Property used internally by isHttpResponseConflict.
*
* @memberof HttpResponseConflict
*/
isHttpResponseConflict = true;
statusCode = 409;
statusMessage = 'CONFLICT';
/**
* Create an instance of HttpResponseConflict.
* @param {*} [body] - Optional body of the response.
* @memberof HttpResponseConflict
*/
constructor(body, options = {}) {
super(body, options);
}
}
exports.HttpResponseConflict = HttpResponseConflict;
/**
* Check if an object is an instance of HttpResponseConflict.
*
* This function is a help when you have several packages using @foal/core.
* Npm can install the package several times, which leads to duplicate class
* definitions. If this is the case, the keyword `instanceof` may return false
* while the object is an instance of the class. This function fixes this
* problem.
*
* @export
* @param {*} obj - The object to check.
* @returns {obj is HttpResponseConflict} - True if the error is an instance of HttpResponseConflict. False otherwise.
*/
function isHttpResponseConflict(obj) {
return obj instanceof HttpResponseConflict ||
(typeof obj === 'object' && obj !== null && obj.isHttpResponseConflict === true);
}
/**
* Represent an HTTP response with the status 429 - TOO MANY REQUESTS.
*
* @export
* @class HttpResponseTooManyRequests
* @extends {HttpResponseClientError}
*/
class HttpResponseTooManyRequests extends HttpResponseClientError {
/**
* Property used internally by isHttpResponseTooManyRequests.
*
* @memberof HttpResponseTooManyRequests
*/
isHttpResponseTooManyRequests = true;
statusCode = 429;
statusMessage = 'TOO MANY REQUESTS';
/**
* Create an instance of HttpResponseTooManyRequests.
* @param {*} [body] - Optional body of the response.
* @memberof HttpResponseTooManyRequests
*/
constructor(body, options = {}) {
super(body, options);
}
}
exports.HttpResponseTooManyRequests = HttpResponseTooManyRequests;
/**
* Check if an object is an instance of HttpResponseTooManyRequests.
*
* This function is a help when you have several packages using @foal/core.
* Npm can install the package several times, which leads to duplicate class
* definitions. If this is the case, the keyword `instanceof` may return false
* while the object is an instance of the class. This function fixes this
* problem.
*
* @export
* @param {*} obj - The object to check.
* @returns {obj is HttpResponseTooManyRequests} - True if the error is an instance of HttpResponseTooManyRequests.
* False otherwise.
*/
function isHttpResponseTooManyRequests(obj) {
return obj instanceof HttpResponseTooManyRequests ||
(typeof obj === 'object' && obj !== null && obj.isHttpResponseTooManyRequests === true);
}
/* 5xx Server Error */
/**
* Represent an HTTP response with a server error status 5xx.
*
* @export
* @abstract
* @class HttpResponseServerError
* @extends {HttpResponse}
*/
class HttpResponseServerError extends HttpResponse {
/**
* Property used internally by isHttpResponseServerError.
*
* @memberof HttpResponseServerError
*/
isHttpResponseServerError = true;
constructor(body, options = {}) {
super(body, options);
}
}
exports.HttpResponseServerError = HttpResponseServerError;
/**
* Check if an object is an instance of HttpResponseServerError.
*
* This function is a help when you have several packages using @foal/core.
* Npm can install the package several times, which leads to duplicate class
* definitions. If this is the case, the keyword `instanceof` may return false
* while the object is an instance of the class. This function fixes this
* problem.
*
* @export
* @param {*} obj - The object to check.
* @returns {obj is HttpResponseServerError} - True if the error is an instance of HttpResponseServerError.
* False otherwise.
*/
function isHttpResponseServerError(obj) {
return obj instanceof HttpResponseServerError ||
(typeof obj === 'object' && obj !== null && obj.isHttpResponseServerError === true);
}
/**
* Represent an HTTP response with the status 500 - INTERNAL SERVER ERROR.
*
* @export
* @class HttpResponseInternalServerError
* @extends {HttpResponseServerError}
*/
class HttpResponseInternalServerError extends HttpResponseServerError {
/**
* Property used internally by isHttpResponseInternalServerError.
*
* @memberof HttpResponseInternalServerError
*/
isHttpResponseInternalServerError = true;
error;
ctx;
statusCode = 500;
statusMessage = 'INTERNAL SERVER ERROR';
/**
* Create an instance of HttpResponseInternalServerError.
* @param {*} [body] - Optional body of the response.
* @memberof HttpResponseInternalServerError
*/
constructor(body, options = {}) {
super(body, options);
this.error = options.error;
this.ctx = options.ctx;
}
}
exports.HttpResponseInternalServerError = HttpResponseInternalServerError;
/**
* Check if an object is an instance of HttpResponseInternalServerError.
*
* This function is a help when you have several packages using @foal/core.
* Npm can install the package several times, which leads to duplicate class
* definitions. If this is the case, the keyword `instanceof` may return false
* while the object is an instance of the class. This function fixes this
* problem.
*
* @export
* @param {*} obj - The object to check.
* @returns {obj is HttpResponseInternalServerError} - True if the error is an instance of
* HttpResponseInternalServerError. False otherwise.
*/
function isHttpResponseInternalServerError(obj) {
return obj instanceof HttpResponseInternalServerError ||
(typeof obj === 'object' && obj !== null && obj.isHttpResponseInternalServerError === true);
}
/**
* Represent an HTTP response with the status 501 - NOT IMPLEMENTED.
*
* @export
* @class HttpResponseNotImplemented
* @extends {HttpResponseServerError}
*/
class HttpResponseNotImplemented extends HttpResponseServerError {
/**
* Property used internally by isHttpResponseNotImplemented.
*
* @memberof HttpResponseNotImplemented
*/
isHttpResponseNotImplemented = true;
statusCode = 501;
statusMessage = 'NOT IMPLEMENTED';
/**
* Create an instance of HttpResponseNotImplemented.
* @param {*} [body] - Optional body of the response.
* @memberof HttpResponseNotImplemented
*/
constructor(body, options = {}) {
super(body, options);
}
}
exports.HttpResponseNotImplemented = HttpResponseNotImplemented;
/**
* Check if an object is an instance of HttpResponseNotImplemented.
*
* This function is a help when you have several packages using @foal/core.
* Npm can install the package several times, which leads to duplicate class
* definitions. If this is the case, the keyword `instanceof` may return false
* while the object is an instance of the class. This function fixes this
* problem.
*
* @export
* @param {*} obj - The object to check.
* @returns {obj is HttpResponseNotImplemented} - True if the error is an instance of HttpResponseNotImplemented.
* False otherwise.
*/
function isHttpResponseNotImplemented(obj) {
return obj instanceof HttpResponseNotImplemented ||
(typeof obj === 'object' && obj !== null && obj.isHttpResponseNotImplemented === true);
}