@onkarnik-tickets/common 1.0.0 → 1.0.4

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.
@@ -0,0 +1,9 @@
1
+ import { CustomError } from "./custom-error";
2
+ export declare class BadRequestError extends CustomError {
3
+ statusCode: number;
4
+ constructor(message: string);
5
+ serializeErrors(): {
6
+ message: string;
7
+ field?: string;
8
+ }[];
9
+ }
@@ -0,0 +1,14 @@
1
+ import { CustomError } from "./custom-error";
2
+ export class BadRequestError extends CustomError {
3
+ statusCode = 400;
4
+ constructor(message) {
5
+ super(message);
6
+ // line for modifying in-built class
7
+ Object.setPrototypeOf(this, BadRequestError.prototype);
8
+ }
9
+ serializeErrors() {
10
+ return [
11
+ { message: this.message }
12
+ ];
13
+ }
14
+ }
@@ -0,0 +1,8 @@
1
+ export declare abstract class CustomError extends Error {
2
+ abstract statusCode: number;
3
+ constructor(message: string);
4
+ abstract serializeErrors(): {
5
+ message: string;
6
+ field?: string;
7
+ }[];
8
+ }
@@ -0,0 +1,6 @@
1
+ export class CustomError extends Error {
2
+ constructor(message) {
3
+ super(message);
4
+ Object.setPrototypeOf(this, CustomError.prototype);
5
+ }
6
+ }
@@ -0,0 +1,9 @@
1
+ import { CustomError } from "./custom-error";
2
+ export declare class DatabaseConnectionError extends CustomError {
3
+ reason: string;
4
+ statusCode: number;
5
+ constructor();
6
+ serializeErrors(): {
7
+ message: string;
8
+ }[];
9
+ }
@@ -0,0 +1,14 @@
1
+ import { CustomError } from "./custom-error";
2
+ export class DatabaseConnectionError extends CustomError {
3
+ reason = 'Error connecting to database';
4
+ statusCode = 500;
5
+ constructor() {
6
+ super('Error conencting to DB');
7
+ Object.setPrototypeOf(this, DatabaseConnectionError.prototype);
8
+ }
9
+ serializeErrors() {
10
+ return [
11
+ { message: this.reason }
12
+ ];
13
+ }
14
+ }
@@ -0,0 +1,9 @@
1
+ import { CustomError } from "./custom-error";
2
+ export declare class NotAuthorisedError extends CustomError {
3
+ statusCode: number;
4
+ constructor();
5
+ serializeErrors(): {
6
+ message: string;
7
+ field?: string;
8
+ }[];
9
+ }
@@ -0,0 +1,13 @@
1
+ import { CustomError } from "./custom-error";
2
+ export class NotAuthorisedError extends CustomError {
3
+ statusCode = 401;
4
+ constructor() {
5
+ super('Not authorised');
6
+ Object.setPrototypeOf(this, NotAuthorisedError.prototype);
7
+ }
8
+ serializeErrors() {
9
+ return [
10
+ { message: 'Not authorised' }
11
+ ];
12
+ }
13
+ }
@@ -0,0 +1,9 @@
1
+ import { CustomError } from "./custom-error";
2
+ export declare class NotFoundError extends CustomError {
3
+ statusCode: number;
4
+ constructor();
5
+ serializeErrors(): {
6
+ message: string;
7
+ field?: string;
8
+ }[];
9
+ }
@@ -0,0 +1,13 @@
1
+ import { CustomError } from "./custom-error";
2
+ export class NotFoundError extends CustomError {
3
+ statusCode = 404;
4
+ constructor() {
5
+ super('Route not found');
6
+ Object.setPrototypeOf(this, NotFoundError.prototype);
7
+ }
8
+ serializeErrors() {
9
+ return [{
10
+ message: 'Not Found'
11
+ }];
12
+ }
13
+ }
@@ -0,0 +1,14 @@
1
+ import { ValidationError } from "express-validator";
2
+ import { CustomError } from "./custom-error";
3
+ export declare class RequestValidationError extends CustomError {
4
+ errors: ValidationError[];
5
+ statusCode: number;
6
+ constructor(errors: ValidationError[]);
7
+ serializeErrors(): ({
8
+ message: any;
9
+ field: string;
10
+ } | {
11
+ message: any;
12
+ field?: undefined;
13
+ })[];
14
+ }
@@ -0,0 +1,19 @@
1
+ import { CustomError } from "./custom-error";
2
+ export class RequestValidationError extends CustomError {
3
+ errors;
4
+ statusCode = 400;
5
+ constructor(errors) {
6
+ super('Invalid request params');
7
+ this.errors = errors;
8
+ // Only because we are extending a build in class
9
+ Object.setPrototypeOf(this, RequestValidationError.prototype);
10
+ }
11
+ serializeErrors() {
12
+ return this.errors.map((err) => {
13
+ if (err.type === 'field') {
14
+ return { message: err.msg, field: err.path };
15
+ }
16
+ return { message: err.msg };
17
+ });
18
+ }
19
+ }
@@ -0,0 +1,10 @@
1
+ export * from './errors/bad-request-error';
2
+ export * from './errors/custom-error';
3
+ export * from './errors/database-connection-error';
4
+ export * from './errors/not-authorised-error';
5
+ export * from './errors/not-found-error';
6
+ export * from './errors/request-validation-error';
7
+ export * from './middlewares/current-user';
8
+ export * from './middlewares/error-handler';
9
+ export * from './middlewares/require-auth';
10
+ export * from './middlewares/validateRequest';
package/build/index.js ADDED
@@ -0,0 +1,10 @@
1
+ export * from './errors/bad-request-error';
2
+ export * from './errors/custom-error';
3
+ export * from './errors/database-connection-error';
4
+ export * from './errors/not-authorised-error';
5
+ export * from './errors/not-found-error';
6
+ export * from './errors/request-validation-error';
7
+ export * from './middlewares/current-user';
8
+ export * from './middlewares/error-handler';
9
+ export * from './middlewares/require-auth';
10
+ export * from './middlewares/validateRequest';
@@ -0,0 +1,14 @@
1
+ import { Request, Response, NextFunction } from "express";
2
+ interface UserPayload {
3
+ id: string;
4
+ email: string;
5
+ }
6
+ declare global {
7
+ namespace Express {
8
+ interface Request {
9
+ currentUser?: UserPayload;
10
+ }
11
+ }
12
+ }
13
+ export declare const currentUser: (req: Request, res: Response, next: NextFunction) => void;
14
+ export {};
@@ -0,0 +1,14 @@
1
+ import * as jwt from 'jsonwebtoken';
2
+ export const currentUser = (req, res, next) => {
3
+ if (!req.session?.jwt) {
4
+ return next();
5
+ }
6
+ try {
7
+ const payload = jwt.verify(req.session.jwt, process.env.JWT_KEY);
8
+ req.currentUser = payload;
9
+ }
10
+ catch (e) {
11
+ console.log(e);
12
+ }
13
+ next();
14
+ };
@@ -0,0 +1,2 @@
1
+ import { Request, Response, NextFunction } from "express";
2
+ export declare const errorHandler: (err: Error, req: Request, res: Response, next: NextFunction) => Response<any, Record<string, any>> | undefined;
@@ -0,0 +1,11 @@
1
+ import { CustomError } from "../errors/custom-error";
2
+ export const errorHandler = (err, req, res, next) => {
3
+ if (err instanceof CustomError) {
4
+ return res.status(err.statusCode).send({
5
+ errors: err.serializeErrors()
6
+ });
7
+ }
8
+ res.status(500).send({
9
+ errors: [{ message: 'Internal Server Error' }]
10
+ });
11
+ };
@@ -0,0 +1,2 @@
1
+ import { Request, Response, NextFunction } from "express";
2
+ export declare const requireAuth: (req: Request, res: Response, next: NextFunction) => void;
@@ -0,0 +1,7 @@
1
+ import { NotAuthorisedError } from "../errors/not-authorised-error";
2
+ export const requireAuth = (req, res, next) => {
3
+ if (!req.currentUser) {
4
+ throw new NotAuthorisedError();
5
+ }
6
+ next();
7
+ };
@@ -0,0 +1,2 @@
1
+ import { Request, Response, NextFunction } from "express";
2
+ export declare const validateRequest: (req: Request, res: Response, next: NextFunction) => void;
@@ -0,0 +1,9 @@
1
+ import { validationResult } from "express-validator";
2
+ import { RequestValidationError } from "../errors/request-validation-error";
3
+ export const validateRequest = (req, res, next) => {
4
+ const errors = validationResult(req);
5
+ if (!errors.isEmpty()) {
6
+ throw new RequestValidationError(errors.array());
7
+ }
8
+ next();
9
+ };
package/package.json CHANGED
@@ -1,13 +1,32 @@
1
1
  {
2
2
  "name": "@onkarnik-tickets/common",
3
- "version": "1.0.0",
3
+ "version": "1.0.4",
4
4
  "description": "",
5
- "main": "index.js",
5
+ "main": "./build/index.js",
6
+ "types": "./build/index.d.ts",
7
+ "files": [
8
+ "build/**/*"
9
+ ],
6
10
  "scripts": {
7
- "test": "echo \"Error: no test specified\" && exit 1"
11
+ "clean": "rimraf ./build",
12
+ "build": "npm run clean && tsc",
13
+ "pub": "git add . && git commit -m \"Updates\" && npm version patch && npm run build && npm publish"
8
14
  },
9
15
  "keywords": [],
10
16
  "author": "",
11
17
  "license": "ISC",
12
- "type": "commonjs"
18
+ "type": "commonjs",
19
+ "devDependencies": {
20
+ "rimraf": "^6.1.3",
21
+ "typescript": "^6.0.3"
22
+ },
23
+ "dependencies": {
24
+ "@types/cookie-session": "^2.0.49",
25
+ "@types/express": "^4.17.25",
26
+ "@types/jsonwebtoken": "^9.0.10",
27
+ "cookie-session": "^2.1.1",
28
+ "express": "^4.22.2",
29
+ "express-validator": "^7.3.2",
30
+ "jsonwebtoken": "^9.0.3"
31
+ }
13
32
  }