@cocreate/api 1.29.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
@@ -1,5 +1,5 @@
1
1
  /********************************************************************************
2
- * Copyright (C) 2023 CoCreate and Contributors.
2
+ * Copyright (C) 2023-2026 CoCreate and Contributors.
3
3
  *
4
4
  * This program is free software: you can redistribute it and/or modify
5
5
  * it under the terms of the GNU Affero General Public License as published
@@ -13,161 +13,145 @@
13
13
  *
14
14
  * You should have received a copy of the GNU Affero General Public License
15
15
  * along with this program. If not, see <https://www.gnu.org/licenses/>.
16
- *
17
16
  ********************************************************************************/
18
17
 
19
- // Commercial Licensing Information:
20
- // For commercial use of this software without the copyleft provisions of the AGPLv3,
21
- // you must obtain a commercial license from CoCreate LLC.
22
- // For details, visit <https://cocreate.app/licenses/> or contact us at sales@cocreate.app.
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
+ */
23
22
 
24
23
  import { URL } from 'node:url';
25
- import { getValueFromObject, objectToSearchParams, astAsync, generateJWT, verifySignature, buildAuthHeaders } from '@cocreate/utils';
24
+ import {
25
+ objectToSearchParams,
26
+ searchParamsToObject,
27
+ astAsync,
28
+ security
29
+ } from '@cocreate/utils';
26
30
 
27
- // Core module references bound dynamically upon initialization
28
31
  let server = null;
29
- let wsManager = null;
30
32
  let crud = null;
31
33
 
32
34
  const API = {
33
- init,
34
- executeApi,
35
- request,
36
- generateJWT,
37
- verifySignature,
38
- makeHttpRequest,
39
- getApiConfig,
40
- normalizeConfig
35
+ send,
36
+ webhook,
37
+ getApiConfig
41
38
  };
42
39
 
40
+ /**
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.
47
+ */
43
48
  export async function init(Server) {
44
49
  server = Server;
45
- wsManager = server.wsManager;
46
50
  crud = server.crud;
47
51
 
48
- wsManager.on("api", (data) => {
49
- executeApi(data);
50
- });
51
-
52
- wsManager.on("endpoint", (data) => {
53
- executeApi(data);
54
- });
55
-
56
52
  return API;
57
53
  }
58
54
 
59
- export function normalizeConfig(apiConfig, name) {
60
- let key = null;
61
- let url = null;
62
- let headers = {};
63
-
64
- if (typeof apiConfig === 'string') {
65
- key = apiConfig;
66
- } else if (apiConfig && typeof apiConfig === 'object') {
67
- url = apiConfig.url;
68
- headers = apiConfig.headers || {};
69
-
70
- if (apiConfig.key) {
71
- if (typeof apiConfig.key === 'string') {
72
- key = apiConfig.key;
73
- } else if (typeof apiConfig.key === 'object') {
74
- key = apiConfig.key.apiKey || apiConfig.key.key || JSON.stringify(apiConfig.key);
75
- }
76
- } else if (apiConfig.apiKey) {
77
- key = apiConfig.apiKey;
78
- }
79
-
80
- if (apiConfig.url) url = apiConfig.url;
81
- if (apiConfig.headers) headers = { ...headers, ...apiConfig.headers };
82
- }
83
-
84
- return { key, url, headers };
85
- }
86
-
87
55
  /**
88
- * Builds a secure sandboxed execution context for evaluateAST.
89
- * Inherits native globals from AST parser automatically, only injecting runtime-specific scopes.
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.
90
62
  */
91
- function createSandbox(context) {
92
- // SECURITY FIREWALL: Automatically proxy local system database calls to seal tenant-isolated metadata.
93
- const secureCrud = context.crud ? new Proxy(context.crud, {
94
- get(target, prop) {
95
- const original = target[prop];
96
- if (typeof original === 'function') {
97
- return function(...args) {
98
- if (args.length === 0) {
99
- args.push({
100
- organization_id: context.organization_id,
101
- host: context.host
102
- });
103
- } else if (args.length > 0 && typeof args[0] === 'object' && args[0] !== null) {
104
- args[0].organization_id = context.organization_id;
105
- args[0].host = context.host;
106
- }
107
- return original.apply(target, args);
108
- };
109
- }
110
- return original;
111
- }
112
- }) : undefined;
113
-
114
- const secureWsManager = context.wsManager ? new Proxy(context.wsManager, {
115
- get(target, prop) {
116
- 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];
117
68
  if (typeof original === 'function') {
118
- return function(...args) {
69
+ return function (...args) {
119
70
  if (args.length === 0) {
120
- args.push({
121
- organization_id: context.organization_id,
122
- host: context.host
123
- });
124
- } else if (args.length > 0 && typeof args[0] === 'object' && args[0] !== null) {
125
- args[0].organization_id = context.organization_id;
126
- 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;
127
75
  }
128
- return original.apply(target, args);
76
+ return original.apply(targetObj, args);
129
77
  };
130
78
  }
131
79
  return original;
132
80
  }
133
- }) : undefined;
81
+ });
82
+ }
134
83
 
135
- // Only expose full server instance to platform admin organization
136
- 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);
137
104
 
138
105
  const sandbox = {
139
- // Core Isolated Dynamic AST Scopes
140
- 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,
141
112
  event: context.event,
142
- actions: context.actions,
143
113
  request: context.request,
144
- rawBody: context.rawBody,
114
+ response: context.response,
115
+ header: context.request?.headers || context.header || {},
116
+ searchParams: context.searchParams,
117
+ objectToSearchParams,
118
+ searchParamsToObject,
119
+ Buffer,
145
120
 
146
- // Safe Proxied System Objects
147
121
  crud: secureCrud,
148
- socket: secureWsManager,
149
- wsManager: secureWsManager,
122
+ socket: secureWs,
123
+ wsManager: secureWs,
124
+ security,
150
125
 
151
- // Host Context Identifiers
152
126
  key: context.key,
153
127
  config: context.config,
154
128
  organization_id: context.organization_id,
155
129
  host: context.host,
156
130
 
157
- // Custom operators rewritten as sandbox properties
158
131
  api: {
159
- send: async (apiData) => {
160
- return await executeApi(apiData, context);
161
- }
162
- },
163
- jwt: (args) => {
164
- const privateKey = args?.privateKey || args?.secret;
165
- if (!privateKey) throw new Error("Missing cryptographic signing key inside $jwt context parameters");
166
- return generateJWT(args, privateKey, args.alg || 'HS256');
132
+ send: (apiData) => send(apiData, context)
167
133
  }
168
134
  };
169
135
 
170
- // 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
+
171
155
  if (isPlatformOrg) {
172
156
  sandbox.server = server;
173
157
  }
@@ -175,141 +159,187 @@ function createSandbox(context) {
175
159
  return sandbox;
176
160
  }
177
161
 
178
- export async function executeApi(data, parentContext = null) {
162
+ /**
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.
181
+ */
182
+ export async function send(data, parentContext = null) {
179
183
  try {
184
+ const wsManager = server?.wsManager;
180
185
  const orgId = parentContext?.organization_id || data.socket?.organization_id || data.organization_id;
181
186
  const host = parentContext?.host || data.socket?.host || data.host;
182
187
 
183
- // PATH A: Native Execution of a Pre-prepared HTTP Request Object
184
- if (data.url && data.method) {
185
- const timeout = data.timeout || 15000;
186
- const options = {
187
- method: data.method.toUpperCase(),
188
- headers: data.headers || {},
189
- timeout
190
- };
191
-
192
- if (data.body) {
193
- options.body = typeof data.body === 'object' ? JSON.stringify(data.body) : data.body;
194
- }
195
-
196
- const response = await makeHttpRequest(data.url, options);
197
- const responsePayload = await response.json();
198
-
199
- if (data.socket && wsManager) {
200
- data.response = responsePayload;
201
- wsManager.send(data);
202
- }
203
- return responsePayload;
204
- }
205
-
206
- // PATH B: Standard Dynamic Configuration Resolve & Execute
207
- if (!data.method || !data.endpoint) {
208
- 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'.");
209
199
  }
210
200
 
211
- const providerName = data.method.split(/[./]/)[0];
212
- const httpMethod = data.endpoint.split(" ")[0].toUpperCase();
201
+ let endpointConfig = {};
213
202
 
214
- const endpointQuery = {
215
- method: 'object.read',
216
- array: 'endpoints',
217
- organization_id: orgId,
218
- host: host,
219
- $filter: {
220
- query: { endpoint: data.endpoint, organization_id: orgId },
221
- 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();
222
213
  }
223
- };
224
- const endpointResponse = await crud.send(endpointQuery);
225
- const endpointConfig = endpointResponse?.object?.[0] || {};
226
214
 
227
- const rawApiConfig = await getApiConfig({ organization_id: orgId, host }, providerName);
228
- if (!rawApiConfig) {
229
- 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] || {};
230
228
  }
231
- const apiConfig = normalizeConfig(rawApiConfig, providerName);
232
229
 
233
- const apiContext = parentContext || {
234
- data: data,
235
- event: {},
236
- actions: [],
230
+ const apiContext = {
231
+ ...(parentContext || {}),
232
+ payload: data,
233
+ event: parentContext?.event || {},
237
234
  organization_id: orgId,
238
- host: host,
239
- crud,
240
- wsManager,
235
+ host,
236
+ crud: parentContext?.crud || crud,
237
+ wsManager: parentContext?.wsManager || wsManager,
241
238
  key: apiConfig.key,
242
239
  config: apiConfig
243
240
  };
244
241
 
245
- const actions = endpointConfig.actions || endpointConfig.action;
246
- if (actions) {
247
- if (Array.isArray(actions)) {
248
- for (let i = 0; i < actions.length; i++) {
249
- const result = await astAsync({ executeAST: actions[i], sandbox: createSandbox(apiContext) });
250
- apiContext.actions.push(result);
251
- }
252
- const finalMacroResult = apiContext.actions[apiContext.actions.length - 1];
253
- if (finalMacroResult !== undefined) {
254
- data[providerName] = finalMacroResult;
255
- }
256
- } else {
257
- const result = await astAsync({ executeAST: actions, sandbox: createSandbox(apiContext) });
258
- apiContext.actions.push(result);
259
- if (result !== undefined) {
260
- data[providerName] = result;
261
- }
262
- }
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;
263
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
+ });
284
+
285
+ if (request.resolved) {
286
+ data.data = request.data
264
287
  if (data.socket && wsManager) {
265
288
  wsManager.send(data);
266
289
  }
267
- return;
290
+ return data;
268
291
  }
269
292
 
270
- // --- Standard Outbound Construction Path ---
271
- if (!apiConfig.url) {
272
- 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"}'.`);
273
296
  }
274
297
 
275
- let url = apiConfig.url.endsWith("/") ? apiConfig.url.slice(0, -1) : apiConfig.url;
276
- let endpointPath = endpointConfig.path || data.endpoint.split(" ")[1];
277
- url += endpointPath.startsWith("/") ? endpointPath : `/${endpointPath}`;
278
-
279
- if (data[providerName] && data[providerName].$searchParams) {
280
- url += objectToSearchParams(data[providerName].$searchParams);
281
- 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}`;
282
301
  }
283
302
 
284
- let headers = { ...(apiConfig.headers || {}), ...(endpointConfig.headers || {}) };
285
- headers = await astAsync({ executeAST: headers, sandbox: createSandbox(apiContext) });
303
+ const searchParams = objectToSearchParams(request.searchParams);
304
+ url += searchParams && url.includes("?") ? `&${searchParams.slice(1)}` : searchParams;
286
305
 
287
- const timeout = 15000;
288
- 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
+ };
289
315
 
290
- if (!["GET", "HEAD"].includes(httpMethod) && data[providerName]) {
291
- const bodyPayload = await astAsync({ executeAST: data[providerName], sandbox: createSandbox(apiContext) });
292
-
293
- if (typeof bodyPayload === 'object' && bodyPayload !== null) {
294
- options.body = JSON.stringify(bodyPayload);
295
- 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"]) {
296
322
  options.headers["Content-Type"] = "application/json; charset=utf-8";
297
323
  }
298
324
  } else {
299
- options.body = String(bodyPayload);
325
+ options.body = String(request.data);
300
326
  }
301
327
  }
302
328
 
303
329
  const response = await makeHttpRequest(url, options);
304
- const responsePayload = await response.json();
305
- data[providerName] = responsePayload;
330
+ data.data = await parseResponseData(response);
306
331
 
307
332
  if (data.socket && wsManager) {
308
333
  wsManager.send(data);
309
334
  }
310
- return responsePayload;
335
+ return data;
311
336
  } catch (error) {
312
- 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
+
313
343
  if (error.response) {
314
344
  data.status = error.response.status;
315
345
  data.statusText = error.response.statusText;
@@ -317,20 +347,30 @@ export async function executeApi(data, parentContext = null) {
317
347
  }
318
348
  if (data.socket && wsManager) {
319
349
  wsManager.send(data);
350
+ return data;
320
351
  }
321
352
  throw error;
322
353
  }
323
354
  }
324
355
 
325
- export async function request(req, res) {
356
+ /**
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>}
363
+ */
364
+ export async function webhook(req, res) {
326
365
  try {
366
+ const wsManager = server?.wsManager;
327
367
  const urlObject = new URL(`http://${req.headers.host}${req.url}`);
328
368
  const hostname = urlObject.hostname;
329
369
 
330
370
  let organization;
331
371
  try {
332
372
  organization = await crud.getOrganization({ host: hostname });
333
- } catch (error) {
373
+ } catch {
334
374
  res.writeHead(404, { "Content-Type": "application/json" });
335
375
  return res.end(JSON.stringify({ error: "Organization mapping not found" }));
336
376
  }
@@ -342,14 +382,12 @@ export async function request(req, res) {
342
382
  array: 'webhooks',
343
383
  organization_id: organization._id,
344
384
  host: hostname,
345
- $filter: {
346
- query: {
347
- path: webhookPath,
348
- organization_id: organization._id
349
- },
385
+ filter: {
386
+ query: { path: webhookPath, enabled: true },
350
387
  limit: 1
351
388
  }
352
389
  };
390
+
353
391
  const webhookResponse = await crud.send(webhookQuery);
354
392
  const webhookConfig = webhookResponse?.object?.[0];
355
393
 
@@ -358,86 +396,44 @@ export async function request(req, res) {
358
396
  return res.end(JSON.stringify({ error: `Webhook configuration for path '${webhookPath}' not found.` }));
359
397
  }
360
398
 
361
- let rawBody = "";
362
- await new Promise((resolve, reject) => {
363
- req.on("data", (chunk) => { rawBody += chunk.toString(); });
364
- req.on("end", resolve);
365
- req.on("error", reject);
366
- });
367
-
368
- const queryParams = Object.fromEntries(urlObject.searchParams.entries());
369
-
370
- let parsedEvent = {};
371
- if (rawBody) {
372
- try {
373
- parsedEvent = JSON.parse(rawBody);
374
- } catch {
375
- parsedEvent = { rawBody };
376
- }
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] || {};
377
412
  }
378
413
 
379
414
  const context = {
380
- data: {},
381
- event: parsedEvent,
382
- actions: [],
383
415
  request: req,
384
416
  response: res,
385
- rawBody: rawBody,
417
+ searchParams: searchParamsToObject(urlObject.searchParams),
386
418
  host: hostname,
387
- organization: organization,
419
+ organization,
388
420
  organization_id: organization._id,
389
- crud: crud,
390
- wsManager: wsManager
421
+ crud,
422
+ wsManager,
423
+ key: apiConfig.key,
424
+ config: apiConfig
391
425
  };
392
426
 
393
- context.request.query = queryParams;
394
-
395
- if (webhookConfig.authenticate) {
396
- const auth = webhookConfig.authenticate;
397
- if (auth.method === "webhooks.constructEvent" && Array.isArray(auth.parameters)) {
398
- const resolvedParams = await astAsync({ executeAST: [...auth.parameters], sandbox: createSandbox(context) });
399
- const payload = resolvedParams[0];
400
- const signature = resolvedParams[1];
401
- const secret = resolvedParams[2];
402
-
403
- if (!verifySignature(payload, signature, secret, "sha256")) {
404
- res.writeHead(401, { "Content-Type": "application/json" });
405
- return res.end(JSON.stringify({ error: "Unauthorized access: signature verification failed." }));
406
- }
407
- }
408
- }
409
-
410
- let executeOperations = null;
411
- const actionConfig = webhookConfig.action;
412
-
413
- if (actionConfig) {
414
- if (typeof actionConfig === 'string') {
415
- const actionName = await astAsync({ executeAST: actionConfig, sandbox: createSandbox(context) });
416
- if (actionName && webhookConfig.actions?.[actionName]) {
417
- executeOperations = webhookConfig.actions[actionName];
418
- }
419
- } else {
420
- executeOperations = actionConfig;
421
- }
422
- }
423
-
424
- if (executeOperations) {
425
- if (Array.isArray(executeOperations)) {
426
- for (let i = 0; i < executeOperations.length; i++) {
427
- const stepResult = await astAsync({ executeAST: executeOperations[i], sandbox: createSandbox(context) });
428
- context.actions.push(stepResult);
429
- }
430
- } else if (typeof executeOperations === 'object' && executeOperations !== null) {
431
- const stepResult = await astAsync({ executeAST: [executeOperations], sandbox: createSandbox(context) });
432
- context.actions.push(stepResult);
433
- }
434
- }
427
+ const result = await astAsync({
428
+ executeAST: webhookConfig,
429
+ sandbox: createSandbox(context)
430
+ });
435
431
 
436
432
  if (res && !res.writableEnded) {
437
433
  res.writeHead(200, { "Content-Type": "application/json" });
438
- res.end(JSON.stringify({
439
- message: "Webhook processed successfully",
440
- payload: context.actions[context.actions.length - 1] || {}
434
+ res.end(JSON.stringify({
435
+ message: "Webhook processed successfully",
436
+ payload: result
441
437
  }));
442
438
  }
443
439
  } catch (error) {
@@ -448,50 +444,112 @@ export async function request(req, res) {
448
444
  }
449
445
  }
450
446
 
451
- // Unified re-exports from security helper module to prevent duplicate function definitions
452
- export { generateJWT, verifySignature, buildAuthHeaders };
453
-
447
+ /**
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.
456
+ */
454
457
  export async function makeHttpRequest(url, options) {
455
- let controller, timeoutId;
456
- if (global.AbortController) {
457
- controller = new global.AbortController();
458
- timeoutId = setTimeout(() => controller.abort(), options.timeout || 15000);
459
- options.signal = controller.signal;
460
- }
461
-
462
- const fetchFn = global.fetch;
463
- if (typeof fetchFn !== "function") {
464
- throw new Error("System runtime environment is missing a valid HTTP fetch implementation.");
465
- }
458
+ const controller = new AbortController();
459
+ const timeoutId = setTimeout(() => controller.abort(), options.timeout || 15000);
460
+ options.signal = controller.signal;
466
461
 
467
462
  try {
468
- const response = await fetchFn(url, options);
469
- if (timeoutId) clearTimeout(timeoutId);
463
+ const response = await fetch(url, options);
464
+ clearTimeout(timeoutId);
470
465
 
471
466
  if (!response.ok) {
472
467
  const text = await response.text();
473
- 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
+ );
474
476
  error.response = {
475
477
  status: response.status,
476
478
  statusText: response.statusText,
477
- data: text
479
+ data: text,
480
+ error: providerError
478
481
  };
479
482
  throw error;
480
483
  }
481
484
  return response;
482
485
  } catch (error) {
483
- if (timeoutId) clearTimeout(timeoutId);
486
+ clearTimeout(timeoutId);
484
487
  throw error;
485
488
  }
486
489
  }
487
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
+
536
+ /**
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.
546
+ */
488
547
  export async function getApiConfig(data, name) {
489
- let orgId = data.organization_id;
490
- let host = data.host;
491
-
548
+ let { organization_id: orgId, host } = data || {};
549
+
492
550
  if (!orgId) {
493
551
  const organization = await crud.getOrganization(data);
494
- if (organization.error) throw new Error(organization.error);
552
+ if (organization?.error) throw new Error(organization.error);
495
553
  orgId = organization._id;
496
554
  }
497
555
 
@@ -499,18 +557,23 @@ export async function getApiConfig(data, name) {
499
557
  method: 'object.read',
500
558
  array: 'apis',
501
559
  organization_id: orgId,
502
- host: host,
503
- $filter: {
504
- query: { name: name, organization_id: orgId },
560
+ host,
561
+ filter: {
562
+ query: { name, enabled: true },
505
563
  limit: 1
506
564
  }
507
565
  };
508
-
566
+
509
567
  const response = await crud.send(query);
510
568
  const apiConfig = response?.object?.[0];
511
569
 
512
- 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
+
513
574
  return apiConfig;
514
575
  }
515
576
 
516
- export default API;
577
+ export default {
578
+ init
579
+ };