biz-email-builder-shared 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (46) hide show
  1. package/entity/index.d.ts +2 -0
  2. package/entity/index.d.ts.map +1 -0
  3. package/entity/index.js +1 -0
  4. package/entity/user.entity.d.ts +38 -0
  5. package/entity/user.entity.d.ts.map +1 -0
  6. package/entity/user.entity.js +10 -0
  7. package/index.d.ts +6 -0
  8. package/index.d.ts.map +1 -0
  9. package/index.js +5 -0
  10. package/middleware/authentication.d.ts +5 -0
  11. package/middleware/authentication.d.ts.map +1 -0
  12. package/middleware/authentication.js +30 -0
  13. package/middleware/index.d.ts +2 -0
  14. package/middleware/index.d.ts.map +1 -0
  15. package/middleware/index.js +1 -0
  16. package/package.json +24 -0
  17. package/resHandler/errorHandler.d.ts +17 -0
  18. package/resHandler/errorHandler.d.ts.map +1 -0
  19. package/resHandler/errorHandler.js +50 -0
  20. package/resHandler/index.d.ts +3 -0
  21. package/resHandler/index.d.ts.map +1 -0
  22. package/resHandler/index.js +2 -0
  23. package/resHandler/successHandler.d.ts +7 -0
  24. package/resHandler/successHandler.d.ts.map +1 -0
  25. package/resHandler/successHandler.js +3 -0
  26. package/types/IController.d.ts +6 -0
  27. package/types/IController.d.ts.map +1 -0
  28. package/types/IController.js +1 -0
  29. package/types/IRequest.d.ts +8 -0
  30. package/types/IRequest.d.ts.map +1 -0
  31. package/types/IRequest.js +1 -0
  32. package/types/index.d.ts +3 -0
  33. package/types/index.d.ts.map +1 -0
  34. package/types/index.js +2 -0
  35. package/utilities/callWithRetries.d.ts +2 -0
  36. package/utilities/callWithRetries.d.ts.map +1 -0
  37. package/utilities/callWithRetries.js +11 -0
  38. package/utilities/createFolder.d.ts +2 -0
  39. package/utilities/createFolder.d.ts.map +1 -0
  40. package/utilities/createFolder.js +11 -0
  41. package/utilities/encryptionUtils.d.ts +4 -0
  42. package/utilities/encryptionUtils.d.ts.map +1 -0
  43. package/utilities/encryptionUtils.js +19 -0
  44. package/utilities/index.d.ts +10 -0
  45. package/utilities/index.d.ts.map +1 -0
  46. package/utilities/index.js +12 -0
@@ -0,0 +1,2 @@
1
+ export * from "./user.entity";
2
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/entity/index.ts"],"names":[],"mappings":"AAAA,cAAc,eAAe,CAAC"}
@@ -0,0 +1 @@
1
+ export * from "./user.entity";
@@ -0,0 +1,38 @@
1
+ /// <reference types="mongoose/types/aggregate" />
2
+ /// <reference types="mongoose/types/callback" />
3
+ /// <reference types="mongoose/types/collection" />
4
+ /// <reference types="mongoose/types/connection" />
5
+ /// <reference types="mongoose/types/cursor" />
6
+ /// <reference types="mongoose/types/document" />
7
+ /// <reference types="mongoose/types/error" />
8
+ /// <reference types="mongoose/types/expressions" />
9
+ /// <reference types="mongoose/types/helpers" />
10
+ /// <reference types="mongoose/types/middlewares" />
11
+ /// <reference types="mongoose/types/indexes" />
12
+ /// <reference types="mongoose/types/models" />
13
+ /// <reference types="mongoose/types/mongooseoptions" />
14
+ /// <reference types="mongoose/types/pipelinestage" />
15
+ /// <reference types="mongoose/types/populate" />
16
+ /// <reference types="mongoose/types/query" />
17
+ /// <reference types="mongoose/types/schemaoptions" />
18
+ /// <reference types="mongoose/types/schematypes" />
19
+ /// <reference types="mongoose/types/session" />
20
+ /// <reference types="mongoose/types/types" />
21
+ /// <reference types="mongoose/types/utility" />
22
+ /// <reference types="mongoose/types/validation" />
23
+ /// <reference types="mongoose/types/virtuals" />
24
+ /// <reference types="mongoose" />
25
+ /// <reference types="mongoose/types/inferschematype" />
26
+ interface IUser {
27
+ email: string | null;
28
+ password: string;
29
+ role: string;
30
+ createdAt: Date;
31
+ updatedAt: Date;
32
+ deletedAt: Date;
33
+ }
34
+ export declare const UserModel: import("mongoose").Model<IUser, {}, {}, {}, import("mongoose").Document<unknown, {}, IUser> & Omit<IUser & {
35
+ _id: import("mongoose").Types.ObjectId;
36
+ }, never>, any>;
37
+ export {};
38
+ //# sourceMappingURL=user.entity.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"user.entity.d.ts","sourceRoot":"","sources":["../../src/entity/user.entity.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;AAEA,UAAU,KAAK;IACb,KAAK,EAAE,MAAM,GAAG,IAAI,CAAC;IACrB,QAAQ,EAAE,MAAM,CAAC;IACjB,IAAI,EAAE,MAAM,CAAC;IACb,SAAS,EAAE,IAAI,CAAC;IAChB,SAAS,EAAE,IAAI,CAAC;IAChB,SAAS,EAAE,IAAI,CAAA;CAChB;AAcD,eAAO,MAAM,SAAS;;eAAmC,CAAC"}
@@ -0,0 +1,10 @@
1
+ import { Schema, model } from "mongoose";
2
+ const UserSchema = new Schema({
3
+ email: { type: String, unique: true, sparse: true },
4
+ password: { type: String, default: null },
5
+ role: { type: String, default: null },
6
+ deletedAt: { type: Date, default: null },
7
+ }, {
8
+ timestamps: true,
9
+ });
10
+ export const UserModel = model("user", UserSchema);
package/index.d.ts ADDED
@@ -0,0 +1,6 @@
1
+ export * from "./entity";
2
+ export * from "./middleware";
3
+ export * from "./resHandler";
4
+ export * from "./types";
5
+ export * from "./utilities";
6
+ //# sourceMappingURL=index.d.ts.map
package/index.d.ts.map ADDED
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,UAAU,CAAC;AACzB,cAAc,cAAc,CAAC;AAC7B,cAAc,cAAc,CAAC;AAC7B,cAAc,SAAS,CAAC;AACxB,cAAc,aAAa,CAAC"}
package/index.js ADDED
@@ -0,0 +1,5 @@
1
+ export * from "./entity";
2
+ export * from "./middleware";
3
+ export * from "./resHandler";
4
+ export * from "./types";
5
+ export * from "./utilities";
@@ -0,0 +1,5 @@
1
+ import { Response, NextFunction } from "express";
2
+ import { IRequest } from "../types";
3
+ declare const authorize: (roles: string[]) => (req: IRequest, res: Response, next: NextFunction) => Promise<Response<any, Record<string, any>> | undefined>;
4
+ export { authorize };
5
+ //# sourceMappingURL=authentication.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"authentication.d.ts","sourceRoot":"","sources":["../../src/middleware/authentication.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,YAAY,EAAE,MAAM,SAAS,CAAC;AAEjD,OAAO,EAAE,QAAQ,EAAE,MAAM,UAAU,CAAC;AAIpC,QAAA,MAAM,SAAS,UAAW,MAAM,EAAE,WACJ,QAAQ,OAAO,QAAQ,QAAQ,YAAY,4DA+BxE,CAAA;AAGD,OAAO,EAAC,SAAS,EAAC,CAAC"}
@@ -0,0 +1,30 @@
1
+ import { verifyUid } from "../utilities";
2
+ import { UserModel } from "../entity";
3
+ const authorize = (roles) => {
4
+ return async function (req, res, next) {
5
+ if (!req.headers.authorization) {
6
+ return res.status(401).json({ message: 'Unauthorized' });
7
+ }
8
+ if (!roles.length) {
9
+ return res.status(401).json({ message: 'Unauthorized' });
10
+ }
11
+ if (req.headers.authorization) {
12
+ const token = await verifyUid(req.headers.authorization);
13
+ if (!token) {
14
+ return res.status(401).json({ message: 'Session Expired' });
15
+ }
16
+ let user = await UserModel.findOne({ email: token.value.email });
17
+ if (!user) {
18
+ return res.status(401).json({ message: 'No User Found' });
19
+ }
20
+ const hasAccess = roles.find(role => role === user.role);
21
+ if (!hasAccess) {
22
+ return res.status(403).json({ message: 'Forbbiden' });
23
+ }
24
+ const { createdAt, updatedAt, deletedAt, password, ...rest } = user.toObject();
25
+ req.user = { ...rest, userId: rest.email };
26
+ next();
27
+ }
28
+ };
29
+ };
30
+ export { authorize };
@@ -0,0 +1,2 @@
1
+ export * from "./authentication";
2
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/middleware/index.ts"],"names":[],"mappings":"AAAA,cAAc,kBAAkB,CAAC"}
@@ -0,0 +1 @@
1
+ export * from "./authentication";
package/package.json ADDED
@@ -0,0 +1,24 @@
1
+ {
2
+ "name": "biz-email-builder-shared",
3
+ "version": "1.0.0",
4
+ "main": "index.js",
5
+ "types": "index.d.ts",
6
+ "scripts": {
7
+ "test": "echo \"Error: no test specified\" && exit 1",
8
+ "build": "tsc"
9
+ },
10
+ "dependencies": {
11
+ "express": "^4.18.2",
12
+ "joi": "^17.11.0",
13
+ "jsonwebtoken": "^9.0.2",
14
+ "typescript": "^5.3.3"
15
+ },
16
+ "devDependencies": {
17
+ "@types/express": "^4.17.21",
18
+ "@types/jsonwebtoken": "^9.0.5"
19
+ },
20
+ "keywords": [],
21
+ "author": "",
22
+ "license": "ISC",
23
+ "description": ""
24
+ }
@@ -0,0 +1,17 @@
1
+ import { Request, Response, NextFunction } from 'express';
2
+ export declare class BadRequestError extends Error {
3
+ constructor(message: string);
4
+ }
5
+ export declare class NotFoundRequestError extends Error {
6
+ constructor(message: string);
7
+ }
8
+ export declare class CustomRequestError {
9
+ name: string;
10
+ message: {
11
+ title: string;
12
+ description: string;
13
+ };
14
+ constructor(title: string, description: string);
15
+ }
16
+ export declare const errorHandler: (err: Error, req: Request, res: Response, next: NextFunction) => void;
17
+ //# sourceMappingURL=errorHandler.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"errorHandler.d.ts","sourceRoot":"","sources":["../../src/resHandler/errorHandler.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,QAAQ,EAAE,YAAY,EAAE,MAAM,SAAS,CAAC;AAE1D,qBAAa,eAAgB,SAAQ,KAAK;gBAC5B,OAAO,EAAE,MAAM;CAK5B;AAED,qBAAa,oBAAqB,SAAQ,KAAK;gBACjC,OAAO,EAAE,MAAM;CAK5B;AAED,qBAAa,kBAAkB;IAC7B,IAAI,EAAE,MAAM,CAAC;IACb,OAAO,EAAE;QACP,KAAK,EAAE,MAAM,CAAC;QACd,WAAW,EAAE,MAAM,CAAA;KACpB,CAAC;gBAEU,KAAK,EAAE,MAAM,EAAE,WAAW,EAAE,MAAM;CAO/C;AAKD,eAAO,MAAM,YAAY,QAClB,KAAK,OACL,OAAO,OACP,QAAQ,QACP,YAAY,SAwBnB,CAAC"}
@@ -0,0 +1,50 @@
1
+ export class BadRequestError extends Error {
2
+ constructor(message) {
3
+ super(message);
4
+ Object.setPrototypeOf(this, BadRequestError.prototype);
5
+ this.name = 'BadRequestError';
6
+ }
7
+ }
8
+ export class NotFoundRequestError extends Error {
9
+ constructor(message) {
10
+ super(message);
11
+ Object.setPrototypeOf(this, NotFoundRequestError.prototype);
12
+ this.name = 'NotFoundRequestError';
13
+ }
14
+ }
15
+ export class CustomRequestError {
16
+ name;
17
+ message;
18
+ constructor(title, description) {
19
+ this.name = 'CustomRequestError';
20
+ this.message = {
21
+ title,
22
+ description
23
+ };
24
+ }
25
+ }
26
+ // Custom error handler middleware
27
+ export const errorHandler = (err, req, res, next) => {
28
+ let statusCode = 500; // Default status code
29
+ let errorMessage = 'Internal Server Error'; // Default error message
30
+ if (err instanceof BadRequestError) {
31
+ statusCode = 400;
32
+ errorMessage = err.message;
33
+ }
34
+ else if (err instanceof NotFoundRequestError) {
35
+ statusCode = 404;
36
+ errorMessage = err.message;
37
+ }
38
+ else if (err instanceof CustomRequestError) {
39
+ statusCode = 422;
40
+ errorMessage = err.message;
41
+ }
42
+ else {
43
+ console.error(err.stack); // Log the error for debugging
44
+ }
45
+ // Send an error response
46
+ res.status(statusCode).json({
47
+ message: errorMessage,
48
+ error: err.name // Optionally send the error name/type
49
+ });
50
+ };
@@ -0,0 +1,3 @@
1
+ export * from "./errorHandler";
2
+ export * from "./successHandler";
3
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/resHandler/index.ts"],"names":[],"mappings":"AAAA,cAAc,gBAAgB,CAAC;AAC/B,cAAc,kBAAkB,CAAC"}
@@ -0,0 +1,2 @@
1
+ export * from "./errorHandler";
2
+ export * from "./successHandler";
@@ -0,0 +1,7 @@
1
+ import { Request, Response, NextFunction } from 'express';
2
+ export interface IResponseData {
3
+ data: any;
4
+ message: string;
5
+ }
6
+ export declare const successHandler: (data: IResponseData, req: Request, res: Response, next: NextFunction) => void;
7
+ //# sourceMappingURL=successHandler.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"successHandler.d.ts","sourceRoot":"","sources":["../../src/resHandler/successHandler.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,QAAQ,EAAE,YAAY,EAAE,MAAM,SAAS,CAAC;AAI1D,MAAM,WAAW,aAAa;IAC5B,IAAI,EAAE,GAAG,CAAC;IACV,OAAO,EAAE,MAAM,CAAC;CACjB;AAED,eAAO,MAAM,cAAc,SACnB,aAAa,OACd,OAAO,OACP,QAAQ,QACP,YAAY,SAGnB,CAAC"}
@@ -0,0 +1,3 @@
1
+ export const successHandler = (data, req, res, next) => {
2
+ res.status(200).json(data);
3
+ };
@@ -0,0 +1,6 @@
1
+ import { Response } from "express";
2
+ import { IRequest } from "./IRequest";
3
+ export interface IController {
4
+ (req: IRequest, res: Response): void;
5
+ }
6
+ //# sourceMappingURL=IController.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"IController.d.ts","sourceRoot":"","sources":["../../src/types/IController.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,MAAM,SAAS,CAAC;AACnC,OAAO,EAAE,QAAQ,EAAE,MAAM,YAAY,CAAC;AAEtC,MAAM,WAAW,WAAW;IAC1B,CAAC,GAAG,EAAE,QAAQ,EAAE,GAAG,EAAE,QAAQ,GAAG,IAAI,CAAC;CACtC"}
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,8 @@
1
+ import { Request } from "express";
2
+ export interface IRequest extends Request {
3
+ user?: any;
4
+ role?: any;
5
+ file?: any;
6
+ features?: Array<any>;
7
+ }
8
+ //# sourceMappingURL=IRequest.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"IRequest.d.ts","sourceRoot":"","sources":["../../src/types/IRequest.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,OAAO,EAAE,MAAM,SAAS,CAAC;AAElC,MAAM,WAAW,QAAS,SAAQ,OAAO;IACrC,IAAI,CAAC,EAAE,GAAG,CAAC;IACX,IAAI,CAAC,EAAE,GAAG,CAAC;IACX,IAAI,CAAC,EAAC,GAAG,CAAC;IACV,QAAQ,CAAC,EAAE,KAAK,CAAC,GAAG,CAAC,CAAA;CACxB"}
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,3 @@
1
+ export * from "./IController";
2
+ export * from "./IRequest";
3
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/types/index.ts"],"names":[],"mappings":"AAAA,cAAc,eAAe,CAAC;AAC9B,cAAc,YAAY,CAAC"}
package/types/index.js ADDED
@@ -0,0 +1,2 @@
1
+ export * from "./IController";
2
+ export * from "./IRequest";
@@ -0,0 +1,2 @@
1
+ export declare function callWithRetries(retryCount: number, failedMessage: string, functionRef: Function, ...args: any): Promise<any>;
2
+ //# sourceMappingURL=callWithRetries.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"callWithRetries.d.ts","sourceRoot":"","sources":["../../src/utilities/callWithRetries.ts"],"names":[],"mappings":"AAAA,wBAAsB,eAAe,CAAC,UAAU,EAAE,MAAM,EAAE,aAAa,EAAE,MAAM,EAAE,WAAW,EAAE,QAAQ,EAAE,GAAG,IAAI,EAAE,GAAG,GAAG,OAAO,CAAC,GAAG,CAAC,CAQlI"}
@@ -0,0 +1,11 @@
1
+ export async function callWithRetries(retryCount, failedMessage, functionRef, ...args) {
2
+ try {
3
+ return await functionRef(...args);
4
+ }
5
+ catch (error) {
6
+ if (retryCount <= 0)
7
+ throw error;
8
+ console.log("callWithRetries", error?.message || error);
9
+ return callWithRetries(retryCount - 1, failedMessage, functionRef, ...args);
10
+ }
11
+ }
@@ -0,0 +1,2 @@
1
+ export declare function createFolder(folderPath: string): Promise<void>;
2
+ //# sourceMappingURL=createFolder.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"createFolder.d.ts","sourceRoot":"","sources":["../../src/utilities/createFolder.ts"],"names":[],"mappings":"AAEA,wBAAsB,YAAY,CAAC,UAAU,EAAE,MAAM,iBAQpD"}
@@ -0,0 +1,11 @@
1
+ import fs from "fs";
2
+ export async function createFolder(folderPath) {
3
+ try {
4
+ if (!fs.existsSync(folderPath)) {
5
+ fs.mkdirSync(folderPath);
6
+ }
7
+ }
8
+ catch (error) {
9
+ console.error(`Error: ${error.message}`);
10
+ }
11
+ }
@@ -0,0 +1,4 @@
1
+ declare const signUid: (value: object) => string;
2
+ declare const verifyUid: (token: string) => Promise<any>;
3
+ export { signUid, verifyUid };
4
+ //# sourceMappingURL=encryptionUtils.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"encryptionUtils.d.ts","sourceRoot":"","sources":["../../src/utilities/encryptionUtils.ts"],"names":[],"mappings":"AAEA,QAAA,MAAM,OAAO,UAAW,MAAM,WAI7B,CAAC;AAEF,QAAA,MAAM,SAAS,UAAiB,MAAM,KAAG,QAAQ,GAAG,CAgBnD,CAAC;AAEF,OAAO,EAAE,OAAO,EAAE,SAAS,EAAE,CAAA"}
@@ -0,0 +1,19 @@
1
+ import jwt from 'jsonwebtoken';
2
+ const signUid = (value) => {
3
+ return jwt.sign({ value }, process.env.JWTSECRET || 'secret', {
4
+ expiresIn: '8h' // expires in 8 hours
5
+ });
6
+ };
7
+ const verifyUid = async (token) => new Promise(resolve => {
8
+ const jwtToken = token.split(" ")[1] || token;
9
+ jwt.verify(jwtToken, process.env.JWTSECRET || 'secret', (err, decoded) => {
10
+ if (err) {
11
+ // console.log("error verifyUid", err)
12
+ resolve(null);
13
+ }
14
+ else {
15
+ resolve(decoded);
16
+ }
17
+ });
18
+ });
19
+ export { signUid, verifyUid };
@@ -0,0 +1,10 @@
1
+ export * from "./callWithRetries";
2
+ export * from "./createFolder";
3
+ export * from "./encryptionUtils";
4
+ export declare enum ROLES {
5
+ ADMIN = "ADMIN",
6
+ USER = "USER"
7
+ }
8
+ export declare enum FEATURES {
9
+ }
10
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/utilities/index.ts"],"names":[],"mappings":"AAAA,cAAc,mBAAmB,CAAC;AAClC,cAAc,gBAAgB,CAAC;AAC/B,cAAc,mBAAmB,CAAC;AAElC,oBAAY,KAAK;IACf,KAAK,UAAU;IACf,IAAI,SAAS;CACd;AAED,oBAAY,QAAQ;CAEnB"}
@@ -0,0 +1,12 @@
1
+ export * from "./callWithRetries";
2
+ export * from "./createFolder";
3
+ export * from "./encryptionUtils";
4
+ export var ROLES;
5
+ (function (ROLES) {
6
+ ROLES["ADMIN"] = "ADMIN";
7
+ ROLES["USER"] = "USER";
8
+ })(ROLES || (ROLES = {}));
9
+ export var FEATURES;
10
+ (function (FEATURES) {
11
+ // We will add features here
12
+ })(FEATURES || (FEATURES = {}));