powr-sdk-api 3.1.3 → 3.1.5

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.
@@ -3,29 +3,37 @@
3
3
  const config = require('../config');
4
4
 
5
5
  /**
6
- * Middleware to inject projectId into requests
6
+ * Middleware to inject and validate projectId into requests
7
7
  * @param {Object} options - Configuration options
8
8
  * @param {boolean} options.isCentralService - Whether this is a central service (like powr-base-cloud)
9
+ * @param {Array} options.excludePaths - Array of paths to exclude from projectId validation
9
10
  * @returns {Function} Express middleware function
10
11
  */
11
12
  const injectProjectId = (options = {}) => {
12
13
  const isCentralService = options.isCentralService || false;
14
+ const excludePaths = options.excludePaths || ['/auth/login', '/auth/register', '/health'];
13
15
  return (req, res, next) => {
14
16
  try {
17
+ // Skip validation for excluded paths
18
+ if (excludePaths.some(path => req.path.startsWith(path))) {
19
+ req.projectId = null;
20
+ return next();
21
+ }
15
22
  if (isCentralService) {
16
23
  // For central services, get projectId from request
17
- const projectId = req.query.projectId || req.body.projectId;
18
- if (!projectId) {
19
- return res.status(400).json({
20
- success: false,
21
- message: 'projectId is required for central services'
22
- });
23
- }
24
- req.projectId = projectId;
24
+ req.projectId = req.query.projectId || req.body.projectId;
25
25
  } else {
26
26
  // For individual APIs, use config.projectId
27
27
  req.projectId = config.projectId;
28
28
  }
29
+
30
+ // Validate projectId (always required except for excluded paths)
31
+ if (!req.projectId) {
32
+ return res.status(400).json({
33
+ success: false,
34
+ message: 'projectId is required'
35
+ });
36
+ }
29
37
  next();
30
38
  } catch (error) {
31
39
  console.error('❌ ProjectId injection error:', error.message);
@@ -52,12 +52,6 @@ router.get('/:formName', async (req, res) => {
52
52
  formName
53
53
  } = req.params;
54
54
  const projectId = req.projectId;
55
- if (!projectId) {
56
- return res.status(400).json({
57
- success: false,
58
- message: 'projectId is required'
59
- });
60
- }
61
55
  const db = await getDb();
62
56
  const collection = db.collection("powrForm");
63
57
  const formData = await collection.findOne({
@@ -170,15 +164,7 @@ router.get('/getCount/:formName', async (req, res) => {
170
164
  // POST /create-form - Upload JSON file and store form data
171
165
  router.post('/create-form', upload.single('jsonFile'), async (req, res) => {
172
166
  try {
173
- const {
174
- projectId
175
- } = req.query;
176
- if (!projectId) {
177
- return res.status(400).json({
178
- success: false,
179
- message: 'projectId is required as query parameter'
180
- });
181
- }
167
+ const projectId = req.projectId;
182
168
  if (!req.file) {
183
169
  return res.status(400).json({
184
170
  success: false,
@@ -90,12 +90,6 @@ router.get('/', async (req, res) => {
90
90
  const query = {
91
91
  projectId
92
92
  };
93
- if (!projectId) {
94
- return res.status(400).json({
95
- success: false,
96
- message: 'projectId is required.'
97
- });
98
- }
99
93
  const db = await getDb();
100
94
  const functionsData = await db.collection("functions").find(query).toArray();
101
95
  return res.status(200).json({
@@ -115,12 +109,6 @@ router.get('/', async (req, res) => {
115
109
  router.post('/', async (req, res) => {
116
110
  try {
117
111
  const projectId = req.projectId;
118
- if (!projectId) {
119
- return res.status(400).json({
120
- success: false,
121
- message: 'projectId is required.'
122
- });
123
- }
124
112
  const newFunction = req.body;
125
113
  newFunction.projectId = projectId;
126
114
  if (!newFunction || Object.keys(newFunction).length === 0) {
@@ -159,12 +147,6 @@ router.put('/:function', async (req, res) => {
159
147
  code
160
148
  } = req.body;
161
149
  const projectId = req.projectId;
162
- if (!projectId) {
163
- return res.status(400).json({
164
- success: false,
165
- message: 'projectId is required.'
166
- });
167
- }
168
150
  if (!code) {
169
151
  return res.status(400).json({
170
152
  success: false,
@@ -222,12 +204,6 @@ router.post("/:function", async (req, res) => {
222
204
  function: functionName
223
205
  } = req.params;
224
206
  const projectId = req.projectId;
225
- if (!projectId) {
226
- return res.status(400).json({
227
- success: false,
228
- message: 'projectId is required.'
229
- });
230
- }
231
207
  try {
232
208
  // Use pre-compiled function (NO COMPILATION HERE)
233
209
  const params = {
@@ -14,12 +14,6 @@ router.get('/', async (req, res) => {
14
14
  const query = {
15
15
  projectId
16
16
  };
17
- if (!projectId) {
18
- return res.status(400).json({
19
- success: false,
20
- message: 'projectId is required.'
21
- });
22
- }
23
17
  const db = await getDb();
24
18
  const invoiceData = await db.collection("invoices").find(query).toArray();
25
19
  return res.status(200).json({
@@ -40,12 +34,6 @@ router.get('/:invoiceId', async (req, res) => {
40
34
  } = req.params;
41
35
  const projectId = req.projectId;
42
36
  try {
43
- if (!projectId) {
44
- return res.status(400).json({
45
- success: false,
46
- message: 'projectId is required.'
47
- });
48
- }
49
37
  const _id = new ObjectId(invoiceId);
50
38
  const db = await getDb();
51
39
  const invoiceData = await db.collection("invoices").findOne({
@@ -74,12 +62,6 @@ router.post('/', async (req, res) => {
74
62
  serviceSection,
75
63
  paymentSection
76
64
  } = req.body;
77
- if (!projectId) {
78
- return res.status(400).json({
79
- success: false,
80
- message: "projectId is required."
81
- });
82
- }
83
65
  const invoice = {
84
66
  invoiceNumber,
85
67
  dateOfIssue,
@@ -119,12 +101,6 @@ router.put('/:invoiceId', async (req, res) => {
119
101
  invoiceId
120
102
  } = req.params;
121
103
  const projectId = req.projectId;
122
- if (!projectId) {
123
- return res.status(400).json({
124
- success: false,
125
- message: "projectId is required."
126
- });
127
- }
128
104
  let filter;
129
105
  try {
130
106
  filter = {
@@ -16,13 +16,13 @@ router.post('/', async (req, res) => {
16
16
  } = req.body;
17
17
  const {
18
18
  userId,
19
- projectId,
20
19
  contentId
21
20
  } = req.query;
22
- if (!userId || typeof liked !== 'boolean' || !projectId) {
21
+ const projectId = req.projectId;
22
+ if (!userId || typeof liked !== 'boolean') {
23
23
  return res.status(400).json({
24
24
  success: false,
25
- message: 'userId and projectId are required in query, liked (boolean) is required in body.'
25
+ message: 'userId is required in query, liked (boolean) is required in body.'
26
26
  });
27
27
  }
28
28
  try {
@@ -58,15 +58,9 @@ router.post('/', async (req, res) => {
58
58
  });
59
59
  router.get('/', async (req, res) => {
60
60
  const {
61
- projectId,
62
61
  contentId
63
62
  } = req.query;
64
- if (!projectId) {
65
- return res.status(400).json({
66
- success: false,
67
- message: 'projectId is required in query.'
68
- });
69
- }
63
+ const projectId = req.projectId;
70
64
  try {
71
65
  const query = {
72
66
  projectId
@@ -97,14 +91,14 @@ router.get('/:likeId', async (req, res) => {
97
91
  likeId
98
92
  } = req.params;
99
93
  const {
100
- projectId,
101
94
  contentId
102
95
  } = req.query;
96
+ const projectId = req.projectId;
103
97
  try {
104
98
  const filter = {
105
- userId: new ObjectId(likeId)
99
+ userId: new ObjectId(likeId),
100
+ projectId
106
101
  };
107
- if (projectId) filter.projectId = projectId;
108
102
  if (contentId) filter.contentId = contentId;
109
103
  const db = await getDb();
110
104
  const like = await db.collection('likes').findOne(filter);
@@ -65,12 +65,6 @@ router.get("/", async (req, res) => {
65
65
  userId
66
66
  } = req.query;
67
67
  const projectId = req.projectId;
68
- if (!projectId) {
69
- return res.status(400).json({
70
- success: false,
71
- message: "projectId is required."
72
- });
73
- }
74
68
  try {
75
69
  const query = {
76
70
  projectId
@@ -106,10 +100,10 @@ router.delete("/", async (req, res) => {
106
100
  ratingId
107
101
  } = req.query;
108
102
  const projectId = req.projectId;
109
- if (!ratingId || !projectId) {
103
+ if (!ratingId) {
110
104
  return res.status(400).json({
111
105
  success: false,
112
- message: "Both ratingId and projectId are required to delete a rating."
106
+ message: "ratingId is required to delete a rating."
113
107
  });
114
108
  }
115
109
  try {
@@ -143,10 +137,10 @@ router.get("/average", async (req, res) => {
143
137
  itemId
144
138
  } = req.query;
145
139
  const projectId = req.projectId;
146
- if (!projectId || !itemId) {
140
+ if (!itemId) {
147
141
  return res.status(400).json({
148
142
  success: false,
149
- message: "Both projectId and itemId are required."
143
+ message: "itemId is required."
150
144
  });
151
145
  }
152
146
  try {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "powr-sdk-api",
3
- "version": "3.1.3",
3
+ "version": "3.1.5",
4
4
  "description": "Shared API core library for PowrStack projects",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",