exstack
Version:
A utility library designed to simplify and enhance express.js applications.
260 lines (259 loc) • 6.61 kB
JavaScript
let node_http = require("node:http");
//#region src/status.ts
/**
* Enum representing HTTP status codes.
*
* @publicApi http-status code
*/
const HttpStatus = Object.freeze({
CONTINUE: 100,
SWITCHING_PROTOCOLS: 101,
PROCESSING: 102,
EARLY_HINTS: 103,
OK: 200,
CREATED: 201,
ACCEPTED: 202,
NON_AUTHORITATIVE_INFORMATION: 203,
NO_CONTENT: 204,
RESET_CONTENT: 205,
PARTIAL_CONTENT: 206,
MULTI_STATUS: 207,
ALREADY_REPORTED: 208,
IM_USED: 226,
MULTIPLE_CHOICES: 300,
MOVED_PERMANENTLY: 301,
FOUND: 302,
SEE_OTHER: 303,
NOT_MODIFIED: 304,
USE_PROXY: 305,
UNUSED: 306,
TEMPORARY_REDIRECT: 307,
PERMANENT_REDIRECT: 308,
BAD_REQUEST: 400,
UNAUTHORIZED: 401,
PAYMENT_REQUIRED: 402,
FORBIDDEN: 403,
NOT_FOUND: 404,
METHOD_NOT_ALLOWED: 405,
NOT_ACCEPTABLE: 406,
PROXY_AUTHENTICATION_REQUIRED: 407,
REQUEST_TIMEOUT: 408,
CONFLICT: 409,
GONE: 410,
LENGTH_REQUIRED: 411,
PRECONDITION_FAILED: 412,
PAYLOAD_TOO_LARGE: 413,
URI_TOO_LONG: 414,
UNSUPPORTED_MEDIA_TYPE: 415,
REQUESTED_RANGE_NOT_SATISFIABLE: 416,
EXPECTATION_FAILED: 417,
IM_A_TEAPOT: 418,
MISDIRECTED_REQUEST: 421,
UNPROCESSABLE_ENTITY: 422,
LOCKED: 423,
FAILED_DEPENDENCY: 424,
TOO_EARLY: 425,
UPGRADE_REQUIRED: 426,
PRECONDITION_REQUIRED: 428,
TOO_MANY_REQUESTS: 429,
REQUEST_HEADER_FIELDS_TOO_LARGE: 431,
UNAVAILABLE_FOR_LEGAL_REASONS: 451,
INTERNAL_SERVER_ERROR: 500,
NOT_IMPLEMENTED: 501,
BAD_GATEWAY: 502,
SERVICE_UNAVAILABLE: 503,
GATEWAY_TIMEOUT: 504,
HTTP_VERSION_NOT_SUPPORTED: 505,
VARIANT_ALSO_NEGOTIATES: 506,
INSUFFICIENT_STORAGE: 507,
LOOP_DETECTED: 508,
BANDWIDTH_LIMIT_EXCEEDED: 509,
NOT_EXTENDED: 510,
NETWORK_AUTHENTICATION_REQUIRED: 511
});
//#endregion
//#region src/helps/errors.ts
const CLEAN_RE = /^\d+|[^a-zA-Z0-9 ]+/g;
const nameCache = /* @__PURE__ */ new Map();
/**
* Get a human-readable error name from the HTTP status code.
* @param {number} status - The HTTP status code.
* @returns {string} - The formatted error name.
*/
const getErrorName = (status) => {
if (status < 400 || status > 511) return "HttpError";
const rawName = node_http.STATUS_CODES[status];
if (!rawName) return "HttpError";
const camel = rawName.replace(CLEAN_RE, "").split(/\s+/).map((word) => word.charAt(0).toUpperCase() + word.slice(1)).join("");
const finalName = camel.endsWith("Error") ? camel : `${camel}Error`;
nameCache.set(status, finalName);
return finalName;
};
/**
* Base class for handling HTTP errors.
* @extends {Error}
*/
var HttpError = class HttpError extends Error {
status;
options;
/**
* Creates an instance of `HTTPException`.
* @param status - HTTP status code for the exception. Defaults to 500.
* @param options - Additional options for the exception.
*/
constructor(status = HttpStatus.INTERNAL_SERVER_ERROR, options) {
super(typeof options.message === "string" ? options.message : getErrorName(status));
this.status = status;
this.options = options;
this.name = getErrorName(status);
Error.captureStackTrace(this, this.constructor);
}
/**
* Check if the given error is an instance of HttpError.
* @param {unknown} value - The error to check.
* @returns {boolean} - True if the error is an instance of HttpError, false otherwise.
*
* @example
* if (HttpError.isHttpError(error)) {
* // Handle the HttpError
* }
*/
static isHttpError = (value) => value instanceof HttpError;
/**
* Convert the HttpError instance to a Body object.
* @example
* const errorBody = new HttpError(404, {message: 'Not Found'}).body;
*/
get body() {
const { name: error, status } = this;
const { message, meta, code } = this.options;
return {
status,
error,
message,
code,
meta
};
}
/**
* Send the json of the error in an HTTP response.
* @param {Response} res - The Express response object.
*
* @example
* new HttpError(404, {message: 'Not Found'}).toJson(res);
*/
toJson = (res) => {
res.status(this.status).json(this.body);
};
};
/**
* Utility function to create custom error classes.
* @param status - HTTP status code.
* @returns - A new error class.
* @example
* const NotFoundError = createHttpErrorClass(HttpStatus.NOT_FOUND);
*/
const createHttpErrorClass = (status) => class extends HttpError {
constructor(message, options = Object.create(null)) {
super(status, {
message,
...options
});
}
};
/**
* Represents a Bad Request HTTP error (400).
* @extends {HttpError}
*/
const BadRequestError = createHttpErrorClass(HttpStatus.BAD_REQUEST);
/**
* Represents a Conflict HTTP error (409).
* @extends {HttpError}
*/
const ConflictError = createHttpErrorClass(HttpStatus.CONFLICT);
/**
* Represents a Forbidden HTTP error (403).
* @extends {HttpError}
*/
const ForbiddenError = createHttpErrorClass(HttpStatus.FORBIDDEN);
/**
* Represents a Not Found HTTP error (404).
* @extends {HttpError}
*/
const NotFoundError = createHttpErrorClass(HttpStatus.NOT_FOUND);
/**
* Represents an UnAuthorized HTTP error (401).
* @extends {HttpError}
*/
const UnAuthorizedError = createHttpErrorClass(HttpStatus.UNAUTHORIZED);
/**
* Represents an Internal Server Error HTTP error (500).
* @extends {HttpError}
*/
const InternalServerError = createHttpErrorClass(HttpStatus.INTERNAL_SERVER_ERROR);
/**
* Represents an Content Too Larger Error HTTP error (413).
* @extends {HttpError}
*/
const ContentTooLargeError = createHttpErrorClass(HttpStatus.PAYLOAD_TOO_LARGE);
//#endregion
Object.defineProperty(exports, "BadRequestError", {
enumerable: true,
get: function() {
return BadRequestError;
}
});
Object.defineProperty(exports, "ConflictError", {
enumerable: true,
get: function() {
return ConflictError;
}
});
Object.defineProperty(exports, "ContentTooLargeError", {
enumerable: true,
get: function() {
return ContentTooLargeError;
}
});
Object.defineProperty(exports, "ForbiddenError", {
enumerable: true,
get: function() {
return ForbiddenError;
}
});
Object.defineProperty(exports, "HttpError", {
enumerable: true,
get: function() {
return HttpError;
}
});
Object.defineProperty(exports, "HttpStatus", {
enumerable: true,
get: function() {
return HttpStatus;
}
});
Object.defineProperty(exports, "InternalServerError", {
enumerable: true,
get: function() {
return InternalServerError;
}
});
Object.defineProperty(exports, "NotFoundError", {
enumerable: true,
get: function() {
return NotFoundError;
}
});
Object.defineProperty(exports, "UnAuthorizedError", {
enumerable: true,
get: function() {
return UnAuthorizedError;
}
});
Object.defineProperty(exports, "createHttpErrorClass", {
enumerable: true,
get: function() {
return createHttpErrorClass;
}
});