@constructive-io/simple-email-fn 0.3.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 +23 -0
- package/README.md +152 -0
- package/index.d.ts +2 -0
- package/index.js +76 -0
- package/package.json +43 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
The MIT License (MIT)
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 Dan Lynch <pyramation@gmail.com>
|
|
4
|
+
Copyright (c) 2025 Constructive <developers@constructive.io>
|
|
5
|
+
Copyright (c) 2020-present, Interweb, Inc.
|
|
6
|
+
|
|
7
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
8
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
9
|
+
in the Software without restriction, including without limitation the rights
|
|
10
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
11
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
12
|
+
furnished to do so, subject to the following conditions:
|
|
13
|
+
|
|
14
|
+
The above copyright notice and this permission notice shall be included in all
|
|
15
|
+
copies or substantial portions of the Software.
|
|
16
|
+
|
|
17
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
18
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
19
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
20
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
21
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
22
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
23
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,152 @@
|
|
|
1
|
+
# @constructive-io/simple-email-fn
|
|
2
|
+
|
|
3
|
+
Simple Knative-compatible email function used with the Constructive jobs system.
|
|
4
|
+
|
|
5
|
+
This function is intentionally minimal: it reads an email payload from the job
|
|
6
|
+
body and **logs it only** in dry‑run mode. When not in dry‑run, it sends via
|
|
7
|
+
the configured email provider. This is useful while wiring up jobs and Knative
|
|
8
|
+
without needing a real mail provider configured.
|
|
9
|
+
|
|
10
|
+
## Expected job payload
|
|
11
|
+
|
|
12
|
+
Jobs should use `task_identifier = 'simple-email'` (or whatever route you
|
|
13
|
+
configure at your Knative gateway) and a JSON payload like:
|
|
14
|
+
|
|
15
|
+
```json
|
|
16
|
+
{
|
|
17
|
+
"to": "user@example.com",
|
|
18
|
+
"subject": "Welcome!",
|
|
19
|
+
"html": "<p>Welcome to our app</p>"
|
|
20
|
+
}
|
|
21
|
+
```
|
|
22
|
+
|
|
23
|
+
Supported fields:
|
|
24
|
+
|
|
25
|
+
- `to` (string, required)
|
|
26
|
+
- `subject` (string, required)
|
|
27
|
+
- `html` (string, optional)
|
|
28
|
+
- `text` (string, optional)
|
|
29
|
+
- `from` (string, optional)
|
|
30
|
+
- `replyTo` (string, optional)
|
|
31
|
+
|
|
32
|
+
At least one of `html` or `text` must be provided. If required fields are
|
|
33
|
+
missing, the function throws and the error is propagated via the
|
|
34
|
+
`@constructive-io/knative-job-fn` wrapper as a job error.
|
|
35
|
+
|
|
36
|
+
## HTTP contract (with knative-job-worker)
|
|
37
|
+
|
|
38
|
+
The function is wrapped by `@constructive-io/knative-job-fn`, so it expects:
|
|
39
|
+
|
|
40
|
+
- HTTP method: `POST`
|
|
41
|
+
- Body: JSON job payload (see above)
|
|
42
|
+
- Headers (set by `@constructive-io/knative-job-worker`):
|
|
43
|
+
- `X-Worker-Id`
|
|
44
|
+
- `X-Job-Id`
|
|
45
|
+
- `X-Database-Id`
|
|
46
|
+
- `X-Callback-Url`
|
|
47
|
+
|
|
48
|
+
The handler:
|
|
49
|
+
|
|
50
|
+
1. Reads the email data directly from the request body.
|
|
51
|
+
2. Logs the email metadata (to/subject/from, and whether html/text are present)
|
|
52
|
+
and the full payload.
|
|
53
|
+
3. Responds with HTTP 200 and:
|
|
54
|
+
|
|
55
|
+
```json
|
|
56
|
+
{ "complete": true }
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
Errors bubble into the error middleware installed by
|
|
60
|
+
`@constructive-io/knative-job-fn`, so they are translated into an `X-Job-Error`
|
|
61
|
+
callback for the worker.
|
|
62
|
+
|
|
63
|
+
## Environment variables
|
|
64
|
+
|
|
65
|
+
Email provider configuration is only required when not running in dry‑run mode.
|
|
66
|
+
|
|
67
|
+
Optional:
|
|
68
|
+
|
|
69
|
+
- `SIMPLE_EMAIL_DRY_RUN` (`true`/`false`): log only, skip send.
|
|
70
|
+
- `EMAIL_SEND_USE_SMTP` (`true`/`false`): use SMTP (`simple-smtp-server`).
|
|
71
|
+
|
|
72
|
+
Mailgun (`@launchql/postmaster`) env vars when `EMAIL_SEND_USE_SMTP` is false:
|
|
73
|
+
|
|
74
|
+
- `MAILGUN_API_KEY`
|
|
75
|
+
- `MAILGUN_DOMAIN`
|
|
76
|
+
- `MAILGUN_FROM`
|
|
77
|
+
|
|
78
|
+
SMTP env vars when `EMAIL_SEND_USE_SMTP` is true:
|
|
79
|
+
|
|
80
|
+
- `SMTP_HOST`
|
|
81
|
+
- `SMTP_PORT`
|
|
82
|
+
- `SMTP_USER`
|
|
83
|
+
- `SMTP_PASS`
|
|
84
|
+
- `SMTP_FROM`
|
|
85
|
+
|
|
86
|
+
## Building locally
|
|
87
|
+
|
|
88
|
+
From the repo root:
|
|
89
|
+
|
|
90
|
+
```bash
|
|
91
|
+
pnpm --filter="@constructive-io/simple-email-fn" build
|
|
92
|
+
```
|
|
93
|
+
|
|
94
|
+
This compiles TypeScript into `dist/`.
|
|
95
|
+
|
|
96
|
+
---
|
|
97
|
+
|
|
98
|
+
## Education and Tutorials
|
|
99
|
+
|
|
100
|
+
1. 🚀 [Quickstart: Getting Up and Running](https://constructive.io/learn/quickstart)
|
|
101
|
+
Get started with modular databases in minutes. Install prerequisites and deploy your first module.
|
|
102
|
+
|
|
103
|
+
2. 📦 [Modular PostgreSQL Development with Database Packages](https://constructive.io/learn/modular-postgres)
|
|
104
|
+
Learn to organize PostgreSQL projects with pgpm workspaces and reusable database modules.
|
|
105
|
+
|
|
106
|
+
3. ✏️ [Authoring Database Changes](https://constructive.io/learn/authoring-database-changes)
|
|
107
|
+
Master the workflow for adding, organizing, and managing database changes with pgpm.
|
|
108
|
+
|
|
109
|
+
4. 🧪 [End-to-End PostgreSQL Testing with TypeScript](https://constructive.io/learn/e2e-postgres-testing)
|
|
110
|
+
Master end-to-end PostgreSQL testing with ephemeral databases, RLS testing, and CI/CD automation.
|
|
111
|
+
|
|
112
|
+
5. ⚡ [Supabase Testing](https://constructive.io/learn/supabase)
|
|
113
|
+
Use TypeScript-first tools to test Supabase projects with realistic RLS, policies, and auth contexts.
|
|
114
|
+
|
|
115
|
+
6. 💧 [Drizzle ORM Testing](https://constructive.io/learn/drizzle-testing)
|
|
116
|
+
Run full-stack tests with Drizzle ORM, including database setup, teardown, and RLS enforcement.
|
|
117
|
+
|
|
118
|
+
7. 🔧 [Troubleshooting](https://constructive.io/learn/troubleshooting)
|
|
119
|
+
Common issues and solutions for pgpm, PostgreSQL, and testing.
|
|
120
|
+
|
|
121
|
+
## Related Constructive Tooling
|
|
122
|
+
|
|
123
|
+
### 📦 Package Management
|
|
124
|
+
|
|
125
|
+
* [pgpm](https://github.com/constructive-io/constructive/tree/main/pgpm/pgpm): **🖥️ PostgreSQL Package Manager** for modular Postgres development. Works with database workspaces, scaffolding, migrations, seeding, and installing database packages.
|
|
126
|
+
|
|
127
|
+
### 🧪 Testing
|
|
128
|
+
|
|
129
|
+
* [pgsql-test](https://github.com/constructive-io/constructive/tree/main/postgres/pgsql-test): **📊 Isolated testing environments** with per-test transaction rollbacks—ideal for integration tests, complex migrations, and RLS simulation.
|
|
130
|
+
* [pgsql-seed](https://github.com/constructive-io/constructive/tree/main/postgres/pgsql-seed): **🌱 PostgreSQL seeding utilities** for CSV, JSON, SQL data loading, and pgpm deployment.
|
|
131
|
+
* [supabase-test](https://github.com/constructive-io/constructive/tree/main/postgres/supabase-test): **🧪 Supabase-native test harness** preconfigured for the local Supabase stack—per-test rollbacks, JWT/role context helpers, and CI/GitHub Actions ready.
|
|
132
|
+
* [graphile-test](https://github.com/constructive-io/constructive/tree/main/graphile/graphile-test): **🔐 Authentication mocking** for Graphile-focused test helpers and emulating row-level security contexts.
|
|
133
|
+
* [pg-query-context](https://github.com/constructive-io/constructive/tree/main/postgres/pg-query-context): **🔒 Session context injection** to add session-local context (e.g., `SET LOCAL`) into queries—ideal for setting `role`, `jwt.claims`, and other session settings.
|
|
134
|
+
|
|
135
|
+
### 🧠 Parsing & AST
|
|
136
|
+
|
|
137
|
+
* [pgsql-parser](https://www.npmjs.com/package/pgsql-parser): **🔄 SQL conversion engine** that interprets and converts PostgreSQL syntax.
|
|
138
|
+
* [libpg-query-node](https://www.npmjs.com/package/libpg-query): **🌉 Node.js bindings** for `libpg_query`, converting SQL into parse trees.
|
|
139
|
+
* [pg-proto-parser](https://www.npmjs.com/package/pg-proto-parser): **📦 Protobuf parser** for parsing PostgreSQL Protocol Buffers definitions to generate TypeScript interfaces, utility functions, and JSON mappings for enums.
|
|
140
|
+
* [@pgsql/enums](https://www.npmjs.com/package/@pgsql/enums): **🏷️ TypeScript enums** for PostgreSQL AST for safe and ergonomic parsing logic.
|
|
141
|
+
* [@pgsql/types](https://www.npmjs.com/package/@pgsql/types): **📝 Type definitions** for PostgreSQL AST nodes in TypeScript.
|
|
142
|
+
* [@pgsql/utils](https://www.npmjs.com/package/@pgsql/utils): **🛠️ AST utilities** for constructing and transforming PostgreSQL syntax trees.
|
|
143
|
+
|
|
144
|
+
## Credits
|
|
145
|
+
|
|
146
|
+
**🛠 Built by the [Constructive](https://constructive.io) team — creators of modular Postgres tooling for secure, composable backends. If you like our work, contribute on [GitHub](https://github.com/constructive-io).**
|
|
147
|
+
|
|
148
|
+
## Disclaimer
|
|
149
|
+
|
|
150
|
+
AS DESCRIBED IN THE LICENSES, THE SOFTWARE IS PROVIDED "AS IS", AT YOUR OWN RISK, AND WITHOUT WARRANTIES OF ANY KIND.
|
|
151
|
+
|
|
152
|
+
No developer or entity involved in creating this software will be liable for any claims or damages whatsoever associated with your use, inability to use, or your interaction with other users of the code, including any direct, indirect, incidental, special, exemplary, punitive or consequential damages, or loss of profits, cryptocurrencies, tokens, or anything else of value.
|
package/index.d.ts
ADDED
package/index.js
ADDED
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
import { createJobApp } from '@constructive-io/knative-job-fn';
|
|
2
|
+
import { send as sendSmtp } from 'simple-smtp-server';
|
|
3
|
+
import { send as sendPostmaster } from '@launchql/postmaster';
|
|
4
|
+
import { parseEnvBoolean } from '@pgpmjs/env';
|
|
5
|
+
import { createLogger } from '@pgpmjs/logger';
|
|
6
|
+
const isNonEmptyString = (value) => typeof value === 'string' && value.trim().length > 0;
|
|
7
|
+
const getRequiredField = (payload, field) => {
|
|
8
|
+
const value = payload[field];
|
|
9
|
+
if (!isNonEmptyString(value)) {
|
|
10
|
+
throw new Error(`Missing required field '${String(field)}'`);
|
|
11
|
+
}
|
|
12
|
+
return value;
|
|
13
|
+
};
|
|
14
|
+
const isDryRun = parseEnvBoolean(process.env.SIMPLE_EMAIL_DRY_RUN) ?? false;
|
|
15
|
+
const useSmtp = parseEnvBoolean(process.env.EMAIL_SEND_USE_SMTP) ?? false;
|
|
16
|
+
const logger = createLogger('simple-email');
|
|
17
|
+
const app = createJobApp();
|
|
18
|
+
app.post('/', async (req, res, next) => {
|
|
19
|
+
try {
|
|
20
|
+
const payload = (req.body || {});
|
|
21
|
+
const to = getRequiredField(payload, 'to');
|
|
22
|
+
const subject = getRequiredField(payload, 'subject');
|
|
23
|
+
const html = isNonEmptyString(payload.html) ? payload.html : undefined;
|
|
24
|
+
const text = isNonEmptyString(payload.text) ? payload.text : undefined;
|
|
25
|
+
if (!html && !text) {
|
|
26
|
+
throw new Error("Either 'html' or 'text' must be provided");
|
|
27
|
+
}
|
|
28
|
+
const fromEnv = useSmtp ? process.env.SMTP_FROM : process.env.MAILGUN_FROM;
|
|
29
|
+
const from = isNonEmptyString(payload.from)
|
|
30
|
+
? payload.from
|
|
31
|
+
: isNonEmptyString(fromEnv)
|
|
32
|
+
? fromEnv
|
|
33
|
+
: undefined;
|
|
34
|
+
const replyTo = isNonEmptyString(payload.replyTo)
|
|
35
|
+
? payload.replyTo
|
|
36
|
+
: undefined;
|
|
37
|
+
const logContext = {
|
|
38
|
+
to,
|
|
39
|
+
subject,
|
|
40
|
+
from,
|
|
41
|
+
replyTo,
|
|
42
|
+
hasHtml: Boolean(html),
|
|
43
|
+
hasText: Boolean(text)
|
|
44
|
+
};
|
|
45
|
+
if (isDryRun) {
|
|
46
|
+
logger.info('DRY RUN email (no send)', logContext);
|
|
47
|
+
}
|
|
48
|
+
else {
|
|
49
|
+
// Send via the Postmaster package (Mailgun or configured provider)
|
|
50
|
+
const sendEmail = useSmtp ? sendSmtp : sendPostmaster;
|
|
51
|
+
await sendEmail({
|
|
52
|
+
to,
|
|
53
|
+
subject,
|
|
54
|
+
...(html && { html }),
|
|
55
|
+
...(text && { text }),
|
|
56
|
+
...(from && { from }),
|
|
57
|
+
...(replyTo && { replyTo })
|
|
58
|
+
});
|
|
59
|
+
logger.info('Sent email', logContext);
|
|
60
|
+
}
|
|
61
|
+
res.status(200).json({ complete: true });
|
|
62
|
+
}
|
|
63
|
+
catch (err) {
|
|
64
|
+
next(err);
|
|
65
|
+
}
|
|
66
|
+
});
|
|
67
|
+
export default app;
|
|
68
|
+
// When executed directly (e.g. `node dist/index.js` in Knative),
|
|
69
|
+
// start an HTTP server on the provided PORT (default 8080).
|
|
70
|
+
if (require.main === module) {
|
|
71
|
+
const port = Number(process.env.PORT ?? 8080);
|
|
72
|
+
// @constructive-io/knative-job-fn exposes a .listen method that delegates to the underlying Express app
|
|
73
|
+
app.listen(port, () => {
|
|
74
|
+
logger.info(`listening on port ${port}`);
|
|
75
|
+
});
|
|
76
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@constructive-io/simple-email-fn",
|
|
3
|
+
"version": "0.3.1",
|
|
4
|
+
"description": "Simple Knative email function that sends emails directly from job payload",
|
|
5
|
+
"author": "Constructive <developers@constructive.io>",
|
|
6
|
+
"homepage": "https://github.com/constructive-io/constructive",
|
|
7
|
+
"license": "MIT",
|
|
8
|
+
"main": "index.js",
|
|
9
|
+
"module": "esm/index.js",
|
|
10
|
+
"types": "index.d.ts",
|
|
11
|
+
"publishConfig": {
|
|
12
|
+
"access": "public",
|
|
13
|
+
"directory": "dist"
|
|
14
|
+
},
|
|
15
|
+
"repository": {
|
|
16
|
+
"type": "git",
|
|
17
|
+
"url": "https://github.com/constructive-io/constructive"
|
|
18
|
+
},
|
|
19
|
+
"bugs": {
|
|
20
|
+
"url": "https://github.com/constructive-io/constructive/issues"
|
|
21
|
+
},
|
|
22
|
+
"directories": {
|
|
23
|
+
"lib": "src",
|
|
24
|
+
"test": "__tests__"
|
|
25
|
+
},
|
|
26
|
+
"scripts": {
|
|
27
|
+
"clean": "makage clean",
|
|
28
|
+
"prepack": "npm run build",
|
|
29
|
+
"build": "makage build",
|
|
30
|
+
"build:dev": "makage build --dev"
|
|
31
|
+
},
|
|
32
|
+
"devDependencies": {
|
|
33
|
+
"makage": "^0.1.10"
|
|
34
|
+
},
|
|
35
|
+
"dependencies": {
|
|
36
|
+
"@constructive-io/knative-job-fn": "^0.3.1",
|
|
37
|
+
"@launchql/postmaster": "0.1.4",
|
|
38
|
+
"@pgpmjs/env": "^2.10.0",
|
|
39
|
+
"@pgpmjs/logger": "^1.4.0",
|
|
40
|
+
"simple-smtp-server": "^0.2.0"
|
|
41
|
+
},
|
|
42
|
+
"gitHead": "3ffd5718e86ea5fa9ca6e0930aeb510cf392f343"
|
|
43
|
+
}
|