@tabletennisshop/common 1.0.0 → 1.0.2

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.
@@ -1,14 +1,16 @@
1
- export abstract class CustomError extends Error{
2
- abstract statusCode: number;
3
- constructor(message: string) {
4
- super(message);
5
- /*
6
- Always use this line in subclasses
7
- This will ensure that return true for "instanceof"
8
- const error = new CustomError("message");
9
- console.log(error instanceof CustomError);
10
- */
11
- Object.setPrototypeOf(this, CustomError.prototype);
12
- }
13
- abstract serializeErrors(): {message: string, details?: any}[];
14
- }
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.CustomError = void 0;
4
+ class CustomError extends Error {
5
+ constructor(message) {
6
+ super(message);
7
+ /*
8
+ Always use this line in subclasses
9
+ This will ensure that return true for "instanceof"
10
+ const error = new CustomError("message");
11
+ console.log(error instanceof CustomError);
12
+ */
13
+ Object.setPrototypeOf(this, CustomError.prototype);
14
+ }
15
+ }
16
+ exports.CustomError = CustomError;
@@ -0,0 +1,15 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.NotAuthorizedError = void 0;
4
+ const custom_error_1 = require("./custom-error");
5
+ class NotAuthorizedError extends custom_error_1.CustomError {
6
+ constructor(message) {
7
+ super(message);
8
+ this.statusCode = 401;
9
+ Object.setPrototypeOf(this, NotAuthorizedError.prototype);
10
+ }
11
+ serializeErrors() {
12
+ return [{ message: this.message }];
13
+ }
14
+ }
15
+ exports.NotAuthorizedError = NotAuthorizedError;
@@ -0,0 +1,15 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.NotFoundError = void 0;
4
+ const custom_error_1 = require("./custom-error");
5
+ class NotFoundError extends custom_error_1.CustomError {
6
+ constructor(message) {
7
+ super(message);
8
+ this.statusCode = 401;
9
+ Object.setPrototypeOf(this, NotFoundError.prototype);
10
+ }
11
+ serializeErrors() {
12
+ return [{ message: this.message }];
13
+ }
14
+ }
15
+ exports.NotFoundError = NotFoundError;
@@ -0,0 +1,19 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.RequestValidateError = void 0;
4
+ const custom_error_1 = require("./custom-error");
5
+ class RequestValidateError extends custom_error_1.CustomError {
6
+ constructor(errors) {
7
+ //Assign message to CustomError abstract class
8
+ super("Invalid request parameters");
9
+ this.statusCode = 400;
10
+ this.errors = errors;
11
+ Object.setPrototypeOf(this, RequestValidateError.prototype);
12
+ }
13
+ serializeErrors() {
14
+ return this.errors.map((err) => {
15
+ return { message: err.msg };
16
+ });
17
+ }
18
+ }
19
+ exports.RequestValidateError = RequestValidateError;
package/build/index.js ADDED
@@ -0,0 +1,23 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ __exportStar(require("./errors/custom-error"), exports);
18
+ __exportStar(require("./errors/not-authorized-error"), exports);
19
+ __exportStar(require("./errors/not-found-error"), exports);
20
+ __exportStar(require("./errors/request-validate-error"), exports);
21
+ __exportStar(require("./middlewares/check-authorized-middleware"), exports);
22
+ __exportStar(require("./middlewares/error-handler"), exports);
23
+ __exportStar(require("./middlewares/validate-request-middleware"), exports);
@@ -0,0 +1,25 @@
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ var __importDefault = (this && this.__importDefault) || function (mod) {
12
+ return (mod && mod.__esModule) ? mod : { "default": mod };
13
+ };
14
+ Object.defineProperty(exports, "__esModule", { value: true });
15
+ exports.CheckAuthorized = void 0;
16
+ const axios_1 = __importDefault(require("axios"));
17
+ const not_authorized_error_1 = require("../errors/not-authorized-error");
18
+ const CheckAuthorized = (req, res, next) => __awaiter(void 0, void 0, void 0, function* () {
19
+ const axiosRes = yield axios_1.default.get("localhost:3000/api/users/current-user");
20
+ if (axiosRes.status != 200) {
21
+ throw new not_authorized_error_1.NotAuthorizedError("Not Authorized");
22
+ }
23
+ next();
24
+ });
25
+ exports.CheckAuthorized = CheckAuthorized;
@@ -0,0 +1,13 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.errorHandler = void 0;
4
+ const custom_error_1 = require("../errors/custom-error");
5
+ const errorHandler = (err, req, res, next) => {
6
+ if (err instanceof custom_error_1.CustomError) {
7
+ res.status(err.statusCode).send({ errors: err.serializeErrors() });
8
+ return;
9
+ }
10
+ res.status(400).send({ errors: [{ message: "Something is wrong" }] });
11
+ return;
12
+ };
13
+ exports.errorHandler = errorHandler;
@@ -0,0 +1,12 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const express_validator_1 = require("express-validator");
4
+ const request_validate_error_1 = require("../errors/request-validate-error");
5
+ const validateRequestMiddleware = (req, res, next) => {
6
+ const errors = (0, express_validator_1.validationResult)(req);
7
+ if (!errors.isEmpty()) {
8
+ throw new request_validate_error_1.RequestValidateError(errors.array());
9
+ }
10
+ next();
11
+ };
12
+ exports.default = validateRequestMiddleware;
package/package.json CHANGED
@@ -1,13 +1,27 @@
1
1
  {
2
2
  "name": "@tabletennisshop/common",
3
- "version": "1.0.0",
3
+ "version": "1.0.2",
4
4
  "description": "",
5
- "main": "index.js",
5
+ "main": "./build/index.js",
6
+ "types": "build/index.d.ts",
6
7
  "scripts": {
7
- "test": "echo \"Error: no test specified\" && exit 1"
8
+ "clean": "del build",
9
+ "build": "npm run clean && tsc",
10
+ "pub": "npm version patch && npm run build && npm publish"
8
11
  },
12
+ "files": [
13
+ "build/**/*"
14
+ ],
9
15
  "keywords": [],
10
16
  "author": "",
11
17
  "license": "ISC",
12
- "type": "commonjs"
18
+ "type": "commonjs",
19
+ "dependencies": {
20
+ "@tabletennisshop/common": "^1.0.1",
21
+ "@types/express": "^5.0.2",
22
+ "axios": "^1.9.0",
23
+ "express": "^5.1.0",
24
+ "express-validator": "^7.2.1",
25
+ "uninstall": "^0.0.0"
26
+ }
13
27
  }
@@ -1,13 +0,0 @@
1
- import { CustomError } from "./custom-error";
2
-
3
- export class NotAuthorizedError extends CustomError{
4
- statusCode: number = 401;
5
- constructor(message: string){
6
- super(message);
7
- Object.setPrototypeOf(this, NotAuthorizedError.prototype);
8
- }
9
-
10
- serializeErrors(): { message: string; details?: any; }[] {
11
- return [{message: this.message}];
12
- }
13
- }
@@ -1,12 +0,0 @@
1
- import { CustomError } from "./custom-error";
2
-
3
- export class NotFoundError extends CustomError{
4
- statusCode: number = 401;
5
- constructor(message: string){
6
- super(message);
7
- Object.setPrototypeOf(this, NotFoundError.prototype);
8
- }
9
- serializeErrors(): { message: string; details?: any; }[] {
10
- return [{message: this.message}];
11
- }
12
- }
@@ -1,17 +0,0 @@
1
- import { CustomError } from './custom-error';
2
- import { ValidationError } from 'express-validator';
3
- export class RequestValidateError extends CustomError{
4
- statusCode = 400;
5
- private errors: ValidationError[];
6
- constructor(errors: ValidationError[]){
7
- //Assign message to CustomError abstract class
8
- super("Invalid request parameters");
9
- this.errors = errors;
10
- Object.setPrototypeOf(this, RequestValidateError.prototype);
11
- }
12
- serializeErrors() {
13
- return this.errors.map((err)=>{
14
- return {message: err.msg};
15
- });
16
- }
17
- }
@@ -1,10 +0,0 @@
1
- import { NextFunction } from "express"
2
- import axios from "axios";
3
- import { NotAuthorizedError } from "../errors/not-authorized-error";
4
- export const CheckAuthorized= async (req: Request, res: Response, next: NextFunction)=>{
5
- const axiosRes = await axios.get("localhost:3000/api/users/current-user");
6
- if (axiosRes.status != 200){
7
- throw new NotAuthorizedError("Not Authorized");
8
- }
9
- next();
10
- }
@@ -1,17 +0,0 @@
1
- import express,{NextFunction, Request, Response } from "express";
2
- import { CustomError } from "../errors/custom-error";
3
- import { RequestValidateError } from "../errors/request-validate-error";
4
-
5
- export const errorHandler = (
6
- err:Error,
7
- req: Request,
8
- res: Response,
9
- next: NextFunction
10
- )=>{
11
- if (err instanceof CustomError){
12
- res.status(err.statusCode).send({errors: err.serializeErrors()});
13
- return;
14
- }
15
- res.status(400).send({errors: [{message: "Something is wrong"}]});
16
- return;
17
- }
@@ -1,12 +0,0 @@
1
- import express, { NextFunction, Request, Response } from 'express';
2
- import { validationResult } from 'express-validator';
3
- import { RequestValidateError } from '../errors/request-validate-error';
4
-
5
- const validateRequestMiddleware = (req: Request, res: Response, next: NextFunction) => {
6
- const errors = validationResult(req);
7
- if (!errors.isEmpty()) {
8
- throw new RequestValidateError(errors.array());
9
- }
10
- next();
11
- }
12
- export default validateRequestMiddleware;