@doffy-gittix/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.
- package/package.json +18 -4
- package/src/config.ts +3 -0
- package/src/errors/bad-request-error.ts +14 -0
- package/src/errors/base-error.ts +13 -0
- package/src/errors/database-connection-error.ts +14 -0
- package/src/errors/index.ts +6 -0
- package/src/errors/notfound-error.ts +14 -0
- package/src/errors/request-validation-error.ts +23 -0
- package/src/errors/unauthorized_error.ts +14 -0
- package/src/index.ts +2 -0
- package/src/middleware/error-handler.ts +14 -0
- package/src/middleware/get-current-user-handler.ts +35 -0
- package/src/middleware/index.ts +4 -0
- package/src/middleware/require-auth-handler.ts +13 -0
- package/src/middleware/validation-handler.ts +15 -0
- package/tsconfig.json +44 -0
package/package.json
CHANGED
|
@@ -1,13 +1,27 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@doffy-gittix/common",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.2",
|
|
4
4
|
"description": "",
|
|
5
|
-
"main": "index.js",
|
|
5
|
+
"main": "./build/index.js",
|
|
6
6
|
"scripts": {
|
|
7
|
-
"
|
|
7
|
+
"build": "del-cli build && tsc",
|
|
8
|
+
"pub": "npm version patch && git add . && git commit -m \"[upd] common\" && git push && npm publish"
|
|
8
9
|
},
|
|
9
10
|
"keywords": [],
|
|
10
11
|
"author": "",
|
|
11
12
|
"license": "ISC",
|
|
12
|
-
"type": "
|
|
13
|
+
"type": "module",
|
|
14
|
+
"devDependencies": {
|
|
15
|
+
"del-cli": "^7.0.0",
|
|
16
|
+
"typescript": "^6.0.3"
|
|
17
|
+
},
|
|
18
|
+
"dependencies": {
|
|
19
|
+
"@types/express": "^5.0.6",
|
|
20
|
+
"@types/express-validator": "^2.20.33",
|
|
21
|
+
"@types/jsonwebtoken": "^9.0.10",
|
|
22
|
+
"express": "^5.2.1",
|
|
23
|
+
"express-validator": "^7.3.2",
|
|
24
|
+
"jsonwebtoken": "^9.0.3",
|
|
25
|
+
"ts-node": "^10.9.2"
|
|
26
|
+
}
|
|
13
27
|
}
|
package/src/config.ts
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { BaseError } from './base-error.js'
|
|
2
|
+
|
|
3
|
+
export class BadRequestError extends BaseError {
|
|
4
|
+
statusCode = 400;
|
|
5
|
+
|
|
6
|
+
constructor(message: string) {
|
|
7
|
+
super(message);
|
|
8
|
+
Object.setPrototypeOf(this, BadRequestError.prototype);
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
serialize() {
|
|
12
|
+
return [{ message: this.message }]
|
|
13
|
+
}
|
|
14
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
export abstract class BaseError extends Error {
|
|
2
|
+
abstract statusCode: number;
|
|
3
|
+
|
|
4
|
+
constructor(message: string) {
|
|
5
|
+
super(message);
|
|
6
|
+
Object.setPrototypeOf(this, BaseError.prototype);
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
abstract serialize(): {
|
|
10
|
+
message: string;
|
|
11
|
+
field?: string;
|
|
12
|
+
}[]
|
|
13
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { BaseError } from './base-error.js'
|
|
2
|
+
|
|
3
|
+
export class DatabaseConnectionError extends BaseError {
|
|
4
|
+
statusCode = 500;
|
|
5
|
+
|
|
6
|
+
constructor() {
|
|
7
|
+
super('database connection error');
|
|
8
|
+
Object.setPrototypeOf(this, DatabaseConnectionError.prototype);
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
serialize() {
|
|
12
|
+
return [{ message: 'database connection error' }]
|
|
13
|
+
}
|
|
14
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { BaseError } from './base-error.js'
|
|
2
|
+
|
|
3
|
+
export class NotFoundError extends BaseError {
|
|
4
|
+
statusCode = 404;
|
|
5
|
+
|
|
6
|
+
constructor() {
|
|
7
|
+
super('not found');
|
|
8
|
+
Object.setPrototypeOf(this, NotFoundError.prototype);
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
serialize() {
|
|
12
|
+
return [{ message: 'not found' }]
|
|
13
|
+
}
|
|
14
|
+
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import type { ValidationError } from 'express-validator';
|
|
2
|
+
import { BaseError } from './base-error.js'
|
|
3
|
+
|
|
4
|
+
export class RequestValidationError extends BaseError {
|
|
5
|
+
statusCode = 400;
|
|
6
|
+
errors: ValidationError[];
|
|
7
|
+
constructor(errors: ValidationError[]) {
|
|
8
|
+
super('validation error');
|
|
9
|
+
this.errors = errors;
|
|
10
|
+
Object.setPrototypeOf(this, RequestValidationError.prototype);
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
serialize() {
|
|
14
|
+
return this.errors.map((error) => {
|
|
15
|
+
return error.type === 'field'
|
|
16
|
+
? {
|
|
17
|
+
message: error.msg as string,
|
|
18
|
+
field: error.path,
|
|
19
|
+
}
|
|
20
|
+
: { message: error.msg as string };
|
|
21
|
+
});
|
|
22
|
+
}
|
|
23
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { BaseError } from './base-error.js';
|
|
2
|
+
|
|
3
|
+
export class UnauthorizedError extends BaseError {
|
|
4
|
+
statusCode = 401;
|
|
5
|
+
|
|
6
|
+
constructor() {
|
|
7
|
+
super('Unauthorized');
|
|
8
|
+
Object.setPrototypeOf(this, UnauthorizedError.prototype);
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
serialize() {
|
|
12
|
+
return [{ message: 'Unauthorized' }];
|
|
13
|
+
}
|
|
14
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import type { Request, Response, NextFunction } from 'express';
|
|
2
|
+
import { BaseError } from '../errors/index.js';
|
|
3
|
+
|
|
4
|
+
export const errorHandler = (
|
|
5
|
+
err: Error,
|
|
6
|
+
req: Request,
|
|
7
|
+
res: Response,
|
|
8
|
+
next: NextFunction,
|
|
9
|
+
) => {
|
|
10
|
+
if (err instanceof BaseError) {
|
|
11
|
+
return res.status(err.statusCode).send({ errors: err.serialize() });
|
|
12
|
+
}
|
|
13
|
+
return res.status(400).send({ errors: [{ message: err.message }] });
|
|
14
|
+
};
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import type { Request, Response, NextFunction } from 'express';
|
|
2
|
+
import jwt from 'jsonwebtoken';
|
|
3
|
+
import { COOKIE_NAME, JWT_KEY } from '../config.js';
|
|
4
|
+
|
|
5
|
+
interface CurrentUser {
|
|
6
|
+
id: string;
|
|
7
|
+
email: string;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
declare global {
|
|
11
|
+
namespace Express {
|
|
12
|
+
interface Request {
|
|
13
|
+
currentUser?: CurrentUser | null;
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export const getCurrentUserHandler = (
|
|
19
|
+
req: Request & { session?: Record<string, string> },
|
|
20
|
+
_res: Response,
|
|
21
|
+
next: NextFunction,
|
|
22
|
+
) => {
|
|
23
|
+
const token = req.session?.[COOKIE_NAME];
|
|
24
|
+
if (!token) {
|
|
25
|
+
req.currentUser = null;
|
|
26
|
+
return next();
|
|
27
|
+
}
|
|
28
|
+
try {
|
|
29
|
+
const verifiedUser = jwt.verify(token, JWT_KEY!) as CurrentUser;
|
|
30
|
+
req.currentUser = verifiedUser;
|
|
31
|
+
} catch (error) {
|
|
32
|
+
req.currentUser = null;
|
|
33
|
+
}
|
|
34
|
+
next();
|
|
35
|
+
};
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import type { Request, Response, NextFunction } from 'express';
|
|
2
|
+
import { UnauthorizedError } from '../errors/index.js';
|
|
3
|
+
|
|
4
|
+
export const requireAuthHandler = (
|
|
5
|
+
req: Request,
|
|
6
|
+
_res: Response,
|
|
7
|
+
next: NextFunction,
|
|
8
|
+
) => {
|
|
9
|
+
if (!req.currentUser) {
|
|
10
|
+
throw new UnauthorizedError();
|
|
11
|
+
}
|
|
12
|
+
next();
|
|
13
|
+
};
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import type { Request, Response, NextFunction } from 'express';
|
|
2
|
+
import { validationResult } from 'express-validator';
|
|
3
|
+
import { RequestValidationError } from '../errors/index.js';
|
|
4
|
+
|
|
5
|
+
export const validationHandler = (
|
|
6
|
+
req: Request,
|
|
7
|
+
_res: Response,
|
|
8
|
+
next: NextFunction,
|
|
9
|
+
) => {
|
|
10
|
+
const errors = validationResult(req);
|
|
11
|
+
if (!errors.isEmpty()) {
|
|
12
|
+
throw new RequestValidationError(errors.array());
|
|
13
|
+
}
|
|
14
|
+
next();
|
|
15
|
+
};
|
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": true,
|
|
39
|
+
"isolatedModules": true,
|
|
40
|
+
"noUncheckedSideEffectImports": true,
|
|
41
|
+
"moduleDetection": "force",
|
|
42
|
+
"skipLibCheck": true,
|
|
43
|
+
}
|
|
44
|
+
}
|