@friggframework/core 2.0.0-next.87 → 2.0.0-next.88

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.
@@ -11,6 +11,12 @@ const {
11
11
  const {
12
12
  FindIntegrationContextByExternalEntityIdUseCase,
13
13
  } = require('../../integrations/use-cases/find-integration-context-by-external-entity-id');
14
+ const {
15
+ FindIntegrationByEntityExternalIdUseCase,
16
+ } = require('../../integrations/use-cases/find-integration-by-entity-external-id');
17
+ const {
18
+ ListIntegrationsByEntityExternalIdUseCase,
19
+ } = require('../../integrations/use-cases/list-integrations-by-entity-external-id');
14
20
  const {
15
21
  GetIntegrationsForUser,
16
22
  } = require('../../integrations/use-cases/get-integrations-for-user');
@@ -70,6 +76,18 @@ function createIntegrationCommands({ integrationClass }) {
70
76
  loadIntegrationContextUseCase: loadIntegrationContextUseCase,
71
77
  });
72
78
 
79
+ const findIntegrationByEntityExternalIdUseCase =
80
+ new FindIntegrationByEntityExternalIdUseCase({
81
+ integrationRepository,
82
+ moduleRepository,
83
+ });
84
+
85
+ const listIntegrationsByEntityExternalIdUseCase =
86
+ new ListIntegrationsByEntityExternalIdUseCase({
87
+ integrationRepository,
88
+ moduleRepository,
89
+ });
90
+
73
91
  const getIntegrationsForUserUseCase = new GetIntegrationsForUser({
74
92
  integrationRepository,
75
93
  integrationClasses: [integrationClass],
@@ -103,6 +121,41 @@ function createIntegrationCommands({ integrationClass }) {
103
121
  }
104
122
  },
105
123
 
124
+ /**
125
+ * Resolve an externalId (e.g. HubSpot portalId, Slack team_id) to a
126
+ * single integration ID. Throws on ambiguous resolution at either the
127
+ * entity or integration layer — cross-tenant routing is refused.
128
+ *
129
+ * @param {string|number} externalId - Provider's stable identifier.
130
+ * @param {string} [moduleName] - Disambiguates when multiple modules in
131
+ * the same app could carry colliding externalIds.
132
+ * @returns {Promise<string|null>} Integration ID, or null on no match.
133
+ * @throws {Error} On ambiguous resolution (multiple entities or
134
+ * multiple owning integrations).
135
+ */
136
+ async findIntegrationByEntityExternalId(externalId, moduleName) {
137
+ return findIntegrationByEntityExternalIdUseCase.execute({
138
+ externalId,
139
+ moduleName,
140
+ });
141
+ },
142
+
143
+ /**
144
+ * List all integration IDs whose module entities match an externalId.
145
+ * Use when one externalId is expected to map to multiple integrations
146
+ * (intentional fan-out). Does not throw on ambiguity.
147
+ *
148
+ * @param {string|number} externalId - Provider's stable identifier.
149
+ * @param {string} [moduleName] - Disambiguates across modules.
150
+ * @returns {Promise<Array<string>>} Array of integration IDs (possibly empty).
151
+ */
152
+ async listIntegrationsByEntityExternalId(externalId, moduleName) {
153
+ return listIntegrationsByEntityExternalIdUseCase.execute({
154
+ externalId,
155
+ moduleName,
156
+ });
157
+ },
158
+
106
159
  async loadIntegrationContextById(integrationId) {
107
160
  try {
108
161
  const context = await loadIntegrationContextUseCase.execute({
@@ -31,6 +31,9 @@ const loadRouterFromObject = (IntegrationClass, routerObject) => {
31
31
  router[method.toLowerCase()](path, async (req, res, next) => {
32
32
  try {
33
33
  const integrationInstance = new IntegrationClass();
34
+ // initialize() registers dynamic user actions AND merges any Tier 3
35
+ // Integration Extension events into instance.events before dispatch.
36
+ await integrationInstance.initialize();
34
37
  const dispatcher = new IntegrationEventDispatcher(
35
38
  integrationInstance
36
39
  );
@@ -212,6 +215,9 @@ const createQueueWorker = (integrationClass) => {
212
215
  logCtx
213
216
  );
214
217
  integrationInstance = new integrationClass();
218
+ // Merge Tier 3 Integration Extension events into instance.events
219
+ // so extension-contributed queue events can be dispatched.
220
+ await integrationInstance.initialize();
215
221
  }
216
222
 
217
223
  const dispatcher = new IntegrationEventDispatcher(
@@ -2,24 +2,47 @@ const { createAppHandler } = require('./../app-handler-helpers');
2
2
  const {
3
3
  loadAppDefinition,
4
4
  } = require('../app-definition-loader');
5
- const { Router } = require('express');
5
+ const express = require('express');
6
+ const { Router } = express;
6
7
  const { loadRouterFromObject } = require('../backend-utils');
8
+ const { getExtensionRoutes } = require('../../integrations/extension');
7
9
 
8
10
  const handlers = {};
9
11
  const { integrations: integrationClasses } = loadAppDefinition();
10
12
 
13
+ const routeKey = (method, path) => `${(method || 'ANY').toUpperCase()} ${path}`;
14
+
11
15
  //todo: this should be in a use case class
12
16
  for (const IntegrationClass of integrationClasses) {
13
17
  const router = Router();
14
18
  const basePath = `/api/${IntegrationClass.Definition.name}-integration`;
19
+ // Track (method, path) tuples to fail fast on conflicts between Definition.routes,
20
+ // extension routes, or two extensions claiming the same path.
21
+ const claimedRoutes = new Map();
22
+ const claim = (method, path, source) => {
23
+ const key = routeKey(method, path);
24
+ if (claimedRoutes.has(key)) {
25
+ const prev = claimedRoutes.get(key);
26
+ throw new Error(
27
+ `Integration "${IntegrationClass.Definition.name}" route conflict: ` +
28
+ `${key} declared by ${prev} and ${source}`
29
+ );
30
+ }
31
+ claimedRoutes.set(key, source);
32
+ };
15
33
 
16
34
  console.log(`\n│ Configuring routes for ${IntegrationClass.Definition.name} Integration:`);
17
35
 
18
- for (const routeDef of IntegrationClass.Definition.routes) {
36
+ const routes = IntegrationClass.Definition.routes || [];
37
+ for (const routeDef of routes) {
19
38
  if (typeof routeDef === 'function') {
20
39
  router.use(basePath, routeDef(IntegrationClass));
21
40
  console.log(`│ ANY ${basePath}/* (function handler)`);
41
+ } else if (routeDef instanceof express.Router) {
42
+ router.use(basePath, routeDef);
43
+ console.log(`│ ANY ${basePath}/* (express router)`);
22
44
  } else if (typeof routeDef === 'object') {
45
+ claim(routeDef.method, routeDef.path, 'Definition.routes');
23
46
  router.use(
24
47
  basePath,
25
48
  loadRouterFromObject(IntegrationClass, routeDef)
@@ -27,11 +50,25 @@ for (const IntegrationClass of integrationClasses) {
27
50
  const method = (routeDef.method || 'ANY').toUpperCase();
28
51
  const fullPath = `${basePath}${routeDef.path}`;
29
52
  console.log(`│ ${method} ${fullPath}`);
30
- } else if (routeDef instanceof express.Router) {
31
- router.use(basePath, routeDef);
32
- console.log(`│ ANY ${basePath}/* (express router)`);
33
53
  }
34
54
  }
55
+
56
+ // Tier 3 Integration Extension routes — see EXTENSIONS.md
57
+ for (const extRoute of getExtensionRoutes(IntegrationClass)) {
58
+ claim(
59
+ extRoute.method,
60
+ extRoute.path,
61
+ `extension "${extRoute.extensionName}" (binding "${extRoute.bindingName}")`
62
+ );
63
+ router.use(
64
+ basePath,
65
+ loadRouterFromObject(IntegrationClass, extRoute)
66
+ );
67
+ const method = extRoute.method.toUpperCase();
68
+ console.log(
69
+ `│ ${method} ${basePath}${extRoute.path} (extension: ${extRoute.extensionName})`
70
+ );
71
+ }
35
72
  console.log('│');
36
73
 
37
74
  handlers[`${IntegrationClass.Definition.name}`] = {
@@ -1,6 +1,13 @@
1
1
  const { createHandler } = require('@friggframework/core');
2
2
  const { loadAppDefinition } = require('../app-definition-loader');
3
3
  const { createQueueWorker } = require('../backend-utils');
4
+ // TODO(Phase 2): mount extension-declared workers in addition to the per-integration
5
+ // default queue worker. Today, getExtensionWorkers(IntegrationClass) returns the
6
+ // declared workers but they are not yet bound to dedicated SQS sources. Extension-
7
+ // contributed *events* still flow through the default queue worker below because
8
+ // _mergeExtensions() registers them in instance.events, so end-to-end webhook
9
+ // delivery for Tier 3 extensions works without this Phase 2 work.
10
+ // const { getExtensionWorkers } = require('../../integrations/extension');
4
11
 
5
12
  const handlers = {};
6
13
  const { integrations: integrationClasses } = loadAppDefinition();
@@ -0,0 +1,215 @@
1
+ # Integration Extensions Quick Start
2
+
3
+ Tier 3 **Integration Extensions** let an API module ship reusable handler bundles — receiver routes, event handlers, queues, workers — that an integration consumes declaratively via `Definition.extensions`. See [ADR-EXTENSIONS](../../../docs/architecture/ADR-EXTENSIONS.md) for the full taxonomy.
4
+
5
+ ## When to use this vs `Definition.webhooks: true`
6
+
7
+ | Use `webhooks: true` ([WEBHOOK-QUICKSTART](./WEBHOOK-QUICKSTART.md)) | Use `extensions: {...}` |
8
+ |---|---|
9
+ | Per-account webhooks scoped to one integration record | App-level webhooks fanned out to many account records by external ID lookup |
10
+ | You'll write the receiver, signature check, and queue dispatch yourself | The API module ships the receiver, signature check, and queue dispatch already |
11
+ | One pattern, one endpoint | Multiple bundles (webhooks + CRM cards + timeline) declared together |
12
+
13
+ ## Step 1: Bind the extension on your Integration's Definition
14
+
15
+ ```javascript
16
+ const hubspot = require('@friggframework/api-module-hubspot');
17
+
18
+ class HubSpotIntegration extends IntegrationBase {
19
+ static Definition = {
20
+ name: 'hubspot',
21
+ version: '1.0.0',
22
+ modules: { hubspot: { definition: hubspot.Definition } },
23
+ extensions: {
24
+ hubspotWebhooks: {
25
+ extension: hubspot.extensions.webhooks,
26
+ handlers: { HUBSPOT_WEBHOOK: 'onHubSpotEvent' },
27
+ },
28
+ },
29
+ };
30
+
31
+ async onHubSpotEvent({ data }) {
32
+ // pure business logic — signature verification, portalId lookup,
33
+ // and queue dispatch are all done by the extension's default handlers
34
+ const { subscriptionType, objectId } = data.body;
35
+ if (subscriptionType === 'contact.creation') {
36
+ await this.upsertContact(objectId);
37
+ }
38
+ }
39
+ }
40
+ ```
41
+
42
+ The binding key (`hubspotWebhooks`) is your local name. The extension reference (`hubspot.extensions.webhooks`) is whatever the API module exports.
43
+
44
+ ## Step 2: Deploy
45
+
46
+ The framework auto-mounts each extension's routes at the integration's base path. Boot logs show:
47
+
48
+ ```
49
+ │ Configuring routes for hubspot Integration:
50
+ │ POST /api/hubspot-integration/webhooks (extension: hubspot-webhooks)
51
+
52
+ ```
53
+
54
+ Hit that URL and the bound method (`onHubSpotEvent`) fires on the resolved per-account integration instance.
55
+
56
+ ## How handler binding works
57
+
58
+ Each binding can map extension event names to method names on your integration:
59
+
60
+ ```javascript
61
+ extensions: {
62
+ hubspotWebhooks: {
63
+ extension: hubspot.extensions.webhooks,
64
+ handlers: {
65
+ HUBSPOT_WEBHOOK: 'onHubSpotEvent', // method on `this`
66
+ },
67
+ },
68
+ },
69
+ ```
70
+
71
+ Resolution priority per event:
72
+
73
+ 1. `binding.handlers[eventName]` → resolves to `this[methodName]`, bound to the instance
74
+ 2. Extension's own default handler from `extension.events[eventName].handler`, bound to the instance
75
+ 3. Otherwise → `initialize()` throws with a message naming the integration, binding, and event
76
+
77
+ Strings (not function refs) are intentional: it dodges the `this`-in-static-Definition bootstrapping problem and centralizes binding inside the framework. The framework resolves the method against the live integration instance at startup.
78
+
79
+ ## Event-name conflicts (and binding the same extension twice)
80
+
81
+ If two bindings in your integration's `extensions` map declare the same event name, the framework **throws at `initialize()`** with a clear conflict error — no silent first/last-writer pick, no surprise routing. The fix is to use distinct event names per binding.
82
+
83
+ This means **binding the same extension twice only works if the extension itself defines disjoint event sets per use-case** (rare). For the common "two webhooks, two handlers" pattern, an API module should ship two distinct extensions instead — for example `hubspot.extensions.webhooks` and `hubspot.extensions.sandboxWebhooks`, each with its own event names.
84
+
85
+ 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
+
87
+ Route path conflicts (two extensions declaring the same `method + path`, or an extension colliding with a `Definition.routes` entry) also throw at boot.
88
+
89
+ ## Authoring an extension (for API module authors)
90
+
91
+ An extension bundle is a plain object exported from your api-module:
92
+
93
+ ```javascript
94
+ // @friggframework/api-module-hubspot/extensions/webhooks/index.js
95
+ module.exports = {
96
+ name: 'hubspot-webhooks',
97
+ routes: [
98
+ { path: '/webhooks', method: 'POST', event: 'HUBSPOT_WEBHOOK_RECEIVED' },
99
+ ],
100
+ events: {
101
+ HUBSPOT_WEBHOOK_RECEIVED: {
102
+ type: 'LIFE_CYCLE_EVENT',
103
+ handler: async function ({ req, res }) {
104
+ // verify signature, look up integration by portalId, queue
105
+ await verifyHubSpotSignature(req);
106
+ for (const evt of req.body) {
107
+ // Reverse-lookup is exposed via friggCommands, the
108
+ // canonical access pattern for cross-cutting lookups.
109
+ // The HubSpot-named wrapper lives in the api-module's
110
+ // extension package.
111
+ const integrationId =
112
+ await this.commands.findIntegrationByEntityExternalId(
113
+ evt.portalId,
114
+ 'hubspot'
115
+ );
116
+ if (!integrationId) continue;
117
+ await this.queueWebhook({
118
+ integrationId,
119
+ body: evt,
120
+ event: 'HUBSPOT_WEBHOOK',
121
+ });
122
+ }
123
+ res.status(200).json({ received: req.body.length });
124
+ },
125
+ },
126
+ HUBSPOT_WEBHOOK: {
127
+ type: 'LIFE_CYCLE_EVENT',
128
+ handler: async function ({ data }) {
129
+ // default no-op; integrations override via binding.handlers
130
+ },
131
+ },
132
+ },
133
+ };
134
+ ```
135
+
136
+ Then expose it on your api-module's index:
137
+
138
+ ```javascript
139
+ // @friggframework/api-module-hubspot/index.js
140
+ module.exports = {
141
+ Definition: require('./api-module-definition'),
142
+ extensions: {
143
+ webhooks: require('./extensions/webhooks'),
144
+ crmCards: require('./extensions/crm-cards'),
145
+ timeline: require('./extensions/timeline'),
146
+ },
147
+ };
148
+ ```
149
+
150
+ ## Contract enforced by the framework
151
+
152
+ At `initialize()`, the framework validates each binding:
153
+
154
+ - `extension` must be an object with a `name`
155
+ - `extension.events` must be an object keyed by event name (if present)
156
+ - `extension.routes` must be an array (if present)
157
+ - Every route's `event` must exist in `extension.events`
158
+ - Every route's `method` must be a known HTTP verb
159
+ - For each event, either `binding.handlers[eventName]` resolves to an instance method, OR `extension.events[eventName].handler` is a function
160
+
161
+ Validation failures throw at boot with a message identifying the integration, binding, and field.
162
+
163
+ ## Reverse-lookup helpers
164
+
165
+ For app-level webhooks (HubSpot, Slack, Asana, Microsoft Teams, etc.) where one URL serves many accounts, extension default handlers need to resolve the inbound external ID (HubSpot `portalId`, Slack `team_id`, Asana `workspace_id`, Teams `tenant_id`) to a Frigg integration record. Two helpers are exposed via [`createFriggCommands`](../application/index.js) — the canonical access pattern for cross-cutting lookups:
166
+
167
+ ```javascript
168
+ // inside an integration class
169
+ this.commands = createFriggCommands({ integrationClass: MyIntegration });
170
+
171
+ // Throws on ambiguous resolution. Use when one externalId is expected
172
+ // to map to exactly one integration.
173
+ const integrationId = await this.commands.findIntegrationByEntityExternalId(
174
+ externalId,
175
+ 'hubspot' // optional moduleName
176
+ );
177
+
178
+ // Returns array. Use when one externalId may legitimately fan out to
179
+ // multiple integrations (e.g. one upstream account broadcasting to
180
+ // several Frigg integration records).
181
+ const integrationIds = await this.commands.listIntegrationsByEntityExternalId(
182
+ externalId,
183
+ 'hubspot'
184
+ );
185
+ ```
186
+
187
+ `findIntegrationByEntityExternalId` throws if:
188
+ - the (externalId, moduleName) tuple matches more than one Entity row, OR
189
+ - the matched entity is owned by more than one Integration record
190
+
191
+ A silent first-match at either layer is a cross-tenant routing risk; the command refuses to pick. The second argument (moduleName) disambiguates when multiple modules in the same app could carry colliding external IDs — pass it whenever an api-module knows its own moduleName.
192
+
193
+ ### Where platform-named wrappers belong
194
+
195
+ The commands are intentionally platform-neutral. Platform-vocabulary wrappers (`findIntegrationByPortalId`, `findIntegrationByTeamId`, `findIntegrationByWorkspaceId`, etc.) belong **inside the api-module's own extension**, not in core:
196
+
197
+ ```javascript
198
+ // inside @friggframework/api-module-hubspot/extensions/webhooks
199
+ async function findIntegrationByPortalId(integration, portalId) {
200
+ // thin wrapper — reads as self-documenting HubSpot code,
201
+ // delegates to the platform-neutral command
202
+ return integration.commands.findIntegrationByEntityExternalId(
203
+ portalId,
204
+ 'hubspot'
205
+ );
206
+ }
207
+ ```
208
+
209
+ This keeps core platform-neutral and reusable while keeping the api-module code self-documenting for the platform's developers. The same rule applies to any helper that can be named in a single platform's vocabulary.
210
+
211
+ ## See also
212
+
213
+ - [ADR-EXTENSIONS](../../../docs/architecture/ADR-EXTENSIONS.md) — the three-tier taxonomy (Core Plugins / Application Extensions / Integration Extensions)
214
+ - [WEBHOOK-QUICKSTART](./WEBHOOK-QUICKSTART.md) — per-account `Definition.webhooks: true` pattern
215
+ - `extension.js` — the validation + flattening helpers (`validateExtensionBinding`, `getExtensionRoutes`, `getExtensionWorkers`)
@@ -0,0 +1,233 @@
1
+ /**
2
+ * Tier 3 — Integration Extensions
3
+ *
4
+ * An Integration Extension is a reusable bundle exported by an API module (or a
5
+ * shared extensions library) that contributes routes, events, queues, and workers
6
+ * to a consumer integration. The integration binds the bundle declaratively via
7
+ * `static Definition.extensions` and the framework merges its contributions into
8
+ * the integration's effective definition at instantiation time.
9
+ *
10
+ * Extension bundle shape:
11
+ *
12
+ * {
13
+ * name: string, // required, unique within the API module
14
+ * routes?: Array<{ // optional, mounted alongside Definition.routes
15
+ * path: string,
16
+ * method: 'GET' | 'POST' | 'PUT' | 'DELETE' | ...,
17
+ * event: string // must exist in `events` below
18
+ * }>,
19
+ * events?: { // optional, merged into instance.events
20
+ * [eventName]: {
21
+ * type?: string, // e.g. 'LIFE_CYCLE_EVENT'
22
+ * handler: Function // default handler; integration may override per-binding
23
+ * }
24
+ * },
25
+ * queues?: Array<Object>, // reserved — Phase 2
26
+ * workers?: Array<Object> // reserved — Phase 2
27
+ * }
28
+ *
29
+ * Integration binding shape (on the integration's static Definition.extensions):
30
+ *
31
+ * extensions: {
32
+ * hubspotWebhooks: { // local binding name (developer's choice)
33
+ * extension: hubspot.extensions.webhooks,
34
+ * handlers: { // optional override map
35
+ * HUBSPOT_WEBHOOK: 'onHubSpotEvent' // event → method name on the integration
36
+ * }
37
+ * }
38
+ * }
39
+ *
40
+ * The same extension may be bound multiple times under different local names; the
41
+ * binding key is the developer-controlled local handle, not a global registry key.
42
+ */
43
+
44
+ const KNOWN_HTTP_METHODS = new Set([
45
+ 'get',
46
+ 'post',
47
+ 'put',
48
+ 'patch',
49
+ 'delete',
50
+ 'options',
51
+ 'head',
52
+ ]);
53
+
54
+ /**
55
+ * Validate the shape of an extension bundle and its binding.
56
+ *
57
+ * @param {Object} extension - The extension bundle to validate.
58
+ * @param {string} bindingName - The local binding key (for error context).
59
+ * @param {string} integrationName - The integration's Definition.name (for error context).
60
+ * @param {Object} [binding] - Optional full binding object; if provided, also validates binding.handlers.
61
+ * @throws {Error} If the extension is missing required fields or is internally inconsistent.
62
+ */
63
+ function validateExtensionBinding(extension, bindingName, integrationName, binding) {
64
+ const ctx = `Integration "${integrationName}" extension binding "${bindingName}"`;
65
+
66
+ if (!extension || typeof extension !== 'object') {
67
+ throw new Error(`${ctx}: extension must be an object`);
68
+ }
69
+ if (!extension.name || typeof extension.name !== 'string') {
70
+ throw new Error(`${ctx}: extension is missing required "name" field`);
71
+ }
72
+
73
+ const events = extension.events || {};
74
+ if (typeof events !== 'object' || Array.isArray(events)) {
75
+ throw new Error(
76
+ `${ctx}: extension "${extension.name}" "events" must be an object keyed by event name`
77
+ );
78
+ }
79
+
80
+ // Validate each event's shape — handler, when present, must be a function.
81
+ for (const [eventName, eventDef] of Object.entries(events)) {
82
+ if (!eventDef || typeof eventDef !== 'object') {
83
+ throw new Error(
84
+ `${ctx}: extension "${extension.name}" event "${eventName}" must be an object`
85
+ );
86
+ }
87
+ if (
88
+ eventDef.handler !== undefined &&
89
+ typeof eventDef.handler !== 'function'
90
+ ) {
91
+ throw new Error(
92
+ `${ctx}: extension "${extension.name}" event "${eventName}" "handler" must be a function`
93
+ );
94
+ }
95
+ }
96
+
97
+ const routes = extension.routes || [];
98
+ if (!Array.isArray(routes)) {
99
+ throw new Error(
100
+ `${ctx}: extension "${extension.name}" "routes" must be an array`
101
+ );
102
+ }
103
+
104
+ for (const route of routes) {
105
+ if (!route || typeof route !== 'object') {
106
+ throw new Error(
107
+ `${ctx}: extension "${extension.name}" has a malformed route entry`
108
+ );
109
+ }
110
+ if (typeof route.path !== 'string' || route.path.length === 0) {
111
+ throw new Error(
112
+ `${ctx}: extension "${extension.name}" route is missing "path"`
113
+ );
114
+ }
115
+ if (typeof route.method !== 'string') {
116
+ throw new Error(
117
+ `${ctx}: extension "${extension.name}" route "${route.path}" is missing "method"`
118
+ );
119
+ }
120
+ if (!KNOWN_HTTP_METHODS.has(route.method.toLowerCase())) {
121
+ throw new Error(
122
+ `${ctx}: extension "${extension.name}" route "${route.path}" has unsupported method "${route.method}"`
123
+ );
124
+ }
125
+ if (typeof route.event !== 'string' || route.event.length === 0) {
126
+ throw new Error(
127
+ `${ctx}: extension "${extension.name}" route "${route.path}" is missing "event"`
128
+ );
129
+ }
130
+ if (!Object.prototype.hasOwnProperty.call(events, route.event)) {
131
+ throw new Error(
132
+ `${ctx}: extension "${extension.name}" route "${route.path}" references event "${route.event}" which is not declared in extension.events`
133
+ );
134
+ }
135
+ }
136
+
137
+ // Validate binding.handlers if a binding was supplied.
138
+ if (binding && binding.handlers !== undefined) {
139
+ if (
140
+ typeof binding.handlers !== 'object' ||
141
+ Array.isArray(binding.handlers) ||
142
+ binding.handlers === null
143
+ ) {
144
+ throw new Error(
145
+ `${ctx}: "handlers" must be an object keyed by event name`
146
+ );
147
+ }
148
+ for (const [eventName, methodRef] of Object.entries(binding.handlers)) {
149
+ if (typeof methodRef !== 'string' || methodRef.length === 0) {
150
+ throw new Error(
151
+ `${ctx}: handler for event "${eventName}" must be a non-empty method name string (got ${typeof methodRef})`
152
+ );
153
+ }
154
+ if (!Object.prototype.hasOwnProperty.call(events, eventName)) {
155
+ throw new Error(
156
+ `${ctx}: binding.handlers references event "${eventName}" which is not declared in extension "${extension.name}".events — check for typos`
157
+ );
158
+ }
159
+ }
160
+ }
161
+ }
162
+
163
+ /**
164
+ * Get the flattened list of extension-contributed routes for an integration class.
165
+ * Each route carries the binding name and extension name alongside the route fields
166
+ * so the router builder can produce useful boot-time logs.
167
+ *
168
+ * @param {Function} IntegrationClass - A class extending IntegrationBase.
169
+ * @returns {Array<{bindingName: string, extensionName: string, path: string, method: string, event: string}>}
170
+ */
171
+ function getExtensionRoutes(IntegrationClass) {
172
+ const extensions = IntegrationClass?.Definition?.extensions || {};
173
+ const integrationName = IntegrationClass?.Definition?.name;
174
+ const flat = [];
175
+ for (const [bindingName, binding] of Object.entries(extensions)) {
176
+ // Fail fast: surface bad bindings at boot, not at first request.
177
+ // Mirrors the validation that _mergeExtensions does at instance time.
178
+ validateExtensionBinding(
179
+ binding && binding.extension,
180
+ bindingName,
181
+ integrationName,
182
+ binding
183
+ );
184
+ const routes = binding.extension.routes || [];
185
+ for (const route of routes) {
186
+ flat.push({
187
+ bindingName,
188
+ extensionName: binding.extension.name,
189
+ path: route.path,
190
+ method: route.method,
191
+ event: route.event,
192
+ });
193
+ }
194
+ }
195
+ return flat;
196
+ }
197
+
198
+ /**
199
+ * Get the flattened list of extension-contributed workers for an integration class.
200
+ * Reserved for Phase 2 — today the per-integration QueueWorker handles all events
201
+ * by name, so extension-contributed events flow through it without a dedicated worker.
202
+ *
203
+ * @param {Function} IntegrationClass - A class extending IntegrationBase.
204
+ * @returns {Array<Object>}
205
+ */
206
+ function getExtensionWorkers(IntegrationClass) {
207
+ const extensions = IntegrationClass?.Definition?.extensions || {};
208
+ const integrationName = IntegrationClass?.Definition?.name;
209
+ const flat = [];
210
+ for (const [bindingName, binding] of Object.entries(extensions)) {
211
+ validateExtensionBinding(
212
+ binding && binding.extension,
213
+ bindingName,
214
+ integrationName,
215
+ binding
216
+ );
217
+ const workers = binding.extension.workers || [];
218
+ for (const worker of workers) {
219
+ flat.push({
220
+ bindingName,
221
+ extensionName: binding.extension.name,
222
+ ...worker,
223
+ });
224
+ }
225
+ }
226
+ return flat;
227
+ }
228
+
229
+ module.exports = {
230
+ validateExtensionBinding,
231
+ getExtensionRoutes,
232
+ getExtensionWorkers,
233
+ };
@@ -10,6 +10,11 @@ const {
10
10
  const {
11
11
  LoadIntegrationContextUseCase,
12
12
  } = require('./use-cases/load-integration-context');
13
+ const {
14
+ validateExtensionBinding,
15
+ getExtensionRoutes,
16
+ getExtensionWorkers,
17
+ } = require('./extension');
13
18
 
14
19
  module.exports = {
15
20
  IntegrationBase,
@@ -18,4 +23,7 @@ module.exports = {
18
23
  checkRequiredParams,
19
24
  getModulesDefinitionFromIntegrationClasses,
20
25
  LoadIntegrationContextUseCase,
26
+ validateExtensionBinding,
27
+ getExtensionRoutes,
28
+ getExtensionWorkers,
21
29
  };
@@ -11,6 +11,7 @@ const {
11
11
  const {
12
12
  UpdateIntegrationMessages,
13
13
  } = require('./use-cases/update-integration-messages');
14
+ const { validateExtensionBinding } = require('./extension');
14
15
 
15
16
  const constantsToBeMigrated = {
16
17
  defaultEvents: {
@@ -60,6 +61,9 @@ class IntegrationBase {
60
61
  supportedVersions: [], // Eventually usable for deprecation and future test version purposes
61
62
 
62
63
  modules: {},
64
+ // Tier 3 Integration Extensions — see packages/core/integrations/EXTENSIONS.md
65
+ // Shape: { [bindingName]: { extension, handlers?: { [eventName]: methodName } } }
66
+ extensions: {},
63
67
  display: {
64
68
  name: 'Integration Name',
65
69
  logo: '',
@@ -430,6 +434,19 @@ class IntegrationBase {
430
434
  // Default: no-op, integrations override this
431
435
  }
432
436
 
437
+ /**
438
+ * Queue a webhook for asynchronous worker dispatch.
439
+ *
440
+ * The dispatch event defaults to `ON_WEBHOOK` for backward compatibility
441
+ * with the `Definition.webhooks: true` path. Extensions (and any caller
442
+ * that needs the worker to invoke a specific bound handler) can override
443
+ * by passing `event` in the payload — it's stripped from the payload and
444
+ * used as the SQS message's dispatch event.
445
+ *
446
+ * @param {Object} data - Webhook payload. May include `event` to override
447
+ * the default `ON_WEBHOOK` dispatch event. All other fields are passed
448
+ * through to the worker as the `data` field of the SQS message.
449
+ */
433
450
  async queueWebhook(data) {
434
451
  const { QueuerUtil } = require('../queues');
435
452
 
@@ -442,10 +459,12 @@ class IntegrationBase {
442
459
  throw new Error(`Queue URL not found for ${queueName}`);
443
460
  }
444
461
 
462
+ const { event: dispatchEvent, ...payload } = data || {};
463
+
445
464
  return QueuerUtil.send(
446
465
  {
447
- event: 'ON_WEBHOOK',
448
- data,
466
+ event: dispatchEvent || 'ON_WEBHOOK',
467
+ data: payload,
449
468
  },
450
469
  queueUrl
451
470
  );
@@ -504,6 +523,97 @@ class IntegrationBase {
504
523
  };
505
524
  }
506
525
 
526
+ /**
527
+ * Merge Tier 3 Integration Extension events into `this.events`.
528
+ *
529
+ * For each binding declared on `static Definition.extensions`, this method:
530
+ * 1. Validates the extension bundle shape and binding handlers
531
+ * 2. For each event the extension declares, resolves the handler in priority order:
532
+ * a. Subclass-defined `this.events[eventName]` (set in the constructor) — wins; if a
533
+ * binding tried to override that event with `handlers`, we log a warning so the
534
+ * author knows their override is shadowed.
535
+ * b. A method-name string in `binding.handlers[eventName]` → method on this instance
536
+ * c. The extension's own default `handler` function
537
+ * d. Otherwise throw — neither side provided a handler
538
+ * 3. Binds the resolved function to this instance and writes it to `this.events[eventName]`
539
+ *
540
+ * Two bindings declaring the same event throw a deterministic conflict error — silent
541
+ * "first/last writer wins" makes routing bugs nearly impossible to diagnose.
542
+ *
543
+ * @private
544
+ */
545
+ _mergeExtensions() {
546
+ const extensions = this.constructor.Definition?.extensions || {};
547
+ const integrationName = this.constructor.Definition?.name;
548
+ // Tracks which event names have been claimed by an extension binding during
549
+ // this merge — distinct from subclass-defined events on `this.events`.
550
+ const mergedByExtension = new Map();
551
+
552
+ for (const [bindingName, binding] of Object.entries(extensions)) {
553
+ if (!binding || typeof binding !== 'object') {
554
+ throw new Error(
555
+ `Integration "${integrationName}" extension binding "${bindingName}" must be an object`
556
+ );
557
+ }
558
+ const { extension, handlers = {} } = binding;
559
+ validateExtensionBinding(
560
+ extension,
561
+ bindingName,
562
+ integrationName,
563
+ binding
564
+ );
565
+
566
+ const extEvents = extension.events || {};
567
+ for (const [eventName, eventDef] of Object.entries(extEvents)) {
568
+ // Conflict detection: another extension binding already claimed this event name.
569
+ if (mergedByExtension.has(eventName)) {
570
+ const prev = mergedByExtension.get(eventName);
571
+ throw new Error(
572
+ `Integration "${integrationName}" extension event conflict: ` +
573
+ `event "${eventName}" is declared by both binding "${prev}" and binding "${bindingName}" — ` +
574
+ `use distinct event names per binding or omit duplicates`
575
+ );
576
+ }
577
+
578
+ // Subclass shadowing: if the subclass set this.events[eventName] before initialize(),
579
+ // it wins. Warn if the binding tried to wire an override that's now ignored.
580
+ if (this.events[eventName]) {
581
+ if (typeof handlers[eventName] === 'string') {
582
+ console.warn(
583
+ `[Frigg] Integration "${integrationName}" binding "${bindingName}": ` +
584
+ `handler "${handlers[eventName]}" for event "${eventName}" is ignored because ` +
585
+ `this.events["${eventName}"] was already set (subclass constructor or earlier merge)`
586
+ );
587
+ }
588
+ continue;
589
+ }
590
+
591
+ let fn;
592
+ const override = handlers[eventName];
593
+ if (typeof override === 'string') {
594
+ if (typeof this[override] !== 'function') {
595
+ throw new Error(
596
+ `Integration "${integrationName}" extension binding "${bindingName}": handler method "${override}" not found on instance`
597
+ );
598
+ }
599
+ fn = this[override];
600
+ } else if (typeof eventDef.handler === 'function') {
601
+ fn = eventDef.handler;
602
+ } else {
603
+ throw new Error(
604
+ `Extension "${extension.name}" event "${eventName}" has no default handler and binding "${bindingName}" did not provide one`
605
+ );
606
+ }
607
+
608
+ this.events[eventName] = {
609
+ type: eventDef.type,
610
+ handler: fn.bind(this),
611
+ };
612
+ mergedByExtension.set(eventName, bindingName);
613
+ }
614
+ }
615
+ }
616
+
507
617
  async initialize() {
508
618
  try {
509
619
  const additionalUserActions = await this.loadDynamicUserActions();
@@ -512,6 +622,7 @@ class IntegrationBase {
512
622
  this.addError(e);
513
623
  }
514
624
 
625
+ this._mergeExtensions();
515
626
  this.registerEventHandlers();
516
627
  }
517
628
 
@@ -0,0 +1,74 @@
1
+ /**
2
+ * Reverse-lookup use case: resolve an externalId (e.g. HubSpot portalId,
3
+ * Slack team_id, Asana workspace_id) to a single integration ID.
4
+ *
5
+ * Refuses to pick on ambiguous resolution at either layer:
6
+ * - more than one matching Entity row for the (externalId, moduleName) tuple
7
+ * - more than one Integration owning the matched entity
8
+ *
9
+ * A silent first-match in either case is a cross-tenant routing risk.
10
+ * Callers that expect a one-to-many fan-out should use
11
+ * {@link ListIntegrationsByEntityExternalIdUseCase} instead.
12
+ */
13
+ class FindIntegrationByEntityExternalIdUseCase {
14
+ constructor({ integrationRepository, moduleRepository } = {}) {
15
+ if (!integrationRepository) {
16
+ throw new Error('integrationRepository is required');
17
+ }
18
+ if (!moduleRepository) {
19
+ throw new Error('moduleRepository is required');
20
+ }
21
+ this.integrationRepository = integrationRepository;
22
+ this.moduleRepository = moduleRepository;
23
+ }
24
+
25
+ async execute({ externalId, moduleName } = {}) {
26
+ if (!externalId) return null;
27
+
28
+ const filter = { externalId: String(externalId) };
29
+ if (moduleName) filter.moduleName = moduleName;
30
+
31
+ const entities = await this.moduleRepository.findEntities(filter);
32
+ if (!entities || entities.length === 0) {
33
+ console.log(
34
+ `[Frigg] findIntegrationByEntityExternalId: no entity for externalId=${externalId}${
35
+ moduleName ? ` moduleName=${moduleName}` : ''
36
+ }`
37
+ );
38
+ return null;
39
+ }
40
+ if (entities.length > 1) {
41
+ const ids = entities.map((e) => e.id).join(', ');
42
+ throw new Error(
43
+ `findIntegrationByEntityExternalId: ambiguous resolution — externalId=${externalId}` +
44
+ `${moduleName ? ` moduleName=${moduleName}` : ''} matches ${entities.length} entities [${ids}]. ` +
45
+ `Refusing to pick one to avoid cross-tenant routing. ` +
46
+ `Pass a moduleName, or use listIntegrationsByEntityExternalId if multiple integrations are expected.`
47
+ );
48
+ }
49
+
50
+ const entity = entities[0];
51
+ const integrations =
52
+ await this.integrationRepository.findIntegrationsByEntityId(
53
+ entity.id
54
+ );
55
+ if (!integrations || integrations.length === 0) {
56
+ console.log(
57
+ `[Frigg] findIntegrationByEntityExternalId: entity ${entity.id} has no owning integrations (orphan)`
58
+ );
59
+ return null;
60
+ }
61
+ if (integrations.length > 1) {
62
+ const ids = integrations.map((i) => i.id).join(', ');
63
+ throw new Error(
64
+ `findIntegrationByEntityExternalId: ambiguous resolution — externalId=${externalId}` +
65
+ `${moduleName ? ` moduleName=${moduleName}` : ''} maps to ${integrations.length} integrations [${ids}]. ` +
66
+ `Refusing to pick one to avoid cross-tenant routing. ` +
67
+ `Use listIntegrationsByEntityExternalId if a one-to-many fan-out is intended.`
68
+ );
69
+ }
70
+ return integrations[0].id;
71
+ }
72
+ }
73
+
74
+ module.exports = { FindIntegrationByEntityExternalIdUseCase };
@@ -0,0 +1,46 @@
1
+ /**
2
+ * List all integration IDs whose module entities match an externalId.
3
+ *
4
+ * Use this instead of {@link FindIntegrationByEntityExternalIdUseCase} when a
5
+ * single externalId is *expected* to map to multiple integrations (intentional
6
+ * fan-out: one upstream account broadcasting to several Frigg integration
7
+ * records, possibly across tenants).
8
+ *
9
+ * Does not throw on ambiguous resolution — that's the whole point.
10
+ */
11
+ class ListIntegrationsByEntityExternalIdUseCase {
12
+ constructor({ integrationRepository, moduleRepository } = {}) {
13
+ if (!integrationRepository) {
14
+ throw new Error('integrationRepository is required');
15
+ }
16
+ if (!moduleRepository) {
17
+ throw new Error('moduleRepository is required');
18
+ }
19
+ this.integrationRepository = integrationRepository;
20
+ this.moduleRepository = moduleRepository;
21
+ }
22
+
23
+ async execute({ externalId, moduleName } = {}) {
24
+ if (!externalId) return [];
25
+
26
+ const filter = { externalId: String(externalId) };
27
+ if (moduleName) filter.moduleName = moduleName;
28
+
29
+ const entities = await this.moduleRepository.findEntities(filter);
30
+ if (!entities || entities.length === 0) return [];
31
+
32
+ const integrationIds = new Set();
33
+ for (const entity of entities) {
34
+ const owners =
35
+ await this.integrationRepository.findIntegrationsByEntityId(
36
+ entity.id
37
+ );
38
+ for (const integration of owners || []) {
39
+ integrationIds.add(integration.id);
40
+ }
41
+ }
42
+ return Array.from(integrationIds);
43
+ }
44
+ }
45
+
46
+ module.exports = { ListIntegrationsByEntityExternalIdUseCase };
@@ -100,6 +100,21 @@ class ModuleRepositoryDocumentDB extends ModuleRepositoryInterface {
100
100
  return this._mapEntity(doc, credential);
101
101
  }
102
102
 
103
+ async findEntities(filter) {
104
+ const query = this._buildFilter(filter);
105
+ const docs = await findMany(this.prisma, 'Entity', query);
106
+ if (!docs || docs.length === 0) return [];
107
+ const credentialMap = await this._fetchCredentialsBulk(
108
+ docs.map((doc) => doc.credentialId)
109
+ );
110
+ return docs.map((doc) =>
111
+ this._mapEntity(
112
+ doc,
113
+ credentialMap.get(fromObjectId(doc.credentialId)) || null
114
+ )
115
+ );
116
+ }
117
+
103
118
  async createEntity(entityData) {
104
119
  const {
105
120
  user,
@@ -91,6 +91,22 @@ class ModuleRepositoryInterface {
91
91
  throw new Error('Method findEntity must be implemented by subclass');
92
92
  }
93
93
 
94
+ /**
95
+ * Find all entities matching a filter.
96
+ *
97
+ * Symmetric with findEntity but returns the full match set. Use this when
98
+ * a single externalId may correspond to more than one Entity row
99
+ * (e.g. shared upstream account across tenants) and the caller needs to
100
+ * detect/handle the multi-match case explicitly.
101
+ *
102
+ * @param {Object} filter - Filter criteria
103
+ * @returns {Promise<Array>} Array of entity objects (empty if no match)
104
+ * @abstract
105
+ */
106
+ async findEntities(filter) {
107
+ throw new Error('Method findEntities must be implemented by subclass');
108
+ }
109
+
94
110
  /**
95
111
  * Create a new entity
96
112
  *
@@ -234,6 +234,34 @@ class ModuleRepositoryMongo extends ModuleRepositoryInterface {
234
234
  };
235
235
  }
236
236
 
237
+ /**
238
+ * Find all entities matching a filter.
239
+ *
240
+ * @param {Object} filter - Filter criteria
241
+ * @returns {Promise<Array>} Array of entity objects with string IDs (empty if no match)
242
+ */
243
+ async findEntities(filter) {
244
+ const where = this._convertFilterToWhere(filter);
245
+ const entities = await this.prisma.entity.findMany({
246
+ where,
247
+ });
248
+
249
+ const credentialIds = entities
250
+ .map((e) => e.credentialId)
251
+ .filter(Boolean);
252
+ const credentialMap = await this._fetchCredentialsBulk(credentialIds);
253
+
254
+ return entities.map((e) => ({
255
+ id: e.id,
256
+ credential: credentialMap.get(e.credentialId) || null,
257
+ userId: e.userId,
258
+ name: e.name,
259
+ externalId: e.externalId,
260
+ moduleName: e.moduleName,
261
+ ...(e.data || {}),
262
+ }));
263
+ }
264
+
237
265
  /**
238
266
  * Create a new entity
239
267
  * Replaces: Entity.create(entityData)
@@ -275,6 +275,34 @@ class ModuleRepositoryPostgres extends ModuleRepositoryInterface {
275
275
  };
276
276
  }
277
277
 
278
+ /**
279
+ * Find all entities matching a filter.
280
+ *
281
+ * @param {Object} filter - Filter criteria
282
+ * @returns {Promise<Array>} Array of entity objects with string IDs (empty if no match)
283
+ */
284
+ async findEntities(filter) {
285
+ const where = this._convertFilterToWhere(filter);
286
+ const entities = await this.prisma.entity.findMany({
287
+ where,
288
+ });
289
+
290
+ const credentialIds = entities
291
+ .map((e) => e.credentialId)
292
+ .filter(Boolean);
293
+ const credentialMap = await this._fetchCredentialsBulk(credentialIds);
294
+
295
+ return entities.map((e) => ({
296
+ id: e.id.toString(),
297
+ credential: credentialMap.get(e.credentialId) || null,
298
+ userId: e.userId?.toString(),
299
+ name: e.name,
300
+ externalId: e.externalId,
301
+ moduleName: e.moduleName,
302
+ ...(e.data || {}),
303
+ }));
304
+ }
305
+
278
306
  /**
279
307
  * Create a new entity
280
308
  * Replaces: Entity.create(entityData)
@@ -172,6 +172,30 @@ class ModuleRepository extends ModuleRepositoryInterface {
172
172
  };
173
173
  }
174
174
 
175
+ /**
176
+ * Find all entities matching a filter.
177
+ *
178
+ * @param {Object} filter - Filter criteria
179
+ * @returns {Promise<Array>} Array of entity objects (empty if no match)
180
+ */
181
+ async findEntities(filter) {
182
+ const where = this._convertFilterToWhere(filter);
183
+ const entities = await this.prisma.entity.findMany({
184
+ where,
185
+ include: { credential: true },
186
+ });
187
+
188
+ return entities.map((e) => ({
189
+ id: e.id,
190
+ credential: e.credential,
191
+ userId: e.userId,
192
+ name: e.name,
193
+ externalId: e.externalId,
194
+ moduleName: e.moduleName,
195
+ ...(e.data || {}),
196
+ }));
197
+ }
198
+
175
199
  /**
176
200
  * Create a new entity
177
201
  * Replaces: Entity.create(entityData)
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.87",
4
+ "version": "2.0.0-next.88",
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.87",
42
- "@friggframework/prettier-config": "2.0.0-next.87",
43
- "@friggframework/test": "2.0.0-next.87",
41
+ "@friggframework/eslint-config": "2.0.0-next.88",
42
+ "@friggframework/prettier-config": "2.0.0-next.88",
43
+ "@friggframework/test": "2.0.0-next.88",
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": "ee72b6f5349ee764da595f67cc6f184a42f763f1"
83
+ "gitHead": "613b1c6f878086fadaf5a400c4227e4911a06d3e"
84
84
  }