@clivly/sdk 0.1.0 → 0.3.0-next.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/README.md +197 -27
- package/dist/drizzle-meta-DMx_9nlj.mjs +4 -0
- package/dist/drizzle-meta-iAu8w1hj.cjs +9 -0
- package/dist/drizzle.cjs +107 -0
- package/dist/drizzle.d.cts +28 -0
- package/dist/drizzle.d.mts +28 -0
- package/dist/drizzle.mjs +105 -0
- package/dist/index.cjs +769 -0
- package/dist/index.d.cts +33 -0
- package/dist/index.d.mts +19 -124
- package/dist/index.mjs +522 -44
- package/dist/namespace-probe-DFpvfl7T.d.mts +299 -0
- package/dist/namespace-probe-DIBgCQca.d.cts +299 -0
- package/dist/vite.cjs +38 -0
- package/dist/vite.d.cts +23 -0
- package/dist/vite.d.mts +23 -0
- package/dist/vite.mjs +37 -0
- package/package.json +49 -8
|
@@ -0,0 +1,299 @@
|
|
|
1
|
+
import { ClivlyEntitiesConfig } from "@clivly/core";
|
|
2
|
+
|
|
3
|
+
//#region src/drizzle-meta.d.ts
|
|
4
|
+
declare const DRIZZLE_META: "__clivly_drizzle_source_meta__";
|
|
5
|
+
interface DrizzleSourceMeta {
|
|
6
|
+
/** Drizzle property keys of every column on the source table. */
|
|
7
|
+
columnKeys: string[];
|
|
8
|
+
/** Schema-v2 typed column metadata derived from the Drizzle table. */
|
|
9
|
+
columnsMeta?: DiscoveredColumn[];
|
|
10
|
+
cursorField: string;
|
|
11
|
+
idField: string;
|
|
12
|
+
/** The backing table's real name, so discovery can key metadata by table. */
|
|
13
|
+
tableName?: string;
|
|
14
|
+
}
|
|
15
|
+
//#endregion
|
|
16
|
+
//#region src/types.d.ts
|
|
17
|
+
interface DiscoveredColumn {
|
|
18
|
+
isForeignKey?: boolean;
|
|
19
|
+
isPrimaryKey?: boolean;
|
|
20
|
+
name: string;
|
|
21
|
+
nullable?: boolean;
|
|
22
|
+
/** The column this FK points at, when it is a foreign key. */
|
|
23
|
+
references?: {
|
|
24
|
+
table: string;
|
|
25
|
+
column: string;
|
|
26
|
+
};
|
|
27
|
+
/** SQL/driver type, e.g. "text", "integer", "timestamp". */
|
|
28
|
+
type?: string;
|
|
29
|
+
}
|
|
30
|
+
interface DiscoveredTable {
|
|
31
|
+
columns: string[];
|
|
32
|
+
/** Schema-v2 typed column metadata (types, PK/FK, nullability). */
|
|
33
|
+
columnsMeta?: DiscoveredColumn[];
|
|
34
|
+
name: string;
|
|
35
|
+
/** Approximate source row count, when cheaply derivable. */
|
|
36
|
+
rowCount?: number;
|
|
37
|
+
}
|
|
38
|
+
interface ClivlySDKConfig {
|
|
39
|
+
/** Org-scoped API token (sk_live_…) from Settings → API keys. */
|
|
40
|
+
apiKey: string;
|
|
41
|
+
/** Clivly API base URL. Defaults to https://api.clivly.com. */
|
|
42
|
+
apiUrl?: string;
|
|
43
|
+
/**
|
|
44
|
+
* The host table Clivly treats as the contact entity.
|
|
45
|
+
* @deprecated Prefer `entities` — the same mapping-derived shape
|
|
46
|
+
* `crm_entity_mappings` (and `@clivly/core`'s `mappingsToEntitiesConfig`)
|
|
47
|
+
* produce, so the SDK, the mappings table, and the sync engine share one
|
|
48
|
+
* model. A bare `contactEntity` is treated as a single contact entity.
|
|
49
|
+
*/
|
|
50
|
+
contactEntity?: string;
|
|
51
|
+
/** Which columns of the contact entity Clivly may read. */
|
|
52
|
+
contactFields?: string[];
|
|
53
|
+
/**
|
|
54
|
+
* Where per-entity delta cursors are persisted between syncs. Defaults to an
|
|
55
|
+
* in-memory store (fine for a long-lived process). On serverless/edge, pass a
|
|
56
|
+
* store backed by your own KV/DB so a cold start resumes delta sync instead of
|
|
57
|
+
* re-paging the whole entity. See `CursorStore`.
|
|
58
|
+
*/
|
|
59
|
+
cursorStore?: CursorStore;
|
|
60
|
+
/**
|
|
61
|
+
* Declarative entity config — the single source of truth shared with
|
|
62
|
+
* `crm_entity_mappings`. When set, discovery and heartbeat are derived from
|
|
63
|
+
* it (each entity's `source` table + mapped columns), superseding the flat
|
|
64
|
+
* `contactEntity`.
|
|
65
|
+
*/
|
|
66
|
+
entities?: ClivlyEntitiesConfig;
|
|
67
|
+
/**
|
|
68
|
+
* 🚧 Not implemented in 0.1.x. Reserved options for tunnel mode and CRM
|
|
69
|
+
* write-back — they have NO effect yet and land with the Cloudflare
|
|
70
|
+
* infrastructure. Grouped under `experimental` so the stable config surface
|
|
71
|
+
* above is unambiguous; nothing here is part of the supported API.
|
|
72
|
+
*/
|
|
73
|
+
experimental?: {
|
|
74
|
+
/** Cloudflare Tunnel token — reserved for tunnel mode. Not wired up yet. */tunnelToken?: string; /** CRM write-back target — reserved. Not wired up yet. */
|
|
75
|
+
writeBack?: {
|
|
76
|
+
enabled?: boolean; /** Only crm_* tables are ever accepted; this narrows further. */
|
|
77
|
+
tables?: string[];
|
|
78
|
+
};
|
|
79
|
+
};
|
|
80
|
+
/** Reported to the portal so it can show the detected stack. */
|
|
81
|
+
framework?: string;
|
|
82
|
+
/** Heartbeat cadence in ms. Defaults to 60s. */
|
|
83
|
+
heartbeatIntervalMs?: number;
|
|
84
|
+
/**
|
|
85
|
+
* Optional live namespace probe, used on-demand by `doctor()`'s "Namespace
|
|
86
|
+
* reachable" check to verify every required source table + column is
|
|
87
|
+
* genuinely reachable. Not consulted by the heartbeat. Build one with
|
|
88
|
+
* `drizzleIntrospector(db)` from `@clivly/sdk/drizzle`.
|
|
89
|
+
*/
|
|
90
|
+
introspector?: NamespaceIntrospector;
|
|
91
|
+
/**
|
|
92
|
+
* Self-reported readiness sent on every heartbeat as-is (no live check).
|
|
93
|
+
* For a real reachability check, configure `introspector` and run
|
|
94
|
+
* `doctor()`.
|
|
95
|
+
*/
|
|
96
|
+
namespaceReady?: boolean;
|
|
97
|
+
orm?: string;
|
|
98
|
+
/**
|
|
99
|
+
* Network retry policy for outbound calls (heartbeat + sync push). Transient
|
|
100
|
+
* failures (network errors, HTTP 429, HTTP 5xx) are retried with exponential
|
|
101
|
+
* backoff + jitter; a `Retry-After` header on a 429 is honoured. Non-transient
|
|
102
|
+
* responses (e.g. 4xx other than 429) are returned immediately, not retried.
|
|
103
|
+
*/
|
|
104
|
+
retry?: {
|
|
105
|
+
/** Total attempts including the first. Defaults to 3. Min 1. */maxAttempts?: number; /** Base backoff in ms (doubled each attempt). Defaults to 500. */
|
|
106
|
+
baseDelayMs?: number;
|
|
107
|
+
};
|
|
108
|
+
/** Tables/columns to report for entity discovery + mapping. */
|
|
109
|
+
schema?: DiscoveredTable[];
|
|
110
|
+
sdkVersion?: string;
|
|
111
|
+
/** Host entities to mirror into Clivly Cloud. */
|
|
112
|
+
source?: {
|
|
113
|
+
contacts: SyncSource;
|
|
114
|
+
companies?: SyncSource;
|
|
115
|
+
deals?: SyncSource; /** Org-defined custom objects to mirror. Each carries its object-type slug. */
|
|
116
|
+
custom?: Array<SyncSource & {
|
|
117
|
+
objectType: string;
|
|
118
|
+
}>;
|
|
119
|
+
};
|
|
120
|
+
sync?: {
|
|
121
|
+
onBoot?: boolean;
|
|
122
|
+
/**
|
|
123
|
+
* Sync cadence in ms for the background keep-alive loop. Defaults to
|
|
124
|
+
* `heartbeatIntervalMs` (60s) so sync and heartbeat share a tick unless you
|
|
125
|
+
* decouple them — e.g. a slow full-mirror alongside frequent heartbeats.
|
|
126
|
+
*/
|
|
127
|
+
intervalMs?: number;
|
|
128
|
+
batchSize?: number;
|
|
129
|
+
};
|
|
130
|
+
/**
|
|
131
|
+
* The remote sync trigger: how Clivly Cloud asks this app to run a sync.
|
|
132
|
+
*
|
|
133
|
+
* Without it, cloud mode is push-only — Clivly can watch the app but can't
|
|
134
|
+
* start anything, so the dashboard's "Run sync" has nothing to call. With it,
|
|
135
|
+
* the SDK reports `url` on each heartbeat and requires every trigger request to
|
|
136
|
+
* be signed with `secret`.
|
|
137
|
+
*/
|
|
138
|
+
syncTrigger?: {
|
|
139
|
+
/**
|
|
140
|
+
* Public URL of the route where `createClivlyHandler()` is mounted, e.g.
|
|
141
|
+
* `https://app.example.com/api/clivly/tick`. Reported to Clivly on each
|
|
142
|
+
* heartbeat; a URL set in the dashboard overrides it.
|
|
143
|
+
*/
|
|
144
|
+
url?: string;
|
|
145
|
+
/**
|
|
146
|
+
* Signing secret (`whsec_…`) issued by Clivly under Integrations → Remote
|
|
147
|
+
* sync. When set, `createClivlyHandler()` rejects any request that isn't
|
|
148
|
+
* validly signed. Leave unset only if the route is already unreachable from
|
|
149
|
+
* the public internet.
|
|
150
|
+
*/
|
|
151
|
+
secret?: string;
|
|
152
|
+
};
|
|
153
|
+
/**
|
|
154
|
+
* Shared secret Clivly Cloud presents (as `Authorization: Bearer <token>`) when
|
|
155
|
+
* calling the `/clivly/auth/verify` endpoint. Required for the verify handler to
|
|
156
|
+
* authenticate the caller; without it the handler fails closed (HTTP 503).
|
|
157
|
+
*/
|
|
158
|
+
verifyToken?: string;
|
|
159
|
+
}
|
|
160
|
+
interface ClivlyUser {
|
|
161
|
+
email: string;
|
|
162
|
+
id: string;
|
|
163
|
+
name?: string;
|
|
164
|
+
}
|
|
165
|
+
interface DoctorCheck {
|
|
166
|
+
/** Human-readable outcome — what passed, or why it failed. */
|
|
167
|
+
detail: string;
|
|
168
|
+
/** Short label, e.g. "API key present". */
|
|
169
|
+
name: string;
|
|
170
|
+
/** True when the check passed. Ignored when `skipped` is true. */
|
|
171
|
+
ok: boolean;
|
|
172
|
+
/** True when the check could not run (e.g. a hand-written source). */
|
|
173
|
+
skipped?: boolean;
|
|
174
|
+
}
|
|
175
|
+
interface DoctorReport {
|
|
176
|
+
checks: DoctorCheck[];
|
|
177
|
+
/** AND of every non-skipped check's `ok`. */
|
|
178
|
+
ok: boolean;
|
|
179
|
+
}
|
|
180
|
+
type ClivlyConnectReason = "ok" | "invalid_api_key" | "forbidden" | "rate_limited" | "server_error" | "client_error" | "network_error";
|
|
181
|
+
interface ClivlyConnectResult {
|
|
182
|
+
ok: boolean;
|
|
183
|
+
/** Org resolved from a successful heartbeat, when the server returns it. */
|
|
184
|
+
org?: {
|
|
185
|
+
id?: string;
|
|
186
|
+
name?: string;
|
|
187
|
+
};
|
|
188
|
+
reason: ClivlyConnectReason;
|
|
189
|
+
/** Whether a retry could plausibly succeed (network, 429, 5xx). */
|
|
190
|
+
retryable: boolean;
|
|
191
|
+
/** HTTP status, or null when the request never completed (network error). */
|
|
192
|
+
status: number | null;
|
|
193
|
+
}
|
|
194
|
+
interface SyncCursor {
|
|
195
|
+
/** The row's id, breaking ties when timestamps collide. */
|
|
196
|
+
id: string;
|
|
197
|
+
/** The row's cursor-field value (typically `updated_at`). */
|
|
198
|
+
time: Date;
|
|
199
|
+
}
|
|
200
|
+
interface SyncSource {
|
|
201
|
+
cursorField: string;
|
|
202
|
+
entity: string;
|
|
203
|
+
fetchPage(args: {
|
|
204
|
+
since: SyncCursor | null;
|
|
205
|
+
limit: number;
|
|
206
|
+
}): Promise<Record<string, unknown>[]>;
|
|
207
|
+
/** Stable, unique tie-breaker column for keyset paging. Defaults to "id". */
|
|
208
|
+
idField?: string;
|
|
209
|
+
/** Set by `fromDrizzle`; lets `doctor` introspect the backing table. */
|
|
210
|
+
[DRIZZLE_META]?: DrizzleSourceMeta;
|
|
211
|
+
}
|
|
212
|
+
type SyncMode = "delta" | "full";
|
|
213
|
+
interface CursorStore {
|
|
214
|
+
get(entity: string): Promise<SyncCursor | null> | SyncCursor | null;
|
|
215
|
+
set(entity: string, cursor: SyncCursor | null): Promise<void> | void;
|
|
216
|
+
}
|
|
217
|
+
interface ClivlySDK {
|
|
218
|
+
/**
|
|
219
|
+
* Send a single heartbeat and report the outcome richly: `ok`, the HTTP
|
|
220
|
+
* `status` (null on network failure), a coarse `reason`, whether it's
|
|
221
|
+
* `retryable`, and the resolved `org` when the server returns it. Never throws
|
|
222
|
+
* — inspect the result, or turn a failure into an exception with
|
|
223
|
+
* `ClivlyError.from(result)`. Prefer this over `heartbeat()` for onboarding,
|
|
224
|
+
* health checks, and one-shot serverless calls.
|
|
225
|
+
*/
|
|
226
|
+
connect(): Promise<ClivlyConnectResult>;
|
|
227
|
+
/**
|
|
228
|
+
* Build the `/clivly/auth/verify` handler the developer mounts in their app.
|
|
229
|
+
* Clivly's cloud calls it to validate a CRM user's identity against the host
|
|
230
|
+
* app's session. Pass a resolver that reads the host session from the request.
|
|
231
|
+
*
|
|
232
|
+
* The handler authenticates the caller against `config.verifyToken` (presented
|
|
233
|
+
* as `Authorization: Bearer <token>`) before invoking the resolver, so the
|
|
234
|
+
* endpoint can't be used by third parties to probe host sessions. It fails
|
|
235
|
+
* closed with HTTP 503 if `verifyToken` is unset (a missing config value, not a
|
|
236
|
+
* server fault — so it won't trip the host's 5xx error alerting).
|
|
237
|
+
*/
|
|
238
|
+
createAuthVerifyHandler(resolveUser: (req: Request) => Promise<ClivlyUser | null> | ClivlyUser | null): (req: Request) => Promise<Response>;
|
|
239
|
+
/**
|
|
240
|
+
* A fetch handler to mount at a route. Each request runs one `runScheduled()`
|
|
241
|
+
* and returns JSON: 200 when the sync succeeded, 500 otherwise.
|
|
242
|
+
*
|
|
243
|
+
* Two callers use it: Clivly Cloud (when someone clicks "Run sync" — those
|
|
244
|
+
* requests are HMAC-signed and carry a run id), and the host's own scheduler
|
|
245
|
+
* (Workers cron, an uptime ping — no body).
|
|
246
|
+
*
|
|
247
|
+
* When `syncTrigger.secret` is set the handler rejects unsigned or
|
|
248
|
+
* badly-signed requests with 401. When it isn't, the route stays open to
|
|
249
|
+
* anyone who can reach it — set a secret if it's publicly routable.
|
|
250
|
+
*/
|
|
251
|
+
createClivlyHandler(): (req: Request) => Promise<Response>;
|
|
252
|
+
/** Validate this setup end-to-end (read-only) before the first sync. */
|
|
253
|
+
doctor(): Promise<DoctorReport>;
|
|
254
|
+
/**
|
|
255
|
+
* Send a single heartbeat now. Returns true on success. Kept for the keep-alive
|
|
256
|
+
* loop and back-compat; use `connect()` when you need the failure reason.
|
|
257
|
+
*/
|
|
258
|
+
heartbeat(): Promise<boolean>;
|
|
259
|
+
/**
|
|
260
|
+
* One-shot heartbeat + a single sync, with no keep-alive timers. The correct
|
|
261
|
+
* entry point on serverless/edge (Workers, Vercel, Lambda), where `start()`'s
|
|
262
|
+
* `setInterval` loop can't persist between invocations — call this from a
|
|
263
|
+
* scheduled/cron trigger. Throws if a sync push fails (so the run is marked
|
|
264
|
+
* failed); heartbeat is best-effort.
|
|
265
|
+
*/
|
|
266
|
+
runScheduled(options?: {
|
|
267
|
+
mode?: SyncMode;
|
|
268
|
+
runId?: string;
|
|
269
|
+
}): Promise<void>;
|
|
270
|
+
/**
|
|
271
|
+
* Send the initial heartbeat and start the keep-alive loop. For long-lived
|
|
272
|
+
* hosts only — on serverless/edge the interval can't persist, so prefer
|
|
273
|
+
* `runScheduled()` / `createClivlyHandler()`. Emits a warning if a serverless
|
|
274
|
+
* runtime is detected.
|
|
275
|
+
*/
|
|
276
|
+
start(): Promise<void>;
|
|
277
|
+
/** Stop the keep-alive loop. */
|
|
278
|
+
stop(): void;
|
|
279
|
+
/**
|
|
280
|
+
* Sync all configured sources now. `delta` (default) pages changed rows since
|
|
281
|
+
* the last cursor; `full` pushes each complete entity so the server can
|
|
282
|
+
* reconcile leavers (archive-on-leave). Throws if a push fails, so callers see
|
|
283
|
+
* the failure — the background keep-alive loop swallows and retries instead.
|
|
284
|
+
*/
|
|
285
|
+
sync(options?: {
|
|
286
|
+
mode?: SyncMode;
|
|
287
|
+
runId?: string;
|
|
288
|
+
}): Promise<void>;
|
|
289
|
+
}
|
|
290
|
+
//#endregion
|
|
291
|
+
//#region src/namespace-probe.d.ts
|
|
292
|
+
interface NamespaceIntrospector {
|
|
293
|
+
/** All base table names in the reachable schema. */
|
|
294
|
+
listTables(): Promise<string[]>;
|
|
295
|
+
/** Prove a table is genuinely selectable (SELECT ... LIMIT 1). Throws if unreachable/denied. */
|
|
296
|
+
sampleSelect(table: string): Promise<void>;
|
|
297
|
+
}
|
|
298
|
+
//#endregion
|
|
299
|
+
export { ClivlySDKConfig as a, DiscoveredTable as c, SyncSource as d, ClivlySDK as i, DoctorCheck as l, ClivlyConnectReason as n, ClivlyUser as o, ClivlyConnectResult as r, CursorStore as s, NamespaceIntrospector as t, DoctorReport as u };
|
|
@@ -0,0 +1,299 @@
|
|
|
1
|
+
import { ClivlyEntitiesConfig } from "@clivly/core";
|
|
2
|
+
|
|
3
|
+
//#region src/drizzle-meta.d.ts
|
|
4
|
+
declare const DRIZZLE_META: "__clivly_drizzle_source_meta__";
|
|
5
|
+
interface DrizzleSourceMeta {
|
|
6
|
+
/** Drizzle property keys of every column on the source table. */
|
|
7
|
+
columnKeys: string[];
|
|
8
|
+
/** Schema-v2 typed column metadata derived from the Drizzle table. */
|
|
9
|
+
columnsMeta?: DiscoveredColumn[];
|
|
10
|
+
cursorField: string;
|
|
11
|
+
idField: string;
|
|
12
|
+
/** The backing table's real name, so discovery can key metadata by table. */
|
|
13
|
+
tableName?: string;
|
|
14
|
+
}
|
|
15
|
+
//#endregion
|
|
16
|
+
//#region src/types.d.ts
|
|
17
|
+
interface DiscoveredColumn {
|
|
18
|
+
isForeignKey?: boolean;
|
|
19
|
+
isPrimaryKey?: boolean;
|
|
20
|
+
name: string;
|
|
21
|
+
nullable?: boolean;
|
|
22
|
+
/** The column this FK points at, when it is a foreign key. */
|
|
23
|
+
references?: {
|
|
24
|
+
table: string;
|
|
25
|
+
column: string;
|
|
26
|
+
};
|
|
27
|
+
/** SQL/driver type, e.g. "text", "integer", "timestamp". */
|
|
28
|
+
type?: string;
|
|
29
|
+
}
|
|
30
|
+
interface DiscoveredTable {
|
|
31
|
+
columns: string[];
|
|
32
|
+
/** Schema-v2 typed column metadata (types, PK/FK, nullability). */
|
|
33
|
+
columnsMeta?: DiscoveredColumn[];
|
|
34
|
+
name: string;
|
|
35
|
+
/** Approximate source row count, when cheaply derivable. */
|
|
36
|
+
rowCount?: number;
|
|
37
|
+
}
|
|
38
|
+
interface ClivlySDKConfig {
|
|
39
|
+
/** Org-scoped API token (sk_live_…) from Settings → API keys. */
|
|
40
|
+
apiKey: string;
|
|
41
|
+
/** Clivly API base URL. Defaults to https://api.clivly.com. */
|
|
42
|
+
apiUrl?: string;
|
|
43
|
+
/**
|
|
44
|
+
* The host table Clivly treats as the contact entity.
|
|
45
|
+
* @deprecated Prefer `entities` — the same mapping-derived shape
|
|
46
|
+
* `crm_entity_mappings` (and `@clivly/core`'s `mappingsToEntitiesConfig`)
|
|
47
|
+
* produce, so the SDK, the mappings table, and the sync engine share one
|
|
48
|
+
* model. A bare `contactEntity` is treated as a single contact entity.
|
|
49
|
+
*/
|
|
50
|
+
contactEntity?: string;
|
|
51
|
+
/** Which columns of the contact entity Clivly may read. */
|
|
52
|
+
contactFields?: string[];
|
|
53
|
+
/**
|
|
54
|
+
* Where per-entity delta cursors are persisted between syncs. Defaults to an
|
|
55
|
+
* in-memory store (fine for a long-lived process). On serverless/edge, pass a
|
|
56
|
+
* store backed by your own KV/DB so a cold start resumes delta sync instead of
|
|
57
|
+
* re-paging the whole entity. See `CursorStore`.
|
|
58
|
+
*/
|
|
59
|
+
cursorStore?: CursorStore;
|
|
60
|
+
/**
|
|
61
|
+
* Declarative entity config — the single source of truth shared with
|
|
62
|
+
* `crm_entity_mappings`. When set, discovery and heartbeat are derived from
|
|
63
|
+
* it (each entity's `source` table + mapped columns), superseding the flat
|
|
64
|
+
* `contactEntity`.
|
|
65
|
+
*/
|
|
66
|
+
entities?: ClivlyEntitiesConfig;
|
|
67
|
+
/**
|
|
68
|
+
* 🚧 Not implemented in 0.1.x. Reserved options for tunnel mode and CRM
|
|
69
|
+
* write-back — they have NO effect yet and land with the Cloudflare
|
|
70
|
+
* infrastructure. Grouped under `experimental` so the stable config surface
|
|
71
|
+
* above is unambiguous; nothing here is part of the supported API.
|
|
72
|
+
*/
|
|
73
|
+
experimental?: {
|
|
74
|
+
/** Cloudflare Tunnel token — reserved for tunnel mode. Not wired up yet. */tunnelToken?: string; /** CRM write-back target — reserved. Not wired up yet. */
|
|
75
|
+
writeBack?: {
|
|
76
|
+
enabled?: boolean; /** Only crm_* tables are ever accepted; this narrows further. */
|
|
77
|
+
tables?: string[];
|
|
78
|
+
};
|
|
79
|
+
};
|
|
80
|
+
/** Reported to the portal so it can show the detected stack. */
|
|
81
|
+
framework?: string;
|
|
82
|
+
/** Heartbeat cadence in ms. Defaults to 60s. */
|
|
83
|
+
heartbeatIntervalMs?: number;
|
|
84
|
+
/**
|
|
85
|
+
* Optional live namespace probe, used on-demand by `doctor()`'s "Namespace
|
|
86
|
+
* reachable" check to verify every required source table + column is
|
|
87
|
+
* genuinely reachable. Not consulted by the heartbeat. Build one with
|
|
88
|
+
* `drizzleIntrospector(db)` from `@clivly/sdk/drizzle`.
|
|
89
|
+
*/
|
|
90
|
+
introspector?: NamespaceIntrospector;
|
|
91
|
+
/**
|
|
92
|
+
* Self-reported readiness sent on every heartbeat as-is (no live check).
|
|
93
|
+
* For a real reachability check, configure `introspector` and run
|
|
94
|
+
* `doctor()`.
|
|
95
|
+
*/
|
|
96
|
+
namespaceReady?: boolean;
|
|
97
|
+
orm?: string;
|
|
98
|
+
/**
|
|
99
|
+
* Network retry policy for outbound calls (heartbeat + sync push). Transient
|
|
100
|
+
* failures (network errors, HTTP 429, HTTP 5xx) are retried with exponential
|
|
101
|
+
* backoff + jitter; a `Retry-After` header on a 429 is honoured. Non-transient
|
|
102
|
+
* responses (e.g. 4xx other than 429) are returned immediately, not retried.
|
|
103
|
+
*/
|
|
104
|
+
retry?: {
|
|
105
|
+
/** Total attempts including the first. Defaults to 3. Min 1. */maxAttempts?: number; /** Base backoff in ms (doubled each attempt). Defaults to 500. */
|
|
106
|
+
baseDelayMs?: number;
|
|
107
|
+
};
|
|
108
|
+
/** Tables/columns to report for entity discovery + mapping. */
|
|
109
|
+
schema?: DiscoveredTable[];
|
|
110
|
+
sdkVersion?: string;
|
|
111
|
+
/** Host entities to mirror into Clivly Cloud. */
|
|
112
|
+
source?: {
|
|
113
|
+
contacts: SyncSource;
|
|
114
|
+
companies?: SyncSource;
|
|
115
|
+
deals?: SyncSource; /** Org-defined custom objects to mirror. Each carries its object-type slug. */
|
|
116
|
+
custom?: Array<SyncSource & {
|
|
117
|
+
objectType: string;
|
|
118
|
+
}>;
|
|
119
|
+
};
|
|
120
|
+
sync?: {
|
|
121
|
+
onBoot?: boolean;
|
|
122
|
+
/**
|
|
123
|
+
* Sync cadence in ms for the background keep-alive loop. Defaults to
|
|
124
|
+
* `heartbeatIntervalMs` (60s) so sync and heartbeat share a tick unless you
|
|
125
|
+
* decouple them — e.g. a slow full-mirror alongside frequent heartbeats.
|
|
126
|
+
*/
|
|
127
|
+
intervalMs?: number;
|
|
128
|
+
batchSize?: number;
|
|
129
|
+
};
|
|
130
|
+
/**
|
|
131
|
+
* The remote sync trigger: how Clivly Cloud asks this app to run a sync.
|
|
132
|
+
*
|
|
133
|
+
* Without it, cloud mode is push-only — Clivly can watch the app but can't
|
|
134
|
+
* start anything, so the dashboard's "Run sync" has nothing to call. With it,
|
|
135
|
+
* the SDK reports `url` on each heartbeat and requires every trigger request to
|
|
136
|
+
* be signed with `secret`.
|
|
137
|
+
*/
|
|
138
|
+
syncTrigger?: {
|
|
139
|
+
/**
|
|
140
|
+
* Public URL of the route where `createClivlyHandler()` is mounted, e.g.
|
|
141
|
+
* `https://app.example.com/api/clivly/tick`. Reported to Clivly on each
|
|
142
|
+
* heartbeat; a URL set in the dashboard overrides it.
|
|
143
|
+
*/
|
|
144
|
+
url?: string;
|
|
145
|
+
/**
|
|
146
|
+
* Signing secret (`whsec_…`) issued by Clivly under Integrations → Remote
|
|
147
|
+
* sync. When set, `createClivlyHandler()` rejects any request that isn't
|
|
148
|
+
* validly signed. Leave unset only if the route is already unreachable from
|
|
149
|
+
* the public internet.
|
|
150
|
+
*/
|
|
151
|
+
secret?: string;
|
|
152
|
+
};
|
|
153
|
+
/**
|
|
154
|
+
* Shared secret Clivly Cloud presents (as `Authorization: Bearer <token>`) when
|
|
155
|
+
* calling the `/clivly/auth/verify` endpoint. Required for the verify handler to
|
|
156
|
+
* authenticate the caller; without it the handler fails closed (HTTP 503).
|
|
157
|
+
*/
|
|
158
|
+
verifyToken?: string;
|
|
159
|
+
}
|
|
160
|
+
interface ClivlyUser {
|
|
161
|
+
email: string;
|
|
162
|
+
id: string;
|
|
163
|
+
name?: string;
|
|
164
|
+
}
|
|
165
|
+
interface DoctorCheck {
|
|
166
|
+
/** Human-readable outcome — what passed, or why it failed. */
|
|
167
|
+
detail: string;
|
|
168
|
+
/** Short label, e.g. "API key present". */
|
|
169
|
+
name: string;
|
|
170
|
+
/** True when the check passed. Ignored when `skipped` is true. */
|
|
171
|
+
ok: boolean;
|
|
172
|
+
/** True when the check could not run (e.g. a hand-written source). */
|
|
173
|
+
skipped?: boolean;
|
|
174
|
+
}
|
|
175
|
+
interface DoctorReport {
|
|
176
|
+
checks: DoctorCheck[];
|
|
177
|
+
/** AND of every non-skipped check's `ok`. */
|
|
178
|
+
ok: boolean;
|
|
179
|
+
}
|
|
180
|
+
type ClivlyConnectReason = "ok" | "invalid_api_key" | "forbidden" | "rate_limited" | "server_error" | "client_error" | "network_error";
|
|
181
|
+
interface ClivlyConnectResult {
|
|
182
|
+
ok: boolean;
|
|
183
|
+
/** Org resolved from a successful heartbeat, when the server returns it. */
|
|
184
|
+
org?: {
|
|
185
|
+
id?: string;
|
|
186
|
+
name?: string;
|
|
187
|
+
};
|
|
188
|
+
reason: ClivlyConnectReason;
|
|
189
|
+
/** Whether a retry could plausibly succeed (network, 429, 5xx). */
|
|
190
|
+
retryable: boolean;
|
|
191
|
+
/** HTTP status, or null when the request never completed (network error). */
|
|
192
|
+
status: number | null;
|
|
193
|
+
}
|
|
194
|
+
interface SyncCursor {
|
|
195
|
+
/** The row's id, breaking ties when timestamps collide. */
|
|
196
|
+
id: string;
|
|
197
|
+
/** The row's cursor-field value (typically `updated_at`). */
|
|
198
|
+
time: Date;
|
|
199
|
+
}
|
|
200
|
+
interface SyncSource {
|
|
201
|
+
cursorField: string;
|
|
202
|
+
entity: string;
|
|
203
|
+
fetchPage(args: {
|
|
204
|
+
since: SyncCursor | null;
|
|
205
|
+
limit: number;
|
|
206
|
+
}): Promise<Record<string, unknown>[]>;
|
|
207
|
+
/** Stable, unique tie-breaker column for keyset paging. Defaults to "id". */
|
|
208
|
+
idField?: string;
|
|
209
|
+
/** Set by `fromDrizzle`; lets `doctor` introspect the backing table. */
|
|
210
|
+
[DRIZZLE_META]?: DrizzleSourceMeta;
|
|
211
|
+
}
|
|
212
|
+
type SyncMode = "delta" | "full";
|
|
213
|
+
interface CursorStore {
|
|
214
|
+
get(entity: string): Promise<SyncCursor | null> | SyncCursor | null;
|
|
215
|
+
set(entity: string, cursor: SyncCursor | null): Promise<void> | void;
|
|
216
|
+
}
|
|
217
|
+
interface ClivlySDK {
|
|
218
|
+
/**
|
|
219
|
+
* Send a single heartbeat and report the outcome richly: `ok`, the HTTP
|
|
220
|
+
* `status` (null on network failure), a coarse `reason`, whether it's
|
|
221
|
+
* `retryable`, and the resolved `org` when the server returns it. Never throws
|
|
222
|
+
* — inspect the result, or turn a failure into an exception with
|
|
223
|
+
* `ClivlyError.from(result)`. Prefer this over `heartbeat()` for onboarding,
|
|
224
|
+
* health checks, and one-shot serverless calls.
|
|
225
|
+
*/
|
|
226
|
+
connect(): Promise<ClivlyConnectResult>;
|
|
227
|
+
/**
|
|
228
|
+
* Build the `/clivly/auth/verify` handler the developer mounts in their app.
|
|
229
|
+
* Clivly's cloud calls it to validate a CRM user's identity against the host
|
|
230
|
+
* app's session. Pass a resolver that reads the host session from the request.
|
|
231
|
+
*
|
|
232
|
+
* The handler authenticates the caller against `config.verifyToken` (presented
|
|
233
|
+
* as `Authorization: Bearer <token>`) before invoking the resolver, so the
|
|
234
|
+
* endpoint can't be used by third parties to probe host sessions. It fails
|
|
235
|
+
* closed with HTTP 503 if `verifyToken` is unset (a missing config value, not a
|
|
236
|
+
* server fault — so it won't trip the host's 5xx error alerting).
|
|
237
|
+
*/
|
|
238
|
+
createAuthVerifyHandler(resolveUser: (req: Request) => Promise<ClivlyUser | null> | ClivlyUser | null): (req: Request) => Promise<Response>;
|
|
239
|
+
/**
|
|
240
|
+
* A fetch handler to mount at a route. Each request runs one `runScheduled()`
|
|
241
|
+
* and returns JSON: 200 when the sync succeeded, 500 otherwise.
|
|
242
|
+
*
|
|
243
|
+
* Two callers use it: Clivly Cloud (when someone clicks "Run sync" — those
|
|
244
|
+
* requests are HMAC-signed and carry a run id), and the host's own scheduler
|
|
245
|
+
* (Workers cron, an uptime ping — no body).
|
|
246
|
+
*
|
|
247
|
+
* When `syncTrigger.secret` is set the handler rejects unsigned or
|
|
248
|
+
* badly-signed requests with 401. When it isn't, the route stays open to
|
|
249
|
+
* anyone who can reach it — set a secret if it's publicly routable.
|
|
250
|
+
*/
|
|
251
|
+
createClivlyHandler(): (req: Request) => Promise<Response>;
|
|
252
|
+
/** Validate this setup end-to-end (read-only) before the first sync. */
|
|
253
|
+
doctor(): Promise<DoctorReport>;
|
|
254
|
+
/**
|
|
255
|
+
* Send a single heartbeat now. Returns true on success. Kept for the keep-alive
|
|
256
|
+
* loop and back-compat; use `connect()` when you need the failure reason.
|
|
257
|
+
*/
|
|
258
|
+
heartbeat(): Promise<boolean>;
|
|
259
|
+
/**
|
|
260
|
+
* One-shot heartbeat + a single sync, with no keep-alive timers. The correct
|
|
261
|
+
* entry point on serverless/edge (Workers, Vercel, Lambda), where `start()`'s
|
|
262
|
+
* `setInterval` loop can't persist between invocations — call this from a
|
|
263
|
+
* scheduled/cron trigger. Throws if a sync push fails (so the run is marked
|
|
264
|
+
* failed); heartbeat is best-effort.
|
|
265
|
+
*/
|
|
266
|
+
runScheduled(options?: {
|
|
267
|
+
mode?: SyncMode;
|
|
268
|
+
runId?: string;
|
|
269
|
+
}): Promise<void>;
|
|
270
|
+
/**
|
|
271
|
+
* Send the initial heartbeat and start the keep-alive loop. For long-lived
|
|
272
|
+
* hosts only — on serverless/edge the interval can't persist, so prefer
|
|
273
|
+
* `runScheduled()` / `createClivlyHandler()`. Emits a warning if a serverless
|
|
274
|
+
* runtime is detected.
|
|
275
|
+
*/
|
|
276
|
+
start(): Promise<void>;
|
|
277
|
+
/** Stop the keep-alive loop. */
|
|
278
|
+
stop(): void;
|
|
279
|
+
/**
|
|
280
|
+
* Sync all configured sources now. `delta` (default) pages changed rows since
|
|
281
|
+
* the last cursor; `full` pushes each complete entity so the server can
|
|
282
|
+
* reconcile leavers (archive-on-leave). Throws if a push fails, so callers see
|
|
283
|
+
* the failure — the background keep-alive loop swallows and retries instead.
|
|
284
|
+
*/
|
|
285
|
+
sync(options?: {
|
|
286
|
+
mode?: SyncMode;
|
|
287
|
+
runId?: string;
|
|
288
|
+
}): Promise<void>;
|
|
289
|
+
}
|
|
290
|
+
//#endregion
|
|
291
|
+
//#region src/namespace-probe.d.ts
|
|
292
|
+
interface NamespaceIntrospector {
|
|
293
|
+
/** All base table names in the reachable schema. */
|
|
294
|
+
listTables(): Promise<string[]>;
|
|
295
|
+
/** Prove a table is genuinely selectable (SELECT ... LIMIT 1). Throws if unreachable/denied. */
|
|
296
|
+
sampleSelect(table: string): Promise<void>;
|
|
297
|
+
}
|
|
298
|
+
//#endregion
|
|
299
|
+
export { ClivlySDKConfig as a, DiscoveredTable as c, SyncSource as d, ClivlySDK as i, DoctorCheck as l, ClivlyConnectReason as n, ClivlyUser as o, ClivlyConnectResult as r, CursorStore as s, NamespaceIntrospector as t, DoctorReport as u };
|
package/dist/vite.cjs
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
Object.defineProperty(exports, Symbol.toStringTag, { value: "Module" });
|
|
2
|
+
//#region src/vite.ts
|
|
3
|
+
/**
|
|
4
|
+
* Vite dev-server plugin that runs the Clivly SDK lifecycle for you: on server
|
|
5
|
+
* boot it calls `sdk.connect()` (for a clear status line) then `sdk.start()`
|
|
6
|
+
* (the heartbeat + sync keep-alive loop), and calls `sdk.stop()` when the dev
|
|
7
|
+
* server closes. Dev-only (`apply: "serve"`) — `start()`'s setInterval loop
|
|
8
|
+
* does not persist on serverless/edge builds, so production on Workers/Lambda
|
|
9
|
+
* should mount `sdk.createClivlyHandler()` and drive it from a cron trigger
|
|
10
|
+
* instead. See docs/guides/connecting-to-clivly-cloud.md.
|
|
11
|
+
*
|
|
12
|
+
* Vite is an optional peer dependency imported type-only (`import type`), so
|
|
13
|
+
* the built bundle carries no runtime dependency on Vite.
|
|
14
|
+
*/
|
|
15
|
+
function clivlyVite(sdk, options = {}) {
|
|
16
|
+
const label = options.name ?? "clivly";
|
|
17
|
+
return {
|
|
18
|
+
name: "clivly",
|
|
19
|
+
apply: "serve",
|
|
20
|
+
configureServer(server) {
|
|
21
|
+
const { logger } = server.config;
|
|
22
|
+
const boot = async () => {
|
|
23
|
+
const result = await sdk.connect();
|
|
24
|
+
if (result.ok) {
|
|
25
|
+
const org = result.org?.name ?? result.org?.id;
|
|
26
|
+
logger.info(`${label}: connected${org ? ` to ${org}` : ""}`);
|
|
27
|
+
} else logger.warn(`${label}: not connected (${result.reason})`);
|
|
28
|
+
await sdk.start();
|
|
29
|
+
};
|
|
30
|
+
boot().catch((error) => {
|
|
31
|
+
logger.error(`${label}: startup failed — ${error instanceof Error ? error.message : String(error)}`);
|
|
32
|
+
});
|
|
33
|
+
server.httpServer?.once("close", () => sdk.stop());
|
|
34
|
+
}
|
|
35
|
+
};
|
|
36
|
+
}
|
|
37
|
+
//#endregion
|
|
38
|
+
exports.clivlyVite = clivlyVite;
|
package/dist/vite.d.cts
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { i as ClivlySDK } from "./namespace-probe-DIBgCQca.cjs";
|
|
2
|
+
import { Plugin } from "vite";
|
|
3
|
+
|
|
4
|
+
//#region src/vite.d.ts
|
|
5
|
+
interface ClivlyViteOptions {
|
|
6
|
+
/** Log prefix for Clivly's boot lines. Defaults to "clivly". */
|
|
7
|
+
name?: string;
|
|
8
|
+
}
|
|
9
|
+
/**
|
|
10
|
+
* Vite dev-server plugin that runs the Clivly SDK lifecycle for you: on server
|
|
11
|
+
* boot it calls `sdk.connect()` (for a clear status line) then `sdk.start()`
|
|
12
|
+
* (the heartbeat + sync keep-alive loop), and calls `sdk.stop()` when the dev
|
|
13
|
+
* server closes. Dev-only (`apply: "serve"`) — `start()`'s setInterval loop
|
|
14
|
+
* does not persist on serverless/edge builds, so production on Workers/Lambda
|
|
15
|
+
* should mount `sdk.createClivlyHandler()` and drive it from a cron trigger
|
|
16
|
+
* instead. See docs/guides/connecting-to-clivly-cloud.md.
|
|
17
|
+
*
|
|
18
|
+
* Vite is an optional peer dependency imported type-only (`import type`), so
|
|
19
|
+
* the built bundle carries no runtime dependency on Vite.
|
|
20
|
+
*/
|
|
21
|
+
declare function clivlyVite(sdk: ClivlySDK, options?: ClivlyViteOptions): Plugin;
|
|
22
|
+
//#endregion
|
|
23
|
+
export { ClivlyViteOptions, clivlyVite };
|
package/dist/vite.d.mts
ADDED
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { i as ClivlySDK } from "./namespace-probe-DFpvfl7T.mjs";
|
|
2
|
+
import { Plugin } from "vite";
|
|
3
|
+
|
|
4
|
+
//#region src/vite.d.ts
|
|
5
|
+
interface ClivlyViteOptions {
|
|
6
|
+
/** Log prefix for Clivly's boot lines. Defaults to "clivly". */
|
|
7
|
+
name?: string;
|
|
8
|
+
}
|
|
9
|
+
/**
|
|
10
|
+
* Vite dev-server plugin that runs the Clivly SDK lifecycle for you: on server
|
|
11
|
+
* boot it calls `sdk.connect()` (for a clear status line) then `sdk.start()`
|
|
12
|
+
* (the heartbeat + sync keep-alive loop), and calls `sdk.stop()` when the dev
|
|
13
|
+
* server closes. Dev-only (`apply: "serve"`) — `start()`'s setInterval loop
|
|
14
|
+
* does not persist on serverless/edge builds, so production on Workers/Lambda
|
|
15
|
+
* should mount `sdk.createClivlyHandler()` and drive it from a cron trigger
|
|
16
|
+
* instead. See docs/guides/connecting-to-clivly-cloud.md.
|
|
17
|
+
*
|
|
18
|
+
* Vite is an optional peer dependency imported type-only (`import type`), so
|
|
19
|
+
* the built bundle carries no runtime dependency on Vite.
|
|
20
|
+
*/
|
|
21
|
+
declare function clivlyVite(sdk: ClivlySDK, options?: ClivlyViteOptions): Plugin;
|
|
22
|
+
//#endregion
|
|
23
|
+
export { ClivlyViteOptions, clivlyVite };
|