first commit

This commit is contained in:
Swapnil
2024-12-22 21:45:08 +05:30
commit 919989de52
72 changed files with 9777 additions and 0 deletions

View File

@@ -0,0 +1,17 @@
export const INTERFACE_TYPE = {
ProductRepository: Symbol.for("ProductRepository"),
ProductInteractor: Symbol.for("ProductInteractor"),
ProductController: Symbol.for("ProductController"),
Mailer: Symbol.for("Mailer"),
MessageBroker: Symbol.for("MessageBroker"),
// Add more interface type here
IamPrincipalRepository: Symbol.for("IamPrincipalRepository"),
IamPrincipalInteractor: Symbol.for("IamPrincipalInteractor"),
TokenRepository: Symbol.for("TokenRepository"),
TokenInteractor: Symbol.for("TokenInteractor"),
LoginController: Symbol.for("LoginController"),
RegistrationController: Symbol.for("RegistrationController"),
}

View File

@@ -0,0 +1,15 @@
import { Request, Response, NextFunction } from 'express';
export function AsyncHandler() {
return function (
target: object,
propertyKey: string,
descriptor: PropertyDescriptor
): void {
const originalMethod = descriptor.value;
descriptor.value = function (req: Request, res: Response, next: NextFunction) {
Promise.resolve(originalMethod.call(this, req, res, next)).catch(next);
};
};
}

View File

@@ -0,0 +1,17 @@
/**
* Create an object composed of the picked object properties.
* @function pick
*
* @param object - The source object to pick properties from.
* @param keys - The array of property names to pick from the source object.
* @returns New object with only the picked properties.
*/
export const pick = <T extends object, K extends keyof T>(object: T, keys: K[]): Pick<T, K> => {
return keys.reduce((result, key) => {
// Check if the object has the specified property
if (Object.prototype.hasOwnProperty.call(object, key)) {
result[key] = object[key]; // Assign the property to the result object
}
return result;
}, {} as Pick<T, K>); // Type the accumulator as Pick<T, K>
};

View File

@@ -0,0 +1,33 @@
class ApiError<T = unknown> extends Error {
statusCode: number;
data: T | null;
message: string;
success: boolean;
errors: Array<Error>;
isOperational: boolean;
stack?: string;
constructor(
statusCode: number,
message: string = 'Something went wrong',
errors: Array<Error> = [],
isOperational: boolean = true,
stack?: string
) {
super(message);
this.statusCode = statusCode;
this.data = null;
this.message = message;
this.success = false;
this.errors = errors;
this.isOperational = isOperational;
if (stack) {
this.stack = stack;
} else {
Error.captureStackTrace(this, this.constructor);
}
}
}
export default ApiError;

View File

@@ -0,0 +1,28 @@
class ApiResponse<T> {
statusCode: number;
data: T | null;
message: string;
success: boolean;
stack?: string; // Made optional
errors?: string [] | Error[]; // Made optional
constructor(
statusCode: number,
data: T | null,
message: string = 'Success',
options: { stack?: string; errors?: string [] | Error[] } = {}
) {
this.statusCode = statusCode;
this.data = data;
this.message = message;
this.success = statusCode < 400;
// Include `stack` and `errors` only if success is false.
if (!this.success) {
this.stack = options.stack || undefined;
this.errors = options.errors || [];
}
}
}
export default ApiResponse;

5
src/utils/index.ts Normal file
View File

@@ -0,0 +1,5 @@
export * from "./handler/async.handler";
export * from "./handler/pick.handler";
export * from "./helper/ApiError";
export * from "./helper/ApiResponse"
export * from "./constant/appConstant";