@sachin-tosa/tosa-backend 1.0.0
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/dist/tosa-backend.cjs.js +1 -0
- package/dist/tosa-backend.es.js +51 -0
- package/package.json +32 -0
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"use strict";Object.defineProperty(exports,Symbol.toStringTag,{value:"Module"});const u=require("mongodb");function l(e="user"){return function(n,t,r){const o=n.user;if(!o)return t.status(401).json({message:"Unauthorized"});if(e!=="user"&&o.role!==e)return t.status(403).json({message:"Forbidden"});r()}}let s,a;async function i(e,n){return s||(s=new u.MongoClient(e),await s.connect(),a=s.db(n),console.log("DB Connected:",n)),a}function d(){if(!a)throw new Error("DB not connected");return a}function g(e){return(n,t,r)=>{const{error:o}=e.validate(n.body);if(o)return t.status(400).json({message:o.details[0].message});r()}}function f(e,n,t,r){console.error(e.stack),t.status(e.status||500).json({message:e.message||"Internal Server Error",stack:process.env.NODE_ENV==="development"?e.stack:void 0})}const m={info:e=>console.log("[INFO]",e),error:e=>console.error("[ERROR]",e),warn:e=>console.warn("[WARN]",e)};let c={};const w={set:(e,n)=>c[e]=n,get:e=>c[e],delete:e=>delete c[e]};exports.authMiddleware=l;exports.cache=w;exports.connectDB=i;exports.errorHandler=f;exports.getDB=d;exports.logger=m;exports.validate=g;
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import { MongoClient as u } from "mongodb";
|
|
2
|
+
function l(e = "user") {
|
|
3
|
+
return function(n, t, r) {
|
|
4
|
+
const o = n.user;
|
|
5
|
+
if (!o) return t.status(401).json({ message: "Unauthorized" });
|
|
6
|
+
if (e !== "user" && o.role !== e)
|
|
7
|
+
return t.status(403).json({ message: "Forbidden" });
|
|
8
|
+
r();
|
|
9
|
+
};
|
|
10
|
+
}
|
|
11
|
+
let s, c;
|
|
12
|
+
async function d(e, n) {
|
|
13
|
+
return s || (s = new u(e), await s.connect(), c = s.db(n), console.log("DB Connected:", n)), c;
|
|
14
|
+
}
|
|
15
|
+
function f() {
|
|
16
|
+
if (!c) throw new Error("DB not connected");
|
|
17
|
+
return c;
|
|
18
|
+
}
|
|
19
|
+
function g(e) {
|
|
20
|
+
return (n, t, r) => {
|
|
21
|
+
const { error: o } = e.validate(n.body);
|
|
22
|
+
if (o) return t.status(400).json({ message: o.details[0].message });
|
|
23
|
+
r();
|
|
24
|
+
};
|
|
25
|
+
}
|
|
26
|
+
function m(e, n, t, r) {
|
|
27
|
+
console.error(e.stack), t.status(e.status || 500).json({
|
|
28
|
+
message: e.message || "Internal Server Error",
|
|
29
|
+
stack: process.env.NODE_ENV === "development" ? e.stack : void 0
|
|
30
|
+
});
|
|
31
|
+
}
|
|
32
|
+
const w = {
|
|
33
|
+
info: (e) => console.log("[INFO]", e),
|
|
34
|
+
error: (e) => console.error("[ERROR]", e),
|
|
35
|
+
warn: (e) => console.warn("[WARN]", e)
|
|
36
|
+
};
|
|
37
|
+
let a = {};
|
|
38
|
+
const v = {
|
|
39
|
+
set: (e, n) => a[e] = n,
|
|
40
|
+
get: (e) => a[e],
|
|
41
|
+
delete: (e) => delete a[e]
|
|
42
|
+
};
|
|
43
|
+
export {
|
|
44
|
+
l as authMiddleware,
|
|
45
|
+
v as cache,
|
|
46
|
+
d as connectDB,
|
|
47
|
+
m as errorHandler,
|
|
48
|
+
f as getDB,
|
|
49
|
+
w as logger,
|
|
50
|
+
g as validate
|
|
51
|
+
};
|
package/package.json
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@sachin-tosa/tosa-backend",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "Reusable backend components for Node.js/Express apps",
|
|
5
|
+
"main": "dist/index.js",
|
|
6
|
+
"type": "module",
|
|
7
|
+
"files": [
|
|
8
|
+
"dist"
|
|
9
|
+
],
|
|
10
|
+
"scripts": {
|
|
11
|
+
"build": "vite build",
|
|
12
|
+
"prepare": "npm run build"
|
|
13
|
+
},
|
|
14
|
+
"keywords": [
|
|
15
|
+
"backend",
|
|
16
|
+
"nodejs",
|
|
17
|
+
"express",
|
|
18
|
+
"components",
|
|
19
|
+
"tosa"
|
|
20
|
+
],
|
|
21
|
+
"author": "Sachin Ruhela",
|
|
22
|
+
"license": "MIT",
|
|
23
|
+
"peerDependencies": {
|
|
24
|
+
"express": ">=4.18.2",
|
|
25
|
+
"mongodb": ">=5.6.0",
|
|
26
|
+
"joi": ">=17.9.2"
|
|
27
|
+
},
|
|
28
|
+
"devDependencies": {
|
|
29
|
+
"vite": "^7.3.1",
|
|
30
|
+
"@vitejs/plugin-node-resolve": "^1.0.0"
|
|
31
|
+
}
|
|
32
|
+
}
|