@cocreate/api 2.0.0 → 2.1.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/CHANGELOG.md CHANGED
@@ -1,3 +1,16 @@
1
+ # [2.1.0](https://github.com/CoCreateJS/api/compare/v2.0.0...v2.1.0) (2026-11-05)
2
+
3
+
4
+ ### Bug Fixes
5
+
6
+ * support authorized manager operation dispatch ([a899876](https://github.com/CoCreateJS/api/commit/a8998763dfd213d4ba64663a3ce294dd117729b8))
7
+ * upgrade CoCreate dependencies to v2 ([4a3a3af](https://github.com/CoCreateJS/api/commit/4a3a3afe4675c977625c9567472dc5fc26820637))
8
+
9
+
10
+ ### Features
11
+
12
+ * add authorized API HTTP request handling ([42894e6](https://github.com/CoCreateJS/api/commit/42894e69525388e6e021758f3d7ffb4f1e4ad687))
13
+
1
14
  # [2.0.0](https://github.com/CoCreateJS/api/compare/v1.23.2...v2.0.0) (2026-11-02)
2
15
 
3
16
 
package/README.md CHANGED
@@ -359,4 +359,9 @@ https://cocreatejs.com/docs/api
359
359
  This software is dual-licensed under the GNU Affero General Public License version 3 (AGPLv3) and a commercial license.
360
360
 
361
361
  - **Open Source Use:** For open-source projects and non-commercial use, this software is available under the AGPLv3. For the full license text, see the LICENSE file.
362
- - **Commercial Use:** Commercial users must obtain a commercial license. Commercial licenses are available when signing up for an API key through the CoCreate platform.
362
+ - **Commercial Use:** Commercial users must obtain a commercial license. Commercial licenses are available when signing up for an API key through the CoCreate platform.
363
+ ## HTTP entrance
364
+
365
+ The initialized API service exposes `http(req, res)` alongside `webhook(req, res)`. The module accepts POST JSON bodies up to 1 MiB, resolves organization scope from the hostname, and authorizes through `server.authorize.request` before execution. Bearer API keys and the envelope `apikey` field are supported; conflicting keys are rejected. Missing permission or authorization service returns 403. Caller scope and socket/user identity are excluded, and execution uses the sanitized envelope without the transport key.
366
+
367
+ `send()` remains the execution API used by trusted internal calls and socket dispatch, where socket authorization already occurs. The existing webhook AST owns provider signature verification.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@cocreate/api",
3
- "version": "2.0.0",
3
+ "version": "2.1.0",
4
4
  "description": "A secure, multi-tenant API execution gateway and proxy layer that dynamically resolves endpoint actions, processes AST micro-operators, handles webhooks, and manages third-party integrations with strict tenant environment isolation.",
5
5
  "keywords": [
6
6
  "api-orchestration",
@@ -51,14 +51,14 @@
51
51
  }
52
52
  },
53
53
  "dependencies": {
54
- "@cocreate/actions": "^1.24.0",
55
- "@cocreate/crud-client": "^1.37.0",
56
- "@cocreate/element-prototype": "^1.35.0",
57
- "@cocreate/render": "^1.51.0",
58
- "@cocreate/socket-client": "^1.43.0",
59
- "@cocreate/utils": "^1.54.0"
54
+ "@cocreate/actions": "^2.0.0",
55
+ "@cocreate/crud-client": "^2.0.0",
56
+ "@cocreate/element-prototype": "^2.0.0",
57
+ "@cocreate/render": "^2.0.0",
58
+ "@cocreate/socket-client": "^2.0.0",
59
+ "@cocreate/utils": "^2.0.0"
60
60
  },
61
61
  "devDependencies": {
62
- "@cocreate/webpack": "^1.9.0"
62
+ "@cocreate/webpack": "^2.0.0"
63
63
  }
64
64
  }
package/src/client.js CHANGED
@@ -402,8 +402,7 @@ export default CoCreateApi;
402
402
  * Declares/registers an API provider name; provider-specific attributes are derived from this name.
403
403
  *
404
404
  * @name api | module
405
- * @kind member
406
- * @attribute
405
+ * @kind attribute
407
406
  * @valueType string
408
407
  * @environment client
409
408
  */
@@ -412,8 +411,7 @@ export default CoCreateApi;
412
411
  * Provider-specific operation/action name.
413
412
  *
414
413
  * @name {name}
415
- * @kind member
416
- * @attribute
414
+ * @kind attribute
417
415
  * @valueType string
418
416
  * @environment client
419
417
  */
@@ -422,8 +420,7 @@ export default CoCreateApi;
422
420
  * Endpoint name or path; may be defined on an ancestor group.
423
421
  *
424
422
  * @name {name}-endpoint
425
- * @kind member
426
- * @attribute
423
+ * @kind attribute
427
424
  * @valueType string
428
425
  * @environment client
429
426
  */
@@ -432,8 +429,7 @@ export default CoCreateApi;
432
429
  * Provider or HTTP method.
433
430
  *
434
431
  * @name {name}-method
435
- * @kind member
436
- * @attribute
432
+ * @kind attribute
437
433
  * @valueType string
438
434
  * @environment client
439
435
  */
@@ -442,8 +438,7 @@ export default CoCreateApi;
442
438
  * Maps an element's value into request data or maps response data back to the element. A key ending in [] accumulates values into an array; {} maps the whole object.
443
439
  *
444
440
  * @name {name}-key
445
- * @kind member
446
- * @attribute
441
+ * @kind attribute
447
442
  * @valueType string
448
443
  * @environment client
449
444
  */
@@ -452,8 +447,7 @@ export default CoCreateApi;
452
447
  * Limits automatic requests to matching event types.
453
448
  *
454
449
  * @name {name}-event
455
- * @kind member
456
- * @attribute
450
+ * @kind attribute
457
451
  * @valueType string
458
452
  * @environment client
459
453
  */
@@ -462,8 +456,7 @@ export default CoCreateApi;
462
456
  * Excludes an element from request collection or response mapping when set to false.
463
457
  *
464
458
  * @name {name}-request | {name}-response
465
- * @kind member
466
- * @attribute
459
+ * @kind attribute
467
460
  * @valueType boolean-like string
468
461
  * @acceptedValues false
469
462
  * @environment client
package/src/server.js CHANGED
@@ -33,6 +33,7 @@ let crud = null;
33
33
 
34
34
  const API = {
35
35
  send,
36
+ http,
36
37
  webhook,
37
38
  getApiConfig
38
39
  };
@@ -366,6 +367,85 @@ export async function send(data, parentContext = null) {
366
367
  }
367
368
  }
368
369
 
370
+ /**
371
+ * Handles inbound API HTTP requests, resolves host scope and authorizes before execution.
372
+ * @param {object} req - Native HTTP request.
373
+ * @param {object} res - Native HTTP response.
374
+ * @param {object} [parsedInput] - Body already read by the manager HTTP entrance.
375
+ * @param {Function} [execute] - Trusted dispatcher invoked only after request authorization.
376
+ * @returns {Promise<object|undefined>} Successful execution result, otherwise undefined.
377
+ * @public
378
+ * @environment server
379
+ */
380
+ export async function http(req, res, parsedInput, execute = request => server.api.send(request)) {
381
+ const reply = (status, body) => {
382
+ res.writeHead(status, { 'Content-Type': 'application/json; charset=utf-8', 'Cache-Control': 'no-store' });
383
+ res.end(JSON.stringify(body));
384
+ };
385
+ if (req.method !== 'POST') {
386
+ res.setHeader('Allow', 'POST');
387
+ return reply(405, { error: 'Use POST for this operation.' });
388
+ }
389
+ if (req.headers['content-type']?.split(';')[0].trim().toLowerCase() !== 'application/json') {
390
+ return reply(415, { error: 'An application/json body is required.' });
391
+ }
392
+ let input = parsedInput;
393
+ try {
394
+ if (input === undefined) {
395
+ const chunks = [];
396
+ let size = 0;
397
+ for await (const chunk of req) {
398
+ const buffer = Buffer.from(chunk);
399
+ size += buffer.length;
400
+ if (size > 1024 * 1024) return reply(413, { error: 'Request body exceeds 1 MiB.' });
401
+ chunks.push(buffer);
402
+ }
403
+ input = JSON.parse(Buffer.concat(chunks).toString('utf8'));
404
+ }
405
+ if (!input || typeof input !== 'object' || Array.isArray(input)) throw new Error();
406
+ } catch {
407
+ return reply(400, { error: 'A JSON object is required.' });
408
+ }
409
+
410
+ const bearer = req.headers.authorization?.match(/^Bearer\s+(\S+)$/i)?.[1];
411
+ const apikey = bearer || input.apikey;
412
+ if ((apikey !== undefined && (typeof apikey !== 'string' || !apikey.trim())) ||
413
+ (bearer && input.apikey !== undefined && input.apikey !== bearer)) {
414
+ return reply(401, { error: 'A valid API key is required.' });
415
+ }
416
+
417
+ try {
418
+ const host = new URL(`https://${req.headers.host}`).hostname;
419
+ const organization = await server.crud.getOrganization({ host });
420
+ if (!organization?._id || organization.error) return reply(404, { error: 'Organization not found.' });
421
+ if (typeof input.api !== 'string' || !input.api.trim() ||
422
+ typeof input.endpoint !== 'string' || !input.endpoint.trim() ||
423
+ typeof input.method !== 'string' || !input.method.trim()) {
424
+ return reply(400, { error: 'api, endpoint and method are required.' });
425
+ }
426
+ const envelope = pickHttpFields(input, ['api', 'endpoint', 'method', 'data', 'region', 'headers', 'searchParams', 'timeout']);
427
+ // Scope and identity are server-resolved, never supplied by the caller.
428
+ Object.assign(envelope, { organization_id: organization._id, host, apikey });
429
+ const request = await server.authorize?.request(envelope);
430
+ if (!request) return reply(403, { error: 'Operation is not authorized.' });
431
+ const result = await execute(request);
432
+ const failed = result?.success === false || hasHttpErrors(result?.error);
433
+ reply(failed ? 400 : 200, result);
434
+ return failed ? undefined : result;
435
+ } catch (error) {
436
+ if (error.code === 'AUTHORIZATION_DENIED') return reply(403, { error: error.message });
437
+ return reply(500, { error: 'Unable to process the operation.' });
438
+ }
439
+ }
440
+
441
+ function hasHttpErrors(error) {
442
+ return Array.isArray(error) ? error.length > 0 : Boolean(error);
443
+ }
444
+
445
+ function pickHttpFields(input, fields) {
446
+ return Object.fromEntries(fields.filter(field => Object.hasOwn(input, field)).map(field => [field, input[field]]));
447
+ }
448
+
369
449
  /**
370
450
  * Processes incoming HTTP webhook requests, verifies HMAC/JWT security signatures, and executes pipelines.
371
451
  *