@meaptickets/common 1.0.0 → 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.
- package/package.json +25 -4
- package/src/errors/bad-request-error.ts +15 -0
- package/src/errors/custom-error.ts +11 -0
- package/src/errors/database-connection-error.ts +16 -0
- package/src/errors/not-authorized-error.ts +15 -0
- package/src/errors/not-found-error.ts +15 -0
- package/src/errors/request-validation-error.ts +21 -0
- package/src/index.ts +11 -0
- package/src/middlewares/current-user.ts +35 -0
- package/src/middlewares/error-handler.ts +17 -0
- package/src/middlewares/require-auth.ts +14 -0
- package/src/middlewares/validate-request.ts +17 -0
- package/tsconfig.json +44 -0
package/package.json
CHANGED
|
@@ -1,12 +1,33 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@meaptickets/common",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.3",
|
|
4
4
|
"description": "",
|
|
5
|
-
"main": "index.js",
|
|
5
|
+
"main": "./build/index.js",
|
|
6
|
+
"types": "./build/index.d.ts",
|
|
7
|
+
"file": [
|
|
8
|
+
"build/**/*"
|
|
9
|
+
],
|
|
6
10
|
"scripts": {
|
|
7
|
-
"
|
|
11
|
+
"clean": "rimraf build",
|
|
12
|
+
"build": "npm run clean && tsc",
|
|
13
|
+
"pub": "git add . && git commit -m \"Updates\" && npm version patch && npm run build && npm publish --access public"
|
|
8
14
|
},
|
|
9
15
|
"keywords": [],
|
|
10
16
|
"author": "",
|
|
11
|
-
"license": "ISC"
|
|
17
|
+
"license": "ISC",
|
|
18
|
+
"dependencies": {
|
|
19
|
+
"@types/cookie-session": "^2.0.49",
|
|
20
|
+
"@types/express": "^5.0.6",
|
|
21
|
+
"@types/jsonwebtoken": "^9.0.10",
|
|
22
|
+
"cookie-session": "^2.1.1",
|
|
23
|
+
"express": "^5.2.1",
|
|
24
|
+
"express-validator": "^7.3.1",
|
|
25
|
+
"jsonwebtoken": "^9.0.3",
|
|
26
|
+
"ts-node": "^10.9.2"
|
|
27
|
+
},
|
|
28
|
+
"devDependencies": {
|
|
29
|
+
"del-cli": "^7.0.0",
|
|
30
|
+
"rimraf": "^6.1.2",
|
|
31
|
+
"typescript": "^5.9.3"
|
|
32
|
+
}
|
|
12
33
|
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { CustomError } from "./custom-error";
|
|
2
|
+
|
|
3
|
+
export class BadRequestError extends CustomError {
|
|
4
|
+
statusCode = 400;
|
|
5
|
+
|
|
6
|
+
constructor(public message: string) {
|
|
7
|
+
super(message);
|
|
8
|
+
|
|
9
|
+
Object.setPrototypeOf(this, BadRequestError.prototype);
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
serializeErrors() {
|
|
13
|
+
return [{ message: this.message }];
|
|
14
|
+
}
|
|
15
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
export abstract class CustomError extends Error {
|
|
2
|
+
abstract statusCode: number;
|
|
3
|
+
|
|
4
|
+
constructor(message: string) {
|
|
5
|
+
super(message);
|
|
6
|
+
|
|
7
|
+
Object.setPrototypeOf(this, CustomError.prototype);
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
abstract serializeErrors(): { message: string, field?: string }[];
|
|
11
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { CustomError } from "./custom-error";
|
|
2
|
+
|
|
3
|
+
export class DatabaseConnectionError extends CustomError {
|
|
4
|
+
statusCode = 500;
|
|
5
|
+
reason = 'Error connecting to database';
|
|
6
|
+
|
|
7
|
+
constructor() {
|
|
8
|
+
super('Error connecting to database');
|
|
9
|
+
|
|
10
|
+
Object.setPrototypeOf(this, DatabaseConnectionError.prototype);
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
serializeErrors() {
|
|
14
|
+
return [{ message: this.reason }];
|
|
15
|
+
}
|
|
16
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { CustomError } from "./custom-error";
|
|
2
|
+
|
|
3
|
+
export class NotAuthorizedError extends CustomError {
|
|
4
|
+
statusCode = 401;
|
|
5
|
+
|
|
6
|
+
constructor() {
|
|
7
|
+
super('Not authorized');
|
|
8
|
+
|
|
9
|
+
Object.setPrototypeOf(this, NotAuthorizedError.prototype);
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
serializeErrors() {
|
|
13
|
+
return [{ message: 'Not authorized' }];
|
|
14
|
+
}
|
|
15
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { CustomError } from "./custom-error";
|
|
2
|
+
|
|
3
|
+
export class NotFoundError extends CustomError {
|
|
4
|
+
statusCode = 404;
|
|
5
|
+
|
|
6
|
+
constructor() {
|
|
7
|
+
super('Route not found');
|
|
8
|
+
|
|
9
|
+
Object.setPrototypeOf(this, NotFoundError.prototype);
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
serializeErrors() {
|
|
13
|
+
return [{ message: 'Not found' }];
|
|
14
|
+
}
|
|
15
|
+
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { ValidationError } from "express-validator";
|
|
2
|
+
import { CustomError } from "./custom-error";
|
|
3
|
+
|
|
4
|
+
export class RequestValidationError extends CustomError{
|
|
5
|
+
statusCode = 400;
|
|
6
|
+
|
|
7
|
+
constructor(public errors: ValidationError[]) {
|
|
8
|
+
super('Invalid request parameters');
|
|
9
|
+
|
|
10
|
+
Object.setPrototypeOf(this, RequestValidationError.prototype);
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
serializeErrors() {
|
|
14
|
+
return this.errors.map(err => {
|
|
15
|
+
if (err.type === 'field') {
|
|
16
|
+
return { message: err.msg, field: err.path }
|
|
17
|
+
}
|
|
18
|
+
return { message: err.msg };
|
|
19
|
+
});
|
|
20
|
+
}
|
|
21
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
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
|
+
|
|
8
|
+
export * from './middlewares/current-user';
|
|
9
|
+
export * from './middlewares/error-handler';
|
|
10
|
+
export * from './middlewares/require-auth';
|
|
11
|
+
export * from './middlewares/validate-request';
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import { Request, Response, NextFunction } from "express";
|
|
2
|
+
import jwt from 'jsonwebtoken';
|
|
3
|
+
import 'cookie-session';
|
|
4
|
+
|
|
5
|
+
interface UserPayload {
|
|
6
|
+
id: string,
|
|
7
|
+
email: string
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
declare global {
|
|
11
|
+
namespace Express {
|
|
12
|
+
interface Request {
|
|
13
|
+
currentUser?: UserPayload
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export const currentUser = (
|
|
19
|
+
req: Request,
|
|
20
|
+
res: Response,
|
|
21
|
+
next: NextFunction
|
|
22
|
+
) => {
|
|
23
|
+
if (!req.session?.jwt) {
|
|
24
|
+
return next();
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
try {
|
|
28
|
+
const payload = jwt.verify(req.session.jwt, process.env.JWT_KEY!) as UserPayload;
|
|
29
|
+
req.currentUser = payload;
|
|
30
|
+
} catch (err) {
|
|
31
|
+
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
next();
|
|
35
|
+
};
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { Request, Response, NextFunction } from "express"
|
|
2
|
+
import { CustomError } from "../errors/custom-error";
|
|
3
|
+
|
|
4
|
+
export const errorHandler = (
|
|
5
|
+
err: Error,
|
|
6
|
+
req: Request,
|
|
7
|
+
res: Response,
|
|
8
|
+
next: NextFunction
|
|
9
|
+
) => {
|
|
10
|
+
if (err instanceof CustomError) {
|
|
11
|
+
return res.status(err.statusCode).send({ errors: err.serializeErrors() });
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
res.status(400).send({
|
|
15
|
+
errors: [{ message: 'Something went wrong' }]
|
|
16
|
+
});
|
|
17
|
+
};
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { Request, Response, NextFunction } from "express";
|
|
2
|
+
import { NotAuthorizedError } from "../errors/not-authorized-error";
|
|
3
|
+
|
|
4
|
+
export const requireAuth = (
|
|
5
|
+
req: Request,
|
|
6
|
+
res: Response,
|
|
7
|
+
next: NextFunction
|
|
8
|
+
) => {
|
|
9
|
+
if (!req.currentUser) {
|
|
10
|
+
throw new NotAuthorizedError();
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
next();
|
|
14
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { Request, Response, NextFunction } from "express";
|
|
2
|
+
import { validationResult } from "express-validator";
|
|
3
|
+
import { RequestValidationError } from "../errors/request-validation-error";
|
|
4
|
+
|
|
5
|
+
export const validateRequest = (
|
|
6
|
+
req: Request,
|
|
7
|
+
res: Response,
|
|
8
|
+
next: NextFunction
|
|
9
|
+
) => {
|
|
10
|
+
const errors = validationResult(req);
|
|
11
|
+
|
|
12
|
+
if(!errors.isEmpty()) {
|
|
13
|
+
throw new RequestValidationError(errors.array());
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
next();
|
|
17
|
+
}
|
package/tsconfig.json
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
{
|
|
2
|
+
// Visit https://aka.ms/tsconfig to read more about this file
|
|
3
|
+
"compilerOptions": {
|
|
4
|
+
// File Layout
|
|
5
|
+
// "rootDir": "./src",
|
|
6
|
+
"outDir": "./build",
|
|
7
|
+
|
|
8
|
+
// Environment Settings
|
|
9
|
+
// See also https://aka.ms/tsconfig/module
|
|
10
|
+
"module": "nodenext",
|
|
11
|
+
"target": "esnext",
|
|
12
|
+
"types": [],
|
|
13
|
+
// For nodejs:
|
|
14
|
+
// "lib": ["esnext"],
|
|
15
|
+
// "types": ["node"],
|
|
16
|
+
// and npm install -D @types/node
|
|
17
|
+
|
|
18
|
+
// Other Outputs
|
|
19
|
+
"sourceMap": true,
|
|
20
|
+
"declaration": true,
|
|
21
|
+
"declarationMap": true,
|
|
22
|
+
|
|
23
|
+
// Stricter Typechecking Options
|
|
24
|
+
"noUncheckedIndexedAccess": true,
|
|
25
|
+
"exactOptionalPropertyTypes": true,
|
|
26
|
+
|
|
27
|
+
// Style Options
|
|
28
|
+
// "noImplicitReturns": true,
|
|
29
|
+
// "noImplicitOverride": true,
|
|
30
|
+
// "noUnusedLocals": true,
|
|
31
|
+
// "noUnusedParameters": true,
|
|
32
|
+
// "noFallthroughCasesInSwitch": true,
|
|
33
|
+
// "noPropertyAccessFromIndexSignature": true,
|
|
34
|
+
|
|
35
|
+
// Recommended Options
|
|
36
|
+
"strict": true,
|
|
37
|
+
"jsx": "react-jsx",
|
|
38
|
+
"verbatimModuleSyntax": false,
|
|
39
|
+
"isolatedModules": true,
|
|
40
|
+
"noUncheckedSideEffectImports": true,
|
|
41
|
+
"moduleDetection": "force",
|
|
42
|
+
"skipLibCheck": true,
|
|
43
|
+
}
|
|
44
|
+
}
|