@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/CONTRIBUTING.md +1 -1
- package/README.md +189 -1
- package/package.json +7 -24
- package/src/client.js +152 -76
- package/src/server.js +363 -329
- package/test/provisioning-storage.test.cjs +198 -0
package/CONTRIBUTING.md
CHANGED
|
@@ -13,7 +13,7 @@ Fork the [project on Github](https://github.com/CoCreate-app/CoCreate-api) and c
|
|
|
13
13
|
```
|
|
14
14
|
git api https://github.com/contributor/CoCreate-api.git
|
|
15
15
|
cd CoCreate-api
|
|
16
|
-
git remote add upstream https://
|
|
16
|
+
git remote add upstream https://gitlab.com/cocreatejs/CoCreate-api.git
|
|
17
17
|
```
|
|
18
18
|
|
|
19
19
|
## Create a Topic Branch
|
package/README.md
CHANGED
|
@@ -2,6 +2,8 @@
|
|
|
2
2
|
|
|
3
3
|
A dynamic, multi-tenant API execution gateway, proxy engine, and webhook orchestration routing layer. It securely manages distributed integration architectures, allowing applications to configure, evaluate, and execute complex third-party API configurations and multi-step macro workflows on the fly. By coupling an Abstract Syntax Tree (AST) evaluation sandbox (`NativeSandbox`) with strict tenant environmental isolation, it transforms declarative configurations into low-latency outbound integrations and secure inbound webhooks without introducing cross-tenant data leaks.
|
|
4
4
|
|
|
5
|
+
The package ships both halves of the integration. On the client, third-party APIs are consumed declaratively through HTML5 attributes or through an equivalent JavaScript API. On the server, the gateway resolves credentials, evaluates configurations, and executes the outbound request, so API keys never reach the browser.
|
|
6
|
+
|
|
5
7
|
---
|
|
6
8
|
|
|
7
9
|
## Documentation
|
|
@@ -14,7 +16,10 @@ For complete API references, integration guides, webhook configuration, and usag
|
|
|
14
16
|
- [Features](#features)
|
|
15
17
|
- [Dynamic Operations Sandbox](#dynamic-operations-sandbox)
|
|
16
18
|
- [Installation](#installation)
|
|
17
|
-
- [Usage](#usage)
|
|
19
|
+
- [Client Usage: HTML5 Attributes](#client-usage-html5-attributes)
|
|
20
|
+
- [Client Usage: JavaScript API](#client-usage-javascript-api)
|
|
21
|
+
- [Attribute Reference](#attribute-reference)
|
|
22
|
+
- [Server Usage](#server-usage)
|
|
18
23
|
- [How it Works](#how-it-works)
|
|
19
24
|
- [Architecture and Payload Specs](#architecture-and-payload-specs)
|
|
20
25
|
- [Security Implementations](#security-implementations)
|
|
@@ -26,6 +31,7 @@ For complete API references, integration guides, webhook configuration, and usag
|
|
|
26
31
|
## Features
|
|
27
32
|
|
|
28
33
|
* **Abstract Syntax Tree (AST) Micro-Operators:** Evaluates functional configuration contexts inline, supporting expressive syntax tags (like `$data`, `$event`, `$actions`, and `$request`) along with custom business logic operators.
|
|
34
|
+
* **Declarative Client Interface:** Configures complete request and response cycles using HTML5 attributes, assembling payloads from form fields and mapping results back into the document without provider-specific code.
|
|
29
35
|
* **Tenant Environmental Isolation Firewall:** Guarantees cryptographic data partitioning by intercepting execution matrices and forcefully injecting bound `organization_id` and verified `host` values into core operations.
|
|
30
36
|
* **Real-Time WebSocket Integration Matrix:** Seamlessly hooks into live server networks (`wsManager`), listening for incoming `api` and `endpoint` actions and streaming real-time operational state payloads directly back to the client.
|
|
31
37
|
* **Dual Outbound Execution Vectors:** Wire-speeds native, pre-constructed HTTP payloads instantly, or dynamically builds custom multi-step outbound integrations by resolving runtime metadata against persistence layers.
|
|
@@ -59,6 +65,162 @@ npm install @cocreate/api
|
|
|
59
65
|
|
|
60
66
|
## Usage
|
|
61
67
|
|
|
68
|
+
The API engine is consumed two ways. On the client, integrations are declared entirely with HTML5 attributes or driven through the JavaScript API. On the server, the gateway resolves credentials, evaluates configurations, and performs the outbound request.
|
|
69
|
+
|
|
70
|
+
---
|
|
71
|
+
|
|
72
|
+
## Client Usage: HTML5 Attributes
|
|
73
|
+
|
|
74
|
+
The client requires no provider-specific code. Declaring a provider on a container registers it, and every element carrying that provider's attributes contributes a field to the outbound payload or receives a field from the response.
|
|
75
|
+
|
|
76
|
+
### Declare the Provider
|
|
77
|
+
|
|
78
|
+
```html
|
|
79
|
+
<form api="stripe"></form>
|
|
80
|
+
```
|
|
81
|
+
|
|
82
|
+
The value of `api` is the provider name. It becomes the attribute prefix used by every field, the action name used to trigger the request, and the payload key sent over the socket. `module` is accepted as an alias.
|
|
83
|
+
|
|
84
|
+
### Build a Request
|
|
85
|
+
|
|
86
|
+
Each participating element declares the method it belongs to and the path it populates.
|
|
87
|
+
|
|
88
|
+
```html
|
|
89
|
+
<form api="stripe">
|
|
90
|
+
<input stripe="customers.create" stripe-key="email" />
|
|
91
|
+
<input stripe="customers.create" stripe-key="metadata.workspace" value="production" hidden />
|
|
92
|
+
|
|
93
|
+
<button actions="stripe.customers.create">Create customer</button>
|
|
94
|
+
</form>
|
|
95
|
+
```
|
|
96
|
+
|
|
97
|
+
Clicking the button collects every `[stripe="customers.create"]` element in the form, reads each value, and assembles the payload from the `stripe-key` paths:
|
|
98
|
+
|
|
99
|
+
```json
|
|
100
|
+
{
|
|
101
|
+
"method": "stripe.customers.create",
|
|
102
|
+
"stripe": {
|
|
103
|
+
"email": "developer@cocreate.app",
|
|
104
|
+
"metadata": { "workspace": "production" }
|
|
105
|
+
},
|
|
106
|
+
"status": "await"
|
|
107
|
+
}
|
|
108
|
+
```
|
|
109
|
+
|
|
110
|
+
Keys use dot notation for nesting and bracket notation for indexes, so `messages[0].content` and `metadata.workspace` both resolve to nested objects. A key ending in `[]` appends to an array, allowing several elements to contribute entries to the same collection.
|
|
111
|
+
|
|
112
|
+
### Receive a Response
|
|
113
|
+
|
|
114
|
+
An element marked `-request="false"` is excluded from the outbound payload and used only to receive a value from the response.
|
|
115
|
+
|
|
116
|
+
```html
|
|
117
|
+
<input
|
|
118
|
+
stripe="customers.create"
|
|
119
|
+
stripe-key="id"
|
|
120
|
+
stripe-request="false"
|
|
121
|
+
hidden />
|
|
122
|
+
```
|
|
123
|
+
|
|
124
|
+
When the response arrives, the value at that path is written into the element. Use `-response="false"` for the inverse: a field that is sent but never overwritten. A key of `{}` maps the entire provider payload rather than a single path.
|
|
125
|
+
|
|
126
|
+
### Chain the Result
|
|
127
|
+
|
|
128
|
+
Because the response is written with the standard value pipeline, any binding module can react to it.
|
|
129
|
+
|
|
130
|
+
```html
|
|
131
|
+
<input
|
|
132
|
+
stripe="customers.create"
|
|
133
|
+
stripe-key="id"
|
|
134
|
+
stripe-request="false"
|
|
135
|
+
input-query="$document #customer-id"
|
|
136
|
+
input.value="$value"
|
|
137
|
+
hidden />
|
|
138
|
+
```
|
|
139
|
+
|
|
140
|
+
### Choose the Trigger
|
|
141
|
+
|
|
142
|
+
By default a request runs only when the action fires. `-event` opts the element into additional triggers.
|
|
143
|
+
|
|
144
|
+
```html
|
|
145
|
+
<input
|
|
146
|
+
currency="rates.read"
|
|
147
|
+
currency-key="base"
|
|
148
|
+
currency-event="input, onload" />
|
|
149
|
+
```
|
|
150
|
+
|
|
151
|
+
Available trigger types are `action`, `input`, `onload`, `nodeObserver`, and `attributeObserver`.
|
|
152
|
+
|
|
153
|
+
### Scope and Endpoints
|
|
154
|
+
|
|
155
|
+
Fields are collected from the nearest ancestor `form`, or from the document when the element is not inside one, so several independent integrations can coexist on one page. Elements inside a `[template]` are skipped, keeping templates out of live payloads.
|
|
156
|
+
|
|
157
|
+
An `endpoint` attribute selects the configured route, and a `query` attribute marks the request as a query.
|
|
158
|
+
|
|
159
|
+
```html
|
|
160
|
+
<input stripe="customers.create" endpoint="POST /v1/customers" hidden />
|
|
161
|
+
```
|
|
162
|
+
|
|
163
|
+
### Render Errors
|
|
164
|
+
|
|
165
|
+
When the gateway returns an error, the client renders it into any template matching the provider name, receiving `type`, `method`, `status`, and `message`.
|
|
166
|
+
|
|
167
|
+
```html
|
|
168
|
+
<div template="stripe-error">{{message}}</div>
|
|
169
|
+
```
|
|
170
|
+
|
|
171
|
+
---
|
|
172
|
+
|
|
173
|
+
## Client Usage: JavaScript API
|
|
174
|
+
|
|
175
|
+
The same pipeline is available programmatically for providers that need registration options, custom handlers, or imperative execution.
|
|
176
|
+
|
|
177
|
+
### Register a Provider
|
|
178
|
+
|
|
179
|
+
```javascript
|
|
180
|
+
import api from "@cocreate/api";
|
|
181
|
+
|
|
182
|
+
api.init({
|
|
183
|
+
name: "stripe",
|
|
184
|
+
endPoints: {},
|
|
185
|
+
options: { socket: true }
|
|
186
|
+
});
|
|
187
|
+
```
|
|
188
|
+
|
|
189
|
+
Calling `init()` without arguments scans the document for `[api]` and `[module]` elements and registers each provider it finds. Registration also creates the socket connection unless `options.socket` is `false`.
|
|
190
|
+
|
|
191
|
+
### Execute a Request
|
|
192
|
+
|
|
193
|
+
```javascript
|
|
194
|
+
api.request({
|
|
195
|
+
name: "stripe",
|
|
196
|
+
method: "customers.create",
|
|
197
|
+
form: document.querySelector("#billing"),
|
|
198
|
+
type: "action"
|
|
199
|
+
});
|
|
200
|
+
```
|
|
201
|
+
|
|
202
|
+
### Work With the Payload Directly
|
|
203
|
+
|
|
204
|
+
```javascript
|
|
205
|
+
const data = await api.getData({
|
|
206
|
+
name: "stripe",
|
|
207
|
+
method: "customers.create",
|
|
208
|
+
form: document.querySelector("#billing")
|
|
209
|
+
});
|
|
210
|
+
|
|
211
|
+
await api.send({ name: "stripe", method: "customers.create", data });
|
|
212
|
+
```
|
|
213
|
+
|
|
214
|
+
`send()` transmits over the CoCreate socket as `provider.method` with the payload under the provider key, awaiting the correlated response before mapping it back into the document.
|
|
215
|
+
|
|
216
|
+
### Custom Handlers
|
|
217
|
+
|
|
218
|
+
If a registered module defines a handler for a method, its `request` and `response` functions take precedence over the default collection and mapping behavior. This is the extension point for providers needing bespoke serialization, such as third-party SDK elements.
|
|
219
|
+
|
|
220
|
+
---
|
|
221
|
+
|
|
222
|
+
## Server Usage
|
|
223
|
+
|
|
62
224
|
### Module Initialization
|
|
63
225
|
|
|
64
226
|
Bind the API framework directly during the bootstrapping phase of your core `CoCreateServer` application matrix.
|
|
@@ -100,6 +262,32 @@ const result = await apiGateway.executeApi(payload);
|
|
|
100
262
|
|
|
101
263
|
---
|
|
102
264
|
|
|
265
|
+
## Attribute Reference
|
|
266
|
+
|
|
267
|
+
`name` refers to the provider declared by the `api` attribute, for example `stripe` or `openai`.
|
|
268
|
+
|
|
269
|
+
| Attribute | Role |
|
|
270
|
+
| --- | --- |
|
|
271
|
+
| `api="name"` | Declares and registers the provider. `module` is an accepted alias. |
|
|
272
|
+
| `name="method"` | Marks the element as a participant in that method's request and response. |
|
|
273
|
+
| `name-key="path"` | Path the element populates or receives. Supports dot and bracket notation, `[]` to append to an array, and `{}` for the entire payload. |
|
|
274
|
+
| `name-event="types"` | Trigger types that perform a request: `action`, `input`, `onload`, `nodeObserver`, `attributeObserver`. Defaults to `action`. |
|
|
275
|
+
| `name-request="false"` | Excludes the element from the outbound payload, making it response-only. |
|
|
276
|
+
| `name-response="false"` | Excludes the element from response mapping, making it request-only. |
|
|
277
|
+
| `endpoint="METHOD /route"` | Sets the endpoint resolved by the gateway. |
|
|
278
|
+
| `query` | Flags the request as a query. |
|
|
279
|
+
| `template_id="id"` | Renders the response through the matching template instead of setting a value. |
|
|
280
|
+
| `actions="name.method"` | Triggers the request, and participates in action chains. |
|
|
281
|
+
|
|
282
|
+
### Notes
|
|
283
|
+
|
|
284
|
+
* Fields are gathered from the nearest ancestor `form`, falling back to the document.
|
|
285
|
+
* Elements inside a `[template]` are excluded from payload collection.
|
|
286
|
+
* Elements carrying an `actions` attribute are skipped during response mapping so that chained behavior is not overwritten.
|
|
287
|
+
* Requests are sent with broadcasting disabled, so a payload is never echoed to other clients.
|
|
288
|
+
|
|
289
|
+
---
|
|
290
|
+
|
|
103
291
|
## How it Works
|
|
104
292
|
|
|
105
293
|
1. **Bootstrap & Event Binding:** During `init()`, the module hooks directly into the server's WebSocket framework (`wsManager`), capturing incoming `api` and `endpoint` event signals to route them to the execution handler.
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@cocreate/api",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.34.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",
|
|
@@ -27,7 +27,7 @@
|
|
|
27
27
|
},
|
|
28
28
|
"repository": {
|
|
29
29
|
"type": "git",
|
|
30
|
-
"url": "git+https://
|
|
30
|
+
"url": "git+https://gitlab.com/cocreatejs/CoCreate-api.git"
|
|
31
31
|
},
|
|
32
32
|
"author": "CoCreate LLC",
|
|
33
33
|
"license": "GPL-3.0",
|
|
@@ -52,30 +52,13 @@
|
|
|
52
52
|
},
|
|
53
53
|
"dependencies": {
|
|
54
54
|
"@cocreate/actions": "^1.24.0",
|
|
55
|
-
"@cocreate/crud-client": "^1.
|
|
55
|
+
"@cocreate/crud-client": "^1.37.0",
|
|
56
56
|
"@cocreate/element-prototype": "^1.35.0",
|
|
57
|
-
"@cocreate/render": "^1.
|
|
58
|
-
"@cocreate/socket-client": "^1.
|
|
59
|
-
"@cocreate/utils": "^1.
|
|
57
|
+
"@cocreate/render": "^1.51.0",
|
|
58
|
+
"@cocreate/socket-client": "^1.43.0",
|
|
59
|
+
"@cocreate/utils": "^1.54.0"
|
|
60
60
|
},
|
|
61
61
|
"devDependencies": {
|
|
62
|
-
"@cocreate/webpack": "^1.
|
|
63
|
-
},
|
|
64
|
-
"allowScripts": {
|
|
65
|
-
"@cocreate/actions@1.21.4": true,
|
|
66
|
-
"@cocreate/api@1.22.5": true,
|
|
67
|
-
"@cocreate/config@1.13.4": true,
|
|
68
|
-
"@cocreate/crud-client@1.34.6": true,
|
|
69
|
-
"@cocreate/element-prototype@1.31.4": true,
|
|
70
|
-
"@cocreate/elements@1.42.9": true,
|
|
71
|
-
"@cocreate/filter@1.33.6": true,
|
|
72
|
-
"@cocreate/indexeddb@1.23.4": true,
|
|
73
|
-
"@cocreate/local-storage@1.16.5": true,
|
|
74
|
-
"@cocreate/observer@1.19.0": true,
|
|
75
|
-
"@cocreate/organizations@1.30.1": true,
|
|
76
|
-
"@cocreate/render@1.46.1": true,
|
|
77
|
-
"@cocreate/socket-client@1.40.5": true,
|
|
78
|
-
"@cocreate/utils@1.42.2": true,
|
|
79
|
-
"@cocreate/webpack@1.4.3": true
|
|
62
|
+
"@cocreate/webpack": "^1.9.0"
|
|
80
63
|
}
|
|
81
64
|
}
|
package/src/client.js
CHANGED
|
@@ -3,10 +3,42 @@ import { getValueFromObject, dotNotationToObject } from "@cocreate/utils";
|
|
|
3
3
|
import Observer from "@cocreate/observer";
|
|
4
4
|
import Socket from "@cocreate/socket-client";
|
|
5
5
|
import Actions from "@cocreate/actions";
|
|
6
|
-
import { render } from "@cocreate/render";
|
|
7
|
-
import "@cocreate/element-prototype";
|
|
8
|
-
|
|
9
|
-
|
|
6
|
+
import { render } from "@cocreate/render";
|
|
7
|
+
import "@cocreate/element-prototype";
|
|
8
|
+
|
|
9
|
+
function getEndpointGroup({ name, action, element, form }) {
|
|
10
|
+
const operation = String(action || "").replaceAll('"', '\\"');
|
|
11
|
+
const operationSelector = operation ? `[${name}="${operation}"]` : null;
|
|
12
|
+
const endpointSelector = `[${name}-endpoint]`;
|
|
13
|
+
return (
|
|
14
|
+
(operationSelector && element?.closest(operationSelector)) ||
|
|
15
|
+
element?.closest(endpointSelector) ||
|
|
16
|
+
(operationSelector && form?.matches?.(operationSelector) ? form : null) ||
|
|
17
|
+
(form?.matches?.(endpointSelector) ? form : null)
|
|
18
|
+
);
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
function getGroupElements({ name, action, element, form, response = false }) {
|
|
22
|
+
const group = getEndpointGroup({ name, action, element, form });
|
|
23
|
+
const stateAttribute = `${name}-${response ? "response" : "request"}`;
|
|
24
|
+
let elements = [];
|
|
25
|
+
|
|
26
|
+
if (group) {
|
|
27
|
+
elements = [group, ...group.querySelectorAll(`[${name}-key]`)].filter(
|
|
28
|
+
(candidate) =>
|
|
29
|
+
getEndpointGroup({ name, action, element: candidate, form }) === group &&
|
|
30
|
+
candidate.getAttribute(stateAttribute) !== "false"
|
|
31
|
+
);
|
|
32
|
+
} else if (form) {
|
|
33
|
+
const selector = `[${name}="${action}"]:not([${stateAttribute}="false"])`;
|
|
34
|
+
if (form.matches?.(selector)) elements.push(form);
|
|
35
|
+
elements.push(...form.querySelectorAll(selector));
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
return elements.length ? elements : element ? [element] : [];
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
const CoCreateApi = {
|
|
10
42
|
modules: {},
|
|
11
43
|
|
|
12
44
|
init: function (moduleConfig) {
|
|
@@ -31,13 +63,18 @@ const CoCreateApi = {
|
|
|
31
63
|
if (typeof this.modules[name] === "undefined") {
|
|
32
64
|
this.modules[name] = { name, endPoints, options };
|
|
33
65
|
|
|
34
|
-
Actions.init({
|
|
35
|
-
name,
|
|
36
|
-
callback: (action) => {
|
|
37
|
-
action.form = action.form || document;
|
|
38
|
-
self.request({
|
|
39
|
-
|
|
40
|
-
|
|
66
|
+
Actions.init({
|
|
67
|
+
name,
|
|
68
|
+
callback: (action) => {
|
|
69
|
+
action.form = action.form || document;
|
|
70
|
+
return self.request({
|
|
71
|
+
...action,
|
|
72
|
+
action: action.method,
|
|
73
|
+
method: undefined,
|
|
74
|
+
type: "action"
|
|
75
|
+
});
|
|
76
|
+
}
|
|
77
|
+
});
|
|
41
78
|
|
|
42
79
|
const inputEvent = (element) => {
|
|
43
80
|
element.addEventListener("input", (e) => {
|
|
@@ -47,7 +84,9 @@ const CoCreateApi = {
|
|
|
47
84
|
});
|
|
48
85
|
};
|
|
49
86
|
|
|
50
|
-
let elements = document.querySelectorAll(
|
|
87
|
+
let elements = document.querySelectorAll(
|
|
88
|
+
`[${name}], [${name}-endpoint]`
|
|
89
|
+
);
|
|
51
90
|
for (let i = 0; i < elements.length; i++) {
|
|
52
91
|
inputEvent(elements[i]);
|
|
53
92
|
this.request({ name, element: elements[i], type: "onload" });
|
|
@@ -56,7 +95,7 @@ const CoCreateApi = {
|
|
|
56
95
|
Observer.init({
|
|
57
96
|
name: `${name}NodeObserver`,
|
|
58
97
|
types: ["addedNodes"],
|
|
59
|
-
selector: `[${name}]`,
|
|
98
|
+
selector: `[${name}], [${name}-endpoint]`,
|
|
60
99
|
callback: function (mutation) {
|
|
61
100
|
inputEvent(mutation.target);
|
|
62
101
|
self.request({
|
|
@@ -70,7 +109,7 @@ const CoCreateApi = {
|
|
|
70
109
|
Observer.init({
|
|
71
110
|
name: `${name}AttributeObserver`,
|
|
72
111
|
types: ["attributes"],
|
|
73
|
-
attributeFilter: [name],
|
|
112
|
+
attributeFilter: [name, `${name}-endpoint`, `${name}-method`],
|
|
74
113
|
callback: function (mutation) {
|
|
75
114
|
self.request({
|
|
76
115
|
name,
|
|
@@ -82,10 +121,16 @@ const CoCreateApi = {
|
|
|
82
121
|
}
|
|
83
122
|
},
|
|
84
123
|
|
|
85
|
-
request: async function (object) {
|
|
86
|
-
if (object.element) {
|
|
87
|
-
if (!object.
|
|
88
|
-
object.
|
|
124
|
+
request: async function (object) {
|
|
125
|
+
if (object.element) {
|
|
126
|
+
if (!object.action)
|
|
127
|
+
object.action = object.element.getAttribute(object.name);
|
|
128
|
+
const group = getEndpointGroup(object);
|
|
129
|
+
if (!object.endpoint)
|
|
130
|
+
object.endpoint =
|
|
131
|
+
group?.getAttribute(`${object.name}-endpoint`) || object.action;
|
|
132
|
+
if (!object.method)
|
|
133
|
+
object.method = group?.getAttribute(`${object.name}-method`);
|
|
89
134
|
if (!object.key)
|
|
90
135
|
object.key = object.element.getAttribute(`${object.name}-key`);
|
|
91
136
|
if (!object.event)
|
|
@@ -94,38 +139,56 @@ const CoCreateApi = {
|
|
|
94
139
|
);
|
|
95
140
|
}
|
|
96
141
|
|
|
97
|
-
if (
|
|
98
|
-
this.modules[object.name][object.
|
|
99
|
-
this.modules[object.name][object.
|
|
100
|
-
)
|
|
101
|
-
this.modules[object.name][object.
|
|
142
|
+
if (
|
|
143
|
+
this.modules[object.name][object.action] &&
|
|
144
|
+
this.modules[object.name][object.action].request
|
|
145
|
+
)
|
|
146
|
+
this.modules[object.name][object.action].request(object);
|
|
102
147
|
else if (
|
|
103
148
|
(!object.event && object.type === "action") ||
|
|
104
149
|
(object.event && object.event.includes(object.type))
|
|
105
|
-
) {
|
|
150
|
+
) {
|
|
106
151
|
object.data = await CoCreateApi.getData(object);
|
|
107
|
-
if (
|
|
108
|
-
|
|
152
|
+
if (
|
|
153
|
+
object.action ||
|
|
154
|
+
object.endpoint ||
|
|
155
|
+
object.method ||
|
|
156
|
+
Object.keys(object.data).length
|
|
157
|
+
)
|
|
158
|
+
return CoCreateApi.send(object);
|
|
159
|
+
}
|
|
109
160
|
},
|
|
110
161
|
|
|
111
|
-
response: function (object) {
|
|
112
|
-
const name = object.name;
|
|
113
|
-
const
|
|
114
|
-
const
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
162
|
+
response: function (object) {
|
|
163
|
+
const name = object.name;
|
|
164
|
+
const action = object.action;
|
|
165
|
+
const response = object.data;
|
|
166
|
+
const data = response.data;
|
|
167
|
+
if (this.modules[name][action] && this.modules[name][action].response)
|
|
168
|
+
this.modules[name][action].response(data);
|
|
169
|
+
else if (response.error) {
|
|
170
|
+
object.abort?.({
|
|
171
|
+
code: "api-request-failed",
|
|
172
|
+
message: response.error?.message || String(response.error),
|
|
173
|
+
cause: response.error
|
|
174
|
+
});
|
|
118
175
|
render({
|
|
119
176
|
selector: `[template*='${name}']`,
|
|
120
177
|
data: [
|
|
121
178
|
{
|
|
122
179
|
type: name,
|
|
123
|
-
|
|
180
|
+
action,
|
|
181
|
+
method: object.method,
|
|
124
182
|
status: "failed",
|
|
125
|
-
message:
|
|
183
|
+
message: response.error
|
|
126
184
|
}
|
|
127
185
|
]
|
|
128
186
|
});
|
|
187
|
+
object.element?.dispatchEvent(
|
|
188
|
+
new CustomEvent("error", {
|
|
189
|
+
detail: { error: response.error, data: response }
|
|
190
|
+
})
|
|
191
|
+
);
|
|
129
192
|
} else {
|
|
130
193
|
CoCreateApi.setData(object);
|
|
131
194
|
|
|
@@ -139,36 +202,52 @@ const CoCreateApi = {
|
|
|
139
202
|
}
|
|
140
203
|
},
|
|
141
204
|
|
|
142
|
-
send: async function (object) {
|
|
143
|
-
object.data
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
205
|
+
send: async function (object) {
|
|
206
|
+
const isDataObject = object.data &&
|
|
207
|
+
typeof object.data === "object" &&
|
|
208
|
+
!Array.isArray(object.data);
|
|
209
|
+
const data = isDataObject ? { ...object.data } : object.data;
|
|
210
|
+
const request = {
|
|
211
|
+
api: object.name,
|
|
212
|
+
method: object.method,
|
|
213
|
+
endpoint: object.endpoint,
|
|
214
|
+
data,
|
|
215
|
+
broadcast: false,
|
|
216
|
+
broadcastClient: false
|
|
217
|
+
};
|
|
218
|
+
|
|
219
|
+
for (const directive of ["url", "headers", "timeout", "searchParams"]) {
|
|
220
|
+
const value = object[directive] ?? (isDataObject ? data[directive] : undefined);
|
|
221
|
+
if (value !== undefined) request[directive] = value;
|
|
222
|
+
if (isDataObject) delete data[directive];
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
try {
|
|
226
|
+
object.data = await Socket.send(request);
|
|
227
|
+
} catch (error) {
|
|
228
|
+
object.data = { error: error?.message || String(error) };
|
|
229
|
+
}
|
|
150
230
|
this.response(object);
|
|
151
231
|
},
|
|
152
232
|
|
|
153
|
-
getData: async function ({ name,
|
|
233
|
+
getData: async function ({ name, action, element, form }) {
|
|
154
234
|
let data = {};
|
|
155
235
|
|
|
156
236
|
if (!form && element) form = element.closest("form");
|
|
157
237
|
if (!form) form = document;
|
|
158
238
|
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
239
|
+
const elements = getGroupElements({
|
|
240
|
+
name,
|
|
241
|
+
action,
|
|
242
|
+
element,
|
|
243
|
+
form
|
|
244
|
+
});
|
|
165
245
|
|
|
166
246
|
for (let i = 0; i < elements.length; i++) {
|
|
167
247
|
if (!elements[i] || elements[i].closest("[template]")) continue;
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
elements[i].stripeElement || (await elements[i].getValue());
|
|
248
|
+
let key = elements[i].getAttribute(`${name}-key`);
|
|
249
|
+
if (key) {
|
|
250
|
+
let value = await elements[i].getValue();
|
|
172
251
|
if (key.endsWith("[]")) {
|
|
173
252
|
if (!data[key]) data[key] = [];
|
|
174
253
|
|
|
@@ -177,11 +256,6 @@ const CoCreateApi = {
|
|
|
177
256
|
} else data[key] = value;
|
|
178
257
|
}
|
|
179
258
|
|
|
180
|
-
let endpoint = elements[i].getAttribute("endpoint");
|
|
181
|
-
if (endpoint) {
|
|
182
|
-
data.endpoint = endpoint;
|
|
183
|
-
}
|
|
184
|
-
|
|
185
259
|
if (
|
|
186
260
|
elements[i].getAttribute("query") &&
|
|
187
261
|
elements[i].getAttribute("query") !== "false"
|
|
@@ -209,20 +283,24 @@ const CoCreateApi = {
|
|
|
209
283
|
|
|
210
284
|
setData: function (object) {
|
|
211
285
|
const name = object.name;
|
|
212
|
-
const data = object.data;
|
|
286
|
+
const data = object.data.data;
|
|
213
287
|
let form = object.form;
|
|
214
288
|
if (!form) form = document;
|
|
215
289
|
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
290
|
+
const elements = getGroupElements({
|
|
291
|
+
name,
|
|
292
|
+
action: object.action,
|
|
293
|
+
element: object.element,
|
|
294
|
+
form,
|
|
295
|
+
response: true
|
|
296
|
+
});
|
|
297
|
+
if (!elements.length) return;
|
|
220
298
|
|
|
221
|
-
for (let i = 0; i < elements.length; i++) {
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
let templateid = elements[i].getAttribute("template_id");
|
|
299
|
+
for (let i = 0; i < elements.length; i++) {
|
|
300
|
+
if (elements[i].hasAttribute("actions")) continue;
|
|
301
|
+
let key = elements[i].getAttribute(`${name}-key`);
|
|
302
|
+
if (key) {
|
|
303
|
+
let templateid = elements[i].getAttribute("template_id");
|
|
226
304
|
if (templateid) {
|
|
227
305
|
let items = document.querySelectorAll(
|
|
228
306
|
`[templateid="${templateid}"]`
|
|
@@ -233,21 +311,19 @@ const CoCreateApi = {
|
|
|
233
311
|
data
|
|
234
312
|
});
|
|
235
313
|
} else if (elements[i].renderValue) {
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
elements[i].renderValue(data[name]);
|
|
314
|
+
if (key === "{}") {
|
|
315
|
+
elements[i].renderValue(data);
|
|
239
316
|
} else {
|
|
240
|
-
let value = getValueFromObject(data
|
|
317
|
+
let value = getValueFromObject(data, key);
|
|
241
318
|
if (typeof value === "function") {
|
|
242
319
|
value = value(); // Call the function and assign its return value.
|
|
243
320
|
}
|
|
244
321
|
elements[i].renderValue(value);
|
|
245
322
|
}
|
|
246
323
|
} else {
|
|
247
|
-
|
|
248
|
-
if (key === "{}") elements[i].setValue(data[name]);
|
|
324
|
+
if (key === "{}") elements[i].setValue(data);
|
|
249
325
|
else {
|
|
250
|
-
let value = getValueFromObject(data
|
|
326
|
+
let value = getValueFromObject(data, key);
|
|
251
327
|
if (typeof value === "function") {
|
|
252
328
|
value = value(); // Call the function and assign its return value.
|
|
253
329
|
}
|