1.0.26 • Published 10 months ago

@atptalos/common v1.0.26

Weekly downloads
-
License
ISC
Repository
-
Last release
10 months ago

Commons Module

The purpose of this module is to share code between services

Middlewares

current-user

Requirement

cookie-session middleware must be used in your Express application

import cookieSession from "cookie-session";
import express from "express";
const app = express();
app.use(
  cookieSession({
    signed: false,
    secure: process.env.NODE_ENV !== "test", //set cookies when request commes from https
  })
);

This is required since current-user middleware will validate the request contains a session property, that was included by the cookieSession middleware.

Usage

current-user will validate the jwt inside session property. If the validation succeed, the request object will receive a currentUser property with the user data

interface UserPayload {
  id: string;
  name: string;
  email: string;
}

Then the request will continue its way until it hits your route.

Use it in your express application by applying it as middleware

import { currentUser } from "@atptalos/common";

app.use(noRequireAuth)
app.use(currentUser) // Routes applied below currentUser will require the user to be authenticated
app.use(routerRequireAuth)

validateAuthorization

Validate if user data exist in the request object. If it doesn't exist. We immediately return NotAuthorizedError

Requirement

current-user middleware in your app

Example

import { validateAuthorization } from "@atptalos/common";

const router = express.Router();
router.get("/api/your/endpoint", validateAuthorization, async (req, res) => { 
    // your route logic here
    // you can access safely req.currentUser
 })

validateRequest

Validate if express validator thrown any error while validating body parameters

Requirement

express-validator middleware must be implemented on your routes.

import { validateRequest } from "@atptalos/common";
import { body } from "express-validator";

const router = express.Router();
router.post(
    "/api/your/endpoint", 
    [
    body("name").not().isEmpty().withMessage("Name is required"),
    body("email").isEmail().withMessage("Email must be valid"),
    ],
    validateRequest, async (req, res) => { 
    // your route logic here

 })

you can access safely to req.body properties since express-validator applied some validations and validateRequest middleware ensures no error was found. If any error was found during the validation, validateRequest will throw RequestValidationError

🚀 Deployment

Automated deployments in @atptalos/common

Everytime your changes in this module get merged in master, deploy-common.yaml workflow will publish a new version in the public @atptalos organizacion

1.0.26

10 months ago

1.0.25

12 months ago

1.0.24

12 months ago

1.0.23

12 months ago

1.0.20

12 months ago

1.0.19

12 months ago

1.0.18

12 months ago

1.0.17

12 months ago

1.0.16

12 months ago

1.0.15

12 months ago

1.0.14

12 months ago

1.0.13

12 months ago

1.0.12

1 year ago

1.0.11

1 year ago

1.0.10

1 year ago

1.0.9

1 year ago

1.0.8

1 year ago

1.0.7

1 year ago

1.0.6

1 year ago

1.0.2

1 year ago