@prepskill/common 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.
@@ -0,0 +1,2 @@
1
+ export declare const generateAccessToken: (userId: string) => string;
2
+ export declare const generateRefreshToken: (userId: string) => string;
@@ -0,0 +1,19 @@
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.generateRefreshToken = exports.generateAccessToken = void 0;
7
+ const jsonwebtoken_1 = __importDefault(require("jsonwebtoken"));
8
+ const generateAccessToken = (userId) => {
9
+ return jsonwebtoken_1.default.sign({ userId }, process.env.JWT_SECRET, {
10
+ expiresIn: "15m",
11
+ });
12
+ };
13
+ exports.generateAccessToken = generateAccessToken;
14
+ const generateRefreshToken = (userId) => {
15
+ return jsonwebtoken_1.default.sign({ userId }, process.env.JWT_REFRESH_SECRET, {
16
+ expiresIn: "7d",
17
+ });
18
+ };
19
+ exports.generateRefreshToken = generateRefreshToken;
@@ -0,0 +1,4 @@
1
+ export declare class AppError extends Error {
2
+ statusCode: number;
3
+ constructor(message: string, statusCode: number);
4
+ }
@@ -0,0 +1,11 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.AppError = void 0;
4
+ class AppError extends Error {
5
+ constructor(message, statusCode) {
6
+ super(message);
7
+ this.statusCode = statusCode;
8
+ Object.setPrototypeOf(this, AppError.prototype);
9
+ }
10
+ }
11
+ exports.AppError = AppError;
@@ -0,0 +1,2 @@
1
+ import { RequestHandler } from "express";
2
+ export declare const asyncHandler: (fn: RequestHandler) => RequestHandler;
@@ -0,0 +1,7 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.asyncHandler = void 0;
4
+ const asyncHandler = (fn) => (req, res, next) => {
5
+ Promise.resolve(fn(req, res, next)).catch(next);
6
+ };
7
+ exports.asyncHandler = asyncHandler;
@@ -0,0 +1,2 @@
1
+ import { Request, Response, NextFunction } from "express";
2
+ export declare const errorHandler: (err: any, req: Request, res: Response, next: NextFunction) => Response<any, Record<string, any>>;
@@ -0,0 +1,17 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.errorHandler = void 0;
4
+ const AppError_1 = require("../errors/AppError");
5
+ const errorHandler = (err, req, res, next) => {
6
+ if (err instanceof AppError_1.AppError) {
7
+ return res.status(err.statusCode).json({
8
+ success: false,
9
+ message: err.message,
10
+ });
11
+ }
12
+ return res.status(500).json({
13
+ success: false,
14
+ message: "Internal Server Error",
15
+ });
16
+ };
17
+ exports.errorHandler = errorHandler;
@@ -0,0 +1,2 @@
1
+ import { Request, Response, NextFunction } from "express";
2
+ export declare const protect: (secret: string) => (req: Request, res: Response, next: NextFunction) => void;
@@ -0,0 +1,26 @@
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.protect = void 0;
7
+ const jsonwebtoken_1 = __importDefault(require("jsonwebtoken"));
8
+ const AppError_1 = require("../errors/AppError");
9
+ const protect = (secret) => {
10
+ return (req, res, next) => {
11
+ const authHeader = req.headers.authorization;
12
+ if (!authHeader || !authHeader.startsWith("Bearer ")) {
13
+ throw new AppError_1.AppError("Not authorized", 401);
14
+ }
15
+ const token = authHeader.split(" ")[1];
16
+ try {
17
+ const decoded = jsonwebtoken_1.default.verify(token, secret);
18
+ req.user = decoded;
19
+ next();
20
+ }
21
+ catch {
22
+ throw new AppError_1.AppError("Invalid token", 401);
23
+ }
24
+ };
25
+ };
26
+ exports.protect = protect;
package/package.json ADDED
@@ -0,0 +1,28 @@
1
+ {
2
+ "name": "@prepskill/common",
3
+ "version": "1.0.2",
4
+ "description": "Common utilities, errors and middlewares for Prepskill microservices",
5
+ "main": "dist/index.js",
6
+ "types": "dist/index.d.ts",
7
+ "files": [
8
+ "dist"
9
+ ],
10
+ "scripts": {
11
+ "build": "tsc"
12
+ },
13
+ "publishConfig": {
14
+ "access": "public"
15
+ },
16
+ "keywords": [],
17
+ "author": "",
18
+ "license": "ISC",
19
+ "dependencies": {
20
+ "express": "^5.2.1",
21
+ "jsonwebtoken": "^9.0.3"
22
+ },
23
+ "devDependencies": {
24
+ "@types/express": "^5.0.6",
25
+ "@types/jsonwebtoken": "^9.0.10",
26
+ "typescript": "^5.9.3"
27
+ }
28
+ }