@cocreate/api 1.31.0 → 1.34.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/src/server.js CHANGED
@@ -15,163 +15,143 @@
15
15
  * along with this program. If not, see <https://www.gnu.org/licenses/>.
16
16
  ********************************************************************************/
17
17
 
18
+ /**
19
+ * @file Core API Handler module for dynamic multi-tenant integrations, webhook routing, and AST macro pipeline execution.
20
+ * @module @cocreate/api-handler
21
+ */
22
+
18
23
  import { URL } from 'node:url';
19
- import { getValueFromObject, objectToSearchParams, astAsync, security } from '@cocreate/utils';
24
+ import {
25
+ objectToSearchParams,
26
+ searchParamsToObject,
27
+ astAsync,
28
+ security
29
+ } from '@cocreate/utils';
20
30
 
21
- // Core module references bound dynamically upon initialization
22
31
  let server = null;
23
- let wsManager = null;
24
32
  let crud = null;
25
33
 
26
34
  const API = {
27
- init,
28
- executeApi,
29
- request,
30
- security,
31
- makeHttpRequest,
32
- getApiConfig,
33
- normalizeConfig
35
+ send,
36
+ webhook,
37
+ getApiConfig
34
38
  };
35
39
 
36
40
  /**
37
- * Binds server core services and registers WebSocket event handlers.
38
- * @param {Object} Server - Server core instance containing wsManager and crud
39
- * @returns {Object} Exported API service interface
41
+ * Binds server core services. Socket access is resolved when a request runs.
42
+ *
43
+ * @async
44
+ * @param {Object} Server - Core server instance containing CRUD and, later, a socket manager on workers.
45
+ * @param {Object} Server.crud - Database CRUD service operator.
46
+ * @returns {Promise<Object>} Exported API interface.
40
47
  */
41
48
  export async function init(Server) {
42
49
  server = Server;
43
- wsManager = server.wsManager;
44
50
  crud = server.crud;
45
51
 
46
- wsManager.on("api", (data) => {
47
- executeApi(data);
48
- });
49
-
50
- wsManager.on("endpoint", (data) => {
51
- executeApi(data);
52
- });
53
-
54
52
  return API;
55
53
  }
56
54
 
57
55
  /**
58
- * Normalizes raw API configuration structures into a uniform key/url/headers shape.
59
- * @param {string|Object} apiConfig - Raw configuration object or API key string
60
- * @param {string} name - Provider identifier
61
- * @returns {Object} Standardized configuration container
62
- */
63
- export function normalizeConfig(apiConfig, name) {
64
- let key = null;
65
- let url = null;
66
- let headers = {};
67
-
68
- if (typeof apiConfig === 'string') {
69
- key = apiConfig;
70
- } else if (apiConfig && typeof apiConfig === 'object') {
71
- url = apiConfig.url;
72
- headers = apiConfig.headers || {};
73
-
74
- if (apiConfig.key) {
75
- if (typeof apiConfig.key === 'string') {
76
- key = apiConfig.key;
77
- } else if (typeof apiConfig.key === 'object') {
78
- key = apiConfig.key.apiKey || apiConfig.key.key || JSON.stringify(apiConfig.key);
79
- }
80
- } else if (apiConfig.apiKey) {
81
- key = apiConfig.apiKey;
82
- }
83
-
84
- if (apiConfig.url) url = apiConfig.url;
85
- if (apiConfig.headers) headers = { ...headers, ...apiConfig.headers };
86
- }
87
-
88
- return { key, url, headers };
89
- }
90
-
91
- /**
92
- * Constructs an isolated execution sandbox enforcing multi-tenant isolation via Proxy firewalls.
93
- * @param {Object} context - Execution context containing organization, host, data, and request info
94
- * @returns {Object} Secure sandbox context for AST execution
56
+ * Creates a Proxy firewall wrapper to automatically inject multi-tenant scope parameters (`organization_id` and `host`) into target method invocations.
57
+ *
58
+ * @param {Object} target - Target service instance to proxy (e.g., crud or wsManager).
59
+ * @param {string} orgId - Multi-tenant organization identifier.
60
+ * @param {string} host - Host domain identifier.
61
+ * @returns {Proxy|undefined} Proxied target service with auto-injected tenant parameters, or undefined if target is falsy.
95
62
  */
96
- function createSandbox(context) {
97
- // SECURITY FIREWALL: Proxy database calls to enforce multi-tenant isolation.
98
- const secureCrud = context.crud ? new Proxy(context.crud, {
99
- get(target, prop) {
100
- const original = target[prop];
101
- if (typeof original === 'function') {
102
- return function(...args) {
103
- if (args.length === 0) {
104
- args.push({
105
- organization_id: context.organization_id,
106
- host: context.host
107
- });
108
- } else if (args.length > 0 && typeof args[0] === 'object' && args[0] !== null) {
109
- args[0].organization_id = context.organization_id;
110
- args[0].host = context.host;
111
- }
112
- return original.apply(target, args);
113
- };
114
- }
115
- return original;
116
- }
117
- }) : undefined;
118
-
119
- // SECURITY FIREWALL: Proxy WebSocket manager calls for multi-tenant isolation.
120
- const secureWsManager = context.wsManager ? new Proxy(context.wsManager, {
121
- get(target, prop) {
122
- const original = target[prop];
63
+ function createTenantProxy(target, orgId, host) {
64
+ if (!target) return undefined;
65
+ return new Proxy(target, {
66
+ get(targetObj, prop) {
67
+ const original = targetObj[prop];
123
68
  if (typeof original === 'function') {
124
- return function(...args) {
69
+ return function (...args) {
125
70
  if (args.length === 0) {
126
- args.push({
127
- organization_id: context.organization_id,
128
- host: context.host
129
- });
130
- } else if (args.length > 0 && typeof args[0] === 'object' && args[0] !== null) {
131
- args[0].organization_id = context.organization_id;
132
- args[0].host = context.host;
71
+ args.push({ organization_id: orgId, host });
72
+ } else if (typeof args[0] === 'object' && args[0] !== null) {
73
+ args[0].organization_id = orgId;
74
+ args[0].host = host;
133
75
  }
134
- return original.apply(target, args);
76
+ return original.apply(targetObj, args);
135
77
  };
136
78
  }
137
79
  return original;
138
80
  }
139
- }) : undefined;
81
+ });
82
+ }
140
83
 
141
- // Only expose full server instance to platform admin organization
142
- const isPlatformOrg = server && server.organization_id && (context.organization_id === server.organization_id);
84
+ /**
85
+ * Constructs an isolated execution sandbox enforcing multi-tenant isolation via proxy firewalls for AST operations.
86
+ *
87
+ * @param {Object} context - Raw execution context.
88
+ * @param {Object} [context.payload] - Execution payload data object.
89
+ * @param {Object} [context.event] - Webhook or inbound trigger event data.
90
+ * @param {Object} [context.request] - Incoming HTTP request object.
91
+ * @param {Object} [context.response] - Outgoing HTTP response object.
92
+ * @param {Object} [context.crud] - Database service interface instance.
93
+ * @param {Object} [context.wsManager] - WebSocket manager instance.
94
+ * @param {string} [context.key] - Provider key/credential string.
95
+ * @param {Object} [context.config] - Full provider API configuration object.
96
+ * @param {string} context.organization_id - Target multi-tenant organization ID.
97
+ * @param {string} context.host - Target host domain.
98
+ * @returns {Object} Secure AST execution sandbox object containing firewalled utilities.
99
+ */
100
+ function createSandbox(context) {
101
+ const isPlatformOrg = server?.organization_id && (context.organization_id === server.organization_id);
102
+ const secureCrud = createTenantProxy(context.crud, context.organization_id, context.host);
103
+ const secureWs = createTenantProxy(context.wsManager, context.organization_id, context.host);
143
104
 
144
105
  const sandbox = {
145
- // Core Isolated Dynamic AST Scopes
146
- data: context.data,
106
+ // Provider fields are configuration variables. For example, a
107
+ // provider record containing `token` exposes `$token` to its AST
108
+ // expressions. Trusted runtime bindings below remain authoritative.
109
+ ...(context.config || {}),
110
+ payload: context.payload,
111
+ data: context.data || context.payload,
147
112
  event: context.event,
148
- actions: context.actions,
149
113
  request: context.request,
150
- rawBody: context.rawBody,
114
+ response: context.response,
115
+ header: context.request?.headers || context.header || {},
116
+ searchParams: context.searchParams,
117
+ objectToSearchParams,
118
+ searchParamsToObject,
119
+ Buffer,
151
120
 
152
- // Safe Proxied System Objects
153
121
  crud: secureCrud,
154
- socket: secureWsManager,
155
- wsManager: secureWsManager,
156
-
157
- // Security Utility Instance
122
+ socket: secureWs,
123
+ wsManager: secureWs,
158
124
  security,
159
125
 
160
- // Host Context Identifiers
161
126
  key: context.key,
162
127
  config: context.config,
163
128
  organization_id: context.organization_id,
164
129
  host: context.host,
165
130
 
166
- // Custom operators rewritten as sandbox properties
167
131
  api: {
168
- send: async (apiData) => {
169
- return await executeApi(apiData, context);
170
- }
132
+ send: (apiData) => send(apiData, context)
171
133
  }
172
134
  };
173
135
 
174
- // Safely inject elevated server core only when authenticated context is root platform
136
+ // Preserve privileged runtime bindings even when an evaluated record has
137
+ // ordinary fields with the same names (for example api: "stripe"). The AST
138
+ // resolver prefers the dollar-prefixed binding for operators such as
139
+ // $api.send, while output fields may continue using their natural names.
140
+ Object.assign(sandbox, {
141
+ $payload: sandbox.payload,
142
+ $data: sandbox.data,
143
+ $event: sandbox.event,
144
+ $request: sandbox.request,
145
+ $response: sandbox.response,
146
+ $header: sandbox.header,
147
+ $crud: sandbox.crud,
148
+ $socket: sandbox.socket,
149
+ $wsManager: sandbox.wsManager,
150
+ $security: sandbox.security,
151
+ $config: sandbox.config,
152
+ $api: sandbox.api
153
+ });
154
+
175
155
  if (isPlatformOrg) {
176
156
  sandbox.server = server;
177
157
  }
@@ -180,145 +160,186 @@ function createSandbox(context) {
180
160
  }
181
161
 
182
162
  /**
183
- * Resolves endpoint definitions, executes AST macros, or dispatches HTTP requests.
184
- * @param {Object} data - API execution payload containing directives
185
- * @param {Object} [parentContext=null] - Optional outer sandbox execution context
186
- * @returns {Promise<any>} Execution response payload
163
+ * Resolves endpoint definitions, executes AST macro actions, or dispatches outgoing HTTP requests.
164
+ *
165
+ * @async
166
+ * @param {Object} data - Execution directive payload.
167
+ * @param {string} [data.url] - Optional pre-formatted target HTTP endpoint URL (Direct Execution Path).
168
+ * @param {string} [data.api] - API configuration name (e.g., 'stripe-test').
169
+ * @param {string} [data.method] - HTTP method (e.g., 'post'). A matched endpoint configuration may override it.
170
+ * @param {string} [data.endpoint] - Stored endpoint name (e.g., 'create-customer') or direct API path (e.g., '/v1/customers').
171
+ * @param {*} [data.data] - Request content passed to the external API.
172
+ * @param {Object} [data.searchParams] - Query parameters appended to the request URL.
173
+ * @param {Object} [data.headers] - Custom HTTP header directives.
174
+ * @param {number} [data.timeout=15000] - Request timeout in milliseconds.
175
+ * @param {Object} [data.socket] - WebSocket client reference for real-time response emit.
176
+ * @param {string} [data.organization_id] - Multi-tenant organization identifier.
177
+ * @param {string} [data.host] - Target host domain.
178
+ * @param {Object|null} [parentContext=null] - Outer sandbox execution context when nested.
179
+ * @returns {Promise<Object>} The mutated execution envelope, with response content assigned to `data.data`.
180
+ * @throws {Error} If required directives are missing, provider configuration is invalid, or HTTP dispatch fails.
187
181
  */
188
- export async function executeApi(data, parentContext = null) {
182
+ export async function send(data, parentContext = null) {
189
183
  try {
184
+ const wsManager = server?.wsManager;
190
185
  const orgId = parentContext?.organization_id || data.socket?.organization_id || data.organization_id;
191
186
  const host = parentContext?.host || data.socket?.host || data.host;
192
187
 
193
- // PATH A: Native Execution of a Pre-prepared HTTP Request Object
194
- if (data.url && data.method) {
195
- const timeout = data.timeout || 15000;
196
- const options = {
197
- method: data.method.toUpperCase(),
198
- headers: data.headers || {},
199
- timeout
200
- };
201
-
202
- if (data.body) {
203
- options.body = typeof data.body === 'object' ? JSON.stringify(data.body) : data.body;
204
- }
205
-
206
- const response = await makeHttpRequest(data.url, options);
207
- const responsePayload = await response.json();
208
-
209
- if (data.socket && wsManager) {
210
- data.response = responsePayload;
211
- wsManager.send(data);
212
- }
213
- return responsePayload;
214
- }
215
-
216
- // PATH B: Standard Dynamic Configuration Resolve & Execute
217
- if (!data.method || !data.endpoint) {
218
- throw new Error("API execution request is missing 'method' or 'endpoint' directives.");
188
+ const apiName = data.api?.trim() || "";
189
+ const endpoint = data.endpoint?.trim() || "";
190
+ const payloadUrl = typeof data.url === "string"
191
+ ? data.url.trim()
192
+ : data.url || "";
193
+ const apiConfig = apiName
194
+ ? await getApiConfig({ organization_id: orgId, host }, apiName)
195
+ : {};
196
+
197
+ if (!endpoint && !payloadUrl && !apiConfig.endpoint && !apiConfig.url) {
198
+ throw new Error("API execution request requires either 'endpoint' or 'url'.");
219
199
  }
220
200
 
221
- const providerName = data.method.split(/[./]/)[0];
222
- const httpMethod = data.endpoint.split(" ")[0].toUpperCase();
201
+ let endpointConfig = {};
223
202
 
224
- const endpointQuery = {
225
- method: 'object.read',
226
- array: 'endpoints',
227
- organization_id: orgId,
228
- host: host,
229
- $filter: {
230
- query: { endpoint: data.endpoint, organization_id: orgId },
231
- limit: 1
203
+ // Every endpoint value can identify a reusable endpoint configuration.
204
+ // When no matching name exists, the value itself becomes the API path.
205
+ if (endpoint && apiConfig._id) {
206
+ const endpointFilter = {
207
+ api_id: apiConfig._id,
208
+ name: endpoint,
209
+ enabled: true
210
+ };
211
+ if (data.method?.trim()) {
212
+ endpointFilter.method = data.method.trim().toLowerCase();
232
213
  }
233
- };
234
- const endpointResponse = await crud.send(endpointQuery);
235
- const endpointConfig = endpointResponse?.object?.[0] || {};
236
214
 
237
- const rawApiConfig = await getApiConfig({ organization_id: orgId, host }, providerName);
238
- if (!rawApiConfig) {
239
- throw new Error(`Connection setting not registered for provider: '${providerName}'.`);
215
+ const endpointQuery = {
216
+ method: 'object.read',
217
+ array: 'endpoints',
218
+ organization_id: orgId,
219
+ host,
220
+ filter: {
221
+ query: endpointFilter,
222
+ limit: 1
223
+ }
224
+ };
225
+
226
+ const endpointResponse = await crud.send(endpointQuery);
227
+ endpointConfig = endpointResponse?.object?.[0] || {};
240
228
  }
241
- const apiConfig = normalizeConfig(rawApiConfig, providerName);
242
229
 
243
- const apiContext = parentContext || {
244
- data: data,
245
- event: {},
246
- actions: [],
230
+ const apiContext = {
231
+ ...(parentContext || {}),
232
+ payload: data,
233
+ event: parentContext?.event || {},
247
234
  organization_id: orgId,
248
- host: host,
249
- crud,
250
- wsManager,
235
+ host,
236
+ crud: parentContext?.crud || crud,
237
+ wsManager: parentContext?.wsManager || wsManager,
251
238
  key: apiConfig.key,
252
239
  config: apiConfig
253
240
  };
254
241
 
255
- const actions = endpointConfig.actions || endpointConfig.action;
256
- if (actions) {
257
- if (Array.isArray(actions)) {
258
- for (let i = 0; i < actions.length; i++) {
259
- const result = await astAsync({ executeAST: actions[i], sandbox: createSandbox(apiContext) });
260
- apiContext.actions.push(result);
261
- }
262
- const finalMacroResult = apiContext.actions[apiContext.actions.length - 1];
263
- if (finalMacroResult !== undefined) {
264
- data[providerName] = finalMacroResult;
265
- }
266
- } else {
267
- const result = await astAsync({ executeAST: actions, sandbox: createSandbox(apiContext) });
268
- apiContext.actions.push(result);
269
- if (result !== undefined) {
270
- data[providerName] = result;
271
- }
272
- }
242
+ // Requests layer from general to specific: payload, API configuration,
243
+ // then the matched endpoint as the authoritative macro override.
244
+ const requestData = endpointConfig.data ?? apiConfig.data ?? data.data;
245
+ const {
246
+ headers: providerHeaders,
247
+ data: providerData,
248
+ endpoint: providerEndpoint,
249
+ url: providerUrl,
250
+ method: providerMethod,
251
+ ...providerVariables
252
+ } = apiConfig;
253
+ let request = {
254
+ // Provider-native variables must exist in the execution envelope
255
+ // before endpoint and header AST expressions are evaluated.
256
+ ...providerVariables,
257
+ ...data,
258
+ ...endpointConfig,
259
+ method: endpointConfig.method?.trim() || apiConfig.method?.trim() || data.method?.trim(),
260
+ endpoint: endpointConfig.endpoint || apiConfig.endpoint || endpoint,
261
+ // URL configurations may be AST objects. Preserve them until the
262
+ // request is evaluated, then normalize the resolved string below.
263
+ url: endpointConfig.url || apiConfig.url || payloadUrl,
264
+ headers: {
265
+ ...(data.headers || {}),
266
+ ...(providerHeaders || {}),
267
+ ...(endpointConfig.headers || {})
268
+ },
269
+ timeout: endpointConfig.timeout ?? apiConfig.timeout ?? data.timeout ?? 15000,
270
+ searchParams: endpointConfig.searchParams ?? apiConfig.searchParams ?? data.searchParams
271
+ };
272
+
273
+ // Request data may depend on named actions, so append it after the
274
+ // endpoint definition to preserve sequential AST evaluation.
275
+ delete request.data;
276
+ request.data = requestData;
277
+
278
+ request = await astAsync({
279
+ executeAST: request,
280
+ // `$data` is the caller's original API envelope. Endpoint records
281
+ // map from it while provider fields remain top-level variables.
282
+ sandbox: createSandbox(apiContext)
283
+ });
273
284
 
285
+ if (request.resolved) {
286
+ data.data = request.data
274
287
  if (data.socket && wsManager) {
275
288
  wsManager.send(data);
276
289
  }
277
- return;
290
+ return data;
278
291
  }
279
292
 
280
- if (!apiConfig.url) {
281
- throw new Error(`Configuration missing 'url' base for provider '${providerName}'.`);
293
+ let url = request.url?.trim();
294
+ if (!url) {
295
+ throw new Error(`Configuration missing 'url' for API '${apiName || "direct"}'.`);
282
296
  }
283
297
 
284
- let url = apiConfig.url.endsWith("/") ? apiConfig.url.slice(0, -1) : apiConfig.url;
285
- let endpointPath = endpointConfig.path || data.endpoint.split(" ")[1];
286
- url += endpointPath.startsWith("/") ? endpointPath : `/${endpointPath}`;
287
-
288
- if (data[providerName] && data[providerName].$searchParams) {
289
- url += objectToSearchParams(data[providerName].$searchParams);
290
- delete data[providerName].$searchParams;
298
+ if (request.endpoint) {
299
+ url = url.endsWith("/") ? url.slice(0, -1) : url;
300
+ url += request.endpoint.startsWith("/") ? request.endpoint : `/${request.endpoint}`;
291
301
  }
292
302
 
293
- let headers = { ...(apiConfig.headers || {}), ...(endpointConfig.headers || {}) };
294
- headers = await astAsync({ executeAST: headers, sandbox: createSandbox(apiContext) });
303
+ const searchParams = objectToSearchParams(request.searchParams);
304
+ url += searchParams && url.includes("?") ? `&${searchParams.slice(1)}` : searchParams;
295
305
 
296
- const timeout = 15000;
297
- let options = { method: httpMethod, headers, timeout };
306
+ const resolvedHttpMethod = request.method?.trim().toUpperCase();
307
+ if (!resolvedHttpMethod) {
308
+ throw new Error(`Configuration missing 'method' for API '${apiName || "direct"}'.`);
309
+ }
310
+ const options = {
311
+ method: resolvedHttpMethod,
312
+ headers: request.headers,
313
+ timeout: request.timeout
314
+ };
298
315
 
299
- if (!["GET", "HEAD"].includes(httpMethod) && data[providerName]) {
300
- const bodyPayload = await astAsync({ executeAST: data[providerName], sandbox: createSandbox(apiContext) });
301
-
302
- if (typeof bodyPayload === 'object' && bodyPayload !== null) {
303
- options.body = JSON.stringify(bodyPayload);
304
- if (!headers["Content-Type"]) {
316
+ if (!["GET", "HEAD"].includes(resolvedHttpMethod) && request.data !== undefined) {
317
+ if (typeof request.data === 'object' && request.data !== null) {
318
+ options.body = request.encoding === "form"
319
+ ? objectToSearchParams(request.data, false)
320
+ : JSON.stringify(request.data);
321
+ if (!options.headers["Content-Type"] && !options.headers["content-type"]) {
305
322
  options.headers["Content-Type"] = "application/json; charset=utf-8";
306
323
  }
307
324
  } else {
308
- options.body = String(bodyPayload);
325
+ options.body = String(request.data);
309
326
  }
310
327
  }
311
328
 
312
329
  const response = await makeHttpRequest(url, options);
313
- const responsePayload = await response.json();
314
- data[providerName] = responsePayload;
330
+ data.data = await parseResponseData(response);
315
331
 
316
332
  if (data.socket && wsManager) {
317
333
  wsManager.send(data);
318
334
  }
319
- return responsePayload;
335
+ return data;
320
336
  } catch (error) {
321
- data.error = error.message;
337
+ const wsManager = server?.wsManager;
338
+ if (!Array.isArray(data.error)) {
339
+ data.error = data.error ? [data.error] : [];
340
+ }
341
+ data.error.push(error.message);
342
+
322
343
  if (error.response) {
323
344
  data.status = error.response.status;
324
345
  data.statusText = error.response.statusText;
@@ -326,25 +347,30 @@ export async function executeApi(data, parentContext = null) {
326
347
  }
327
348
  if (data.socket && wsManager) {
328
349
  wsManager.send(data);
350
+ return data;
329
351
  }
330
352
  throw error;
331
353
  }
332
354
  }
333
355
 
334
356
  /**
335
- * Processes incoming HTTP webhook requests, validates security signatures, and executes pipelines.
336
- * @param {Object} req - HTTP request object
337
- * @param {Object} res - HTTP response object
357
+ * Processes incoming HTTP webhook requests, verifies HMAC/JWT security signatures, and executes pipelines.
358
+ *
359
+ * @async
360
+ * @param {Object} req - Native Node.js HTTP incoming request stream.
361
+ * @param {Object} res - Native Node.js HTTP server response stream.
362
+ * @returns {Promise<void>}
338
363
  */
339
- export async function request(req, res) {
364
+ export async function webhook(req, res) {
340
365
  try {
366
+ const wsManager = server?.wsManager;
341
367
  const urlObject = new URL(`http://${req.headers.host}${req.url}`);
342
368
  const hostname = urlObject.hostname;
343
369
 
344
370
  let organization;
345
371
  try {
346
372
  organization = await crud.getOrganization({ host: hostname });
347
- } catch (error) {
373
+ } catch {
348
374
  res.writeHead(404, { "Content-Type": "application/json" });
349
375
  return res.end(JSON.stringify({ error: "Organization mapping not found" }));
350
376
  }
@@ -356,14 +382,12 @@ export async function request(req, res) {
356
382
  array: 'webhooks',
357
383
  organization_id: organization._id,
358
384
  host: hostname,
359
- $filter: {
360
- query: {
361
- path: webhookPath,
362
- organization_id: organization._id
363
- },
385
+ filter: {
386
+ query: { path: webhookPath, enabled: true },
364
387
  limit: 1
365
388
  }
366
389
  };
390
+
367
391
  const webhookResponse = await crud.send(webhookQuery);
368
392
  const webhookConfig = webhookResponse?.object?.[0];
369
393
 
@@ -372,89 +396,44 @@ export async function request(req, res) {
372
396
  return res.end(JSON.stringify({ error: `Webhook configuration for path '${webhookPath}' not found.` }));
373
397
  }
374
398
 
375
- let rawBody = "";
376
- await new Promise((resolve, reject) => {
377
- req.on("data", (chunk) => { rawBody += chunk.toString(); });
378
- req.on("end", resolve);
379
- req.on("error", reject);
380
- });
381
-
382
- const queryParams = Object.fromEntries(urlObject.searchParams.entries());
383
-
384
- let parsedEvent = {};
385
- if (rawBody) {
386
- try {
387
- parsedEvent = JSON.parse(rawBody);
388
- } catch {
389
- parsedEvent = { rawBody };
390
- }
399
+ let apiConfig = {};
400
+ if (webhookConfig.api_id) {
401
+ const apiResponse = await crud.send({
402
+ method: 'object.read',
403
+ array: 'apis',
404
+ organization_id: organization._id,
405
+ host: hostname,
406
+ filter: {
407
+ query: { _id: webhookConfig.api_id, enabled: true },
408
+ limit: 1
409
+ }
410
+ });
411
+ apiConfig = apiResponse?.object?.[0] || {};
391
412
  }
392
413
 
393
414
  const context = {
394
- data: {},
395
- event: parsedEvent,
396
- actions: [],
397
415
  request: req,
398
416
  response: res,
399
- rawBody: rawBody,
417
+ searchParams: searchParamsToObject(urlObject.searchParams),
400
418
  host: hostname,
401
- organization: organization,
419
+ organization,
402
420
  organization_id: organization._id,
403
- crud: crud,
404
- wsManager: wsManager
421
+ crud,
422
+ wsManager,
423
+ key: apiConfig.key,
424
+ config: apiConfig
405
425
  };
406
426
 
407
- context.request.query = queryParams;
408
-
409
- if (webhookConfig.authenticate) {
410
- const auth = webhookConfig.authenticate;
411
- if (auth.method === "webhooks.constructEvent" && Array.isArray(auth.parameters)) {
412
- const resolvedParams = await astAsync({ executeAST: [...auth.parameters], sandbox: createSandbox(context) });
413
- const payload = resolvedParams[0];
414
- const signature = resolvedParams[1];
415
- const secret = resolvedParams[2];
416
-
417
- const computedSignature = security.createWebhookSignature(payload, secret, { format: 'github' });
418
- const expectedSignature = String(signature);
419
-
420
- if (!security.timingSafeEqual(computedSignature, expectedSignature)) {
421
- res.writeHead(401, { "Content-Type": "application/json" });
422
- return res.end(JSON.stringify({ error: "Unauthorized access: signature verification failed." }));
423
- }
424
- }
425
- }
426
-
427
- let executeOperations = null;
428
- const actionConfig = webhookConfig.action;
429
-
430
- if (actionConfig) {
431
- if (typeof actionConfig === 'string') {
432
- const actionName = await astAsync({ executeAST: actionConfig, sandbox: createSandbox(context) });
433
- if (actionName && webhookConfig.actions?.[actionName]) {
434
- executeOperations = webhookConfig.actions[actionName];
435
- }
436
- } else {
437
- executeOperations = actionConfig;
438
- }
439
- }
440
-
441
- if (executeOperations) {
442
- if (Array.isArray(executeOperations)) {
443
- for (let i = 0; i < executeOperations.length; i++) {
444
- const stepResult = await astAsync({ executeAST: executeOperations[i], sandbox: createSandbox(context) });
445
- context.actions.push(stepResult);
446
- }
447
- } else if (typeof executeOperations === 'object' && executeOperations !== null) {
448
- const stepResult = await astAsync({ executeAST: [executeOperations], sandbox: createSandbox(context) });
449
- context.actions.push(stepResult);
450
- }
451
- }
427
+ const result = await astAsync({
428
+ executeAST: webhookConfig,
429
+ sandbox: createSandbox(context)
430
+ });
452
431
 
453
432
  if (res && !res.writableEnded) {
454
433
  res.writeHead(200, { "Content-Type": "application/json" });
455
- res.end(JSON.stringify({
456
- message: "Webhook processed successfully",
457
- payload: context.actions[context.actions.length - 1] || {}
434
+ res.end(JSON.stringify({
435
+ message: "Webhook processed successfully",
436
+ payload: result
458
437
  }));
459
438
  }
460
439
  } catch (error) {
@@ -465,62 +444,112 @@ export async function request(req, res) {
465
444
  }
466
445
  }
467
446
 
468
- // Re-export security library instance
469
- export { security };
470
-
471
447
  /**
472
- * Low-level HTTP dispatch utility wrapping global fetch with AbortController timeout handling.
473
- * @param {string} url - Target URL endpoint
474
- * @param {Object} options - Fetch options (method, headers, body, signal, timeout)
475
- * @returns {Promise<Response>} Native fetch response instance
448
+ * Low-level HTTP dispatch utility wrapping native `fetch` with AbortController timeout handling.
449
+ *
450
+ * @async
451
+ * @param {string} url - Absolute target URL endpoint.
452
+ * @param {Object} [options={}] - Fetch request options (method, headers, body, signal).
453
+ * @param {number} [options.timeout=15000] - Timeout threshold in milliseconds before aborting.
454
+ * @returns {Promise<Response>} Native fetch Response instance.
455
+ * @throws {Error} Throws HTTP error status or network timeout error.
476
456
  */
477
457
  export async function makeHttpRequest(url, options) {
478
- let controller, timeoutId;
479
- if (global.AbortController) {
480
- controller = new global.AbortController();
481
- timeoutId = setTimeout(() => controller.abort(), options.timeout || 15000);
482
- options.signal = controller.signal;
483
- }
484
-
485
- const fetchFn = global.fetch;
486
- if (typeof fetchFn !== "function") {
487
- throw new Error("System runtime environment is missing a valid HTTP fetch implementation.");
488
- }
458
+ const controller = new AbortController();
459
+ const timeoutId = setTimeout(() => controller.abort(), options.timeout || 15000);
460
+ options.signal = controller.signal;
489
461
 
490
462
  try {
491
- const response = await fetchFn(url, options);
492
- if (timeoutId) clearTimeout(timeoutId);
463
+ const response = await fetch(url, options);
464
+ clearTimeout(timeoutId);
493
465
 
494
466
  if (!response.ok) {
495
467
  const text = await response.text();
496
- const error = new Error(`HTTP error! Status: ${response.status} ${response.statusText}`);
468
+ const providerError = parseXmlError(text);
469
+ const detail = providerError?.message;
470
+ const code = providerError?.code;
471
+ const error = new Error(
472
+ detail
473
+ ? `${code ? `${code}: ` : ""}${detail}`
474
+ : `HTTP error! Status: ${response.status} ${response.statusText}`
475
+ );
497
476
  error.response = {
498
477
  status: response.status,
499
478
  statusText: response.statusText,
500
- data: text
479
+ data: text,
480
+ error: providerError
501
481
  };
502
482
  throw error;
503
483
  }
504
484
  return response;
505
485
  } catch (error) {
506
- if (timeoutId) clearTimeout(timeoutId);
486
+ clearTimeout(timeoutId);
507
487
  throw error;
508
488
  }
509
489
  }
510
490
 
491
+ function decodeXmlText(value) {
492
+ if (value === undefined) return undefined;
493
+ return value
494
+ .replace(/&#x([\da-f]+);/gi, (_, hex) => String.fromCodePoint(parseInt(hex, 16)))
495
+ .replace(/&#(\d+);/g, (_, decimal) => String.fromCodePoint(parseInt(decimal, 10)))
496
+ .replace(/&lt;/g, "<")
497
+ .replace(/&gt;/g, ">")
498
+ .replace(/&quot;/g, '"')
499
+ .replace(/&apos;/g, "'")
500
+ .replace(/&amp;/g, "&");
501
+ }
502
+
503
+ function getXmlTagValue(xml, tagName) {
504
+ if (!xml || typeof xml !== "string") return undefined;
505
+ const match = xml.match(
506
+ new RegExp(`<${tagName}(?:\\s[^>]*)?>([\\s\\S]*?)<\\/${tagName}>`, "i")
507
+ );
508
+ return match ? decodeXmlText(match[1].trim()) : undefined;
509
+ }
510
+
511
+ function parseXmlError(text) {
512
+ if (!text || typeof text !== "string" || !text.trimStart().startsWith("<")) {
513
+ return undefined;
514
+ }
515
+
516
+ const code = getXmlTagValue(text, "Code");
517
+ const message = getXmlTagValue(text, "Message");
518
+ const requestId = getXmlTagValue(text, "RequestId") ||
519
+ getXmlTagValue(text, "RequestID");
520
+
521
+ if (!code && !message && !requestId) return undefined;
522
+ return { code, message, requestId };
523
+ }
524
+
525
+ async function parseResponseData(response) {
526
+ const text = await response.text();
527
+ if (!text) return null;
528
+
529
+ try {
530
+ return JSON.parse(text);
531
+ } catch {
532
+ return text;
533
+ }
534
+ }
535
+
511
536
  /**
512
- * Queries database storage for provider connection credentials and API configuration.
513
- * @param {Object} data - Context query parameters (organization_id, host)
514
- * @param {string} name - API provider name
515
- * @returns {Promise<Object>} API provider configuration object
537
+ * Queries database storage for provider connection credentials and API configuration settings.
538
+ *
539
+ * @async
540
+ * @param {Object} data - Context object containing search criteria.
541
+ * @param {string} [data.organization_id] - Target organization ID (resolves via hostname if absent).
542
+ * @param {string} [data.host] - Target host domain.
543
+ * @param {string} name - API provider identifier (e.g., 'stripe', 'openai', 'twilio').
544
+ * @returns {Promise<Object>} Standardized API configuration object containing key, url, and headers.
545
+ * @throws {Error} If organization mapping fails or integration settings do not exist.
516
546
  */
517
547
  export async function getApiConfig(data, name) {
518
- let orgId = data.organization_id;
519
- let host = data.host;
520
-
548
+ let { organization_id: orgId, host } = data || {};
549
+
521
550
  if (!orgId) {
522
551
  const organization = await crud.getOrganization(data);
523
- if (organization.error) throw new Error(organization.error);
552
+ if (organization?.error) throw new Error(organization.error);
524
553
  orgId = organization._id;
525
554
  }
526
555
 
@@ -528,18 +557,23 @@ export async function getApiConfig(data, name) {
528
557
  method: 'object.read',
529
558
  array: 'apis',
530
559
  organization_id: orgId,
531
- host: host,
532
- $filter: {
533
- query: { name: name, organization_id: orgId },
560
+ host,
561
+ filter: {
562
+ query: { name, enabled: true },
534
563
  limit: 1
535
564
  }
536
565
  };
537
-
566
+
538
567
  const response = await crud.send(query);
539
568
  const apiConfig = response?.object?.[0];
540
569
 
541
- if (!apiConfig) throw new Error(`Integration settings are missing for provider: '${name}'`);
570
+ if (!apiConfig) {
571
+ throw new Error(`Integration settings are missing for provider: '${name}'`);
572
+ }
573
+
542
574
  return apiConfig;
543
575
  }
544
576
 
545
- export default API;
577
+ export default {
578
+ init
579
+ };