powr-sdk-api 3.1.3 → 3.1.4

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,22 +3,31 @@
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 {boolean} options.requireProjectId - Whether to require projectId validation (default: true)
10
+ * @param {Array} options.excludePaths - Array of paths to exclude from projectId validation
9
11
  * @returns {Function} Express middleware function
10
12
  */
11
13
  const injectProjectId = (options = {}) => {
12
14
  const isCentralService = options.isCentralService || false;
15
+ const requireProjectId = options.requireProjectId !== false; // Default to true
16
+ const excludePaths = options.excludePaths || ['/auth/login', '/auth/register', '/health'];
13
17
  return (req, res, next) => {
14
18
  try {
19
+ // Skip validation for excluded paths
20
+ if (excludePaths.some(path => req.path.startsWith(path))) {
21
+ req.projectId = null;
22
+ return next();
23
+ }
15
24
  if (isCentralService) {
16
25
  // For central services, get projectId from request
17
26
  const projectId = req.query.projectId || req.body.projectId;
18
- if (!projectId) {
27
+ if (requireProjectId && !projectId) {
19
28
  return res.status(400).json({
20
29
  success: false,
21
- message: 'projectId is required for central services'
30
+ message: 'projectId is required'
22
31
  });
23
32
  }
24
33
  req.projectId = projectId;
@@ -26,6 +35,14 @@ const injectProjectId = (options = {}) => {
26
35
  // For individual APIs, use config.projectId
27
36
  req.projectId = config.projectId;
28
37
  }
38
+
39
+ // Validate projectId if required
40
+ if (requireProjectId && !req.projectId) {
41
+ return res.status(400).json({
42
+ success: false,
43
+ message: 'projectId is required'
44
+ });
45
+ }
29
46
  next();
30
47
  } catch (error) {
31
48
  console.error('❌ ProjectId injection error:', error.message);
@@ -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({
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.4",
4
4
  "description": "Shared API core library for PowrStack projects",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",