powr-sdk-api 3.0.1 → 3.0.3

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/index.js CHANGED
@@ -17,6 +17,9 @@ const {
17
17
  createPowrRoutes,
18
18
  initializePlexx
19
19
  } = require("./routes");
20
+ const {
21
+ verifyToken
22
+ } = require("./middleware/jwtToken");
20
23
  module.exports = {
21
24
  errorCatcher,
22
25
  errorHandler,
@@ -24,5 +27,6 @@ module.exports = {
24
27
  createSwaggerSpec,
25
28
  notFoundHandler,
26
29
  createPowrRoutes,
27
- initializePlexx
30
+ initializePlexx,
31
+ verifyToken
28
32
  };
@@ -3,6 +3,9 @@
3
3
  const express = require("express");
4
4
  const router = express.Router();
5
5
  const plexxManager = require("../services/plexx");
6
+ const {
7
+ getDb
8
+ } = require("../services/mongo");
6
9
 
7
10
  // Get Plexx status
8
11
  router.get("/status", async (req, res) => {
@@ -77,7 +80,150 @@ router.post("/toggle", async (req, res) => {
77
80
  });
78
81
  }
79
82
  });
80
- router.get("/:route", async (req, res) => {
83
+
84
+ // CRUD Operations for Routes
85
+
86
+ // Get all routes entries
87
+ router.get('/routes', async (req, res) => {
88
+ const {
89
+ projectId
90
+ } = req.query;
91
+ try {
92
+ const query = {
93
+ projectId
94
+ };
95
+ if (!projectId) {
96
+ return res.status(400).json({
97
+ success: false,
98
+ message: 'projectId is required.'
99
+ });
100
+ }
101
+ const db = await getDb();
102
+ const routesData = await db.collection("routes").find(query).toArray();
103
+ return res.status(200).json({
104
+ success: true,
105
+ allroutes: routesData
106
+ });
107
+ } catch (error) {
108
+ console.error("Error retrieving routes entries:", error);
109
+ return res.status(500).json({
110
+ success: false,
111
+ message: "Failed to retrieve routes entries."
112
+ });
113
+ }
114
+ });
115
+
116
+ // Create a new route
117
+ router.post('/routes', async (req, res) => {
118
+ try {
119
+ const {
120
+ projectId
121
+ } = req.query;
122
+ if (!projectId) {
123
+ return res.status(400).json({
124
+ success: false,
125
+ message: 'projectId is required.'
126
+ });
127
+ }
128
+ const newRoute = req.body;
129
+ newRoute.projectId = projectId;
130
+ if (!newRoute || Object.keys(newRoute).length === 0) {
131
+ return res.status(400).json({
132
+ success: false,
133
+ message: 'Request body is empty or invalid.'
134
+ });
135
+ }
136
+ const db = await getDb();
137
+ const result = await db.collection('routes').insertOne(newRoute);
138
+
139
+ // Recompile the new route using Plexx manager (only if enabled)
140
+ if (plexxManager.getStats().isEnabled) {
141
+ await plexxManager.updateRoute(projectId, newRoute.route, newRoute.code);
142
+ }
143
+ return res.status(201).json({
144
+ success: true,
145
+ entry: result.ops ? result.ops[0] : newRoute
146
+ });
147
+ } catch (error) {
148
+ console.error('Error inserting route entry:', error);
149
+ return res.status(500).json({
150
+ success: false,
151
+ message: 'Failed to insert route entry.'
152
+ });
153
+ }
154
+ });
155
+
156
+ // Update route code
157
+ router.put('/routes/:route', async (req, res) => {
158
+ try {
159
+ const {
160
+ route
161
+ } = req.params;
162
+ const {
163
+ code
164
+ } = req.body;
165
+ const {
166
+ projectId
167
+ } = req.query;
168
+ if (!projectId) {
169
+ return res.status(400).json({
170
+ success: false,
171
+ message: 'projectId is required.'
172
+ });
173
+ }
174
+ if (!code) {
175
+ return res.status(400).json({
176
+ success: false,
177
+ message: 'Code field is required.'
178
+ });
179
+ }
180
+ const query = {
181
+ route,
182
+ projectId
183
+ };
184
+ const db = await getDb();
185
+ const existingRoute = await db.collection('routes').findOne(query);
186
+ if (!existingRoute) {
187
+ return res.status(404).json({
188
+ success: false,
189
+ message: 'Route not found.'
190
+ });
191
+ }
192
+
193
+ // Update database
194
+ const result = await db.collection('routes').updateOne(query, {
195
+ $set: {
196
+ code
197
+ }
198
+ });
199
+ if (result.matchedCount === 0) {
200
+ return res.status(404).json({
201
+ success: false,
202
+ message: 'Route not found.'
203
+ });
204
+ }
205
+
206
+ // Recompile the updated route using Plexx manager (only if enabled)
207
+ if (plexxManager.getStats().isEnabled) {
208
+ await plexxManager.updateRoute(projectId, route, code);
209
+ }
210
+ const updatedRoute = await db.collection('routes').findOne(query);
211
+ return res.status(200).json({
212
+ success: true,
213
+ entry: updatedRoute
214
+ });
215
+ } catch (error) {
216
+ console.error('Error updating route code:', error);
217
+ return res.status(500).json({
218
+ success: false,
219
+ message: 'Failed to update route code.',
220
+ error: error.message
221
+ });
222
+ }
223
+ });
224
+
225
+ // Execute dynamic route
226
+ router.post("/:route", async (req, res) => {
81
227
  const {
82
228
  route
83
229
  } = req.params;
@@ -17,9 +17,9 @@ const connectDB = async () => {
17
17
  client = new MongoClient(config.mongoUri);
18
18
  await client.connect();
19
19
  db = client.db();
20
- console.log('Connected to MongoDB via SDK');
20
+ console.log('Connected to PowrBase Database');
21
21
  } catch (error) {
22
- console.error('MongoDB connection error:', error);
22
+ console.error('PowrBase Database connection error:', error);
23
23
  throw error;
24
24
  }
25
25
  };
@@ -77,8 +77,8 @@ class PlexxManager {
77
77
  const cached = this.compiledFunctions.get(cacheKey);
78
78
  if (!cached) {
79
79
  if (this.isCentralService) {
80
- // Try to load project routes dynamically
81
- await this.loadProjectRoutes(projectId);
80
+ // Try to load only the specific route dynamically
81
+ await this.loadSpecificRoute(projectId, route);
82
82
  const retryCached = this.compiledFunctions.get(cacheKey);
83
83
  if (retryCached) {
84
84
  return retryCached.function(params);
@@ -89,6 +89,28 @@ class PlexxManager {
89
89
  return cached.function(params);
90
90
  }
91
91
 
92
+ // Load specific route for central service
93
+ async loadSpecificRoute(projectId, route) {
94
+ if (!this.isCentralService) {
95
+ throw new Error("Dynamic loading only available in central service mode");
96
+ }
97
+ try {
98
+ const routeData = await getDb().collection("routes").findOne({
99
+ projectId,
100
+ route
101
+ });
102
+ if (routeData) {
103
+ this.compileAndCache(routeData);
104
+ console.log(`✅ Loaded route: ${route} for project ${projectId}`);
105
+ } else {
106
+ console.log(`❌ Route not found: ${route} for project ${projectId}`);
107
+ }
108
+ } catch (error) {
109
+ console.error(`❌ Failed to load route ${route} for project ${projectId}:`, error);
110
+ throw error;
111
+ }
112
+ }
113
+
92
114
  // Dynamic loading for central service
93
115
  async loadProjectRoutes(projectId) {
94
116
  if (!this.isCentralService) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "powr-sdk-api",
3
- "version": "3.0.1",
3
+ "version": "3.0.3",
4
4
  "description": "Shared API core library for PowrStack projects",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",