1.0.42 • Published 1 year ago

@alpha-lib/shared-lib v1.0.42

Weekly downloads
-
License
ISC
Repository
-
Last release
1 year ago

This is the shared libreary (@alpha-lib/shared-lib)

  • install with yarn
    yarn add @alpha-lib/shared-lib
  • install with npm
    npm install @alpha-lib/shared-lib

Error Format

 {
    errors: {message: string; field?: string}[],
    errorType: ErrorTypes
 }

ErrorTypes

NOT_ACTIVATED_ACCOUNT = "User Account Not Activated",
EXISTING_USER = "User already exists",
INPUT_VALIDATION_ERROR = "Input Validation Error",
INTERNAL_SERVER_ERROR = "Internal Server Error",
NOT_FOUND = "not found",
NOT_AUTHERIZED = "Unautherized Activity",
ALREADY_ACTIVATED_ACCOUNT = "Already Activated Account"

contenet of the libreary

error handling

  • comman error class
    CommonError(statusCode: number, errorTypes: ErrorTypes, errorMessage: string)
import {CommonError} from "@alpha-lib/shared-lib"

new CommonError(404, ErrorTypes.NOT_FOUND, "route not found");
  • request validation class - return INPUT_VALIDATION_ERROR\ this method return the formatted error message for the "express-validator" error result
import {RequestValidationError} from "@alpha-lib/shared-lib"
import { validationResult } from "express-validator";

const validateRequest = (req: Request, res: Response, next: NextFunction) => {
    const errors = validationResult(req);

        if (!errors.isEmpty()) {
        return next(new RequestValidationError(422, errors.array()));
    }
}

middleware

  • handled unhandled routes \ * this middleware should add after all the routes * \ becase this middleware catch all the routes and return a error as "Could not found this route"
import express from "express";
import { unhandledRouteMiddleware } from "@alpha-lib/shared-lib";

const app = express();

app.use(unhandledRouteMiddleware);
  • default error handling \ this middleware catch the all the errors and send as a formatted error
import express from "express";
import { errorMiddleware } from "@alpha-lib/shared-lib";

const app = express();

app.use(errorMiddleware);
  • validation result handling \ this middleware use to handle the "express-validator" validationResult \ this middleware should use after setting the validations
    import { Router } from "express";
    import { body } from "express-validator";
    import { requestValidationMiddleware } from "@alpha-lib/shared-lib";

    const router = Router();

    router.post(
        "/", 
        [
            body("email")
                .isEmail()
                .normalizeEmail()
        ],
        requestValidationMiddleware
    );
  • current-user-middleware \ this middleware used to get the current user details that are attached to jwt token in session \ in the session has jwt token (req.session.jwt) verify user and then set payload as req.currentUser\ if not set jwt, return to next middleware through the next();
import express from "express";
import { currentUserMiddleware } from "@alpha-lib/shared-lib";

const app = express();

app.use(currentUserMiddleware);
  • require auth middleware \ this meddileware check if user logged in or not and if not logged in return "Not Autherized" error with "401"status Code before run this it's required to run currentUserMiddleware
import express from "express";
import { currentUserMiddleware, requireAuthMiddleware } from "@alpha-lib/shared-lib";

const app = express();

app.use(currentUserMiddleware, requireAuthMiddleware);
  • file upload middleware

this add image to "upload/images" folder. This folder should create in the server

    router.post(
        "/signup",
        fileUpload.single("profilePic")
    );

    const filePath = req.file.path;
1.0.39

1 year ago

1.0.38

1 year ago

1.0.40

1 year ago

1.0.42

1 year ago

1.0.41

1 year ago

1.0.37

1 year ago

1.0.36

1 year ago

1.0.35

1 year ago

1.0.34

1 year ago

1.0.33

1 year ago

1.0.32

1 year ago

1.0.31

1 year ago

1.0.30

1 year ago

1.0.29

1 year ago

1.0.28

1 year ago

1.0.27

1 year ago

1.0.26

1 year ago

1.0.25

1 year ago

1.0.23

1 year ago

1.0.22

1 year ago

1.0.21

1 year ago

1.0.20

1 year ago

1.0.19

1 year ago

1.0.18

1 year ago

1.0.17

1 year ago

1.0.16

1 year ago

1.0.15

1 year ago

1.0.14

1 year ago

1.0.13

1 year ago

1.0.12

1 year ago

1.0.11

1 year ago

1.0.10

2 years ago

1.0.9

2 years ago

1.0.8

2 years ago

1.0.7

2 years ago

1.0.6

2 years ago

1.0.5

2 years ago

1.0.4

2 years ago

1.0.3

2 years ago

1.0.2

2 years ago

1.0.1

2 years ago