@raymanselltickets/common 1.0.1 → 1.0.3

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.js';
2
+ export declare class BadRequestError extends CustomError {
3
+ message: string;
4
+ statusCode: number;
5
+ constructor(message: string);
6
+ serializeErrors(): {
7
+ message: string;
8
+ }[];
9
+ }
@@ -0,0 +1,18 @@
1
+ import { CustomError } from './custom-error.js';
2
+ export class BadRequestError extends CustomError {
3
+ message;
4
+ statusCode = 400;
5
+ constructor(message) {
6
+ super(message);
7
+ this.message = message;
8
+ // only because we are extending a built in class
9
+ // Object.setPrototypeOf(this, BadRequestError.prototype);
10
+ }
11
+ serializeErrors() {
12
+ return [
13
+ {
14
+ message: this.message,
15
+ },
16
+ ];
17
+ }
18
+ }
@@ -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,7 @@
1
+ export class CustomError extends Error {
2
+ constructor(message) {
3
+ super(message);
4
+ // only because we are extending a built in class
5
+ // Object.setPrototypeOf(this, CustomError.prototype)
6
+ }
7
+ }
@@ -0,0 +1,9 @@
1
+ import { CustomError } from './custom-error.js';
2
+ export declare class DatabaseConnectionError extends CustomError {
3
+ statusCode: number;
4
+ reason: string;
5
+ constructor();
6
+ serializeErrors(): {
7
+ message: string;
8
+ }[];
9
+ }
@@ -0,0 +1,17 @@
1
+ import { CustomError } from './custom-error.js';
2
+ export class DatabaseConnectionError extends CustomError {
3
+ statusCode = 500;
4
+ reason = 'Error connecting to database';
5
+ constructor() {
6
+ super('Error connecting to database');
7
+ // only because we are extending a built in class
8
+ // Object.setPrototypeOf(this, DatabaseConnectionError.prototype)
9
+ }
10
+ serializeErrors() {
11
+ return [
12
+ {
13
+ message: this.reason,
14
+ },
15
+ ];
16
+ }
17
+ }
@@ -0,0 +1,9 @@
1
+ import { CustomError } from './custom-error.js';
2
+ export declare class NotAuthorizedError extends CustomError {
3
+ statusCode: number;
4
+ constructor();
5
+ serializeErrors(): {
6
+ message: string;
7
+ field?: string;
8
+ }[];
9
+ }
@@ -0,0 +1,16 @@
1
+ import { CustomError } from './custom-error.js';
2
+ export class NotAuthorizedError extends CustomError {
3
+ statusCode = 401;
4
+ constructor() {
5
+ super('Not authorized');
6
+ // only because we are extending a built in class
7
+ // Object.setPrototypeOf(this, NotAuthorizedError.prototype)
8
+ }
9
+ serializeErrors() {
10
+ return [
11
+ {
12
+ message: 'Not authorized',
13
+ },
14
+ ];
15
+ }
16
+ }
@@ -0,0 +1,8 @@
1
+ import { CustomError } from './custom-error.js';
2
+ export declare class NotFoundError extends CustomError {
3
+ statusCode: number;
4
+ constructor();
5
+ serializeErrors(): {
6
+ message: string;
7
+ }[];
8
+ }
@@ -0,0 +1,16 @@
1
+ import { CustomError } from './custom-error.js';
2
+ export class NotFoundError extends CustomError {
3
+ statusCode = 404;
4
+ constructor() {
5
+ super('Route not found');
6
+ // only because we are extending a built in class
7
+ // Object.setPrototypeOf(this, NotFoundError.prototype);
8
+ }
9
+ serializeErrors() {
10
+ return [
11
+ {
12
+ message: 'Not Found',
13
+ },
14
+ ];
15
+ }
16
+ }
@@ -0,0 +1,14 @@
1
+ import { ValidationError } from 'express-validator';
2
+ import { CustomError } from './custom-error.js';
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,24 @@
1
+ import { CustomError } from './custom-error.js';
2
+ export class RequestValidationError extends CustomError {
3
+ errors;
4
+ statusCode = 400;
5
+ constructor(errors) {
6
+ super('Invalid request parameters');
7
+ this.errors = errors;
8
+ // only because we are extending a built in class
9
+ // Object.setPrototypeOf(this, RequestValidationError.prototype)
10
+ }
11
+ serializeErrors() {
12
+ return this.errors.map((error) => {
13
+ if (error.type === 'field') {
14
+ return {
15
+ message: error.msg,
16
+ field: error.path,
17
+ };
18
+ }
19
+ return {
20
+ message: error.msg,
21
+ };
22
+ });
23
+ }
24
+ }
package/build/index.d.ts CHANGED
@@ -1,7 +1,9 @@
1
- interface Color {
2
- red: number;
3
- blue: number;
4
- green: number;
5
- }
6
- declare function printColor(color: Color): void;
7
- export default printColor;
1
+ export * from './errors/bad-request-error.js';
2
+ export * from './errors/custom-error.js';
3
+ export * from './errors/database-connection-error.js';
4
+ export * from './errors/not-authorized-error.js';
5
+ export * from './errors/request-validation-error.js';
6
+ export * from './middlewares/current-user.js';
7
+ export * from './middlewares/error-handler.js';
8
+ export * from './middlewares/require-auth.js';
9
+ export * from './middlewares/validate-request.js';
package/build/index.js CHANGED
@@ -1,10 +1,9 @@
1
- const color = {
2
- red: 10,
3
- blue: 10,
4
- green: 10,
5
- };
6
- function printColor(color) {
7
- console.log(color);
8
- }
9
- printColor(color);
10
- export default printColor;
1
+ export * from './errors/bad-request-error.js';
2
+ export * from './errors/custom-error.js';
3
+ export * from './errors/database-connection-error.js';
4
+ export * from './errors/not-authorized-error.js';
5
+ export * from './errors/request-validation-error.js';
6
+ export * from './middlewares/current-user.js';
7
+ export * from './middlewares/error-handler.js';
8
+ export * from './middlewares/require-auth.js';
9
+ export * from './middlewares/validate-request.js';
@@ -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,12 @@
1
+ import 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 (error) { }
11
+ next();
12
+ };
@@ -0,0 +1,2 @@
1
+ import { Request, Response, NextFunction } from 'express';
2
+ export declare function errorHandler(err: Error, req: Request, res: Response, next: NextFunction): Response<any, Record<string, any>> | undefined;
@@ -0,0 +1,13 @@
1
+ import { CustomError } from '../../../common/src/errors/custom-error.js';
2
+ export function errorHandler(err, req, res, next) {
3
+ if (err instanceof CustomError) {
4
+ return res.status(err.statusCode).send({ errors: err.serializeErrors() });
5
+ }
6
+ res.status(400).send({
7
+ errors: [
8
+ {
9
+ message: 'Something went wrong',
10
+ },
11
+ ],
12
+ });
13
+ }
@@ -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 { NotAuthorizedError } from '../../../common/src/errors/not-authorized-error.js';
2
+ export const requireAuth = (req, res, next) => {
3
+ if (!req.currentUser) {
4
+ throw new NotAuthorizedError();
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 '../../../common/src/errors/request-validation-error.js';
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,6 +1,6 @@
1
1
  {
2
2
  "name": "@raymanselltickets/common",
3
- "version": "1.0.1",
3
+ "version": "1.0.3",
4
4
  "description": "",
5
5
  "main": "./build/index.js",
6
6
  "types": "./build/index.d.ts",
@@ -9,7 +9,8 @@
9
9
  ],
10
10
  "scripts": {
11
11
  "clean": "del ./build/*",
12
- "build": "npm run clean && tsc"
12
+ "build": "npm run clean && tsc",
13
+ "pub": "git add . && git commit -m \"Updates\" && npm version patch && npm run build && npm publish"
13
14
  },
14
15
  "keywords": [],
15
16
  "author": "",
@@ -17,8 +18,17 @@
17
18
  "type": "module",
18
19
  "devDependencies": {
19
20
  "@tsconfig/node24": "^24.0.3",
21
+ "@types/cookie-session": "^2.0.49",
22
+ "@types/express": "^5.0.6",
23
+ "@types/jsonwebtoken": "^9.0.10",
20
24
  "@types/node": "^25.0.3",
21
25
  "del-cli": "^7.0.0",
22
26
  "typescript": "^5.9.3"
27
+ },
28
+ "dependencies": {
29
+ "cookie-session": "^2.1.1",
30
+ "express": "^5.2.1",
31
+ "express-validator": "^7.3.1",
32
+ "jsonwebtoken": "^9.0.3"
23
33
  }
24
34
  }