@kordynlab/common 0.0.0-stage → 0.1.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.
@@ -0,0 +1,9 @@
1
+ import { CustomError } from "./custome-error-abstract.js";
2
+ export declare class BadRequestError extends CustomError {
3
+ reason: string;
4
+ statusCode: number;
5
+ constructor(reason: string);
6
+ serializeErrors(): {
7
+ message: string;
8
+ }[];
9
+ }
@@ -0,0 +1,13 @@
1
+ import { CustomError } from "./custome-error-abstract.js";
2
+ export class BadRequestError extends CustomError {
3
+ reason;
4
+ statusCode = 400;
5
+ constructor(reason) {
6
+ super(reason);
7
+ this.reason = reason;
8
+ Object.setPrototypeOf(this, BadRequestError.prototype);
9
+ }
10
+ serializeErrors() {
11
+ return [{ message: this.reason }];
12
+ }
13
+ }
@@ -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 "./custome-error-abstract.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,12 @@
1
+ import { CustomError } from "./custome-error-abstract.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
+ Object.setPrototypeOf(this, DatabaseConnectionError.prototype);
8
+ }
9
+ serializeErrors() {
10
+ return [{ message: this.reason }];
11
+ }
12
+ }
@@ -0,0 +1,8 @@
1
+ import { CustomError } from "./custome-error-abstract.js";
2
+ export declare class NotAuthorizedError extends CustomError {
3
+ statusCode: number;
4
+ constructor();
5
+ serializeErrors(): {
6
+ message: string;
7
+ }[];
8
+ }
@@ -0,0 +1,11 @@
1
+ import { CustomError } from "./custome-error-abstract.js";
2
+ export class NotAuthorizedError extends CustomError {
3
+ statusCode = 401;
4
+ constructor() {
5
+ super('Not authorized');
6
+ Object.setPrototypeOf(this, NotAuthorizedError.prototype);
7
+ }
8
+ serializeErrors() {
9
+ return [{ message: 'Not authorized' }];
10
+ }
11
+ }
@@ -0,0 +1,8 @@
1
+ import { CustomError } from "./custome-error-abstract.js";
2
+ export declare class NotFoundError extends CustomError {
3
+ statusCode: number;
4
+ constructor();
5
+ serializeErrors(): {
6
+ message: string;
7
+ }[];
8
+ }
@@ -0,0 +1,11 @@
1
+ import { CustomError } from "./custome-error-abstract.js";
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 [{ message: 'Route not found' }];
10
+ }
11
+ }
@@ -0,0 +1,14 @@
1
+ import { type ValidationError } from 'express-validator';
2
+ import { CustomError } from './custome-error-abstract.js';
3
+ export declare class RequestValidatorError extends CustomError {
4
+ reasons: ValidationError[];
5
+ statusCode: number;
6
+ constructor(reasons: ValidationError[]);
7
+ serializeErrors(): ({
8
+ message: any;
9
+ field: string;
10
+ } | {
11
+ message: any;
12
+ field?: undefined;
13
+ })[];
14
+ }
@@ -0,0 +1,20 @@
1
+ import {} from 'express-validator';
2
+ import { CustomError } from './custome-error-abstract.js';
3
+ export class RequestValidatorError extends CustomError {
4
+ reasons;
5
+ statusCode = 400;
6
+ constructor(reasons) {
7
+ super(" Invalid request parameters");
8
+ this.reasons = reasons;
9
+ // Only because we are extending a built-in class
10
+ Object.setPrototypeOf(this, RequestValidatorError.prototype);
11
+ }
12
+ serializeErrors() {
13
+ return this.reasons.map((error) => {
14
+ if (error.type === "field") {
15
+ return { message: error.msg, field: error.path };
16
+ }
17
+ return { message: error.msg };
18
+ });
19
+ }
20
+ }
@@ -0,0 +1,10 @@
1
+ export * from './errors/bad-request-error.js';
2
+ export * from './errors/custome-error-abstract.js';
3
+ export * from './errors/database-connection-errors.js';
4
+ export * from './errors/not-authorized-errors.js';
5
+ export * from './errors/not-found-error.js';
6
+ export * from './errors/request-validator-error.js';
7
+ export * from './middlewares/current-user.js';
8
+ export * from './middlewares/error-handler.js';
9
+ export * from './middlewares/require-auth.js';
10
+ export * from './middlewares/validate-request.js';
package/dist/index.js ADDED
@@ -0,0 +1,10 @@
1
+ export * from './errors/bad-request-error.js';
2
+ export * from './errors/custome-error-abstract.js';
3
+ export * from './errors/database-connection-errors.js';
4
+ export * from './errors/not-authorized-errors.js';
5
+ export * from './errors/not-found-error.js';
6
+ export * from './errors/request-validator-error.js';
7
+ export * from './middlewares/current-user.js';
8
+ export * from './middlewares/error-handler.js';
9
+ export * from './middlewares/require-auth.js';
10
+ export * from './middlewares/validate-request.js';
@@ -0,0 +1,14 @@
1
+ import type { NextFunction, Request, Response } 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 jwt from 'jsonwebtoken';
2
+ export const currentUser = (req, res, next) => {
3
+ if (!req.session || !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 (err) {
11
+ console.error('Error verifying JWT:', err);
12
+ }
13
+ next();
14
+ };
@@ -0,0 +1,2 @@
1
+ import type { NextFunction, Request, Response } from "express";
2
+ export declare const errorHandler: (err: Error, req: Request, res: Response, next: NextFunction) => Response<any, Record<string, any>> | undefined;
@@ -0,0 +1,12 @@
1
+ import { CustomError } from "../errors/custome-error-abstract.js";
2
+ export const errorHandler = (err, req, res, next) => {
3
+ if (process.env.NODE_ENV !== 'test') {
4
+ console.error(err);
5
+ }
6
+ if (err instanceof CustomError) {
7
+ return res.status(err.statusCode).send({ errors: err.serializeErrors() });
8
+ }
9
+ res.status(400).send({
10
+ message: err.message
11
+ });
12
+ };
@@ -0,0 +1,2 @@
1
+ import type { 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 '../errors/not-authorized-errors.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 type { NextFunction, Request, Response } 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 { RequestValidatorError } from "../errors/request-validator-error.js";
3
+ export const validateRequest = (req, res, next) => {
4
+ const errors = validationResult(req);
5
+ if (!errors.isEmpty()) {
6
+ throw new RequestValidatorError(errors.array());
7
+ }
8
+ next();
9
+ };
package/package.json CHANGED
@@ -1,6 +1,34 @@
1
1
  {
2
2
  "name": "@kordynlab/common",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
6
- }
3
+ "version": "0.1.0",
4
+ "description": "Shared library for the Ticketing services",
5
+ "type": "module",
6
+ "main": "./dist/index.js",
7
+ "types": "./dist/index.d.ts",
8
+ "exports": {
9
+ ".": {
10
+ "types": "./dist/index.d.ts",
11
+ "import": "./dist/index.js"
12
+ }
13
+ },
14
+ "files": ["dist"],
15
+ "publishConfig": {
16
+ "access": "public",
17
+ "registry": "https://registry.npmjs.org/"
18
+ },
19
+ "scripts": {
20
+ "build": "tsc -p tsconfig.json",
21
+ "prepack": "npm run build"
22
+ },
23
+ "dependencies": {
24
+ "@types/express": "^4.17.25",
25
+ "express-validator": "^7.3.2",
26
+ "jsonwebtoken": "^9.0.3"
27
+ },
28
+ "devDependencies": {
29
+ "@types/cookie-session": "^2.0.49",
30
+ "@types/jsonwebtoken": "^9.0.10",
31
+ "@types/node": "^22.0.0",
32
+ "typescript": "^6.0.3"
33
+ }
34
+ }
package/README.md DELETED
@@ -1,3 +0,0 @@
1
- # Temporary Holding Version
2
-
3
- This version is a temporary placeholder for this package. An operational version to replace this has been submitted for review and is awaiting a staged release.