@alirezajvh/common 1.0.2 → 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,8 @@
1
+ import { AbstractError } from './base.ts';
2
+ import type { ErrorItem } from './types.ts';
3
+ export declare class BadRequestError extends AbstractError {
4
+ readonly message: string;
5
+ statusCode: number;
6
+ constructor(message: string);
7
+ serlizeErrors(): ErrorItem[];
8
+ }
@@ -0,0 +1,12 @@
1
+ import { AbstractError } from "./base.js";
2
+ export class BadRequestError extends AbstractError {
3
+ message;
4
+ statusCode = 400;
5
+ constructor(message) {
6
+ super();
7
+ this.message = message;
8
+ }
9
+ serlizeErrors() {
10
+ return [{ message: this.message }];
11
+ }
12
+ }
@@ -0,0 +1,6 @@
1
+ import { type ErrorItem } from './types.ts';
2
+ export declare abstract class AbstractError extends Error {
3
+ abstract statusCode: number;
4
+ constructor();
5
+ abstract serlizeErrors(): ErrorItem[];
6
+ }
@@ -0,0 +1,7 @@
1
+ import {} from "./types.js";
2
+ export class AbstractError extends Error {
3
+ constructor() {
4
+ super();
5
+ Object.setPrototypeOf(this, new.target.prototype);
6
+ }
7
+ }
@@ -0,0 +1,8 @@
1
+ import { AbstractError } from './base.ts';
2
+ import type { ErrorItem } from './types.ts';
3
+ export declare class DatabaseConnectionError extends AbstractError {
4
+ reason: string;
5
+ statusCode: number;
6
+ constructor();
7
+ serlizeErrors(): ErrorItem[];
8
+ }
@@ -0,0 +1,11 @@
1
+ import { AbstractError } from "./base.js";
2
+ export class DatabaseConnectionError extends AbstractError {
3
+ reason = 'Error connecting to database';
4
+ statusCode = 500;
5
+ constructor() {
6
+ super();
7
+ }
8
+ serlizeErrors() {
9
+ return [{ message: this.reason }];
10
+ }
11
+ }
@@ -0,0 +1,6 @@
1
+ export { AbstractError } from './base.ts';
2
+ export { RequestValidationError } from './request-validation.ts';
3
+ export { DatabaseConnectionError } from './database-connection.ts';
4
+ export { NotFoundError } from './not-found.ts';
5
+ export { BadRequestError } from './bad-request.ts';
6
+ export type * from './types.ts';
@@ -0,0 +1,5 @@
1
+ export { AbstractError } from "./base.js";
2
+ export { RequestValidationError } from "./request-validation.js";
3
+ export { DatabaseConnectionError } from "./database-connection.js";
4
+ export { NotFoundError } from "./not-found.js";
5
+ export { BadRequestError } from "./bad-request.js";
@@ -0,0 +1,7 @@
1
+ import { AbstractError } from './base.ts';
2
+ import type { ErrorItem } from './types.ts';
3
+ export declare class NotAuthrizedError extends AbstractError {
4
+ statusCode: number;
5
+ constructor();
6
+ serlizeErrors(): ErrorItem[];
7
+ }
@@ -0,0 +1,10 @@
1
+ import { AbstractError } from "./base.js";
2
+ export class NotAuthrizedError extends AbstractError {
3
+ statusCode = 401;
4
+ constructor() {
5
+ super();
6
+ }
7
+ serlizeErrors() {
8
+ return [{ message: 'Not Authorized' }];
9
+ }
10
+ }
@@ -0,0 +1,7 @@
1
+ import { AbstractError } from './base.ts';
2
+ import type { ErrorItem } from './types.ts';
3
+ export declare class NotFoundError extends AbstractError {
4
+ statusCode: number;
5
+ constructor();
6
+ serlizeErrors(): ErrorItem[];
7
+ }
@@ -0,0 +1,10 @@
1
+ import { AbstractError } from "./base.js";
2
+ export class NotFoundError extends AbstractError {
3
+ statusCode = 404;
4
+ constructor() {
5
+ super();
6
+ }
7
+ serlizeErrors() {
8
+ return [{ message: 'Not Found' }];
9
+ }
10
+ }
@@ -0,0 +1,9 @@
1
+ import type { ValidationError } from 'express-validator';
2
+ import { AbstractError } from './base.ts';
3
+ import { type ErrorItem } from './types.ts';
4
+ export declare class RequestValidationError extends AbstractError {
5
+ errors: ValidationError[];
6
+ statusCode: number;
7
+ constructor(errors: ValidationError[]);
8
+ serlizeErrors(): ErrorItem[];
9
+ }
@@ -0,0 +1,16 @@
1
+ import { AbstractError } from "./base.js";
2
+ import {} from "./types.js";
3
+ export class RequestValidationError extends AbstractError {
4
+ errors;
5
+ statusCode = 400;
6
+ constructor(errors) {
7
+ super();
8
+ this.errors = errors;
9
+ }
10
+ serlizeErrors() {
11
+ return this.errors.map((error) => ({
12
+ message: `${error.msg}`,
13
+ filed: error.type == 'field' ? error.path : error.type,
14
+ }));
15
+ }
16
+ }
@@ -0,0 +1,7 @@
1
+ export interface ErrorItem {
2
+ message: string;
3
+ field?: string;
4
+ }
5
+ export type ReturnError = {
6
+ errors: ErrorItem[];
7
+ };
@@ -0,0 +1,5 @@
1
+ export * from './errors/index.ts';
2
+ export * from './middlewares/current-user.ts';
3
+ export * from './middlewares/error-handler.ts';
4
+ export * from './middlewares/require-auth.ts';
5
+ export * from './middlewares/validate-request.ts';
package/build/index.js ADDED
@@ -0,0 +1,5 @@
1
+ export * from "./errors/index.js";
2
+ export * from "./middlewares/current-user.js";
3
+ export * from "./middlewares/error-handler.js";
4
+ export * from "./middlewares/require-auth.js";
5
+ export * from "./middlewares/validate-request.js";
@@ -0,0 +1,17 @@
1
+ import type { 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
+ session?: {
10
+ jwt?: string;
11
+ };
12
+ currentUser?: UserPayload;
13
+ }
14
+ }
15
+ }
16
+ export declare const currentUser: (req: Request, res: Response, next: NextFunction) => void;
17
+ export {};
@@ -0,0 +1,11 @@
1
+ import jwt from 'jsonwebtoken';
2
+ export const currentUser = (req, res, next) => {
3
+ if (!req.session?.jwt)
4
+ return next();
5
+ try {
6
+ const payload = jwt.verify(req.session.jwt, process.env.JWT_KEY);
7
+ req.currentUser = payload;
8
+ }
9
+ catch (_) { }
10
+ next();
11
+ };
@@ -0,0 +1,2 @@
1
+ import type { Request, Response, NextFunction } from 'express';
2
+ export declare const errorHandler: (err: Error, req: Request, res: Response, next: NextFunction) => Response<any, Record<string, any>>;
@@ -0,0 +1,9 @@
1
+ import { AbstractError } from "../errors/index.js";
2
+ export const errorHandler = (err, req, res, next) => {
3
+ if (err instanceof AbstractError) {
4
+ return res.status(err.statusCode).send({ errors: err.serlizeErrors() });
5
+ }
6
+ return res.status(400).send({
7
+ errors: [{ message: 'Something went wrong' }],
8
+ });
9
+ };
@@ -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 { NotAuthrizedError } from "../errors/not-authorized.js";
2
+ export const requireAuth = (req, res, next) => {
3
+ if (!req.currentUser) {
4
+ throw new NotAuthrizedError();
5
+ }
6
+ next();
7
+ };
@@ -0,0 +1,2 @@
1
+ import type { 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/index.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,9 +1,10 @@
1
1
  {
2
2
  "name": "@alirezajvh/common",
3
- "version": "1.0.2",
3
+ "version": "1.0.4",
4
4
  "description": "",
5
5
  "main": "./build/index.js",
6
6
  "types": "./build/index.d.ts",
7
+ "type": "module",
7
8
  "files": [
8
9
  "build/**/*"
9
10
  ],
@@ -15,6 +16,15 @@
15
16
  "keywords": [],
16
17
  "author": "",
17
18
  "license": "ISC",
19
+ "dependencies": {
20
+ "@types/cookie-session": "^2.0.49",
21
+ "@types/express": "^5.0.6",
22
+ "@types/jsonwebtoken": "^9.0.10",
23
+ "cookie-session": "^2.1.1",
24
+ "express": "^5.2.1",
25
+ "express-validator": "^7.3.1",
26
+ "jsonwebtoken": "^9.0.3"
27
+ },
18
28
  "devDependencies": {
19
29
  "del-cli": "^7.0.0",
20
30
  "typescript": "^5.9.3"
@@ -1,2 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
File without changes