@colixsystems/actions-client 0.3.0 → 0.4.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/README.md +3 -1
- package/dist/client.js +31 -18
- package/dist/client.test.js +32 -0
- package/dist/index.d.ts +5 -0
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -10,7 +10,7 @@ An Action is a server-side script the workspace owns. The app may run one only w
|
|
|
10
10
|
|
|
11
11
|
## Status
|
|
12
12
|
|
|
13
|
-
`v0.
|
|
13
|
+
`v0.4.0` — adds `enqueue`, which queues an Action run instead of waiting for it. `v0.3.0` added `createConnectorsClient`, which calls a workspace's REST connectors through the operations it listed for apps.
|
|
14
14
|
|
|
15
15
|
## Public API
|
|
16
16
|
|
|
@@ -43,6 +43,8 @@ const { available, reason } = await actions.status(actionId); // reason: "ACTION
|
|
|
43
43
|
|
|
44
44
|
The request is `POST {baseUrl}/app-actions/:actionId/run` with `Authorization: Bearer <token>` and `x-tenant-id`. The body is `{ input }`; when `input` is omitted or `null` no body is sent. It answers `{ ok, result }`, where `result` is the value the script passed to `respond()`, or `null`.
|
|
45
45
|
|
|
46
|
+
`enqueue(actionId, input)` sends the same request to `POST {baseUrl}/app-actions/:actionId/enqueue` and resolves `{ queued: true }` once the server accepts it (202). It meets the same refusals as `run`, but nothing waits on the script: it runs later, away from the request, with the Action's normal limits instead of the 500 ms an app run gets, and its `respond()` value is not returned. Use it to start work the app does not need an answer from. Beyond `run`'s refusals it answers 409 `ACTION_DISABLED` / `ACTION_PAUSED` for an Action that would not run, 429 `RATE_LIMITED` when the workspace already has 10 queued runs, and 503 `ACTION_QUEUE_UNREACHABLE` when the broker did not confirm the run was queued (the outcome is then unknown). The client never retries it, but the server may run a queued run twice if it restarts mid-run, so the Action must be safe to repeat.
|
|
47
|
+
|
|
46
48
|
An empty, `.` or `..` `actionId` rejects with a `TypeError` before any request. A run is **never retried**: an Action can have side effects.
|
|
47
49
|
|
|
48
50
|
## Errors
|
package/dist/client.js
CHANGED
|
@@ -106,6 +106,26 @@ export function createActionsClient(opts) {
|
|
|
106
106
|
}
|
|
107
107
|
}
|
|
108
108
|
|
|
109
|
+
async function post(actionId, operation, input) {
|
|
110
|
+
assertActionId(actionId, operation);
|
|
111
|
+
try {
|
|
112
|
+
return await send(
|
|
113
|
+
"POST",
|
|
114
|
+
`/app-actions/${encodeURIComponent(actionId)}/${operation}`,
|
|
115
|
+
input == null ? undefined : { input },
|
|
116
|
+
operation,
|
|
117
|
+
);
|
|
118
|
+
} catch (err) {
|
|
119
|
+
if (err instanceof ActionError) throw err;
|
|
120
|
+
// No HTTP answer (offline, or no session to send) keeps the generic code.
|
|
121
|
+
throw new ActionError(`Action request failed: ${err?.message || err}`, {
|
|
122
|
+
code: "ACTION_FAILED",
|
|
123
|
+
status: 0,
|
|
124
|
+
cause: err,
|
|
125
|
+
});
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
|
|
109
129
|
return {
|
|
110
130
|
/**
|
|
111
131
|
* Whether this caller's run would reach the Action's script. Resolves
|
|
@@ -139,26 +159,19 @@ export function createActionsClient(opts) {
|
|
|
139
159
|
* pass null) to send no body. Never retried: a run can have side effects.
|
|
140
160
|
*/
|
|
141
161
|
async run(actionId, input) {
|
|
142
|
-
|
|
143
|
-
let body;
|
|
144
|
-
try {
|
|
145
|
-
body = await send(
|
|
146
|
-
"POST",
|
|
147
|
-
`/app-actions/${encodeURIComponent(actionId)}/run`,
|
|
148
|
-
input == null ? undefined : { input },
|
|
149
|
-
"run",
|
|
150
|
-
);
|
|
151
|
-
} catch (err) {
|
|
152
|
-
if (err instanceof ActionError) throw err;
|
|
153
|
-
// No HTTP answer (offline, or no session to send) keeps the generic code.
|
|
154
|
-
throw new ActionError(`Action request failed: ${err?.message || err}`, {
|
|
155
|
-
code: "ACTION_FAILED",
|
|
156
|
-
status: 0,
|
|
157
|
-
cause: err,
|
|
158
|
-
});
|
|
159
|
-
}
|
|
162
|
+
const body = await post(actionId, "run", input);
|
|
160
163
|
const src = body && typeof body === "object" ? body : {};
|
|
161
164
|
return { ok: src.ok === true, result: src.result === undefined ? null : src.result };
|
|
162
165
|
},
|
|
166
|
+
|
|
167
|
+
/**
|
|
168
|
+
* Queue one Action and resolve to `{ queued: true }` as soon as the server
|
|
169
|
+
* accepted it — the same refusals as `run`, but nothing waits on the
|
|
170
|
+
* script, which runs later with the Action's normal limits. Never retried.
|
|
171
|
+
*/
|
|
172
|
+
async enqueue(actionId, input) {
|
|
173
|
+
await post(actionId, "enqueue", input);
|
|
174
|
+
return { queued: true };
|
|
175
|
+
},
|
|
163
176
|
};
|
|
164
177
|
}
|
package/dist/client.test.js
CHANGED
|
@@ -234,3 +234,35 @@ test("status rejects when no answer came, so the caller keeps what it knew", asy
|
|
|
234
234
|
await assert.rejects(makeClient(makeFetch({ status: 500 }).fetchImpl).status("a"), { status: 500 });
|
|
235
235
|
await assert.rejects(makeClient(makeFetch({ status: 200 }).fetchImpl).status(".."), TypeError);
|
|
236
236
|
});
|
|
237
|
+
|
|
238
|
+
// sc-15876 — a queued run: same request shape on its own path, answered at once.
|
|
239
|
+
test("enqueue posts { input } to the enqueue path and resolves { queued: true }", async () => {
|
|
240
|
+
const { fetchImpl, calls } = makeFetch({ status: 202, body: { queued: true } });
|
|
241
|
+
const out = await makeClient(fetchImpl).enqueue("act-1", { batch: 7 });
|
|
242
|
+
|
|
243
|
+
assert.deepEqual(out, { queued: true });
|
|
244
|
+
assert.equal(calls.length, 1);
|
|
245
|
+
assert.equal(calls[0].url, "https://api.example.test/api/v1/app-actions/act-1/enqueue");
|
|
246
|
+
assert.equal(calls[0].init.method, "POST");
|
|
247
|
+
assert.deepEqual(JSON.parse(calls[0].init.body), { input: { batch: 7 } });
|
|
248
|
+
assert.equal(calls[0].init.headers["x-tenant-id"], "tenant-abc");
|
|
249
|
+
});
|
|
250
|
+
|
|
251
|
+
test("enqueue meets the same refusals as run and is never retried", async () => {
|
|
252
|
+
const { fetchImpl, calls } = makeFetch({ status: 403, body: { code: "ACTION_NOT_APP_INVOKABLE" } });
|
|
253
|
+
await assert.rejects(
|
|
254
|
+
() => makeClient(fetchImpl).enqueue("act-1"),
|
|
255
|
+
(err) => err instanceof ForbiddenError && err.code === "ACTION_NOT_APP_INVOKABLE",
|
|
256
|
+
);
|
|
257
|
+
assert.equal(calls.length, 1);
|
|
258
|
+
assert.equal(calls[0].init.body, undefined);
|
|
259
|
+
await assert.rejects(() => makeClient(fetchImpl).enqueue(".."), TypeError);
|
|
260
|
+
});
|
|
261
|
+
|
|
262
|
+
test("enqueue with no answer rejects ACTION_FAILED", async () => {
|
|
263
|
+
const { fetchImpl } = makeFetch({ throws: new Error("offline") });
|
|
264
|
+
await assert.rejects(
|
|
265
|
+
() => makeClient(fetchImpl).enqueue("act-1"),
|
|
266
|
+
(err) => err instanceof ActionError && err.code === "ACTION_FAILED" && err.status === 0,
|
|
267
|
+
);
|
|
268
|
+
});
|
package/dist/index.d.ts
CHANGED
|
@@ -25,6 +25,11 @@ export interface ActionsClient {
|
|
|
25
25
|
* An empty, ".", or ".." id rejects with a TypeError before any request.
|
|
26
26
|
*/
|
|
27
27
|
run(actionId: string, input?: Record<string, unknown> | null): Promise<ActionRunResult>;
|
|
28
|
+
/**
|
|
29
|
+
* POST /app-actions/:actionId/enqueue. Resolves once the run is queued; the
|
|
30
|
+
* script runs later with the Action's normal limits and returns nothing here.
|
|
31
|
+
*/
|
|
32
|
+
enqueue(actionId: string, input?: Record<string, unknown> | null): Promise<{ queued: true }>;
|
|
28
33
|
}
|
|
29
34
|
|
|
30
35
|
export interface RequestHeaderContext {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@colixsystems/actions-client",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.4.0",
|
|
4
4
|
"description": "Scoped clients for running a workspace's Actions and calling its REST connectors from an AppStudio app. Used by widgets through the injected WidgetContext.actions and WidgetContext.connectors.",
|
|
5
5
|
"homepage": "https://github.com/Colix-AB/AppStudio",
|
|
6
6
|
"type": "module",
|