exp-core
Version:
Core utilities and middleware for building robust Express applications, including standard API responses, async error handling, request sanitization, and extended request types.
99 lines (93 loc) • 3.13 kB
JavaScript
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
// src/index.ts
var index_exports = {};
__export(index_exports, {
ApiError: () => ApiError_lib_default,
aw: () => aw_middleware_default,
errorResponse: () => errorResponse,
initRequestBody: () => body_middleware_default,
successResponse: () => successResponse
});
module.exports = __toCommonJS(index_exports);
// src/utils/apiResponse.util.ts
var successResponse = (res, options) => {
if (!options.statusCode) options.statusCode = 200;
return apiResponse(res, true, options);
};
var errorResponse = (res, options) => {
if (!options.statusCode) options.statusCode = 400;
return apiResponse(res, false, options);
};
function apiResponse(res, isSuccess, options) {
const responseObject = {
isSuccess,
message: options.message,
...options.meta && { meta: options.meta },
...options.data && { data: options.data },
...options.errors && { errors: options.errors }
};
res.status(options.statusCode).json(responseObject);
}
// src/middlewares/aw.middleware.ts
var aw = (requestHandler) => {
return (req, res, next) => {
Promise.resolve(requestHandler(req, res, next)).catch((error) => {
const message = error.message || "An unexpected error occurred. Please try again later." /* SERVER_ERROR */;
return errorResponse(res, {
message,
statusCode: error.statusCode || 400,
errors: error.errors || null
});
});
};
};
var aw_middleware_default = aw;
// src/libs/ApiError.lib.ts
var ApiError = class extends Error {
/**
* Creates a new ApiError instance.
*
* @param {string} message - A human-readable error message.
* @param {number} [statusCode=400] - Optional HTTP status code.
* @param {Record<string, string[]> | null} [errors=null] - Optional detailed errors.
*/
constructor(message, statusCode = 400, errors = null) {
super(message);
this.statusCode = statusCode;
this.errors = errors;
}
};
var ApiError_lib_default = ApiError;
// src/middlewares/body.middleware.ts
var initRequestBody = aw_middleware_default(
async (req, res, next) => {
if (!req.body) req.body = {};
next();
}
);
var body_middleware_default = initRequestBody;
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
ApiError,
aw,
errorResponse,
initRequestBody,
successResponse
});
;