@thebester/common 1.0.5 → 1.0.7

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.
package/package.json CHANGED
@@ -1,9 +1,9 @@
1
1
  {
2
2
  "name": "@thebester/common",
3
- "version": "1.0.5",
3
+ "version": "1.0.7",
4
4
  "description": "",
5
- "main": "build/index.js",
6
- "types": "build/index.d.ts",
5
+ "main": "./build/index.js",
6
+ "types": "./build/index.d.ts",
7
7
  "files": [
8
8
  "build/**/*"
9
9
  ],
@@ -15,6 +15,7 @@
15
15
  "keywords": [],
16
16
  "author": "",
17
17
  "license": "ISC",
18
+ "type": "module",
18
19
  "devDependencies": {
19
20
  "del-cli": "^7.0.0",
20
21
  "typescript": "^5.9.3"
@@ -1,10 +0,0 @@
1
- import { CustomError } from "./custom-error";
2
- export declare class BadRequestError extends CustomError {
3
- message: string;
4
- statusCode: number;
5
- constructor(message: string);
6
- serializeErrors(): {
7
- message: string;
8
- field?: string;
9
- }[];
10
- }
@@ -1,14 +0,0 @@
1
- import { CustomError } from "./custom-error";
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 [{ message: this.message }];
13
- }
14
- }
@@ -1,8 +0,0 @@
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
- }
@@ -1,7 +0,0 @@
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
- }
@@ -1,9 +0,0 @@
1
- import { CustomError } from "./custom-error";
2
- export declare class DatabaseConnectionError extends CustomError {
3
- statusCode: number;
4
- reason: string;
5
- constructor();
6
- serializeErrors(): {
7
- message: string;
8
- }[];
9
- }
@@ -1,17 +0,0 @@
1
- import { CustomError } from "./custom-error";
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
- }
@@ -1,8 +0,0 @@
1
- import { CustomError } from "./custom-error";
2
- export declare class NotAuthorizedError extends CustomError {
3
- statusCode: number;
4
- constructor();
5
- serializeErrors(): {
6
- message: string;
7
- }[];
8
- }
@@ -1,12 +0,0 @@
1
- import { CustomError } from "./custom-error";
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 [{ message: "Not authorized" }];
11
- }
12
- }
@@ -1,8 +0,0 @@
1
- import { CustomError } from "./custom-error";
2
- export declare class NotFoundError extends CustomError {
3
- statusCode: number;
4
- constructor();
5
- serializeErrors(): {
6
- message: string;
7
- }[];
8
- }
@@ -1,16 +0,0 @@
1
- import { CustomError } from "./custom-error";
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
- }
@@ -1,14 +0,0 @@
1
- import type { 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?: never;
13
- })[];
14
- }
@@ -1,20 +0,0 @@
1
- import { CustomError } from "./custom-error";
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(err => {
13
- if (err.type === 'field') {
14
- return { message: err.msg, field: err.path };
15
- }
16
- else
17
- return { message: err.msg };
18
- });
19
- }
20
- }
package/build/index.d.ts DELETED
@@ -1,10 +0,0 @@
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-authorized-error";
5
- export * from "./errors/not-found-error";
6
- export * from "./errors/request-validation-error";
7
- export * from "./middelwares/current-user";
8
- export * from "./middelwares/error-handler";
9
- export * from "./middelwares/require-auth";
10
- export * from "./middelwares/validate-request";
package/build/index.js DELETED
@@ -1,11 +0,0 @@
1
- // Re-export stuff from errors and middlewares
2
- export * from "./errors/bad-request-error";
3
- export * from "./errors/custom-error";
4
- export * from "./errors/database-connection-error";
5
- export * from "./errors/not-authorized-error";
6
- export * from "./errors/not-found-error";
7
- export * from "./errors/request-validation-error";
8
- export * from "./middelwares/current-user";
9
- export * from "./middelwares/error-handler";
10
- export * from "./middelwares/require-auth";
11
- export * from "./middelwares/validate-request";
@@ -1,14 +0,0 @@
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
- currentUser?: UserPayload;
10
- }
11
- }
12
- }
13
- export declare const currentUser: (req: Request, res: Response, next: NextFunction) => void;
14
- export {};
@@ -1,13 +0,0 @@
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 (err) {
11
- }
12
- next();
13
- };
@@ -1,2 +0,0 @@
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>> | undefined;
@@ -1,9 +0,0 @@
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({ errors: err.serializeErrors() });
5
- }
6
- res.status(400).send({
7
- errors: [{ message: "Something went wrong" }]
8
- });
9
- };
@@ -1,2 +0,0 @@
1
- import type { Request, Response, NextFunction } from "express";
2
- export declare const requireAuth: (req: Request, res: Response, next: NextFunction) => void;
@@ -1,7 +0,0 @@
1
- import { NotAuthorizedError } from "../errors/not-authorized-error";
2
- export const requireAuth = (req, res, next) => {
3
- if (!req.currentUser) {
4
- throw new NotAuthorizedError();
5
- }
6
- next();
7
- };
@@ -1,2 +0,0 @@
1
- import type { Request, Response, NextFunction } from "express";
2
- export declare const validateRequest: (req: Request, res: Response, next: NextFunction) => void;
@@ -1,9 +0,0 @@
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
- };