@friggframework/core 2.0.0-next.88 → 2.0.0-next.89
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/core/create-handler.js
CHANGED
|
@@ -49,6 +49,7 @@ const createHandler = (optionByName = {}) => {
|
|
|
49
49
|
eventName = 'Event',
|
|
50
50
|
isUserFacingResponse = true,
|
|
51
51
|
method,
|
|
52
|
+
shouldUseDatabase = true,
|
|
52
53
|
} = optionByName;
|
|
53
54
|
|
|
54
55
|
if (!method) {
|
|
@@ -79,6 +80,12 @@ const createHandler = (optionByName = {}) => {
|
|
|
79
80
|
// If enabled (i.e. if SECRET_ARN is set in process.env) Fetch secrets from AWS Secrets Manager, and set them as environment variables.
|
|
80
81
|
await secretsToEnv();
|
|
81
82
|
|
|
83
|
+
// Lazy-required so DB-free handlers never load the Prisma client.
|
|
84
|
+
if (shouldUseDatabase) {
|
|
85
|
+
const { connectPrisma } = require('../database/prisma');
|
|
86
|
+
await connectPrisma();
|
|
87
|
+
}
|
|
88
|
+
|
|
82
89
|
// Helps reuse the database connection. Lowers response times.
|
|
83
90
|
context.callbackWaitsForEmptyEventLoop = false;
|
|
84
91
|
|
|
@@ -511,6 +511,8 @@ router.get('/health/ready', async (_req, res) => {
|
|
|
511
511
|
});
|
|
512
512
|
});
|
|
513
513
|
|
|
514
|
-
|
|
514
|
+
// DB-free: /health/ready probes the DB itself and degrades to 503. Eager-connect
|
|
515
|
+
// here would turn a DB outage into a 500, killing otherwise-healthy containers.
|
|
516
|
+
const handler = createAppHandler('HTTP Event: Health', router, false);
|
|
515
517
|
|
|
516
518
|
module.exports = { handler, router };
|
|
@@ -12,6 +12,9 @@ const { integrations: integrationClasses } = loadAppDefinition();
|
|
|
12
12
|
|
|
13
13
|
const routeKey = (method, path) => `${(method || 'ANY').toUpperCase()} ${path}`;
|
|
14
14
|
|
|
15
|
+
// Serverless function keys must be alphanumeric; binding keys are developer-chosen.
|
|
16
|
+
const sanitizeBindingKey = (name) => String(name).replace(/[^A-Za-z0-9]/g, '');
|
|
17
|
+
|
|
15
18
|
//todo: this should be in a use case class
|
|
16
19
|
for (const IntegrationClass of integrationClasses) {
|
|
17
20
|
const router = Router();
|
|
@@ -53,20 +56,29 @@ for (const IntegrationClass of integrationClasses) {
|
|
|
53
56
|
}
|
|
54
57
|
}
|
|
55
58
|
|
|
56
|
-
//
|
|
59
|
+
// Each extension binding gets its own namespaced handler (/{bindingName}),
|
|
60
|
+
// so two modules' extensions can share a path like /webhooks without colliding.
|
|
61
|
+
const bindingGroups = new Map();
|
|
57
62
|
for (const extRoute of getExtensionRoutes(IntegrationClass)) {
|
|
63
|
+
const namespacedPath = `/${extRoute.bindingName}${extRoute.path}`;
|
|
58
64
|
claim(
|
|
59
65
|
extRoute.method,
|
|
60
|
-
|
|
66
|
+
namespacedPath,
|
|
61
67
|
`extension "${extRoute.extensionName}" (binding "${extRoute.bindingName}")`
|
|
62
68
|
);
|
|
63
|
-
|
|
64
|
-
|
|
69
|
+
if (!bindingGroups.has(extRoute.bindingName)) {
|
|
70
|
+
bindingGroups.set(extRoute.bindingName, {
|
|
71
|
+
router: Router(),
|
|
72
|
+
useDatabase: extRoute.useDatabase,
|
|
73
|
+
});
|
|
74
|
+
}
|
|
75
|
+
const group = bindingGroups.get(extRoute.bindingName);
|
|
76
|
+
group.router.use(
|
|
77
|
+
`${basePath}/${extRoute.bindingName}`,
|
|
65
78
|
loadRouterFromObject(IntegrationClass, extRoute)
|
|
66
79
|
);
|
|
67
|
-
const method = extRoute.method.toUpperCase();
|
|
68
80
|
console.log(
|
|
69
|
-
`│ ${method} ${basePath}${extRoute.path} (extension: ${extRoute.extensionName})`
|
|
81
|
+
`│ ${extRoute.method.toUpperCase()} ${basePath}/${extRoute.bindingName}${extRoute.path} (extension: ${extRoute.extensionName}, useDatabase: ${extRoute.useDatabase})`
|
|
70
82
|
);
|
|
71
83
|
}
|
|
72
84
|
console.log('│');
|
|
@@ -77,6 +89,29 @@ for (const IntegrationClass of integrationClasses) {
|
|
|
77
89
|
router
|
|
78
90
|
),
|
|
79
91
|
};
|
|
92
|
+
|
|
93
|
+
for (const [bindingName, group] of bindingGroups) {
|
|
94
|
+
// Wire contract: integration-builder.js (devtools) derives the identical
|
|
95
|
+
// function key for the serverless config. Keep both in sync.
|
|
96
|
+
const fnKey = `${IntegrationClass.Definition.name}__${sanitizeBindingKey(
|
|
97
|
+
bindingName
|
|
98
|
+
)}`;
|
|
99
|
+
// Distinct binding keys can sanitize to the same fnKey — fail loud rather than overwrite.
|
|
100
|
+
if (Object.prototype.hasOwnProperty.call(handlers, fnKey)) {
|
|
101
|
+
throw new Error(
|
|
102
|
+
`Integration "${IntegrationClass.Definition.name}" extension handler conflict: ` +
|
|
103
|
+
`binding "${bindingName}" sanitizes to "${fnKey}", which is already taken. ` +
|
|
104
|
+
`Use binding keys that are distinct after stripping non-alphanumeric characters.`
|
|
105
|
+
);
|
|
106
|
+
}
|
|
107
|
+
handlers[fnKey] = {
|
|
108
|
+
handler: createAppHandler(
|
|
109
|
+
`HTTP Event: ${IntegrationClass.Definition.name} extension ${bindingName}`,
|
|
110
|
+
group.router,
|
|
111
|
+
group.useDatabase
|
|
112
|
+
),
|
|
113
|
+
};
|
|
114
|
+
}
|
|
80
115
|
}
|
|
81
116
|
|
|
82
117
|
module.exports = { handlers };
|
|
@@ -24,6 +24,8 @@ class HubSpotIntegration extends IntegrationBase {
|
|
|
24
24
|
hubspotWebhooks: {
|
|
25
25
|
extension: hubspot.extensions.webhooks,
|
|
26
26
|
handlers: { HUBSPOT_WEBHOOK: 'onHubSpotEvent' },
|
|
27
|
+
// optional: override the extension's declared useDatabase
|
|
28
|
+
// useDatabase: true,
|
|
27
29
|
},
|
|
28
30
|
},
|
|
29
31
|
};
|
|
@@ -39,19 +41,42 @@ class HubSpotIntegration extends IntegrationBase {
|
|
|
39
41
|
}
|
|
40
42
|
```
|
|
41
43
|
|
|
42
|
-
The binding key (`hubspotWebhooks`) is your local name. The extension reference (`hubspot.extensions.webhooks`) is whatever the API module exports.
|
|
44
|
+
The binding key (`hubspotWebhooks`) is your local name. It is also the **URL namespace** for the extension's routes (see below), so pick something readable — `hubspot` yields a cleaner URL than `hubspotWebhooks`. The extension reference (`hubspot.extensions.webhooks`) is whatever the API module exports.
|
|
43
45
|
|
|
44
46
|
## Step 2: Deploy
|
|
45
47
|
|
|
46
|
-
|
|
48
|
+
Each extension binding is mounted under its **binding key**, on its own dedicated handler/Lambda function. This means two modules' extensions (e.g. a HubSpot and a Clockwork webhooks extension on the same integration) never collide — each lives at a distinct namespaced path. Boot logs show:
|
|
47
49
|
|
|
48
50
|
```
|
|
49
51
|
│ Configuring routes for hubspot Integration:
|
|
50
|
-
│ POST /api/hubspot-integration/webhooks (extension: hubspot-webhooks)
|
|
52
|
+
│ POST /api/hubspot-integration/hubspotWebhooks/webhooks (extension: hubspot-webhooks, useDatabase: false)
|
|
51
53
|
│
|
|
52
54
|
```
|
|
53
55
|
|
|
54
|
-
|
|
56
|
+
So the full URL is `/api/{integration-name}-integration/{bindingKey}{route.path}`. Register that URL with the upstream provider (e.g. paste it into your HubSpot app's webhook settings). Hit it and the bound method (`onHubSpotEvent`) fires on the resolved per-account integration instance.
|
|
57
|
+
|
|
58
|
+
> **⚠️ Breaking:** extension routes used to mount un-namespaced (`/api/{x}-integration/webhooks`). They are now namespaced under the binding key. Any provider webhook already registered against the old path must be re-pointed at the new `/{bindingKey}` URL — and for signature schemes that sign the full URL (e.g. HubSpot v3), the old registration will also fail verification until updated.
|
|
59
|
+
|
|
60
|
+
## `useDatabase` — does the receiver open a DB connection?
|
|
61
|
+
|
|
62
|
+
Each extension declares whether its route handler should open a database connection:
|
|
63
|
+
|
|
64
|
+
```javascript
|
|
65
|
+
// in the extension bundle (api-module side)
|
|
66
|
+
module.exports = {
|
|
67
|
+
name: 'hubspot-webhooks',
|
|
68
|
+
useDatabase: false, // default — the receiver is DB-free
|
|
69
|
+
routes: [ /* ... */ ],
|
|
70
|
+
events: { /* ... */ },
|
|
71
|
+
};
|
|
72
|
+
```
|
|
73
|
+
|
|
74
|
+
- **Default is `false`** — a webhook receiver that only verifies a signature and enqueues should not pay for a DB connection (faster cold start; at build time its Lambda doesn't get the Prisma layer).
|
|
75
|
+
- Set `useDatabase: true` at the **extension level** if the receiver itself needs the DB. A binding may override it locally (`extensions: { x: { extension, useDatabase: true } }`), though that's rarely needed.
|
|
76
|
+
- Resolution order: `binding.useDatabase ?? extension.useDatabase ?? false`.
|
|
77
|
+
- Scope note: `false` is the default **for extension routes**. `createHandler` itself still defaults `shouldUseDatabase: true` for the integration's own catch-all handler and the legacy `Definition.webhooks: true` path — those connect as before. The `false` default applies only to the per-binding extension handler.
|
|
78
|
+
|
|
79
|
+
If `useDatabase` is `false`, the receiver must not touch the database. Work that needs the DB (e.g. resolving `portalId → integrationId`) belongs in the queue worker that processes the dispatched event, not in the receiver.
|
|
55
80
|
|
|
56
81
|
## How handler binding works
|
|
57
82
|
|
|
@@ -84,7 +109,7 @@ This means **binding the same extension twice only works if the extension itself
|
|
|
84
109
|
|
|
85
110
|
Subclass overrides via `this.events[eventName]` (set in the constructor) take precedence over extension-declared events. If a binding tried to wire a handler that's now shadowed, the framework logs a warning naming the integration, binding, and ignored method.
|
|
86
111
|
|
|
87
|
-
|
|
112
|
+
**Routes do not collide across bindings** — each binding's routes are namespaced under its binding key (`/{bindingKey}{route.path}`), so two extensions can both declare `POST /webhooks` and live at distinct URLs. A route conflict only throws at boot if a *single* binding declares two routes with the same `method + path` (or a `Definition.routes` entry exactly matches an extension's namespaced path). Note this is independent of event-name conflicts above: namespacing disambiguates URLs, but two bindings still must use distinct **event** names since events are merged into one `this.events` map.
|
|
88
113
|
|
|
89
114
|
## Authoring an extension (for API module authors)
|
|
90
115
|
|
|
@@ -70,6 +70,24 @@ function validateExtensionBinding(extension, bindingName, integrationName, bindi
|
|
|
70
70
|
throw new Error(`${ctx}: extension is missing required "name" field`);
|
|
71
71
|
}
|
|
72
72
|
|
|
73
|
+
if (
|
|
74
|
+
extension.useDatabase !== undefined &&
|
|
75
|
+
typeof extension.useDatabase !== 'boolean'
|
|
76
|
+
) {
|
|
77
|
+
throw new Error(
|
|
78
|
+
`${ctx}: extension "${extension.name}" "useDatabase" must be a boolean`
|
|
79
|
+
);
|
|
80
|
+
}
|
|
81
|
+
if (
|
|
82
|
+
binding &&
|
|
83
|
+
binding.useDatabase !== undefined &&
|
|
84
|
+
typeof binding.useDatabase !== 'boolean'
|
|
85
|
+
) {
|
|
86
|
+
throw new Error(
|
|
87
|
+
`${ctx}: binding "useDatabase" must be a boolean`
|
|
88
|
+
);
|
|
89
|
+
}
|
|
90
|
+
|
|
73
91
|
const events = extension.events || {};
|
|
74
92
|
if (typeof events !== 'object' || Array.isArray(events)) {
|
|
75
93
|
throw new Error(
|
|
@@ -181,6 +199,8 @@ function getExtensionRoutes(IntegrationClass) {
|
|
|
181
199
|
integrationName,
|
|
182
200
|
binding
|
|
183
201
|
);
|
|
202
|
+
const useDatabase =
|
|
203
|
+
binding.useDatabase ?? binding.extension.useDatabase ?? false;
|
|
184
204
|
const routes = binding.extension.routes || [];
|
|
185
205
|
for (const route of routes) {
|
|
186
206
|
flat.push({
|
|
@@ -189,6 +209,7 @@ function getExtensionRoutes(IntegrationClass) {
|
|
|
189
209
|
path: route.path,
|
|
190
210
|
method: route.method,
|
|
191
211
|
event: route.event,
|
|
212
|
+
useDatabase,
|
|
192
213
|
});
|
|
193
214
|
}
|
|
194
215
|
}
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@friggframework/core",
|
|
3
3
|
"prettier": "@friggframework/prettier-config",
|
|
4
|
-
"version": "2.0.0-next.
|
|
4
|
+
"version": "2.0.0-next.89",
|
|
5
5
|
"dependencies": {
|
|
6
6
|
"@aws-sdk/client-apigatewaymanagementapi": "^3.588.0",
|
|
7
7
|
"@aws-sdk/client-kms": "^3.588.0",
|
|
@@ -38,9 +38,9 @@
|
|
|
38
38
|
}
|
|
39
39
|
},
|
|
40
40
|
"devDependencies": {
|
|
41
|
-
"@friggframework/eslint-config": "2.0.0-next.
|
|
42
|
-
"@friggframework/prettier-config": "2.0.0-next.
|
|
43
|
-
"@friggframework/test": "2.0.0-next.
|
|
41
|
+
"@friggframework/eslint-config": "2.0.0-next.89",
|
|
42
|
+
"@friggframework/prettier-config": "2.0.0-next.89",
|
|
43
|
+
"@friggframework/test": "2.0.0-next.89",
|
|
44
44
|
"@prisma/client": "^6.17.0",
|
|
45
45
|
"@types/lodash": "4.17.15",
|
|
46
46
|
"@typescript-eslint/eslint-plugin": "^8.0.0",
|
|
@@ -80,5 +80,5 @@
|
|
|
80
80
|
"publishConfig": {
|
|
81
81
|
"access": "public"
|
|
82
82
|
},
|
|
83
|
-
"gitHead": "
|
|
83
|
+
"gitHead": "19d4f89c10f2c0214fda28dbbd6c3bd83430da6b"
|
|
84
84
|
}
|