@ever-works/hubspot-connector-plugin 1.0.0
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/README.md +55 -0
- package/dist/index.cjs +487 -0
- package/dist/index.d.cts +157 -0
- package/dist/index.d.ts +157 -0
- package/dist/index.js +471 -0
- package/package.json +116 -0
package/README.md
ADDED
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
# @ever-works/hubspot-connector-plugin
|
|
2
|
+
|
|
3
|
+
First-party **HubSpot CRM connector** for the Ever Works platform, built on the
|
|
4
|
+
official [`@hubspot/api-client`](https://www.npmjs.com/package/@hubspot/api-client).
|
|
5
|
+
|
|
6
|
+
Capabilities: `connector`, `connector-hubspot`, `event-source`.
|
|
7
|
+
|
|
8
|
+
## What it does
|
|
9
|
+
|
|
10
|
+
- **Outbound message** — `send()` appends a Note engagement to a CRM record
|
|
11
|
+
(`targetConfig.associatedObjectId` / `settings.defaultAssociatedObjectId`),
|
|
12
|
+
associated with the record's HubSpot-defined note association type,
|
|
13
|
+
idempotent on `messageRef`.
|
|
14
|
+
- **Outbound record** — `createRecord()` writes a contact / company / deal (or
|
|
15
|
+
any custom object) via `crm.objects.basicApi.create`, idempotent on
|
|
16
|
+
`idempotencyKey`.
|
|
17
|
+
- **Event source** — `pullEvents()` sweeps each configured CRM object type in
|
|
18
|
+
turn through the Search API, filtered server-side on that type's
|
|
19
|
+
last-modified property, normalized into `IngestedEventEnvelope`s
|
|
20
|
+
(`hubspot.contact` / `hubspot.company` / `hubspot.deal`, custom objects fall
|
|
21
|
+
back to `hubspot.record`) for the event-ingest spine. With a `portalId`
|
|
22
|
+
configured each envelope carries a record deep link as `sourceUrl` so
|
|
23
|
+
Memory/Activity rows link back to the origin.
|
|
24
|
+
- **Opt-in historical backfill** — `backfillDays` (default `0` = off, max 90)
|
|
25
|
+
widens the FIRST pull's window only, bounded to
|
|
26
|
+
`HUBSPOT_BACKFILL_MAX_PAGES` search pages per object type.
|
|
27
|
+
|
|
28
|
+
## Degradation is loud, never silent
|
|
29
|
+
|
|
30
|
+
An unconfigured connector never quietly returns nothing:
|
|
31
|
+
|
|
32
|
+
- `verifyConnection()` reports the missing `accessToken` (and surfaces the
|
|
33
|
+
HubSpot error body when the read probe fails, e.g. a missing scope).
|
|
34
|
+
- `send()` / `createRecord()` throw on a missing token, a missing record id, or
|
|
35
|
+
an object type that has no note association (rather than writing an orphan
|
|
36
|
+
note).
|
|
37
|
+
- `pullEvents()` throws `EventSourceNotConfiguredError`.
|
|
38
|
+
|
|
39
|
+
## Settings
|
|
40
|
+
|
|
41
|
+
| Key | Notes |
|
|
42
|
+
| --------------------------- | ------------------------------------------------------------------- |
|
|
43
|
+
| `accessToken` | Private-app token — secret, env `HUBSPOT_ACCESS_TOKEN`. |
|
|
44
|
+
| `objectTypes` | Comma-separated sweep list (contacts, companies, deals when empty). |
|
|
45
|
+
| `portalId` | Portal (hub) id — enables record deep links on every envelope. |
|
|
46
|
+
| `backfillDays` | Opt-in first-pull history window (0 = off, max 90). |
|
|
47
|
+
| `defaultObjectType` | Default object type for `createRecord` + the verify probe. |
|
|
48
|
+
| `defaultAssociatedObjectId` | Default CRM record outbound notes attach to. |
|
|
49
|
+
|
|
50
|
+
No credential is ever hardcoded, logged, or written into an ingested envelope.
|
|
51
|
+
|
|
52
|
+
## Testing
|
|
53
|
+
|
|
54
|
+
Hermetic Vitest suite (`pnpm test`) — the SDK client is stubbed through the
|
|
55
|
+
`createClient` seam; no network calls.
|
package/dist/index.cjs
ADDED
|
@@ -0,0 +1,487 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
|
+
|
|
5
|
+
var crypto = require('crypto');
|
|
6
|
+
var apiClient = require('@hubspot/api-client');
|
|
7
|
+
require('@ever-works/contracts');
|
|
8
|
+
|
|
9
|
+
// src/hubspot-connector-plugin.ts
|
|
10
|
+
var EventSourceNotConfiguredError = class extends Error {
|
|
11
|
+
constructor(message) {
|
|
12
|
+
super(message ?? "Event-source plugin is not configured in this runtime.");
|
|
13
|
+
this.name = "EventSourceNotConfiguredError";
|
|
14
|
+
}
|
|
15
|
+
};
|
|
16
|
+
var PLUGIN_CAPABILITIES = {
|
|
17
|
+
// Connectors ("Connector fabric") — first-party BIDIRECTIONAL
|
|
18
|
+
// communication-channel plugins. Each connector declares `CONNECTOR`
|
|
19
|
+
// (umbrella, for discovery/grouping) plus its `CONNECTOR_<provider>`
|
|
20
|
+
// constant. Superset of the outbound-only notification channels; see
|
|
21
|
+
// `capabilities/connector.interface.ts`.
|
|
22
|
+
CONNECTOR: "connector",
|
|
23
|
+
// CRM / enrichment connectors — first-party native connectors over
|
|
24
|
+
// the vendors' own Node SDKs. Outbound writes CRM records/notes,
|
|
25
|
+
// the event-source leg streams record changes into the ingest spine.
|
|
26
|
+
CONNECTOR_HUBSPOT: "connector-hubspot",
|
|
27
|
+
// Event-ingest spine (Wave 6) — plugins that pull/push normalized
|
|
28
|
+
// external events into the platform ingest pipeline. See
|
|
29
|
+
// capabilities/event-source.interface.ts.
|
|
30
|
+
EVENT_SOURCE: "event-source"};
|
|
31
|
+
|
|
32
|
+
// src/hubspot-connector-plugin.ts
|
|
33
|
+
var HUBSPOT_EVENT_TEXT_MAX_CHARS = 4e3;
|
|
34
|
+
var HUBSPOT_PULL_PAGE_SIZE = 50;
|
|
35
|
+
var HUBSPOT_BACKFILL_MAX_PAGES = 10;
|
|
36
|
+
var HUBSPOT_DEFAULT_OBJECT_TYPES = ["contacts", "companies", "deals"];
|
|
37
|
+
var HUBSPOT_OBJECT_META = {
|
|
38
|
+
contacts: {
|
|
39
|
+
kind: "hubspot.contact",
|
|
40
|
+
lastModifiedProperty: "lastmodifieddate",
|
|
41
|
+
properties: ["firstname", "lastname", "email", "company", "lifecyclestage", "createdate", "lastmodifieddate"],
|
|
42
|
+
objectTypeId: "0-1",
|
|
43
|
+
noteAssociationTypeId: 202
|
|
44
|
+
},
|
|
45
|
+
companies: {
|
|
46
|
+
kind: "hubspot.company",
|
|
47
|
+
lastModifiedProperty: "hs_lastmodifieddate",
|
|
48
|
+
properties: ["name", "domain", "industry", "createdate", "hs_lastmodifieddate"],
|
|
49
|
+
objectTypeId: "0-2",
|
|
50
|
+
noteAssociationTypeId: 190
|
|
51
|
+
},
|
|
52
|
+
deals: {
|
|
53
|
+
kind: "hubspot.deal",
|
|
54
|
+
lastModifiedProperty: "hs_lastmodifieddate",
|
|
55
|
+
properties: ["dealname", "dealstage", "amount", "pipeline", "createdate", "hs_lastmodifieddate"],
|
|
56
|
+
objectTypeId: "0-3",
|
|
57
|
+
noteAssociationTypeId: 214
|
|
58
|
+
}
|
|
59
|
+
};
|
|
60
|
+
var GENERIC_OBJECT_META = {
|
|
61
|
+
kind: "hubspot.record",
|
|
62
|
+
lastModifiedProperty: "hs_lastmodifieddate",
|
|
63
|
+
properties: ["hs_object_id", "createdate", "hs_lastmodifieddate"]
|
|
64
|
+
};
|
|
65
|
+
function objectMeta(objectType) {
|
|
66
|
+
return HUBSPOT_OBJECT_META[objectType] ?? GENERIC_OBJECT_META;
|
|
67
|
+
}
|
|
68
|
+
function clampBackfillDays(value) {
|
|
69
|
+
const num = typeof value === "number" ? value : Number(value);
|
|
70
|
+
if (!Number.isFinite(num) || num <= 0) return 0;
|
|
71
|
+
return Math.min(Math.floor(num), 90);
|
|
72
|
+
}
|
|
73
|
+
function truncateText(text) {
|
|
74
|
+
return text.length > HUBSPOT_EVENT_TEXT_MAX_CHARS ? text.slice(0, HUBSPOT_EVENT_TEXT_MAX_CHARS) : text;
|
|
75
|
+
}
|
|
76
|
+
function resolveObjectTypes(settings) {
|
|
77
|
+
const raw = settings?.objectTypes;
|
|
78
|
+
if (typeof raw === "string" && raw.trim().length > 0) {
|
|
79
|
+
const parsed = raw.split(/[\s,]+/).map((t) => t.trim()).filter((t) => t.length > 0);
|
|
80
|
+
if (parsed.length > 0) return parsed;
|
|
81
|
+
}
|
|
82
|
+
return [...HUBSPOT_DEFAULT_OBJECT_TYPES];
|
|
83
|
+
}
|
|
84
|
+
function hubspotErrorMessage(err) {
|
|
85
|
+
const e = err;
|
|
86
|
+
return e.body?.message ?? e.message ?? (typeof e.code === "number" ? `HTTP ${e.code}` : "unknown error");
|
|
87
|
+
}
|
|
88
|
+
function toIso(value) {
|
|
89
|
+
if (value instanceof Date) return value.toISOString();
|
|
90
|
+
if (typeof value === "number" && Number.isFinite(value)) return new Date(value).toISOString();
|
|
91
|
+
if (typeof value === "string") {
|
|
92
|
+
const ms = Date.parse(value);
|
|
93
|
+
if (Number.isFinite(ms)) return new Date(ms).toISOString();
|
|
94
|
+
}
|
|
95
|
+
return (/* @__PURE__ */ new Date(0)).toISOString();
|
|
96
|
+
}
|
|
97
|
+
function parsePullCursor(cursor) {
|
|
98
|
+
if (!cursor) return void 0;
|
|
99
|
+
try {
|
|
100
|
+
const parsed = JSON.parse(cursor);
|
|
101
|
+
if (parsed && typeof parsed.t === "string" && parsed.t.length > 0 && typeof parsed.s === "string") {
|
|
102
|
+
return parsed;
|
|
103
|
+
}
|
|
104
|
+
} catch {
|
|
105
|
+
}
|
|
106
|
+
return void 0;
|
|
107
|
+
}
|
|
108
|
+
function resolveAccessToken(config, options) {
|
|
109
|
+
const candidates = [config.accessToken, options.settings?.accessToken];
|
|
110
|
+
for (const c of candidates) {
|
|
111
|
+
if (typeof c === "string" && c.length > 0) return c;
|
|
112
|
+
}
|
|
113
|
+
return void 0;
|
|
114
|
+
}
|
|
115
|
+
function firstString(candidates) {
|
|
116
|
+
for (const c of candidates) {
|
|
117
|
+
if (typeof c === "string" && c.trim().length > 0) return c.trim();
|
|
118
|
+
}
|
|
119
|
+
return void 0;
|
|
120
|
+
}
|
|
121
|
+
function resolveNoteTarget(config, options) {
|
|
122
|
+
const objectId = firstString([
|
|
123
|
+
config.associatedObjectId,
|
|
124
|
+
config.defaultAssociatedObjectId,
|
|
125
|
+
options.settings?.defaultAssociatedObjectId
|
|
126
|
+
]);
|
|
127
|
+
if (!objectId) {
|
|
128
|
+
throw new Error(
|
|
129
|
+
"hubspot-connector: a CRM record id is required for an outbound note (targetConfig.associatedObjectId or settings.defaultAssociatedObjectId)"
|
|
130
|
+
);
|
|
131
|
+
}
|
|
132
|
+
const objectType = firstString([config.associatedObjectType, config.defaultObjectType, options.settings?.defaultObjectType]) ?? "contacts";
|
|
133
|
+
return { objectType, objectId };
|
|
134
|
+
}
|
|
135
|
+
function recordTitle(objectType, properties) {
|
|
136
|
+
const props = properties ?? {};
|
|
137
|
+
const str = (key) => {
|
|
138
|
+
const value = props[key];
|
|
139
|
+
return typeof value === "string" && value.trim().length > 0 ? value.trim() : void 0;
|
|
140
|
+
};
|
|
141
|
+
if (objectType === "contacts") {
|
|
142
|
+
const name = [str("firstname"), str("lastname")].filter(Boolean).join(" ").trim();
|
|
143
|
+
return name.length > 0 ? name : str("email");
|
|
144
|
+
}
|
|
145
|
+
if (objectType === "companies") return str("name") ?? str("domain");
|
|
146
|
+
if (objectType === "deals") return str("dealname");
|
|
147
|
+
return str("name");
|
|
148
|
+
}
|
|
149
|
+
var HubSpotConnectorPlugin = class {
|
|
150
|
+
constructor() {
|
|
151
|
+
this.id = "hubspot-connector";
|
|
152
|
+
this.name = "HubSpot Connector";
|
|
153
|
+
this.version = "1.0.0";
|
|
154
|
+
this.category = "connector";
|
|
155
|
+
this.capabilities = [
|
|
156
|
+
PLUGIN_CAPABILITIES.CONNECTOR,
|
|
157
|
+
PLUGIN_CAPABILITIES.CONNECTOR_HUBSPOT,
|
|
158
|
+
PLUGIN_CAPABILITIES.EVENT_SOURCE
|
|
159
|
+
];
|
|
160
|
+
this.providerName = "hubspot";
|
|
161
|
+
this.connector = {
|
|
162
|
+
direction: "outbound",
|
|
163
|
+
transport: "poll",
|
|
164
|
+
flags: {
|
|
165
|
+
outboundMessage: true,
|
|
166
|
+
outboundRecord: true,
|
|
167
|
+
inbound: false,
|
|
168
|
+
reply: false,
|
|
169
|
+
pairing: false,
|
|
170
|
+
richOutbound: false
|
|
171
|
+
}
|
|
172
|
+
};
|
|
173
|
+
this.settingsSchema = {
|
|
174
|
+
type: "object",
|
|
175
|
+
required: ["accessToken"],
|
|
176
|
+
properties: {
|
|
177
|
+
accessToken: {
|
|
178
|
+
type: "string",
|
|
179
|
+
title: "HubSpot private-app access token (pat-\u2026)",
|
|
180
|
+
"x-secret": true,
|
|
181
|
+
"x-envVar": "HUBSPOT_ACCESS_TOKEN"
|
|
182
|
+
},
|
|
183
|
+
objectTypes: {
|
|
184
|
+
type: "string",
|
|
185
|
+
title: "CRM object types to ingest (comma-separated; contacts, companies, deals when empty)",
|
|
186
|
+
default: "contacts,companies,deals"
|
|
187
|
+
},
|
|
188
|
+
portalId: {
|
|
189
|
+
type: "string",
|
|
190
|
+
title: "HubSpot portal (hub) id \u2014 enables deep links back to each record"
|
|
191
|
+
},
|
|
192
|
+
backfillDays: {
|
|
193
|
+
type: "number",
|
|
194
|
+
title: "Historical backfill window in days on first pull (0 = off, max 90)",
|
|
195
|
+
default: 0,
|
|
196
|
+
minimum: 0,
|
|
197
|
+
maximum: 90
|
|
198
|
+
},
|
|
199
|
+
defaultObjectType: {
|
|
200
|
+
type: "string",
|
|
201
|
+
title: "Default CRM object type for record writes",
|
|
202
|
+
default: "contacts"
|
|
203
|
+
},
|
|
204
|
+
defaultAssociatedObjectId: {
|
|
205
|
+
type: "string",
|
|
206
|
+
title: "Default CRM record id outbound notes are attached to"
|
|
207
|
+
}
|
|
208
|
+
}
|
|
209
|
+
};
|
|
210
|
+
this.idempotencyCache = /* @__PURE__ */ new Map();
|
|
211
|
+
this.recordIdempotencyCache = /* @__PURE__ */ new Map();
|
|
212
|
+
}
|
|
213
|
+
async onLoad() {
|
|
214
|
+
}
|
|
215
|
+
async onUnload() {
|
|
216
|
+
this.idempotencyCache.clear();
|
|
217
|
+
this.recordIdempotencyCache.clear();
|
|
218
|
+
}
|
|
219
|
+
/** Testable seam — specs stub this; production uses the real SDK. */
|
|
220
|
+
createClient(accessToken) {
|
|
221
|
+
return new apiClient.Client({ accessToken });
|
|
222
|
+
}
|
|
223
|
+
async verifyConnection(config, options) {
|
|
224
|
+
const accessToken = resolveAccessToken(config, options);
|
|
225
|
+
if (!accessToken) {
|
|
226
|
+
return { valid: false, message: "accessToken is required" };
|
|
227
|
+
}
|
|
228
|
+
const objectTypes = resolveObjectTypes(options.settings);
|
|
229
|
+
const probeType = objectTypes[0];
|
|
230
|
+
try {
|
|
231
|
+
const page = await this.createClient(accessToken).crm.objects.basicApi.getPage(probeType, 1);
|
|
232
|
+
return {
|
|
233
|
+
valid: true,
|
|
234
|
+
details: {
|
|
235
|
+
probedObjectType: probeType,
|
|
236
|
+
objectTypes: objectTypes.join(","),
|
|
237
|
+
sampled: (page?.results ?? []).length
|
|
238
|
+
}
|
|
239
|
+
};
|
|
240
|
+
} catch (err) {
|
|
241
|
+
return {
|
|
242
|
+
valid: false,
|
|
243
|
+
message: `HubSpot ${probeType} read failed: ${hubspotErrorMessage(err)}`
|
|
244
|
+
};
|
|
245
|
+
}
|
|
246
|
+
}
|
|
247
|
+
/** Outbound leg — append a Note engagement to a CRM record. */
|
|
248
|
+
async send(payload, options) {
|
|
249
|
+
const config = payload.target ?? options.target ?? {};
|
|
250
|
+
const accessToken = resolveAccessToken(config, options);
|
|
251
|
+
if (!accessToken) {
|
|
252
|
+
throw new Error(
|
|
253
|
+
"hubspot-connector: an access token is required (targetConfig.accessToken or settings.accessToken)"
|
|
254
|
+
);
|
|
255
|
+
}
|
|
256
|
+
const { objectType, objectId } = resolveNoteTarget(config, options);
|
|
257
|
+
const associationTypeId = objectMeta(objectType).noteAssociationTypeId;
|
|
258
|
+
if (associationTypeId === void 0) {
|
|
259
|
+
throw new Error(
|
|
260
|
+
`hubspot-connector: outbound notes are not supported for object type '${objectType}' \u2014 use one of ${Object.keys(HUBSPOT_OBJECT_META).join(", ")}`
|
|
261
|
+
);
|
|
262
|
+
}
|
|
263
|
+
const cacheKey = `${options.connectorId ?? ""}\0${objectType}\0${objectId}\0${payload.messageRef}`;
|
|
264
|
+
const cached = this.idempotencyCache.get(cacheKey);
|
|
265
|
+
if (cached) return cached;
|
|
266
|
+
let noteId;
|
|
267
|
+
try {
|
|
268
|
+
const res = await this.createClient(accessToken).crm.objects.basicApi.create("notes", {
|
|
269
|
+
properties: {
|
|
270
|
+
hs_note_body: payload.text,
|
|
271
|
+
hs_timestamp: (/* @__PURE__ */ new Date()).toISOString()
|
|
272
|
+
},
|
|
273
|
+
associations: [
|
|
274
|
+
{
|
|
275
|
+
to: { id: objectId },
|
|
276
|
+
types: [{ associationCategory: "HUBSPOT_DEFINED", associationTypeId }]
|
|
277
|
+
}
|
|
278
|
+
]
|
|
279
|
+
});
|
|
280
|
+
noteId = typeof res?.id === "string" ? res.id : void 0;
|
|
281
|
+
} catch (err) {
|
|
282
|
+
throw new Error(`HubSpot note create failed: ${hubspotErrorMessage(err)}`);
|
|
283
|
+
}
|
|
284
|
+
const result = {
|
|
285
|
+
provider: this.id,
|
|
286
|
+
providerMessageId: noteId ?? `hubspot-${payload.messageRef}`,
|
|
287
|
+
deliveredAt: /* @__PURE__ */ new Date()
|
|
288
|
+
};
|
|
289
|
+
this.rememberSend(cacheKey, result);
|
|
290
|
+
return result;
|
|
291
|
+
}
|
|
292
|
+
/** Outbound record leg — create a CRM record of any object type. */
|
|
293
|
+
async createRecord(record, options) {
|
|
294
|
+
const config = options.target ?? {};
|
|
295
|
+
const accessToken = resolveAccessToken(config, options);
|
|
296
|
+
if (!accessToken) {
|
|
297
|
+
throw new Error(
|
|
298
|
+
"hubspot-connector: an access token is required (targetConfig.accessToken or settings.accessToken)"
|
|
299
|
+
);
|
|
300
|
+
}
|
|
301
|
+
const objectType = firstString([record.collection, config.defaultObjectType, options.settings?.defaultObjectType]) ?? "contacts";
|
|
302
|
+
const cacheKey = `${options.connectorId ?? ""}\0${objectType}\0${record.idempotencyKey}`;
|
|
303
|
+
const cached = this.recordIdempotencyCache.get(cacheKey);
|
|
304
|
+
if (cached) return cached;
|
|
305
|
+
let recordId;
|
|
306
|
+
try {
|
|
307
|
+
const res = await this.createClient(accessToken).crm.objects.basicApi.create(objectType, {
|
|
308
|
+
properties: record.fields
|
|
309
|
+
});
|
|
310
|
+
recordId = typeof res?.id === "string" ? res.id : void 0;
|
|
311
|
+
} catch (err) {
|
|
312
|
+
throw new Error(`HubSpot ${objectType} create failed: ${hubspotErrorMessage(err)}`);
|
|
313
|
+
}
|
|
314
|
+
if (!recordId) {
|
|
315
|
+
throw new Error(`HubSpot ${objectType} create returned no record id`);
|
|
316
|
+
}
|
|
317
|
+
const result = { provider: this.id, recordId };
|
|
318
|
+
this.recordIdempotencyCache.set(cacheKey, result);
|
|
319
|
+
if (this.recordIdempotencyCache.size > 500) {
|
|
320
|
+
const firstKey = this.recordIdempotencyCache.keys().next().value;
|
|
321
|
+
if (firstKey) this.recordIdempotencyCache.delete(firstKey);
|
|
322
|
+
}
|
|
323
|
+
return result;
|
|
324
|
+
}
|
|
325
|
+
rememberSend(cacheKey, result) {
|
|
326
|
+
this.idempotencyCache.set(cacheKey, result);
|
|
327
|
+
if (this.idempotencyCache.size > 500) {
|
|
328
|
+
const firstKey = this.idempotencyCache.keys().next().value;
|
|
329
|
+
if (firstKey) this.idempotencyCache.delete(firstKey);
|
|
330
|
+
}
|
|
331
|
+
}
|
|
332
|
+
// ── Event source (pull) ─────────────────────────────────────────────
|
|
333
|
+
/**
|
|
334
|
+
* Pull one search page of the current object-type phase since the
|
|
335
|
+
* effective watermark, normalized to envelopes. The returned cursor
|
|
336
|
+
* resumes the same phase (HubSpot `after` token) or advances to the
|
|
337
|
+
* next configured object type; no cursor means the sweep is done.
|
|
338
|
+
*
|
|
339
|
+
* First pull (epoch/absent watermark, no cursor): the window is
|
|
340
|
+
* `now - backfillDays` when the opt-in backfill is on, otherwise
|
|
341
|
+
* `now` — history stays untouched unless the user asked for it.
|
|
342
|
+
*/
|
|
343
|
+
async pullEvents(input) {
|
|
344
|
+
const accessToken = input.settings?.accessToken;
|
|
345
|
+
if (typeof accessToken !== "string" || accessToken.length === 0) {
|
|
346
|
+
throw new EventSourceNotConfiguredError(
|
|
347
|
+
"hubspot-connector: settings.accessToken is required to pull events"
|
|
348
|
+
);
|
|
349
|
+
}
|
|
350
|
+
const objectTypes = resolveObjectTypes(input.settings);
|
|
351
|
+
const cursor = parsePullCursor(input.cursor);
|
|
352
|
+
let objectType;
|
|
353
|
+
let after;
|
|
354
|
+
let since;
|
|
355
|
+
let backfill;
|
|
356
|
+
let pagesUsed;
|
|
357
|
+
if (cursor) {
|
|
358
|
+
objectType = cursor.t;
|
|
359
|
+
after = cursor.n;
|
|
360
|
+
since = cursor.s;
|
|
361
|
+
backfill = cursor.f === 1;
|
|
362
|
+
pagesUsed = cursor.b ?? 0;
|
|
363
|
+
} else {
|
|
364
|
+
const sinceMs = Date.parse(input.since);
|
|
365
|
+
const firstPull = !Number.isFinite(sinceMs) || sinceMs <= 0;
|
|
366
|
+
const backfillDays = clampBackfillDays(input.settings?.backfillDays);
|
|
367
|
+
if (firstPull) {
|
|
368
|
+
const start = backfillDays > 0 ? Date.now() - backfillDays * 864e5 : Date.now();
|
|
369
|
+
since = new Date(start).toISOString();
|
|
370
|
+
backfill = backfillDays > 0;
|
|
371
|
+
} else {
|
|
372
|
+
since = new Date(sinceMs).toISOString();
|
|
373
|
+
backfill = false;
|
|
374
|
+
}
|
|
375
|
+
objectType = objectTypes[0];
|
|
376
|
+
after = void 0;
|
|
377
|
+
pagesUsed = 0;
|
|
378
|
+
}
|
|
379
|
+
let typeIndex = objectTypes.indexOf(objectType);
|
|
380
|
+
if (typeIndex === -1) {
|
|
381
|
+
typeIndex = 0;
|
|
382
|
+
objectType = objectTypes[0];
|
|
383
|
+
after = void 0;
|
|
384
|
+
pagesUsed = 0;
|
|
385
|
+
}
|
|
386
|
+
const meta = objectMeta(objectType);
|
|
387
|
+
const portalId = typeof input.settings?.portalId === "string" ? input.settings.portalId : void 0;
|
|
388
|
+
const client = this.createClient(accessToken);
|
|
389
|
+
const page = await client.crm.objects.searchApi.doSearch(objectType, {
|
|
390
|
+
filterGroups: [
|
|
391
|
+
{
|
|
392
|
+
filters: [
|
|
393
|
+
{
|
|
394
|
+
propertyName: meta.lastModifiedProperty,
|
|
395
|
+
operator: "GTE",
|
|
396
|
+
value: String(Date.parse(since))
|
|
397
|
+
}
|
|
398
|
+
]
|
|
399
|
+
}
|
|
400
|
+
],
|
|
401
|
+
// No `sorts`: the SDK types it as `string[]` while the API expects
|
|
402
|
+
// sort objects, and ordering is not load-bearing here — the
|
|
403
|
+
// server-side filter guarantees every matching record is visited
|
|
404
|
+
// across the `after` pages, and the ingest pipeline dedupes on
|
|
405
|
+
// `(source, sourceEventId)`.
|
|
406
|
+
properties: [...meta.properties],
|
|
407
|
+
limit: HUBSPOT_PULL_PAGE_SIZE,
|
|
408
|
+
...after ? { after } : {}
|
|
409
|
+
});
|
|
410
|
+
const events = [];
|
|
411
|
+
for (const record of page.results ?? []) {
|
|
412
|
+
const envelope = this.normalizeRecord(record, objectType, since, portalId);
|
|
413
|
+
if (envelope) events.push(envelope);
|
|
414
|
+
}
|
|
415
|
+
const nextAfter = page.paging?.next?.after;
|
|
416
|
+
pagesUsed += 1;
|
|
417
|
+
const pageBudgetExhausted = backfill && pagesUsed >= HUBSPOT_BACKFILL_MAX_PAGES;
|
|
418
|
+
let next;
|
|
419
|
+
if (nextAfter && !pageBudgetExhausted) {
|
|
420
|
+
next = { t: objectType, n: nextAfter, s: since, ...backfill ? { f: 1 } : {}, b: pagesUsed };
|
|
421
|
+
} else if (typeIndex + 1 < objectTypes.length) {
|
|
422
|
+
next = { t: objectTypes[typeIndex + 1], s: since, ...backfill ? { f: 1 } : {}, b: 0 };
|
|
423
|
+
}
|
|
424
|
+
return next ? { events, nextCursor: JSON.stringify(next) } : { events };
|
|
425
|
+
}
|
|
426
|
+
normalizeRecord(record, objectType, since, portalId) {
|
|
427
|
+
if (!record.id) return null;
|
|
428
|
+
const meta = objectMeta(objectType);
|
|
429
|
+
const properties = record.properties ?? {};
|
|
430
|
+
const updatedAt = toIso(record.updatedAt ?? properties[meta.lastModifiedProperty] ?? record.createdAt);
|
|
431
|
+
const createdAt = toIso(record.createdAt ?? properties.createdate);
|
|
432
|
+
const changeType = Date.parse(createdAt) >= Date.parse(since) ? "created" : "updated";
|
|
433
|
+
const title = recordTitle(objectType, properties);
|
|
434
|
+
const sourceUrl = portalId && meta.objectTypeId ? `https://app.hubspot.com/contacts/${encodeURIComponent(portalId)}/record/${meta.objectTypeId}/${encodeURIComponent(record.id)}` : void 0;
|
|
435
|
+
return {
|
|
436
|
+
id: crypto.randomUUID(),
|
|
437
|
+
source: this.id,
|
|
438
|
+
sourceEventId: `${objectType}:${record.id}:${updatedAt}`,
|
|
439
|
+
kind: meta.kind,
|
|
440
|
+
occurredAt: updatedAt,
|
|
441
|
+
subject: {
|
|
442
|
+
type: "crm-record",
|
|
443
|
+
externalId: record.id,
|
|
444
|
+
...title ? { title } : {}
|
|
445
|
+
},
|
|
446
|
+
...sourceUrl ? { sourceUrl } : {},
|
|
447
|
+
payload: {
|
|
448
|
+
objectType,
|
|
449
|
+
recordId: record.id,
|
|
450
|
+
changeType,
|
|
451
|
+
createdAt,
|
|
452
|
+
updatedAt,
|
|
453
|
+
...record.archived === true ? { archived: true } : {},
|
|
454
|
+
properties: this.sanitizeProperties(properties, meta)
|
|
455
|
+
}
|
|
456
|
+
};
|
|
457
|
+
}
|
|
458
|
+
/**
|
|
459
|
+
* Payload properties are limited to the whitelist the sweep asked
|
|
460
|
+
* for and each string value is capped, so a CRM record with a
|
|
461
|
+
* huge free-text field can never blow the envelope byte budget.
|
|
462
|
+
*/
|
|
463
|
+
sanitizeProperties(properties, meta) {
|
|
464
|
+
const out = {};
|
|
465
|
+
for (const key of meta.properties) {
|
|
466
|
+
const value = properties[key];
|
|
467
|
+
if (value === void 0 || value === null) continue;
|
|
468
|
+
out[key] = typeof value === "string" ? truncateText(value) : value;
|
|
469
|
+
}
|
|
470
|
+
return out;
|
|
471
|
+
}
|
|
472
|
+
};
|
|
473
|
+
var hubspotConnectorPlugin = new HubSpotConnectorPlugin();
|
|
474
|
+
|
|
475
|
+
exports.HUBSPOT_BACKFILL_MAX_PAGES = HUBSPOT_BACKFILL_MAX_PAGES;
|
|
476
|
+
exports.HUBSPOT_DEFAULT_OBJECT_TYPES = HUBSPOT_DEFAULT_OBJECT_TYPES;
|
|
477
|
+
exports.HUBSPOT_EVENT_TEXT_MAX_CHARS = HUBSPOT_EVENT_TEXT_MAX_CHARS;
|
|
478
|
+
exports.HUBSPOT_OBJECT_META = HUBSPOT_OBJECT_META;
|
|
479
|
+
exports.HUBSPOT_PULL_PAGE_SIZE = HUBSPOT_PULL_PAGE_SIZE;
|
|
480
|
+
exports.HubSpotConnectorPlugin = HubSpotConnectorPlugin;
|
|
481
|
+
exports.clampBackfillDays = clampBackfillDays;
|
|
482
|
+
exports.default = HubSpotConnectorPlugin;
|
|
483
|
+
exports.hubspotConnectorPlugin = hubspotConnectorPlugin;
|
|
484
|
+
exports.objectMeta = objectMeta;
|
|
485
|
+
exports.recordTitle = recordTitle;
|
|
486
|
+
exports.resolveNoteTarget = resolveNoteTarget;
|
|
487
|
+
exports.resolveObjectTypes = resolveObjectTypes;
|