@hogsend/engine 0.0.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 +93 -0
- package/README.md +18 -0
- package/package.json +58 -0
- package/src/app.ts +102 -0
- package/src/container.ts +172 -0
- package/src/env.ts +56 -0
- package/src/index.ts +114 -0
- package/src/journeys/define-journey.ts +188 -0
- package/src/journeys/journey-context.ts +179 -0
- package/src/journeys/registry-singleton.ts +21 -0
- package/src/journeys/registry.ts +53 -0
- package/src/lib/alerting.ts +205 -0
- package/src/lib/api-key-hash.ts +19 -0
- package/src/lib/auth.ts +39 -0
- package/src/lib/backfill.ts +84 -0
- package/src/lib/contacts.ts +68 -0
- package/src/lib/db.ts +13 -0
- package/src/lib/email-service-types.ts +115 -0
- package/src/lib/email-stats.ts +33 -0
- package/src/lib/email.ts +94 -0
- package/src/lib/enrollment-guards.ts +56 -0
- package/src/lib/hatchet.ts +20 -0
- package/src/lib/html.ts +25 -0
- package/src/lib/ingestion.ts +162 -0
- package/src/lib/logger.ts +32 -0
- package/src/lib/mailer.ts +266 -0
- package/src/lib/notifications.ts +61 -0
- package/src/lib/posthog.ts +19 -0
- package/src/lib/redis.ts +30 -0
- package/src/lib/schemas.ts +8 -0
- package/src/lib/tracked.ts +175 -0
- package/src/lib/tracking-event-names.ts +5 -0
- package/src/lib/tracking-events.ts +84 -0
- package/src/lib/tracking.ts +78 -0
- package/src/middleware/api-key.ts +129 -0
- package/src/middleware/audit.ts +47 -0
- package/src/middleware/auth.ts +24 -0
- package/src/middleware/error-handler.ts +22 -0
- package/src/middleware/rate-limit.ts +65 -0
- package/src/middleware/request-logger.ts +19 -0
- package/src/routes/admin/alerts.ts +347 -0
- package/src/routes/admin/api-keys.ts +211 -0
- package/src/routes/admin/audit-logs.ts +102 -0
- package/src/routes/admin/bulk.ts +503 -0
- package/src/routes/admin/contacts.ts +342 -0
- package/src/routes/admin/dlq.ts +202 -0
- package/src/routes/admin/emails.ts +269 -0
- package/src/routes/admin/events.ts +132 -0
- package/src/routes/admin/index.ts +36 -0
- package/src/routes/admin/journey-logs.ts +117 -0
- package/src/routes/admin/journeys.ts +677 -0
- package/src/routes/admin/metrics.ts +559 -0
- package/src/routes/admin/preferences.ts +165 -0
- package/src/routes/admin/timeline.ts +221 -0
- package/src/routes/email/index.ts +8 -0
- package/src/routes/email/preferences.ts +144 -0
- package/src/routes/email/unsubscribe.ts +161 -0
- package/src/routes/health.ts +131 -0
- package/src/routes/index.ts +32 -0
- package/src/routes/ingest.ts +71 -0
- package/src/routes/tracking/click.ts +103 -0
- package/src/routes/tracking/index.ts +9 -0
- package/src/routes/tracking/open.ts +71 -0
- package/src/routes/webhooks/index.ts +17 -0
- package/src/routes/webhooks/resend.ts +68 -0
- package/src/routes/webhooks/sources.ts +97 -0
- package/src/webhook-sources/define-webhook-source.ts +34 -0
- package/src/worker.ts +64 -0
- package/src/workflows/check-alerts.ts +24 -0
- package/src/workflows/import-contacts.ts +134 -0
- package/src/workflows/send-email.ts +54 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
Elastic License 2.0 (ELv2)
|
|
2
|
+
|
|
3
|
+
Copyright 2025 Doug Silkstone and Hogsend contributors
|
|
4
|
+
|
|
5
|
+
## Acceptance
|
|
6
|
+
|
|
7
|
+
By using the software, you agree to all of the terms and conditions below.
|
|
8
|
+
|
|
9
|
+
## Copyright License
|
|
10
|
+
|
|
11
|
+
The licensor grants you a non-exclusive, royalty-free, worldwide,
|
|
12
|
+
non-sublicensable, non-transferable license to use, copy, distribute, make
|
|
13
|
+
available, and prepare derivative works of the software, in each case subject to
|
|
14
|
+
the limitations and conditions below.
|
|
15
|
+
|
|
16
|
+
## Limitations
|
|
17
|
+
|
|
18
|
+
You may not provide the software to third parties as a hosted or managed
|
|
19
|
+
service, where the service provides users with access to any substantial set of
|
|
20
|
+
the features or functionality of the software.
|
|
21
|
+
|
|
22
|
+
You may not move, change, disable, or circumvent the license key functionality
|
|
23
|
+
in the software, and you may not remove or obscure any functionality in the
|
|
24
|
+
software that is protected by the license key.
|
|
25
|
+
|
|
26
|
+
You may not alter, remove, or obscure any licensing, copyright, or other notices
|
|
27
|
+
of the licensor in the software. Any use of the licensor's trademarks is subject
|
|
28
|
+
to applicable law.
|
|
29
|
+
|
|
30
|
+
## Patents
|
|
31
|
+
|
|
32
|
+
The licensor grants you a license, under any patent claims the licensor can
|
|
33
|
+
license, or becomes able to license, to make, have made, use, sell, offer for
|
|
34
|
+
sale, import and have imported the software, in each case subject to the
|
|
35
|
+
limitations and conditions in this license. This license does not cover any
|
|
36
|
+
patent claims that you cause to be infringed by modifications or additions to
|
|
37
|
+
the software. If you or your company make any written claim that the software
|
|
38
|
+
infringes or contributes to infringement of any patent, your patent license for
|
|
39
|
+
the software granted under these terms ends immediately. If your company makes
|
|
40
|
+
such a claim, your patent license ends immediately for work on behalf of your
|
|
41
|
+
company.
|
|
42
|
+
|
|
43
|
+
## Notices
|
|
44
|
+
|
|
45
|
+
You must ensure that anyone who gets a copy of any part of the software from you
|
|
46
|
+
also gets a copy of these terms.
|
|
47
|
+
|
|
48
|
+
If you modify the software, you must include in any modified copies of the
|
|
49
|
+
software prominent notices stating that you have modified the software.
|
|
50
|
+
|
|
51
|
+
## No Other Rights
|
|
52
|
+
|
|
53
|
+
These terms do not imply any licenses other than those expressly granted in
|
|
54
|
+
these terms.
|
|
55
|
+
|
|
56
|
+
## Termination
|
|
57
|
+
|
|
58
|
+
If you use the software in violation of these terms, such use is not licensed,
|
|
59
|
+
and your licenses will automatically terminate. If the licensor provides you
|
|
60
|
+
with a notice of your violation, and you cease all violation of this license no
|
|
61
|
+
later than 30 days after you receive that notice, your licenses will be
|
|
62
|
+
reinstated retroactively. However, if you violate these terms after such
|
|
63
|
+
reinstatement, any additional violation of these terms will cause your licenses
|
|
64
|
+
to terminate automatically and permanently.
|
|
65
|
+
|
|
66
|
+
## No Liability
|
|
67
|
+
|
|
68
|
+
*As far as the law allows, the software comes as is, without any warranty or
|
|
69
|
+
condition, and the licensor will not be liable to you for any damages arising out
|
|
70
|
+
of these terms or the use or nature of the software, under any kind of legal
|
|
71
|
+
claim.*
|
|
72
|
+
|
|
73
|
+
## Definitions
|
|
74
|
+
|
|
75
|
+
The **licensor** is the entity offering these terms, and the **software** is the
|
|
76
|
+
software the licensor makes available under these terms, including any portion
|
|
77
|
+
of it.
|
|
78
|
+
|
|
79
|
+
**you** refers to the individual or entity agreeing to these terms.
|
|
80
|
+
|
|
81
|
+
**your company** is any legal entity, sole proprietorship, or other kind of
|
|
82
|
+
organization that you work for, plus all organizations that have control over,
|
|
83
|
+
are under the control of, or are under common control with that organization.
|
|
84
|
+
**control** means ownership of substantially all the assets of an entity, or the
|
|
85
|
+
power to direct its management and policies by vote, contract, or otherwise.
|
|
86
|
+
Control can be direct or indirect.
|
|
87
|
+
|
|
88
|
+
**your licenses** are all the licenses granted to you for the software under
|
|
89
|
+
these terms.
|
|
90
|
+
|
|
91
|
+
**use** means anything you do with the software requiring one of your licenses.
|
|
92
|
+
|
|
93
|
+
**trademark** means trademarks, service marks, and similar rights.
|
package/README.md
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
# @hogsend/engine
|
|
2
|
+
|
|
3
|
+
The Hogsend framework: `createApp`, `createHogsendClient`, `createWorker`,
|
|
4
|
+
`defineJourney`, `defineWebhookSource`, the ingestion + tracking pipeline, the
|
|
5
|
+
built-in routes, and the registries. This is the public API surface consumers
|
|
6
|
+
build their lifecycle apps on top of.
|
|
7
|
+
|
|
8
|
+
Scaffold a consumer app with `pnpm dlx create-hogsend@latest`. The engine line
|
|
9
|
+
(`@hogsend/engine`, `@hogsend/db`, `@hogsend/core`) is versioned in lockstep so a
|
|
10
|
+
new engine migration always bumps the engine and DB together; the boot guard
|
|
11
|
+
asserts the database ledger matches the bundled migrations. See
|
|
12
|
+
[engine-boundary.md](https://github.com/dougwithseismic/hogsend/blob/main/docs/engine-boundary.md)
|
|
13
|
+
for the committed API surface and
|
|
14
|
+
[RELEASING.md](https://github.com/dougwithseismic/hogsend/blob/main/docs/RELEASING.md)
|
|
15
|
+
for semver discipline.
|
|
16
|
+
|
|
17
|
+
This package ships raw TypeScript source; consumers bundle it via their own build
|
|
18
|
+
(tsup `noExternal`).
|
package/package.json
ADDED
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@hogsend/engine",
|
|
3
|
+
"version": "0.0.1",
|
|
4
|
+
"type": "module",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"repository": {
|
|
7
|
+
"type": "git",
|
|
8
|
+
"url": "https://github.com/dougwithseismic/hogsend.git",
|
|
9
|
+
"directory": "packages/engine"
|
|
10
|
+
},
|
|
11
|
+
"main": "./src/index.ts",
|
|
12
|
+
"types": "./src/index.ts",
|
|
13
|
+
"exports": {
|
|
14
|
+
".": "./src/index.ts",
|
|
15
|
+
"./worker": "./src/worker.ts"
|
|
16
|
+
},
|
|
17
|
+
"files": [
|
|
18
|
+
"src",
|
|
19
|
+
"README.md"
|
|
20
|
+
],
|
|
21
|
+
"publishConfig": {
|
|
22
|
+
"access": "public"
|
|
23
|
+
},
|
|
24
|
+
"dependencies": {
|
|
25
|
+
"@hatchet-dev/typescript-sdk": "^1.22.3",
|
|
26
|
+
"@hono/node-server": "^2.0.4",
|
|
27
|
+
"@hono/zod-openapi": "^1.4.0",
|
|
28
|
+
"@scalar/hono-api-reference": "^0.10.19",
|
|
29
|
+
"@t3-oss/env-core": "^0.13.11",
|
|
30
|
+
"better-auth": "^1.6.11",
|
|
31
|
+
"drizzle-orm": "^0.45.2",
|
|
32
|
+
"hono": "^4.12.22",
|
|
33
|
+
"ioredis": "^5.10.1",
|
|
34
|
+
"papaparse": "^5.5.3",
|
|
35
|
+
"resend": "^6.12.3",
|
|
36
|
+
"winston": "^3.19.0",
|
|
37
|
+
"zod": "^4.4.3",
|
|
38
|
+
"@hogsend/core": "^0.0.1",
|
|
39
|
+
"@hogsend/db": "^0.0.1",
|
|
40
|
+
"@hogsend/email": "^0.0.1",
|
|
41
|
+
"@hogsend/plugin-resend": "^0.0.1",
|
|
42
|
+
"@hogsend/plugin-posthog": "^0.0.1"
|
|
43
|
+
},
|
|
44
|
+
"devDependencies": {
|
|
45
|
+
"@types/node": "^22.15.3",
|
|
46
|
+
"@types/papaparse": "^5.5.2",
|
|
47
|
+
"tsx": "^4.22.3",
|
|
48
|
+
"@repo/typescript-config": "0.0.0"
|
|
49
|
+
},
|
|
50
|
+
"engines": {
|
|
51
|
+
"node": ">=22"
|
|
52
|
+
},
|
|
53
|
+
"scripts": {
|
|
54
|
+
"check-types": "tsc --noEmit",
|
|
55
|
+
"lint": "biome check .",
|
|
56
|
+
"clean": "rm -rf node_modules .turbo"
|
|
57
|
+
}
|
|
58
|
+
}
|
package/src/app.ts
ADDED
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
import { OpenAPIHono } from "@hono/zod-openapi";
|
|
2
|
+
import { apiReference } from "@scalar/hono-api-reference";
|
|
3
|
+
import { compress } from "hono/compress";
|
|
4
|
+
import { cors } from "hono/cors";
|
|
5
|
+
import { requestId } from "hono/request-id";
|
|
6
|
+
import { secureHeaders } from "hono/secure-headers";
|
|
7
|
+
import type { ErrorHandler, MiddlewareHandler } from "hono/types";
|
|
8
|
+
import type { HogsendClient } from "./container.js";
|
|
9
|
+
import { API_VERSION } from "./env.js";
|
|
10
|
+
import type { Auth } from "./lib/auth.js";
|
|
11
|
+
import type { ApiKeyContext } from "./middleware/api-key.js";
|
|
12
|
+
import { errorHandler } from "./middleware/error-handler.js";
|
|
13
|
+
import { requestLogger } from "./middleware/request-logger.js";
|
|
14
|
+
import { registerRoutes } from "./routes/index.js";
|
|
15
|
+
import type { DefinedWebhookSource } from "./webhook-sources/define-webhook-source.js";
|
|
16
|
+
|
|
17
|
+
type AuthSession = Awaited<ReturnType<Auth["api"]["getSession"]>>;
|
|
18
|
+
|
|
19
|
+
export type AppEnv = {
|
|
20
|
+
Variables: {
|
|
21
|
+
container: HogsendClient;
|
|
22
|
+
requestId: string;
|
|
23
|
+
user: NonNullable<AuthSession>["user"] | null;
|
|
24
|
+
session: NonNullable<AuthSession>["session"] | null;
|
|
25
|
+
apiKey: ApiKeyContext | undefined;
|
|
26
|
+
};
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
export interface CreateAppOptions {
|
|
30
|
+
/** Mount custom routers after the built-in routes. */
|
|
31
|
+
routes?: (app: OpenAPIHono<AppEnv>) => void;
|
|
32
|
+
/** Extra middleware applied after the built-in stack. */
|
|
33
|
+
middleware?: MiddlewareHandler[];
|
|
34
|
+
/** Webhook sources served at `/v1/webhooks/:sourceId`. */
|
|
35
|
+
webhookSources?: DefinedWebhookSource[];
|
|
36
|
+
/** Override the default error handler. */
|
|
37
|
+
onError?: ErrorHandler<AppEnv>;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
export function createApp(
|
|
41
|
+
container: HogsendClient,
|
|
42
|
+
opts: CreateAppOptions = {},
|
|
43
|
+
) {
|
|
44
|
+
const app = new OpenAPIHono<AppEnv>();
|
|
45
|
+
|
|
46
|
+
app.use("*", async (c, next) => {
|
|
47
|
+
c.set("container", container);
|
|
48
|
+
await next();
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
app.use("*", secureHeaders());
|
|
52
|
+
app.use("*", cors());
|
|
53
|
+
app.use("*", compress());
|
|
54
|
+
app.use("*", requestId());
|
|
55
|
+
app.use("*", requestLogger);
|
|
56
|
+
|
|
57
|
+
for (const mw of opts.middleware ?? []) {
|
|
58
|
+
app.use("*", mw);
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
app.onError(opts.onError ?? errorHandler);
|
|
62
|
+
|
|
63
|
+
app.notFound((c) => {
|
|
64
|
+
return c.json({ error: "Not Found" }, 404);
|
|
65
|
+
});
|
|
66
|
+
|
|
67
|
+
app.on(["POST", "GET"], "/api/auth/*", (c) => {
|
|
68
|
+
const { auth } = c.get("container");
|
|
69
|
+
return auth.handler(c.req.raw);
|
|
70
|
+
});
|
|
71
|
+
|
|
72
|
+
registerRoutes(app, { webhookSources: opts.webhookSources ?? [] });
|
|
73
|
+
|
|
74
|
+
opts.routes?.(app);
|
|
75
|
+
|
|
76
|
+
if (container.env.NODE_ENV !== "production") {
|
|
77
|
+
app.doc("/openapi.json", {
|
|
78
|
+
openapi: "3.1.0",
|
|
79
|
+
info: {
|
|
80
|
+
title: "Hogsend API",
|
|
81
|
+
version: API_VERSION,
|
|
82
|
+
description: "Journey orchestration API",
|
|
83
|
+
},
|
|
84
|
+
servers: [
|
|
85
|
+
{
|
|
86
|
+
url: `http://localhost:${container.env.PORT}`,
|
|
87
|
+
description: "Local development",
|
|
88
|
+
},
|
|
89
|
+
],
|
|
90
|
+
});
|
|
91
|
+
|
|
92
|
+
app.get(
|
|
93
|
+
"/docs",
|
|
94
|
+
apiReference({
|
|
95
|
+
spec: { url: "/openapi.json" },
|
|
96
|
+
theme: "kepler",
|
|
97
|
+
}),
|
|
98
|
+
);
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
return app;
|
|
102
|
+
}
|
package/src/container.ts
ADDED
|
@@ -0,0 +1,172 @@
|
|
|
1
|
+
import type { HatchetClient } from "@hatchet-dev/typescript-sdk/v1/index.js";
|
|
2
|
+
import type { JourneyRegistry } from "@hogsend/core/registry";
|
|
3
|
+
import {
|
|
4
|
+
createDatabase,
|
|
5
|
+
type Database,
|
|
6
|
+
type DatabaseClient,
|
|
7
|
+
type JournalShape,
|
|
8
|
+
} from "@hogsend/db";
|
|
9
|
+
import type { TemplateRegistry } from "@hogsend/email";
|
|
10
|
+
import type { PostHogService } from "@hogsend/plugin-posthog";
|
|
11
|
+
import {
|
|
12
|
+
createResendClient,
|
|
13
|
+
createResendProvider,
|
|
14
|
+
type EmailProvider,
|
|
15
|
+
} from "@hogsend/plugin-resend";
|
|
16
|
+
import type { Resend } from "resend";
|
|
17
|
+
import { env } from "./env.js";
|
|
18
|
+
import type { DefinedJourney } from "./journeys/define-journey.js";
|
|
19
|
+
import { buildJourneyRegistry } from "./journeys/registry.js";
|
|
20
|
+
import { type Auth, createAuth } from "./lib/auth.js";
|
|
21
|
+
import { setEmailService } from "./lib/email.js";
|
|
22
|
+
import type { EmailService } from "./lib/email-service-types.js";
|
|
23
|
+
import { hatchet } from "./lib/hatchet.js";
|
|
24
|
+
import { createLogger, type Logger } from "./lib/logger.js";
|
|
25
|
+
import { createTrackedMailer } from "./lib/mailer.js";
|
|
26
|
+
import { getPostHog } from "./lib/posthog.js";
|
|
27
|
+
import { prepareTrackedHtml } from "./lib/tracking.js";
|
|
28
|
+
|
|
29
|
+
export interface HogsendClient {
|
|
30
|
+
env: typeof env;
|
|
31
|
+
logger: Logger;
|
|
32
|
+
db: Database;
|
|
33
|
+
dbClient: DatabaseClient;
|
|
34
|
+
auth: Auth;
|
|
35
|
+
email: Resend;
|
|
36
|
+
emailService: EmailService;
|
|
37
|
+
analytics?: PostHogService;
|
|
38
|
+
registry: JourneyRegistry;
|
|
39
|
+
hatchet: HatchetClient;
|
|
40
|
+
/**
|
|
41
|
+
* The client repo's migration journal (`migrations/meta/_journal.json`),
|
|
42
|
+
* powering the `schema.client` block of `GET /v1/health`. Defaults to an
|
|
43
|
+
* empty journal — a client that injects none has a trivially-in-sync client
|
|
44
|
+
* track. The CLIENT track never gates boot (client-owned); engine does.
|
|
45
|
+
*/
|
|
46
|
+
clientJournal: JournalShape;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
export interface HogsendClientOptions {
|
|
50
|
+
/** Journeys to register in the {@link JourneyRegistry}. Defaults to none. */
|
|
51
|
+
journeys?: DefinedJourney[];
|
|
52
|
+
/**
|
|
53
|
+
* Email is a first-class channel. Its config is grouped here rather than
|
|
54
|
+
* spread across top-level args — the engine owns the cohesive email pipeline
|
|
55
|
+
* (templates → render → preference checks → tracking → `email_sends` write),
|
|
56
|
+
* and the {@link EmailProvider} is only the swappable wire under it.
|
|
57
|
+
*
|
|
58
|
+
* - `provider` — the swappable email provider (Resend, Postmark, SES…).
|
|
59
|
+
* Defaults to a Resend provider built from env (`RESEND_API_KEY` /
|
|
60
|
+
* `RESEND_WEBHOOK_SECRET`). Tracking/rendering/preferences come along for
|
|
61
|
+
* free regardless of which provider you supply.
|
|
62
|
+
* - `templates` — the app's template registry (key → component + subject +
|
|
63
|
+
* category), threaded into the engine mailer and onward to
|
|
64
|
+
* `getTemplate(..., { registry })`. The engine bakes in no business
|
|
65
|
+
* templates; clients own their `.tsx` files + registry. Defaults to an
|
|
66
|
+
* empty registry (no sendable template keys).
|
|
67
|
+
*
|
|
68
|
+
* Other channels (SMS, push, Slack) are NOT configured here — they're plain
|
|
69
|
+
* functions a journey imports and calls.
|
|
70
|
+
*/
|
|
71
|
+
email?: {
|
|
72
|
+
provider?: EmailProvider;
|
|
73
|
+
templates?: TemplateRegistry;
|
|
74
|
+
};
|
|
75
|
+
/**
|
|
76
|
+
* The PostHog-style analytics service used for person properties + event
|
|
77
|
+
* capture. Lives at the top level (not under `email`) because the engine
|
|
78
|
+
* itself uses it — tracking routes and ingestion fire captures. Defaults to
|
|
79
|
+
* {@link getPostHog} (a no-op when `POSTHOG_API_KEY` is unset).
|
|
80
|
+
*/
|
|
81
|
+
analytics?: PostHogService;
|
|
82
|
+
/**
|
|
83
|
+
* Comma-separated ids (or `*`) controlling which journeys load. Defaults to
|
|
84
|
+
* `env.ENABLED_JOURNEYS`.
|
|
85
|
+
*/
|
|
86
|
+
enabledJourneys?: string;
|
|
87
|
+
/**
|
|
88
|
+
* The client repo's migration journal for the `schema.client` health block.
|
|
89
|
+
* Defaults to `{ entries: [] }` (empty client track ⇒ trivially in sync).
|
|
90
|
+
*/
|
|
91
|
+
clientJournal?: JournalShape;
|
|
92
|
+
/**
|
|
93
|
+
* Genuinely advanced / test-only seams. You probably don't need these —
|
|
94
|
+
* prefer the first-class `email` / `analytics` args above.
|
|
95
|
+
* `mailer` replaces the engine-built {@link EmailService} wholesale (used by
|
|
96
|
+
* tests to inject a mock); `auth`, `hatchet`, and `db` swap their respective
|
|
97
|
+
* infrastructure singletons.
|
|
98
|
+
*/
|
|
99
|
+
overrides?: {
|
|
100
|
+
mailer?: EmailService;
|
|
101
|
+
auth?: Auth;
|
|
102
|
+
hatchet?: HatchetClient;
|
|
103
|
+
db?: Database;
|
|
104
|
+
};
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
export function createHogsendClient(
|
|
108
|
+
opts: HogsendClientOptions = {},
|
|
109
|
+
): HogsendClient {
|
|
110
|
+
const logger = createLogger(env.LOG_LEVEL);
|
|
111
|
+
const created = createDatabase({ url: env.DATABASE_URL });
|
|
112
|
+
const db = opts.overrides?.db ?? created.db;
|
|
113
|
+
|
|
114
|
+
const auth =
|
|
115
|
+
opts.overrides?.auth ??
|
|
116
|
+
createAuth({
|
|
117
|
+
db,
|
|
118
|
+
secret: env.BETTER_AUTH_SECRET,
|
|
119
|
+
baseURL: env.BETTER_AUTH_URL,
|
|
120
|
+
});
|
|
121
|
+
|
|
122
|
+
const email = createResendClient({ apiKey: env.RESEND_API_KEY });
|
|
123
|
+
|
|
124
|
+
const registry = buildJourneyRegistry(
|
|
125
|
+
opts.journeys ?? [],
|
|
126
|
+
opts.enabledJourneys ?? env.ENABLED_JOURNEYS,
|
|
127
|
+
);
|
|
128
|
+
|
|
129
|
+
const provider =
|
|
130
|
+
opts.email?.provider ??
|
|
131
|
+
createResendProvider({
|
|
132
|
+
apiKey: env.RESEND_API_KEY,
|
|
133
|
+
webhookSecret: env.RESEND_WEBHOOK_SECRET,
|
|
134
|
+
});
|
|
135
|
+
|
|
136
|
+
const emailService =
|
|
137
|
+
opts.overrides?.mailer ??
|
|
138
|
+
createTrackedMailer(
|
|
139
|
+
{
|
|
140
|
+
defaultFrom: env.RESEND_FROM_EMAIL,
|
|
141
|
+
templates: opts.email?.templates ?? ({} as TemplateRegistry),
|
|
142
|
+
db,
|
|
143
|
+
webhookSecret: env.RESEND_WEBHOOK_SECRET,
|
|
144
|
+
bounceThreshold: 3,
|
|
145
|
+
baseUrl: env.API_PUBLIC_URL,
|
|
146
|
+
},
|
|
147
|
+
{
|
|
148
|
+
provider,
|
|
149
|
+
prepareTrackedHtml,
|
|
150
|
+
},
|
|
151
|
+
);
|
|
152
|
+
|
|
153
|
+
setEmailService(emailService);
|
|
154
|
+
|
|
155
|
+
const analytics = opts.analytics ?? getPostHog();
|
|
156
|
+
|
|
157
|
+
logger.info(`Journey registry loaded: ${registry.count()} journeys`);
|
|
158
|
+
|
|
159
|
+
return {
|
|
160
|
+
env,
|
|
161
|
+
logger,
|
|
162
|
+
db,
|
|
163
|
+
dbClient: created.client,
|
|
164
|
+
auth,
|
|
165
|
+
email,
|
|
166
|
+
emailService,
|
|
167
|
+
analytics,
|
|
168
|
+
registry,
|
|
169
|
+
hatchet: opts.overrides?.hatchet ?? hatchet,
|
|
170
|
+
clientJournal: opts.clientJournal ?? { entries: [] },
|
|
171
|
+
};
|
|
172
|
+
}
|
package/src/env.ts
ADDED
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import { createEnv } from "@t3-oss/env-core";
|
|
2
|
+
import { z } from "zod";
|
|
3
|
+
|
|
4
|
+
export const API_VERSION = "0.0.1";
|
|
5
|
+
|
|
6
|
+
export const env = createEnv({
|
|
7
|
+
server: {
|
|
8
|
+
NODE_ENV: z
|
|
9
|
+
.enum(["development", "production", "test"])
|
|
10
|
+
.default("development"),
|
|
11
|
+
PORT: z.coerce.number().default(3002),
|
|
12
|
+
LOG_LEVEL: z
|
|
13
|
+
.enum(["error", "warn", "info", "http", "debug"])
|
|
14
|
+
.default("info"),
|
|
15
|
+
DATABASE_URL: z.string().min(1),
|
|
16
|
+
REDIS_URL: z.string().min(1).default("redis://localhost:6379"),
|
|
17
|
+
BETTER_AUTH_SECRET: z.string().min(1),
|
|
18
|
+
BETTER_AUTH_URL: z.string().url().default("http://localhost:3002"),
|
|
19
|
+
RESEND_API_KEY: z.string().min(1),
|
|
20
|
+
RESEND_FROM_EMAIL: z.string().email().default("noreply@hogsend.com"),
|
|
21
|
+
// Hatchet connection contract. The @hatchet-dev SDK also reads these straight
|
|
22
|
+
// from process.env via its own config-loader, so this schema is a presence /
|
|
23
|
+
// shape check that keeps the contract in one place — the values still flow to
|
|
24
|
+
// the SDK independently. We pass token/host_port/tls/namespace explicitly into
|
|
25
|
+
// HatchetClient.init() (see lib/hatchet.ts) so env.ts is the source of truth.
|
|
26
|
+
//
|
|
27
|
+
// Step 1 of onboarding is "acquire a Hatchet": grab HATCHET_CLIENT_TOKEN from
|
|
28
|
+
// Hatchet Cloud or the local hatchet-lite dashboard (:8888 → Settings → API
|
|
29
|
+
// Tokens) and set it yourself. This is the bring-your-own-token contract; there
|
|
30
|
+
// is no auto-mint.
|
|
31
|
+
HATCHET_CLIENT_TOKEN: z.string().min(1),
|
|
32
|
+
HATCHET_CLIENT_HOST_PORT: z.string().min(1).default("localhost:7077"),
|
|
33
|
+
// Secure by default: `tls` matches the SDK and Hatchet Cloud. The local
|
|
34
|
+
// insecure hatchet-lite path explicitly overrides to `none` in compose/.env —
|
|
35
|
+
// we never default to plaintext gRPC.
|
|
36
|
+
HATCHET_CLIENT_TLS_STRATEGY: z.enum(["none", "tls", "mtls"]).default("tls"),
|
|
37
|
+
// Future per-tenant isolation knob (one shared Hatchet engine, namespaced
|
|
38
|
+
// tasks). Default-empty today; documented so it stays part of the contract.
|
|
39
|
+
HATCHET_CLIENT_NAMESPACE: z.string().optional(),
|
|
40
|
+
// Emergency boot-guard bypass, read raw in apps/api/src/index.ts. Declared
|
|
41
|
+
// here so it is part of the validated env contract (single source of truth).
|
|
42
|
+
SKIP_SCHEMA_CHECK: z.coerce.boolean().default(false),
|
|
43
|
+
// Client-migration track folder, read raw in @hogsend/db migrate-client.ts
|
|
44
|
+
// (the db package can't depend on engine). Declared here for contract parity.
|
|
45
|
+
CLIENT_MIGRATIONS_FOLDER: z.string().min(1).optional(),
|
|
46
|
+
POSTHOG_API_KEY: z.string().min(1).optional(),
|
|
47
|
+
POSTHOG_HOST: z.string().url().optional(),
|
|
48
|
+
POSTHOG_WEBHOOK_SECRET: z.string().min(1).optional(),
|
|
49
|
+
RESEND_WEBHOOK_SECRET: z.string().min(1).optional(),
|
|
50
|
+
ADMIN_API_KEY: z.string().min(1).optional(),
|
|
51
|
+
API_PUBLIC_URL: z.string().url().default("http://localhost:3002"),
|
|
52
|
+
ENABLED_JOURNEYS: z.string().default("*"),
|
|
53
|
+
},
|
|
54
|
+
runtimeEnv: process.env,
|
|
55
|
+
emptyStringAsUndefined: true,
|
|
56
|
+
});
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
// @hogsend/engine — public API surface (the committed semver boundary).
|
|
2
|
+
//
|
|
3
|
+
// Content (journeys, webhook sources, workflows) is injected into these
|
|
4
|
+
// factories by client app code; the engine never imports content.
|
|
5
|
+
|
|
6
|
+
// Core helpers used by content journeys (days/hours/minutes, condition + journey
|
|
7
|
+
// types) so content can import everything from `@hogsend/engine`.
|
|
8
|
+
export * from "@hogsend/core";
|
|
9
|
+
export { JourneyRegistry } from "@hogsend/core/registry";
|
|
10
|
+
// --- Re-exports for content ---
|
|
11
|
+
// Schema/version helpers used by the boot guard and the /v1/health route.
|
|
12
|
+
export {
|
|
13
|
+
getBundledMigrations,
|
|
14
|
+
getClientSchemaVersion,
|
|
15
|
+
getEngineSchemaVersion,
|
|
16
|
+
getSchemaVersion,
|
|
17
|
+
type JournalShape,
|
|
18
|
+
type SchemaVersion,
|
|
19
|
+
} from "@hogsend/db";
|
|
20
|
+
// --- App / container / worker factories ---
|
|
21
|
+
export { type AppEnv, type CreateAppOptions, createApp } from "./app.js";
|
|
22
|
+
export {
|
|
23
|
+
createHogsendClient,
|
|
24
|
+
type HogsendClient,
|
|
25
|
+
type HogsendClientOptions,
|
|
26
|
+
} from "./container.js";
|
|
27
|
+
// --- Env ---
|
|
28
|
+
export { API_VERSION, env } from "./env.js";
|
|
29
|
+
// --- Journeys ---
|
|
30
|
+
export {
|
|
31
|
+
type DefinedJourney,
|
|
32
|
+
defineJourney,
|
|
33
|
+
} from "./journeys/define-journey.js";
|
|
34
|
+
export { createJourneyContext } from "./journeys/journey-context.js";
|
|
35
|
+
export {
|
|
36
|
+
buildJourneyRegistry,
|
|
37
|
+
parseEnabledFilter,
|
|
38
|
+
selectJourneyTasks,
|
|
39
|
+
} from "./journeys/registry.js";
|
|
40
|
+
export {
|
|
41
|
+
getJourneyRegistrySingleton,
|
|
42
|
+
setJourneyRegistry,
|
|
43
|
+
} from "./journeys/registry-singleton.js";
|
|
44
|
+
// --- Auth ---
|
|
45
|
+
export { type Auth, createAuth } from "./lib/auth.js";
|
|
46
|
+
// --- Backfill ---
|
|
47
|
+
export {
|
|
48
|
+
type BatchedBackfillOptions,
|
|
49
|
+
type BatchedBackfillResult,
|
|
50
|
+
runBatchedBackfill,
|
|
51
|
+
} from "./lib/backfill.js";
|
|
52
|
+
// --- Infrastructure singletons ---
|
|
53
|
+
export { getDb } from "./lib/db.js";
|
|
54
|
+
// --- Email ---
|
|
55
|
+
export {
|
|
56
|
+
type SendEmailOptions,
|
|
57
|
+
type SendEmailResult,
|
|
58
|
+
sendEmail,
|
|
59
|
+
setEmailService,
|
|
60
|
+
} from "./lib/email.js";
|
|
61
|
+
// --- Email service (engine-owned tracked mailer) ---
|
|
62
|
+
export type {
|
|
63
|
+
EmailService,
|
|
64
|
+
EmailServiceConfig,
|
|
65
|
+
EmailServiceSendOptions,
|
|
66
|
+
EmailServiceWebhookOptions,
|
|
67
|
+
EmailServiceWebhookResult,
|
|
68
|
+
SendTrackedEmailOptions,
|
|
69
|
+
TrackedSendResult,
|
|
70
|
+
} from "./lib/email-service-types.js";
|
|
71
|
+
// --- Enrollment guards ---
|
|
72
|
+
export { checkEmailPreferences } from "./lib/enrollment-guards.js";
|
|
73
|
+
export { hatchet } from "./lib/hatchet.js";
|
|
74
|
+
// --- Ingestion pipeline ---
|
|
75
|
+
export {
|
|
76
|
+
type IngestEvent,
|
|
77
|
+
type IngestResult,
|
|
78
|
+
ingestEvent,
|
|
79
|
+
} from "./lib/ingestion.js";
|
|
80
|
+
// --- Logging ---
|
|
81
|
+
export { createLogger, type Logger } from "./lib/logger.js";
|
|
82
|
+
export { createTrackedMailer } from "./lib/mailer.js";
|
|
83
|
+
export { getPostHog } from "./lib/posthog.js";
|
|
84
|
+
export { getRedisIfConnected } from "./lib/redis.js";
|
|
85
|
+
export {
|
|
86
|
+
type PrepareTrackedHtmlFn,
|
|
87
|
+
sendTrackedEmail,
|
|
88
|
+
} from "./lib/tracked.js";
|
|
89
|
+
// --- Tracking ---
|
|
90
|
+
export {
|
|
91
|
+
injectOpenPixel,
|
|
92
|
+
prepareTrackedHtml,
|
|
93
|
+
rewriteLinks,
|
|
94
|
+
} from "./lib/tracking.js";
|
|
95
|
+
export {
|
|
96
|
+
pushTrackingEvent,
|
|
97
|
+
resolveEmailSendContext,
|
|
98
|
+
} from "./lib/tracking-events.js";
|
|
99
|
+
// --- Webhook sources ---
|
|
100
|
+
export {
|
|
101
|
+
type DefinedWebhookSource,
|
|
102
|
+
defineWebhookSource,
|
|
103
|
+
type WebhookSourceCtx,
|
|
104
|
+
type WebhookSourceMeta,
|
|
105
|
+
} from "./webhook-sources/define-webhook-source.js";
|
|
106
|
+
export {
|
|
107
|
+
type CreateWorkerOptions,
|
|
108
|
+
createWorker,
|
|
109
|
+
type Worker,
|
|
110
|
+
} from "./worker.js";
|
|
111
|
+
export { checkAlertsTask } from "./workflows/check-alerts.js";
|
|
112
|
+
export { importContactsTask } from "./workflows/import-contacts.js";
|
|
113
|
+
// --- Built-in Hatchet workflow tasks ---
|
|
114
|
+
export { sendEmailTask } from "./workflows/send-email.js";
|