@mtg-tracker/common 1.0.10 → 1.0.12
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/build/errors/bad-request-error.d.ts +9 -0
- package/build/errors/bad-request-error.js +16 -0
- package/build/errors/custom-error.d.ts +8 -0
- package/build/errors/custom-error.js +10 -0
- package/build/errors/request-validation-error.d.ts +14 -0
- package/build/errors/request-validation-error.js +22 -0
- package/build/functions/runMigrations.d.ts +1 -1
- package/build/functions/runMigrations.js +11 -8
- package/build/index.d.ts +6 -0
- package/build/index.js +6 -0
- package/build/middlewares/current-user.d.ts +14 -0
- package/build/middlewares/current-user.js +20 -0
- package/build/middlewares/error-handler.d.ts +2 -0
- package/build/middlewares/error-handler.js +13 -0
- package/build/middlewares/validate-request.d.ts +2 -0
- package/build/middlewares/validate-request.js +13 -0
- package/package.json +1 -1
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.BadRequestError = void 0;
|
|
4
|
+
const custom_error_1 = require("./custom-error");
|
|
5
|
+
class BadRequestError extends custom_error_1.CustomError {
|
|
6
|
+
constructor(message) {
|
|
7
|
+
super(message);
|
|
8
|
+
this.message = message;
|
|
9
|
+
this.statusCode = 400;
|
|
10
|
+
Object.setPrototypeOf(this, BadRequestError.prototype);
|
|
11
|
+
}
|
|
12
|
+
serializeErrors() {
|
|
13
|
+
return [{ message: this.message }];
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
exports.BadRequestError = BadRequestError;
|
|
@@ -0,0 +1,10 @@
|
|
|
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
|
+
Object.setPrototypeOf(this, CustomError.prototype);
|
|
8
|
+
}
|
|
9
|
+
}
|
|
10
|
+
exports.CustomError = CustomError;
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { 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?: undefined;
|
|
13
|
+
})[];
|
|
14
|
+
}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.RequestValidationError = void 0;
|
|
4
|
+
const custom_error_1 = require("./custom-error");
|
|
5
|
+
class RequestValidationError extends custom_error_1.CustomError {
|
|
6
|
+
constructor(errors) {
|
|
7
|
+
super('Invalid request parameters');
|
|
8
|
+
this.errors = errors;
|
|
9
|
+
this.statusCode = 400;
|
|
10
|
+
// Only because we are extending a built in class
|
|
11
|
+
Object.setPrototypeOf(this, RequestValidationError.prototype);
|
|
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
|
+
}
|
|
22
|
+
exports.RequestValidationError = RequestValidationError;
|
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
import mysql from 'mysql2/promise';
|
|
2
|
-
export declare function runMigrations(pool: mysql.Pool): Promise<void>;
|
|
2
|
+
export declare function runMigrations(pool: mysql.Pool, service: string): Promise<void>;
|
|
@@ -15,24 +15,27 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
15
15
|
exports.runMigrations = runMigrations;
|
|
16
16
|
const fs_1 = __importDefault(require("fs"));
|
|
17
17
|
const path_1 = __importDefault(require("path"));
|
|
18
|
-
function runMigrations(pool) {
|
|
18
|
+
function runMigrations(pool, service) {
|
|
19
19
|
return __awaiter(this, void 0, void 0, function* () {
|
|
20
|
-
console.log(
|
|
20
|
+
console.log(`Running database migrations for the ${service} service...`);
|
|
21
|
+
// Sanitize service name
|
|
22
|
+
const sanitizedService = service.replace(/[^a-zA-Z0-9_]/g, '');
|
|
23
|
+
const migrationsTable = `migrations_${sanitizedService}`;
|
|
21
24
|
// Create migrations tracking table if it doesn't exist
|
|
22
25
|
yield pool.query(`
|
|
23
|
-
CREATE TABLE IF NOT EXISTS
|
|
26
|
+
CREATE TABLE IF NOT EXISTS ${migrationsTable} (
|
|
24
27
|
id INT AUTO_INCREMENT PRIMARY KEY,
|
|
25
28
|
filename VARCHAR(255) NOT NULL UNIQUE,
|
|
26
29
|
executed_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
|
|
27
30
|
)
|
|
28
31
|
`);
|
|
29
32
|
// Get all migration files
|
|
30
|
-
const migrationsDir = path_1.default.join(__dirname);
|
|
33
|
+
const migrationsDir = path_1.default.join(__dirname, 'migrations');
|
|
31
34
|
const files = fs_1.default.readdirSync(migrationsDir)
|
|
32
35
|
.filter(file => file.endsWith('_up.sql'))
|
|
33
|
-
.sort();
|
|
36
|
+
.sort(); // Sort to ensure correct order
|
|
34
37
|
// Check which migrations have already been run
|
|
35
|
-
const [executedMigrations] = yield pool.query(
|
|
38
|
+
const [executedMigrations] = yield pool.query(`SELECT filename FROM ${migrationsTable}`);
|
|
36
39
|
const executedFiles = new Set(executedMigrations.map(row => row.filename));
|
|
37
40
|
// Run pending migrations
|
|
38
41
|
for (const file of files) {
|
|
@@ -47,7 +50,7 @@ function runMigrations(pool) {
|
|
|
47
50
|
// Execute the migration
|
|
48
51
|
yield pool.query(sql);
|
|
49
52
|
// Record that this migration was executed
|
|
50
|
-
yield pool.query(
|
|
53
|
+
yield pool.query(`INSERT INTO ${migrationsTable} (filename) VALUES (?)`, [file]);
|
|
51
54
|
console.log(`Migration ${file} completed successfully`);
|
|
52
55
|
}
|
|
53
56
|
catch (error) {
|
|
@@ -55,6 +58,6 @@ function runMigrations(pool) {
|
|
|
55
58
|
throw error;
|
|
56
59
|
}
|
|
57
60
|
}
|
|
58
|
-
console.log('All migrations
|
|
61
|
+
console.log('All migrations completed for the ' + service + ' service');
|
|
59
62
|
});
|
|
60
63
|
}
|
package/build/index.d.ts
CHANGED
|
@@ -1 +1,7 @@
|
|
|
1
1
|
export * from './functions/runMigrations';
|
|
2
|
+
export * from './errors/bad-request-error';
|
|
3
|
+
export * from './errors/custom-error';
|
|
4
|
+
export * from './errors/request-validation-error';
|
|
5
|
+
export * from './middlewares/current-user';
|
|
6
|
+
export * from './middlewares/error-handler';
|
|
7
|
+
export * from './middlewares/validate-request';
|
package/build/index.js
CHANGED
|
@@ -15,3 +15,9 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
|
15
15
|
};
|
|
16
16
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
17
|
__exportStar(require("./functions/runMigrations"), exports);
|
|
18
|
+
__exportStar(require("./errors/bad-request-error"), exports);
|
|
19
|
+
__exportStar(require("./errors/custom-error"), exports);
|
|
20
|
+
__exportStar(require("./errors/request-validation-error"), exports);
|
|
21
|
+
__exportStar(require("./middlewares/current-user"), exports);
|
|
22
|
+
__exportStar(require("./middlewares/error-handler"), exports);
|
|
23
|
+
__exportStar(require("./middlewares/validate-request"), exports);
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { 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 {};
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.currentUser = void 0;
|
|
7
|
+
const jsonwebtoken_1 = __importDefault(require("jsonwebtoken"));
|
|
8
|
+
const currentUser = (req, res, next) => {
|
|
9
|
+
var _a;
|
|
10
|
+
if (!((_a = req.session) === null || _a === void 0 ? void 0 : _a.jwt)) {
|
|
11
|
+
return next();
|
|
12
|
+
}
|
|
13
|
+
try {
|
|
14
|
+
const payload = jsonwebtoken_1.default.verify(req.session.jwt, process.env.JWT_KEY || 'your-secret-key');
|
|
15
|
+
req.currentUser = payload;
|
|
16
|
+
}
|
|
17
|
+
catch (err) { }
|
|
18
|
+
next();
|
|
19
|
+
};
|
|
20
|
+
exports.currentUser = currentUser;
|
|
@@ -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
|
+
return res.status(err.statusCode).send({ errors: err.serializeErrors() });
|
|
8
|
+
}
|
|
9
|
+
res.status(400).send({
|
|
10
|
+
errors: [{ message: 'Something went wrong' }]
|
|
11
|
+
});
|
|
12
|
+
};
|
|
13
|
+
exports.errorHandler = errorHandler;
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.validateRequest = void 0;
|
|
4
|
+
const express_validator_1 = require("express-validator");
|
|
5
|
+
const request_validation_error_1 = require("../errors/request-validation-error");
|
|
6
|
+
const validateRequest = (req, res, next) => {
|
|
7
|
+
const errors = (0, express_validator_1.validationResult)(req);
|
|
8
|
+
if (!errors.isEmpty()) {
|
|
9
|
+
throw new request_validation_error_1.RequestValidationError(errors.array());
|
|
10
|
+
}
|
|
11
|
+
next();
|
|
12
|
+
};
|
|
13
|
+
exports.validateRequest = validateRequest;
|