@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 CHANGED
@@ -1,13 +1,27 @@
1
1
  {
2
2
  "name": "@doffy-gittix/common",
3
- "version": "1.0.0",
3
+ "version": "1.0.2",
4
4
  "description": "",
5
- "main": "index.js",
5
+ "main": "./build/index.js",
6
6
  "scripts": {
7
- "test": "echo \"Error: no test specified\" && exit 1"
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": "commonjs"
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,3 @@
1
+ export const COOKIE_NAME = process.env.COOKIE_NAME;
2
+ export const COOKIE_KEY = process.env.COOKIE_KEY;
3
+ export const JWT_KEY = process.env.JWT_KEY;
@@ -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,6 @@
1
+ export * from './base-error.js';
2
+ export * from './request-validation-error.js';
3
+ export * from './database-connection-error.js';
4
+ export * from './notfound-error.js';
5
+ export * from './bad-request-error.js';
6
+ export * from './unauthorized_error.js';
@@ -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,2 @@
1
+ export * from './errors/index.js';
2
+ export * from './middleware/index.js';
@@ -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,4 @@
1
+ export * from './error-handler.js';
2
+ export * from './get-current-user-handler.js';
3
+ export * from './require-auth-handler.js';
4
+ export * from './validation-handler.js';
@@ -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
+ }