Knative-compatible email link function used with the Constructive jobs system. It is designed to be invoked by @constructive-io/knative-job-worker as an HTTP function named send-verification-link.
The function:
- Reads metadata about the tenant/site from a GraphQL API
- Generates a styled HTML email using
@launchql/mjml - Sends the email via
@launchql/postmaster - Supports invite, password reset, and email verification flows
Jobs should use task_identifier = 'email:send_verification_link' and a JSON payload like:
{
"email_type": "invite_email",
"email": "[email protected]",
"invite_token": "abc123",
"sender_id": "00000000-0000-0000-0000-000000000001"
}Supported email_type values and parameters:
invite_emailemail(string, required)invite_token(string, required)sender_id(UUID string, required)
forgot_passwordemail(string, required)user_id(UUID string, required)reset_token(string, required)
email_verificationemail(string, required)email_id(UUID string, required)verification_token(string, required)
If required fields are missing the function returns a small JSON object like:
{ "missing": "email_type" }The function is wrapped by @constructive-io/knative-job-fn, so it expects:
- HTTP method:
POST - Body: JSON job payload (see above)
- Headers (set by
@constructive-io/knative-job-worker):X-Worker-IdX-Job-IdX-Database-IdX-Callback-Url
The handler will:
- Resolve the tenant/site by
databaseIdvia GraphQL - Generate an email link and HTML via
@launchql/mjml - Send the email with
@launchql/postmaster - Respond with HTTP 200 and JSON:
{ "complete": true }Errors are propagated through the Express error middleware installed by @constructive-io/knative-job-fn, so they can be translated into X-Job-Error callbacks by your gateway/callback server.
Required:
GRAPHQL_URL
GraphQL endpoint for the tenant database (forGetUserand/or per-tenant data).
Recommended / optional:
META_GRAPHQL_URL
GraphQL endpoint for meta/database-level schema. Defaults toGRAPHQL_URLwhen not set.GRAPHQL_AUTH_TOKEN
Bearer token to send asAuthorizationheader for GraphQL requests.DEFAULT_DATABASE_ID
Used ifX-Database-Idis not provided by the worker. In normal jobs usage,X-Database-Idshould always be present.LOCAL_APP_PORT
Optional port suffix for localhost-style hosts (e.g.3000). When the resolved hostname islocalhost/*.localhostandSEND_VERIFICATION_LINK_DRY_RUN=true, links are generated ashttp://localhost:LOCAL_APP_PORT/.... Ignored for non-local hostnames and in production.
Email delivery (default: @launchql/postmaster):
-
Set
EMAIL_SEND_USE_SMTP=trueto switch tosimple-smtp-server(SMTP). Otherwise it uses@launchql/postmaster. -
Mailgun or another provider; consult
@launchql/postmasterdocs. A common pattern is:MAILGUN_API_KEYMAILGUN_DOMAINMAILGUN_FROM
-
SMTP variables when
EMAIL_SEND_USE_SMTP=true:SMTP_HOSTSMTP_PORTSMTP_USERSMTP_PASSSMTP_FROM
From the repo root:
pnpm --filter="@constructive-io/send-verification-link-fn" buildThis compiles TypeScript into dist/.
The function is intended to be containerized and run as a Knative Service. A minimal Dockerfile:
FROM node:18-alpine
WORKDIR /usr/src/app
# Install production dependencies
COPY package.json pnpm-lock.yaml ./
RUN npm install -g pnpm@9 && pnpm install --prod
# Copy compiled code
COPY dist ./dist
ENV NODE_ENV=production
ENV PORT=8080
CMD ["node", "dist/index.js"]Build and push:
pnpm --filter="@constructive-io/send-verification-link-fn" build
docker build -t your-registry/send-verification-link-fn:latest functions/send-verification-link
docker push your-registry/send-verification-link-fn:latestapiVersion: serving.knative.dev/v1
kind: Service
metadata:
name: send-verification-link
namespace: default
spec:
template:
spec:
containers:
- image: your-registry/send-verification-link-fn:latest
env:
- name: GRAPHQL_URL
value: "https://api.your-domain.com/graphql"
- name: META_GRAPHQL_URL
value: "https://meta-api.your-domain.com/graphql"
- name: GRAPHQL_AUTH_TOKEN
valueFrom:
secretKeyRef:
name: graphql-auth
key: token
# MAILGUN / Postmaster config here...
- name: MAILGUN_API_KEY
valueFrom:
secretKeyRef:
name: mailgun
key: api-keyOnce deployed, point @constructive-io/knative-job-worker at this service by configuring:
KNATIVE_SERVICE_URLto route/send-verification-linkto this functionJOBS_SUPPORTED=send-verification-link(orJOBS_SUPPORT_ANY=true)