@protobase/workflow-plugins 0.0.0-stage → 0.68.1
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/LICENSE +202 -0
- package/dist/app.d.ts +13 -0
- package/dist/app.js +21 -0
- package/dist/http.js +151 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.js +2 -0
- package/dist/mail.js +84 -0
- package/dist/piece.js +35 -0
- package/dist/record-trigger.js +46 -0
- package/dist/records.js +115 -0
- package/package.json +32 -4
- package/README.md +0 -3
package/LICENSE
ADDED
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
|
|
2
|
+
Apache License
|
|
3
|
+
Version 2.0, January 2004
|
|
4
|
+
http://www.apache.org/licenses/
|
|
5
|
+
|
|
6
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
7
|
+
|
|
8
|
+
1. Definitions.
|
|
9
|
+
|
|
10
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
11
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
12
|
+
|
|
13
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
14
|
+
the copyright owner that is granting the License.
|
|
15
|
+
|
|
16
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
17
|
+
other entities that control, are controlled by, or are under common
|
|
18
|
+
control with that entity. For the purposes of this definition,
|
|
19
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
20
|
+
direction or management of such entity, whether by contract or
|
|
21
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
22
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
23
|
+
|
|
24
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
25
|
+
exercising permissions granted by this License.
|
|
26
|
+
|
|
27
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
28
|
+
including but not limited to software source code, documentation
|
|
29
|
+
source, and configuration files.
|
|
30
|
+
|
|
31
|
+
"Object" form shall mean any form resulting from mechanical
|
|
32
|
+
transformation or translation of a Source form, including but
|
|
33
|
+
not limited to compiled object code, generated documentation,
|
|
34
|
+
and conversions to other media types.
|
|
35
|
+
|
|
36
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
37
|
+
Object form, made available under the License, as indicated by a
|
|
38
|
+
copyright notice that is included in or attached to the work
|
|
39
|
+
(an example is provided in the Appendix below).
|
|
40
|
+
|
|
41
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
42
|
+
form, that is based on (or derived from) the Work and for which the
|
|
43
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
44
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
45
|
+
of this License, Derivative Works shall not include works that remain
|
|
46
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
47
|
+
the Work and Derivative Works thereof.
|
|
48
|
+
|
|
49
|
+
"Contribution" shall mean any work of authorship, including
|
|
50
|
+
the original version of the Work and any modifications or additions
|
|
51
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
52
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
53
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
54
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
55
|
+
means any form of electronic, verbal, or written communication sent
|
|
56
|
+
to the Licensor or its representatives, including but not limited to
|
|
57
|
+
communication on electronic mailing lists, source code control systems,
|
|
58
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
59
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
60
|
+
excluding communication that is conspicuously marked or otherwise
|
|
61
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
62
|
+
|
|
63
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
64
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
65
|
+
subsequently incorporated within the Work.
|
|
66
|
+
|
|
67
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
68
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
69
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
70
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
71
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
72
|
+
Work and such Derivative Works in Source or Object form.
|
|
73
|
+
|
|
74
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
75
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
76
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
77
|
+
(except as stated in this section) patent license to make, have made,
|
|
78
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
79
|
+
where such license applies only to those patent claims licensable
|
|
80
|
+
by such Contributor that are necessarily infringed by their
|
|
81
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
82
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
83
|
+
institute patent litigation against any entity (including a
|
|
84
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
85
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
86
|
+
or contributory patent infringement, then any patent licenses
|
|
87
|
+
granted to You under this License for that Work shall terminate
|
|
88
|
+
as of the date such litigation is filed.
|
|
89
|
+
|
|
90
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
91
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
92
|
+
modifications, and in Source or Object form, provided that You
|
|
93
|
+
meet the following conditions:
|
|
94
|
+
|
|
95
|
+
(a) You must give any other recipients of the Work or
|
|
96
|
+
Derivative Works a copy of this License; and
|
|
97
|
+
|
|
98
|
+
(b) You must cause any modified files to carry prominent notices
|
|
99
|
+
stating that You changed the files; and
|
|
100
|
+
|
|
101
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
102
|
+
that You distribute, all copyright, patent, trademark, and
|
|
103
|
+
attribution notices from the Source form of the Work,
|
|
104
|
+
excluding those notices that do not pertain to any part of
|
|
105
|
+
the Derivative Works; and
|
|
106
|
+
|
|
107
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
108
|
+
distribution, then any Derivative Works that You distribute must
|
|
109
|
+
include a readable copy of the attribution notices contained
|
|
110
|
+
within such NOTICE file, excluding those notices that do not
|
|
111
|
+
pertain to any part of the Derivative Works, in at least one
|
|
112
|
+
of the following places: within a NOTICE text file distributed
|
|
113
|
+
as part of the Derivative Works; within the Source form or
|
|
114
|
+
documentation, if provided along with the Derivative Works; or,
|
|
115
|
+
within a display generated by the Derivative Works, if and
|
|
116
|
+
wherever such third-party notices normally appear. The contents
|
|
117
|
+
of the NOTICE file are for informational purposes only and
|
|
118
|
+
do not modify the License. You may add Your own attribution
|
|
119
|
+
notices within Derivative Works that You distribute, alongside
|
|
120
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
121
|
+
that such additional attribution notices cannot be construed
|
|
122
|
+
as modifying the License.
|
|
123
|
+
|
|
124
|
+
You may add Your own copyright statement to Your modifications and
|
|
125
|
+
may provide additional or different license terms and conditions
|
|
126
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
127
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
128
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
129
|
+
the conditions stated in this License.
|
|
130
|
+
|
|
131
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
132
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
133
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
134
|
+
this License, without any additional terms or conditions.
|
|
135
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
136
|
+
the terms of any separate license agreement you may have executed
|
|
137
|
+
with Licensor regarding such Contributions.
|
|
138
|
+
|
|
139
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
140
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
141
|
+
except as required for reasonable and customary use in describing the
|
|
142
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
143
|
+
|
|
144
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
145
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
146
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
147
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
148
|
+
implied, including, without limitation, any warranties or conditions
|
|
149
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
150
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
151
|
+
appropriateness of using or redistributing the Work and assume any
|
|
152
|
+
risks associated with Your exercise of permissions under this License.
|
|
153
|
+
|
|
154
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
155
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
156
|
+
unless required by applicable law (such as deliberate and grossly
|
|
157
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
158
|
+
liable to You for damages, including any direct, indirect, special,
|
|
159
|
+
incidental, or consequential damages of any character arising as a
|
|
160
|
+
result of this License or out of the use or inability to use the
|
|
161
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
162
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
163
|
+
other commercial damages or losses), even if such Contributor
|
|
164
|
+
has been advised of the possibility of such damages.
|
|
165
|
+
|
|
166
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
167
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
168
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
169
|
+
or other liability obligations and/or rights consistent with this
|
|
170
|
+
License. However, in accepting such obligations, You may act only
|
|
171
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
172
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
173
|
+
defend, and hold each Contributor harmless for any liability
|
|
174
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
175
|
+
of your accepting any such warranty or additional liability.
|
|
176
|
+
|
|
177
|
+
END OF TERMS AND CONDITIONS
|
|
178
|
+
|
|
179
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
180
|
+
|
|
181
|
+
To apply the Apache License to your work, attach the following
|
|
182
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
183
|
+
replaced with your own identifying information. (Don't include
|
|
184
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
185
|
+
comment syntax for the file format. We also recommend that a
|
|
186
|
+
file or class name and description of purpose be included on the
|
|
187
|
+
same "printed page" as the copyright notice for easier
|
|
188
|
+
identification within third-party archives.
|
|
189
|
+
|
|
190
|
+
Copyright [yyyy] [name of copyright owner]
|
|
191
|
+
|
|
192
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
193
|
+
you may not use this file except in compliance with the License.
|
|
194
|
+
You may obtain a copy of the License at
|
|
195
|
+
|
|
196
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
197
|
+
|
|
198
|
+
Unless required by applicable law or agreed to in writing, software
|
|
199
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
200
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
201
|
+
See the License for the specific language governing permissions and
|
|
202
|
+
limitations under the License.
|
package/dist/app.d.ts
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
//#region src/app.d.ts
|
|
2
|
+
/** The built-in types, `app:<key>`: one plugin like any other, on the public interface alone. */
|
|
3
|
+
export declare const appPlugin: {
|
|
4
|
+
readonly name: "app";
|
|
5
|
+
readonly label: "Built-in";
|
|
6
|
+
readonly steps: {
|
|
7
|
+
readonly [x: string]: import("@protobase/workflows/plugin").StepType;
|
|
8
|
+
};
|
|
9
|
+
readonly triggers: {
|
|
10
|
+
readonly [x: string]: import("@protobase/workflows/plugin").ReadyTrigger;
|
|
11
|
+
};
|
|
12
|
+
};
|
|
13
|
+
//#endregion
|
package/dist/app.js
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { httpSteps } from "./http.js";
|
|
2
|
+
import { mailSteps } from "./mail.js";
|
|
3
|
+
import { pieceSteps } from "./piece.js";
|
|
4
|
+
import { recordTriggers } from "./record-trigger.js";
|
|
5
|
+
import { recordsSteps } from "./records.js";
|
|
6
|
+
import { definePlugin } from "@protobase/workflows/plugin";
|
|
7
|
+
//#region src/app.ts
|
|
8
|
+
/** The built-in types, `app:<key>`: one plugin like any other, on the public interface alone. */
|
|
9
|
+
const appPlugin = definePlugin({
|
|
10
|
+
name: "app",
|
|
11
|
+
label: "Built-in",
|
|
12
|
+
steps: {
|
|
13
|
+
...httpSteps,
|
|
14
|
+
...recordsSteps,
|
|
15
|
+
...mailSteps,
|
|
16
|
+
...pieceSteps
|
|
17
|
+
},
|
|
18
|
+
triggers: { ...recordTriggers }
|
|
19
|
+
});
|
|
20
|
+
//#endregion
|
|
21
|
+
export { appPlugin };
|
package/dist/http.js
ADDED
|
@@ -0,0 +1,151 @@
|
|
|
1
|
+
import { AddressRefusedError, ResponseTooLargeError, StepError, fetchMaxBytes, isFileValue } from "@protobase/workflows/plugin";
|
|
2
|
+
//#region src/http.ts
|
|
3
|
+
/** How much of a failing answer's body its error keeps, after secrets are blanked out of it. */
|
|
4
|
+
const causeBodyLength = 200;
|
|
5
|
+
const methods = [
|
|
6
|
+
"GET",
|
|
7
|
+
"POST",
|
|
8
|
+
"PUT",
|
|
9
|
+
"PATCH",
|
|
10
|
+
"DELETE"
|
|
11
|
+
];
|
|
12
|
+
const textRecord = {
|
|
13
|
+
type: "object",
|
|
14
|
+
additionalProperties: { type: "string" }
|
|
15
|
+
};
|
|
16
|
+
const invalid = (reason) => new StepError("Http.InvalidArguments", reason);
|
|
17
|
+
const checked = (input) => {
|
|
18
|
+
if (typeof input.url !== "string" || !URL.canParse(input.url)) throw invalid("url must be an http or https URL");
|
|
19
|
+
const method = input.method === void 0 ? "GET" : String(input.method).toUpperCase();
|
|
20
|
+
if (!methods.includes(method)) throw invalid(`method must be one of ${methods.join(", ")}`);
|
|
21
|
+
const url = new URL(input.url);
|
|
22
|
+
for (const [name, value] of Object.entries(input.query ?? {})) url.searchParams.append(name, value);
|
|
23
|
+
return {
|
|
24
|
+
url,
|
|
25
|
+
method,
|
|
26
|
+
headers: new Headers(input.headers),
|
|
27
|
+
reading: input.response ?? "json"
|
|
28
|
+
};
|
|
29
|
+
};
|
|
30
|
+
const requestBody = async (given, method, headers, { files }) => {
|
|
31
|
+
if (given === void 0 || method === "GET") return;
|
|
32
|
+
if (isFileValue(given)) {
|
|
33
|
+
const file = await files.read(given, "body");
|
|
34
|
+
if (!headers.has("content-type")) headers.set("content-type", file.type ?? "application/octet-stream");
|
|
35
|
+
return file.bytes;
|
|
36
|
+
}
|
|
37
|
+
if (typeof given === "string") return given;
|
|
38
|
+
if (!headers.has("content-type")) headers.set("content-type", "application/json");
|
|
39
|
+
return JSON.stringify(given);
|
|
40
|
+
};
|
|
41
|
+
const fileName = (url) => {
|
|
42
|
+
const segment = url.pathname.slice(url.pathname.lastIndexOf("/") + 1);
|
|
43
|
+
try {
|
|
44
|
+
return decodeURIComponent(segment);
|
|
45
|
+
} catch (error) {
|
|
46
|
+
if (!(error instanceof URIError)) throw error;
|
|
47
|
+
return segment;
|
|
48
|
+
}
|
|
49
|
+
};
|
|
50
|
+
const answerBody = async (bytes, reading, url, type, { files }) => {
|
|
51
|
+
if (reading === "file") return files.keep(bytes, fileName(url), type?.split(";")[0]?.trim() || "application/octet-stream");
|
|
52
|
+
const text = new TextDecoder().decode(bytes);
|
|
53
|
+
if (reading === "text") return text;
|
|
54
|
+
if (text.trim() === "") return null;
|
|
55
|
+
try {
|
|
56
|
+
return JSON.parse(text);
|
|
57
|
+
} catch (error) {
|
|
58
|
+
if (!(error instanceof SyntaxError)) throw error;
|
|
59
|
+
throw new StepError("Http.InvalidBody", `${url.host} answered with a body that is not JSON; read it with "response": "text"`);
|
|
60
|
+
}
|
|
61
|
+
};
|
|
62
|
+
const loggedUrl = (given) => given.replace(/^([^:/?#]+:\/\/)[^/?#]*@/, "$1").replace(/[?#].*$/, "");
|
|
63
|
+
const send = async (context) => {
|
|
64
|
+
const { input, signal, log, redact } = context;
|
|
65
|
+
const { url, method, headers, reading } = checked(input);
|
|
66
|
+
const body = await requestBody(input.body, method, headers, context);
|
|
67
|
+
const started = Date.now();
|
|
68
|
+
const answer = await context.fetch(url.href, {
|
|
69
|
+
method,
|
|
70
|
+
headers,
|
|
71
|
+
...body !== void 0 && { body },
|
|
72
|
+
signal
|
|
73
|
+
}).catch((error) => {
|
|
74
|
+
if (error instanceof AddressRefusedError) throw new StepError("Http.AddressRefused", error.message);
|
|
75
|
+
if (error instanceof ResponseTooLargeError) throw new StepError("Http.ResponseTooLarge", `The answer's body is larger than ${fetchMaxBytes / 1024 / 1024} MiB, the most the http step reads`);
|
|
76
|
+
if (signal.aborted) throw error;
|
|
77
|
+
throw new StepError("Http.Network", `No answer from ${url.host}: ${error instanceof Error ? error.message : String(error)}`);
|
|
78
|
+
});
|
|
79
|
+
const bytes = new Uint8Array(await answer.arrayBuffer());
|
|
80
|
+
log(`${method} ${loggedUrl(input.url)}: ${answer.status} in ${Date.now() - started} ms`);
|
|
81
|
+
if (answer.status < 200 || answer.status > 299) {
|
|
82
|
+
const excerpt = redact(new TextDecoder().decode(bytes)).slice(0, causeBodyLength);
|
|
83
|
+
throw new StepError("Http.Status", `${url.host} answered ${answer.status}${excerpt ? `: ${excerpt}` : ""}`);
|
|
84
|
+
}
|
|
85
|
+
const kept = Object.fromEntries([...answer.headers].filter(([name]) => name !== "set-cookie"));
|
|
86
|
+
return {
|
|
87
|
+
status: answer.status,
|
|
88
|
+
headers: kept,
|
|
89
|
+
body: await answerBody(bytes, reading, url, answer.headers.get("content-type"), context)
|
|
90
|
+
};
|
|
91
|
+
};
|
|
92
|
+
/**
|
|
93
|
+
* `app:http`: one request to a URL built at run time, through the plugin context's guarded `fetch`, which connects only
|
|
94
|
+
* to a public address it checked, follows no redirect and reads at most 10 MiB of the answer.
|
|
95
|
+
*/
|
|
96
|
+
const httpSteps = { http: {
|
|
97
|
+
label: "HTTP request",
|
|
98
|
+
description: "Sends one HTTP request to a public address and reads the answer.",
|
|
99
|
+
errorPrefix: "Http",
|
|
100
|
+
input: {
|
|
101
|
+
type: "object",
|
|
102
|
+
required: ["url"],
|
|
103
|
+
properties: {
|
|
104
|
+
url: {
|
|
105
|
+
type: "string",
|
|
106
|
+
description: "an http or https URL"
|
|
107
|
+
},
|
|
108
|
+
method: {
|
|
109
|
+
type: "string",
|
|
110
|
+
description: "GET (default), POST, PUT, PATCH or DELETE"
|
|
111
|
+
},
|
|
112
|
+
headers: {
|
|
113
|
+
...textRecord,
|
|
114
|
+
description: "an object of text values"
|
|
115
|
+
},
|
|
116
|
+
query: {
|
|
117
|
+
...textRecord,
|
|
118
|
+
description: "an object of text values, added to the URL's query"
|
|
119
|
+
},
|
|
120
|
+
body: { description: "text sent as it is, a file value sent as its bytes, any other value sent as JSON; a GET sends none" },
|
|
121
|
+
response: {
|
|
122
|
+
enum: [
|
|
123
|
+
"json",
|
|
124
|
+
"text",
|
|
125
|
+
"file"
|
|
126
|
+
],
|
|
127
|
+
description: "how the answer's body is read: json (default; empty is null), text, or file (kept as a file value of the run)"
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
},
|
|
131
|
+
output: {
|
|
132
|
+
type: "object",
|
|
133
|
+
description: "{ status, headers, body }",
|
|
134
|
+
properties: {
|
|
135
|
+
status: { type: "integer" },
|
|
136
|
+
headers: { type: "object" },
|
|
137
|
+
body: {}
|
|
138
|
+
}
|
|
139
|
+
},
|
|
140
|
+
errors: {
|
|
141
|
+
Status: "a status outside 2xx; the cause names it with the start of the body",
|
|
142
|
+
Network: "no answer",
|
|
143
|
+
InvalidArguments: "an argument has the wrong type, named in the cause",
|
|
144
|
+
InvalidBody: "response json, and the body is not JSON",
|
|
145
|
+
AddressRefused: "the host is not a public address",
|
|
146
|
+
ResponseTooLarge: "a body over 10 MiB"
|
|
147
|
+
},
|
|
148
|
+
run: send
|
|
149
|
+
} };
|
|
150
|
+
//#endregion
|
|
151
|
+
export { httpSteps };
|
package/dist/index.d.ts
ADDED
package/dist/index.js
ADDED
package/dist/mail.js
ADDED
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
import { StepError } from "@protobase/workflows/plugin";
|
|
2
|
+
/** The most bytes the attachments of one mail hold together. */
|
|
3
|
+
const attachmentBytes = 10485760;
|
|
4
|
+
const invalid = (reason) => new StepError("Mail.InvalidArguments", reason);
|
|
5
|
+
const line = (value, name) => {
|
|
6
|
+
if (typeof value !== "string" || value.trim() === "") throw invalid(`${name} must be text`);
|
|
7
|
+
if (/[\r\n]/.test(value)) throw invalid(`${name} must be one line`);
|
|
8
|
+
return value;
|
|
9
|
+
};
|
|
10
|
+
const recipients = (value) => {
|
|
11
|
+
const list = Array.isArray(value) ? value : [value];
|
|
12
|
+
if (list.length === 0 || list.length > 50) throw invalid(`to must be one address or a list of 1 to 50`);
|
|
13
|
+
return list.map((address, index) => {
|
|
14
|
+
const name = Array.isArray(value) ? `to[${index}]` : "to";
|
|
15
|
+
if (/[,;]/.test(line(address, name))) throw invalid(`${name} must be one address; give several as a list`);
|
|
16
|
+
return address;
|
|
17
|
+
}).join(", ");
|
|
18
|
+
};
|
|
19
|
+
const attachmentsOf = async (value, { files }) => {
|
|
20
|
+
if (value === void 0 || value === null) return [];
|
|
21
|
+
if (!Array.isArray(value)) throw invalid("attachments must be a list of file values");
|
|
22
|
+
const attachments = [];
|
|
23
|
+
let total = 0;
|
|
24
|
+
for (const [index, item] of value.entries()) {
|
|
25
|
+
const file = await files.read(item, `attachments[${index}]`);
|
|
26
|
+
total += file.bytes.length;
|
|
27
|
+
if (total > attachmentBytes) throw invalid(`attachments are larger than ${attachmentBytes / 1024 / 1024} MiB together`);
|
|
28
|
+
attachments.push({
|
|
29
|
+
filename: file.name,
|
|
30
|
+
content: file.bytes,
|
|
31
|
+
...file.type && { contentType: file.type }
|
|
32
|
+
});
|
|
33
|
+
}
|
|
34
|
+
return attachments;
|
|
35
|
+
};
|
|
36
|
+
const send = async (context) => {
|
|
37
|
+
const { input, mail, idempotencyKey } = context;
|
|
38
|
+
if (!mail) throw new StepError("States.TaskFailed", "The app sends no mail: set its mailer, or PROTOBASE_SMTP_URL and PROTOBASE_MAIL_FROM");
|
|
39
|
+
if (typeof input.text !== "string") throw invalid("text must be text");
|
|
40
|
+
if (input.html !== void 0 && typeof input.html !== "string") throw invalid("html must be text");
|
|
41
|
+
const messageId = `<${idempotencyKey.replace(/[^A-Za-z0-9.-]/g, ".")}@workflows.invalid>`;
|
|
42
|
+
await mail.send({
|
|
43
|
+
to: recipients(input.to),
|
|
44
|
+
subject: line(input.subject, "subject"),
|
|
45
|
+
text: input.text,
|
|
46
|
+
...input.html !== void 0 && { html: input.html },
|
|
47
|
+
headers: { "Message-ID": messageId },
|
|
48
|
+
attachments: await attachmentsOf(input.attachments, context)
|
|
49
|
+
});
|
|
50
|
+
return { message_id: messageId };
|
|
51
|
+
};
|
|
52
|
+
/**
|
|
53
|
+
* `app:mail.send`: one message from the app's own sender; a workflow cannot set the sender. Its Message-ID comes from
|
|
54
|
+
* the step's idempotency key, the same for each try, so a receiver can tell a repeated try from a new mail.
|
|
55
|
+
*/
|
|
56
|
+
const mailSteps = { "mail.send": {
|
|
57
|
+
label: "Send mail",
|
|
58
|
+
description: "Sends a mail from the app's own sender.",
|
|
59
|
+
errorPrefix: "Mail",
|
|
60
|
+
input: {
|
|
61
|
+
type: "object",
|
|
62
|
+
required: [
|
|
63
|
+
"to",
|
|
64
|
+
"subject",
|
|
65
|
+
"text"
|
|
66
|
+
],
|
|
67
|
+
properties: {
|
|
68
|
+
to: { description: `one address, or a list of at most 50` },
|
|
69
|
+
subject: { description: "one line of text" },
|
|
70
|
+
text: { description: "the body as text" },
|
|
71
|
+
html: { description: "the body as HTML too" },
|
|
72
|
+
attachments: { description: "a list of file values the run keeps, at most 10 MiB together" }
|
|
73
|
+
}
|
|
74
|
+
},
|
|
75
|
+
output: {
|
|
76
|
+
type: "object",
|
|
77
|
+
description: "{ message_id }",
|
|
78
|
+
properties: { message_id: { type: "string" } }
|
|
79
|
+
},
|
|
80
|
+
errors: { InvalidArguments: "an argument has the wrong type, named in the cause" },
|
|
81
|
+
run: send
|
|
82
|
+
} };
|
|
83
|
+
//#endregion
|
|
84
|
+
export { mailSteps };
|
package/dist/piece.js
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import { StepError } from "@protobase/workflows/plugin";
|
|
2
|
+
//#region src/piece.ts
|
|
3
|
+
/**
|
|
4
|
+
* `app:piece`, which a definition may name but which runs no piece yet: it fails with `Piece.Error` until a plugin gives
|
|
5
|
+
* connectors a handler that sends through the plugin context's guarded fetch.
|
|
6
|
+
*/
|
|
7
|
+
const pieceSteps = { piece: {
|
|
8
|
+
label: "Connector action",
|
|
9
|
+
description: "Runs an action of a connector. Not implemented yet: a run fails it.",
|
|
10
|
+
errorPrefix: "Piece",
|
|
11
|
+
input: {
|
|
12
|
+
type: "object",
|
|
13
|
+
required: [
|
|
14
|
+
"piece",
|
|
15
|
+
"action",
|
|
16
|
+
"connection"
|
|
17
|
+
],
|
|
18
|
+
properties: {
|
|
19
|
+
piece: { description: "the connector's name" },
|
|
20
|
+
action: { description: "one of its actions" },
|
|
21
|
+
connection: { description: "the connection holding its sign-in" },
|
|
22
|
+
props: { description: "the action's arguments, by name" }
|
|
23
|
+
}
|
|
24
|
+
},
|
|
25
|
+
output: { description: "what the action returns" },
|
|
26
|
+
errors: {
|
|
27
|
+
Error: "always, until connectors are implemented",
|
|
28
|
+
InvalidArguments: "piece, action or connection is missing"
|
|
29
|
+
},
|
|
30
|
+
run: async () => {
|
|
31
|
+
throw new StepError("Piece.Error", "No piece runs in this app yet");
|
|
32
|
+
}
|
|
33
|
+
} };
|
|
34
|
+
//#endregion
|
|
35
|
+
export { pieceSteps };
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
//#region src/record-trigger.ts
|
|
2
|
+
const name = {
|
|
3
|
+
type: "string",
|
|
4
|
+
minLength: 1
|
|
5
|
+
};
|
|
6
|
+
/**
|
|
7
|
+
* `app:record`: one run per record of `resource` that becomes ready, as `when` says, with the record as the run's input.
|
|
8
|
+
* Without `when`, every record is ready once it exists, so each new record starts one run.
|
|
9
|
+
*/
|
|
10
|
+
const recordTriggers = { record: {
|
|
11
|
+
kind: "ready",
|
|
12
|
+
label: "Record ready",
|
|
13
|
+
description: "Starts one run for each record that becomes ready, at once or when a time in it comes, once per ready key.",
|
|
14
|
+
input: {
|
|
15
|
+
type: "object",
|
|
16
|
+
additionalProperties: false,
|
|
17
|
+
required: ["resource"],
|
|
18
|
+
properties: {
|
|
19
|
+
resource: {
|
|
20
|
+
...name,
|
|
21
|
+
description: "the resource's name, as list_resources gives it"
|
|
22
|
+
},
|
|
23
|
+
when: {
|
|
24
|
+
type: "string",
|
|
25
|
+
minLength: 1,
|
|
26
|
+
description: "a filter on indexed fields, such as status = 'sent' and due_at <= now() - 3d; without it every record is ready"
|
|
27
|
+
},
|
|
28
|
+
key: {
|
|
29
|
+
type: "array",
|
|
30
|
+
items: name,
|
|
31
|
+
description: "fields whose new value makes a record ready again, such as due_at"
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
},
|
|
35
|
+
output: {
|
|
36
|
+
type: "object",
|
|
37
|
+
description: "{ resource, key, record }: the record as the workflow's last editor reads it"
|
|
38
|
+
},
|
|
39
|
+
ready: (input) => ({
|
|
40
|
+
resource: input.resource,
|
|
41
|
+
...input.when !== void 0 && { when: input.when },
|
|
42
|
+
...input.key !== void 0 && { key: input.key }
|
|
43
|
+
})
|
|
44
|
+
} };
|
|
45
|
+
//#endregion
|
|
46
|
+
export { recordTriggers };
|
package/dist/records.js
ADDED
|
@@ -0,0 +1,115 @@
|
|
|
1
|
+
import { StepError } from "@protobase/workflows/plugin";
|
|
2
|
+
//#region src/records.ts
|
|
3
|
+
const resource = {
|
|
4
|
+
type: "string",
|
|
5
|
+
minLength: 1,
|
|
6
|
+
description: "the resource's name, as list_resources gives it"
|
|
7
|
+
};
|
|
8
|
+
const key = {
|
|
9
|
+
description: "the record's key",
|
|
10
|
+
anyOf: [
|
|
11
|
+
{ type: "string" },
|
|
12
|
+
{ type: "number" },
|
|
13
|
+
{
|
|
14
|
+
type: "array",
|
|
15
|
+
items: { type: ["string", "number"] }
|
|
16
|
+
}
|
|
17
|
+
]
|
|
18
|
+
};
|
|
19
|
+
const fieldValues = {
|
|
20
|
+
type: "object",
|
|
21
|
+
description: "an object of field values"
|
|
22
|
+
};
|
|
23
|
+
const errors = {
|
|
24
|
+
Refused: "the API refused it, its status and message in the cause",
|
|
25
|
+
InvalidArguments: "an argument has the wrong type, named in the cause"
|
|
26
|
+
};
|
|
27
|
+
const step = (label, description, input, output, run) => ({
|
|
28
|
+
label,
|
|
29
|
+
description,
|
|
30
|
+
errorPrefix: "Records",
|
|
31
|
+
input: {
|
|
32
|
+
type: "object",
|
|
33
|
+
...input
|
|
34
|
+
},
|
|
35
|
+
output: { description: output },
|
|
36
|
+
errors,
|
|
37
|
+
run
|
|
38
|
+
});
|
|
39
|
+
const listOf = (data) => {
|
|
40
|
+
if (!Array.isArray(data)) return;
|
|
41
|
+
return data.map((item, index) => {
|
|
42
|
+
if (typeof item !== "object" || item === null || Array.isArray(item)) throw new StepError("Records.InvalidArguments", `data[${index}] must be an object of field values`);
|
|
43
|
+
return item;
|
|
44
|
+
});
|
|
45
|
+
};
|
|
46
|
+
/**
|
|
47
|
+
* The `app:records.*` steps: create, update, get and list through the app's API as the user who saved the version the
|
|
48
|
+
* run runs, so its access rules, tenant scope, validation and write hooks apply as they do to that user's requests.
|
|
49
|
+
*/
|
|
50
|
+
const recordsSteps = {
|
|
51
|
+
"records.create": step("Create records", "Creates records of the app's resources, as the workflow's last editor.", {
|
|
52
|
+
required: ["resource", "data"],
|
|
53
|
+
properties: {
|
|
54
|
+
resource,
|
|
55
|
+
data: {
|
|
56
|
+
description: "an object of field values, or a list of them, created in one batch: all or none",
|
|
57
|
+
anyOf: [fieldValues, {
|
|
58
|
+
type: "array",
|
|
59
|
+
items: fieldValues
|
|
60
|
+
}]
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
}, "the created record, or the list of them", async ({ input, records }) => {
|
|
64
|
+
const many = listOf(input.data);
|
|
65
|
+
if (many) return records.createMany(input.resource, many);
|
|
66
|
+
return records.create(input.resource, input.data);
|
|
67
|
+
}),
|
|
68
|
+
"records.update": step("Update a record", "Updates one record, as the workflow's last editor.", {
|
|
69
|
+
required: [
|
|
70
|
+
"resource",
|
|
71
|
+
"key",
|
|
72
|
+
"data"
|
|
73
|
+
],
|
|
74
|
+
properties: {
|
|
75
|
+
resource,
|
|
76
|
+
key,
|
|
77
|
+
data: {
|
|
78
|
+
...fieldValues,
|
|
79
|
+
description: "an object of the field values to change"
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
}, "the updated record", async ({ input, records }) => records.update(input.resource, input.key, input.data)),
|
|
83
|
+
"records.get": step("Get a record", "Reads one record, as the workflow's last editor.", {
|
|
84
|
+
required: ["resource", "key"],
|
|
85
|
+
properties: {
|
|
86
|
+
resource,
|
|
87
|
+
key
|
|
88
|
+
}
|
|
89
|
+
}, "the record", async ({ input, records }) => records.get(input.resource, input.key)),
|
|
90
|
+
"records.list": step("List records", "Lists records, as the workflow's last editor.", {
|
|
91
|
+
required: ["resource"],
|
|
92
|
+
properties: {
|
|
93
|
+
resource,
|
|
94
|
+
filter: {
|
|
95
|
+
type: "string",
|
|
96
|
+
description: "a filter in the API's syntax, such as status = \"open\""
|
|
97
|
+
},
|
|
98
|
+
order_by: {
|
|
99
|
+
type: "string",
|
|
100
|
+
description: "such as \"created_at desc\""
|
|
101
|
+
},
|
|
102
|
+
limit: {
|
|
103
|
+
type: "integer",
|
|
104
|
+
minimum: 1,
|
|
105
|
+
description: "how many at most"
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
}, "{ records }", async ({ input, records }) => ({ records: await records.list(input.resource, {
|
|
109
|
+
...input.filter !== void 0 && { filter: input.filter },
|
|
110
|
+
...input.order_by !== void 0 && { order_by: input.order_by },
|
|
111
|
+
...input.limit !== void 0 && { limit: input.limit }
|
|
112
|
+
}) }))
|
|
113
|
+
};
|
|
114
|
+
//#endregion
|
|
115
|
+
export { recordsSteps };
|
package/package.json
CHANGED
|
@@ -1,6 +1,34 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@protobase/workflow-plugins",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.68.1",
|
|
4
|
+
"description": "The built-in workflow trigger and step types, written on the public plugin interface like any other plugin.",
|
|
5
|
+
"license": "Apache-2.0",
|
|
6
|
+
"repository": {
|
|
7
|
+
"type": "git",
|
|
8
|
+
"url": "git+https://github.com/PrototeamHQ/protobase.git",
|
|
9
|
+
"directory": "packages/workflow-plugins"
|
|
10
|
+
},
|
|
11
|
+
"homepage": "https://docs.protobase.net",
|
|
12
|
+
"type": "module",
|
|
13
|
+
"sideEffects": false,
|
|
14
|
+
"engines": {
|
|
15
|
+
"node": ">=22.12.0"
|
|
16
|
+
},
|
|
17
|
+
"exports": {
|
|
18
|
+
".": "./dist/index.js"
|
|
19
|
+
},
|
|
20
|
+
"types": "./dist/index.d.ts",
|
|
21
|
+
"files": [
|
|
22
|
+
"dist"
|
|
23
|
+
],
|
|
24
|
+
"scripts": {
|
|
25
|
+
"build": "tsdown",
|
|
26
|
+
"prepack": "tsdown"
|
|
27
|
+
},
|
|
28
|
+
"publishConfig": {
|
|
29
|
+
"access": "public"
|
|
30
|
+
},
|
|
31
|
+
"dependencies": {
|
|
32
|
+
"@protobase/workflows": "0.68.1"
|
|
33
|
+
}
|
|
34
|
+
}
|
package/README.md
DELETED