@withone/sdk 0.0.3 → 0.2.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/dist/index.d.ts CHANGED
@@ -1,7 +1,362 @@
1
- import { RetryConfig, JsonValue, ClientOptions as ClientOptions$1, One as One$1 } from '../native.js';
1
+ import { JsonValue, RetryConfig, ClientOptions as ClientOptions$1, One as One$1 } from '../native.js';
2
2
  export { JsonValue, RetryConfig } from '../native.js';
3
3
  import { A as Action, a as ActionCall } from './action-arV2Nfkm.js';
4
4
 
5
+ /** A failed SDK operation with stable transport and retry metadata. */
6
+ declare class OneError extends Error {
7
+ /** Stable error kind (`status`, `timeout`, `transport`, `missing_secret`, ...). */
8
+ readonly code: string;
9
+ /** Upstream HTTP status, for an upstream (`status`) failure. */
10
+ readonly status: number | undefined;
11
+ /** Upstream response body, for an upstream (`status`) failure. */
12
+ readonly body: string | undefined;
13
+ /** Whether retrying the call might succeed. */
14
+ readonly retryable: boolean;
15
+ constructor(
16
+ /** Stable error kind (`status`, `timeout`, `transport`, `missing_secret`, ...). */
17
+ code: string, message: string,
18
+ /** Upstream HTTP status, for an upstream (`status`) failure. */
19
+ status: number | undefined,
20
+ /** Upstream response body, for an upstream (`status`) failure. */
21
+ body: string | undefined,
22
+ /** Whether retrying the call might succeed. */
23
+ retryable: boolean);
24
+ }
25
+
26
+ /** A verified delivery handed to your handlers. */
27
+ interface Delivery<P = JsonValue> {
28
+ /** On-the-wire event string (`connection.created`, ...). */
29
+ readonly event: string;
30
+ /** Stable delivery id, identical on every retry. Deduplicate on it. */
31
+ readonly id: string;
32
+ /** When One emitted the event. */
33
+ readonly timestamp: Date;
34
+ /** The event-specific payload. */
35
+ readonly payload: P;
36
+ /**
37
+ * Metadata recorded on the event when it was captured, if any. This is the
38
+ * event's own metadata - a subscription's `metadata` field is not echoed back
39
+ * on delivery.
40
+ */
41
+ readonly metadata: JsonValue | null;
42
+ /**
43
+ * The connection this delivery concerns, for events that carry one. `null`
44
+ * for account-level events and for an `oauth.failed` that fired before the
45
+ * connection existed.
46
+ */
47
+ readonly connection: string | null;
48
+ /**
49
+ * Whether this is a test delivery from the dashboard's "send test" action.
50
+ *
51
+ * A test delivery reuses a real event name but carries a synthetic payload,
52
+ * so typed handlers are skipped for it and only `onAny` runs.
53
+ */
54
+ readonly isTest: boolean;
55
+ }
56
+ /** A webhook handler. May be async; `handle` awaits it. */
57
+ type Handler<P = JsonValue> = (delivery: Delivery<P>) => void | Promise<void>;
58
+ /** Options for a webhook listener. */
59
+ interface WebhookOptions {
60
+ /** The subscription's signing secret, used to verify every delivery. */
61
+ secret: string;
62
+ /**
63
+ * Rejects deliveries whose *event* is older than this many milliseconds.
64
+ *
65
+ * **Off by default, and not replay protection.** The signed timestamp is when
66
+ * the event occurred, not when the attempt was sent, so any limit starts
67
+ * rejecting authentic deliveries once the delivery queue falls behind.
68
+ * Deduplicate on `Delivery.id` instead - it is stable across retries.
69
+ */
70
+ toleranceMs?: number;
71
+ }
72
+ interface Registered {
73
+ connection: string | null;
74
+ run: Handler;
75
+ }
76
+ /**
77
+ * Registry and dispatch for webhook deliveries.
78
+ *
79
+ * Signature checking, the tolerance window, and envelope parsing live in the
80
+ * native client so the Rust and TypeScript SDKs cannot drift on them. Only the
81
+ * handler registry is here, because handlers are closures and those should not
82
+ * cross the native boundary.
83
+ */
84
+ declare class WebhooksBase {
85
+ private readonly client;
86
+ private readonly options;
87
+ protected readonly handlers: Map<string, Registered[]>;
88
+ protected readonly anyHandlers: Registered[];
89
+ constructor(client: One, options: WebhookOptions);
90
+ /** @internal Registers a typed handler; generated methods wrap this. */
91
+ protected register(event: string, connection: string | null, run: Handler): this;
92
+ /**
93
+ * Registers handlers scoped to one connection: they fire only for deliveries
94
+ * carrying that connection's key.
95
+ *
96
+ * The block keeps the scope lexical, so a reader never has to scan backwards
97
+ * to work out which connection a handler belongs to, and only the
98
+ * connection-carrying events are offered inside it.
99
+ */
100
+ connection(key: string, block: (scope: ConnectionWebhooksBase) => unknown): this;
101
+ /** @internal The generated subclass supplies its own scope type. */
102
+ protected scopeConstructor(): new (key: string) => ConnectionWebhooksBase;
103
+ /**
104
+ * Registers a handler fired for every verified delivery, including event
105
+ * types this SDK build does not recognize. Runs after event-specific
106
+ * handlers.
107
+ */
108
+ onAny(handler: Handler): this;
109
+ /**
110
+ * The event types `listen` subscribes to.
111
+ *
112
+ * An unscoped catch-all subscribes to this build's whole catalog; the server
113
+ * matches exact event strings, so events added after this build are not
114
+ * covered. A catch-all scoped to a connection adds only the
115
+ * connection-carrying events, since the server cannot filter by connection
116
+ * and the rest would just be discarded on arrival.
117
+ */
118
+ events(): string[];
119
+ /** @internal The generated subclass supplies the full event catalog. */
120
+ protected catalog(): readonly string[];
121
+ /** @internal The generated subclass supplies the connection-carrying subset. */
122
+ protected connectionCatalog(): readonly string[];
123
+ /**
124
+ * Verifies the signature and freshness of a delivery without dispatching.
125
+ * Use it when you want to route deliveries yourself.
126
+ *
127
+ * `rawBody` must be the exact bytes One sent. A JSON body parser that has
128
+ * already re-serialized the request will fail verification.
129
+ */
130
+ verify(rawBody: Buffer, signature: string): Delivery;
131
+ /**
132
+ * Verifies, parses, and dispatches a delivery to every handler registered for
133
+ * its event type, then to the catch-all handlers. Handlers run sequentially
134
+ * and are awaited.
135
+ */
136
+ handle(rawBody: Buffer, signature: string): Promise<Delivery>;
137
+ /**
138
+ * Registers this listener with One under the stable name `key`, pointing at
139
+ * `url` and subscribing to exactly the events handlers were attached for.
140
+ *
141
+ * Idempotent: calling it on every boot reconciles the one listener named
142
+ * `key` rather than accumulating duplicates.
143
+ */
144
+ listen(key: string, url: string): Promise<JsonValue>;
145
+ }
146
+ /** Handlers scoped to a single connection. */
147
+ declare class ConnectionWebhooksBase {
148
+ protected readonly key: string;
149
+ private readonly handlers;
150
+ private readonly anyHandlers;
151
+ constructor(key: string);
152
+ /** @internal Registers a typed handler; generated methods wrap this. */
153
+ protected register(event: string, run: Handler): this;
154
+ /** Registers a handler for every event concerning this connection. */
155
+ onAny(handler: Handler): this;
156
+ /** @internal Hands the collected handlers to the parent registry. */
157
+ drain(): Map<string, Registered[]>;
158
+ /** @internal Hands the collected catch-all handlers to the parent registry. */
159
+ drainAny(): Registered[];
160
+ }
161
+
162
+ interface ConnectionCreatedPayload {
163
+ connection_id: string;
164
+ connection_type: string;
165
+ created_at: string;
166
+ environment: string;
167
+ key: string;
168
+ name?: string | null;
169
+ organization_id?: string | null;
170
+ platform: string;
171
+ project_id?: string | null;
172
+ state: string;
173
+ }
174
+ interface ConnectionDeletedPayload {
175
+ connection_id: string;
176
+ deleted_at: string;
177
+ key?: string | null;
178
+ name?: string | null;
179
+ organization_id?: string | null;
180
+ platform: string;
181
+ project_id?: string | null;
182
+ }
183
+ interface ConnectionOAuthCreatedPayload {
184
+ authorized_at: string;
185
+ connection_id: string;
186
+ key: string;
187
+ platform: string;
188
+ user_id: string;
189
+ }
190
+ interface MemberAddedPayload {
191
+ added_at: string;
192
+ email: string;
193
+ organization_id?: string | null;
194
+ organization_name?: string | null;
195
+ project_id?: string | null;
196
+ project_name?: string | null;
197
+ user_id: string;
198
+ }
199
+ interface MemberRemovedPayload {
200
+ email: string;
201
+ organization_id?: string | null;
202
+ organization_name?: string | null;
203
+ project_id?: string | null;
204
+ project_name?: string | null;
205
+ removed_at: string;
206
+ user_id: string;
207
+ }
208
+ interface MemberRoleUpdatedPayload {
209
+ email: string;
210
+ new_role: string;
211
+ old_role: string;
212
+ organization_id?: string | null;
213
+ organization_name?: string | null;
214
+ project_id?: string | null;
215
+ project_name?: string | null;
216
+ updated_at: string;
217
+ user_id: string;
218
+ }
219
+ interface OAuthFailedPayload {
220
+ connection_id?: string | null;
221
+ error: string;
222
+ failed_at: string;
223
+ key?: string | null;
224
+ platform: string;
225
+ }
226
+ interface OAuthRefreshedPayload {
227
+ connection_id: string;
228
+ key?: string | null;
229
+ platform: string;
230
+ refreshed_at: string;
231
+ }
232
+ interface OrganizationCreatedPayload {
233
+ created_at: string;
234
+ id: string;
235
+ name: string;
236
+ }
237
+ interface OrganizationDeletedPayload {
238
+ deleted_at: string;
239
+ id: string;
240
+ name: string;
241
+ }
242
+ interface OrganizationUpdatedPayload {
243
+ id: string;
244
+ name: string;
245
+ updated_at: string;
246
+ }
247
+ interface PassthroughExecutedPayload {
248
+ connection_id: string;
249
+ duration_ms: number;
250
+ error?: string | null;
251
+ key?: string | null;
252
+ method: string;
253
+ path: string;
254
+ platform: string;
255
+ status_code?: number | null;
256
+ timestamp: string;
257
+ }
258
+ interface ProjectCreatedPayload {
259
+ created_at: string;
260
+ id: string;
261
+ name: string;
262
+ organization_id: string;
263
+ }
264
+ interface ProjectDeletedPayload {
265
+ deleted_at: string;
266
+ id: string;
267
+ name: string;
268
+ organization_id: string;
269
+ }
270
+ interface ProjectUpdatedPayload {
271
+ id: string;
272
+ name: string;
273
+ organization_id: string;
274
+ updated_at: string;
275
+ }
276
+ /** Every event a One webhook subscription can deliver. */
277
+ type WebhookEventType = 'connection.created' | 'connection.deleted' | 'connection.oauth.created' | 'member.added' | 'member.removed' | 'member.role_updated' | 'oauth.failed' | 'oauth.refreshed' | 'organization.created' | 'organization.deleted' | 'organization.updated' | 'passthrough.executed' | 'project.created' | 'project.deleted' | 'project.updated';
278
+ /** Maps each event to the payload its deliveries carry. */
279
+ interface WebhookPayloads {
280
+ 'connection.created': ConnectionCreatedPayload;
281
+ 'connection.deleted': ConnectionDeletedPayload;
282
+ 'connection.oauth.created': ConnectionOAuthCreatedPayload;
283
+ 'member.added': MemberAddedPayload;
284
+ 'member.removed': MemberRemovedPayload;
285
+ 'member.role_updated': MemberRoleUpdatedPayload;
286
+ 'oauth.failed': OAuthFailedPayload;
287
+ 'oauth.refreshed': OAuthRefreshedPayload;
288
+ 'organization.created': OrganizationCreatedPayload;
289
+ 'organization.deleted': OrganizationDeletedPayload;
290
+ 'organization.updated': OrganizationUpdatedPayload;
291
+ 'passthrough.executed': PassthroughExecutedPayload;
292
+ 'project.created': ProjectCreatedPayload;
293
+ 'project.deleted': ProjectDeletedPayload;
294
+ 'project.updated': ProjectUpdatedPayload;
295
+ }
296
+ /**
297
+ * A registry of typed webhook handlers plus the listener registration that
298
+ * makes One send them.
299
+ */
300
+ declare class Webhooks extends WebhooksBase {
301
+ /** Scopes to the generated connection registry, so a `connection(..)` block
302
+ * offers the typed per-event methods rather than the bare base. */
303
+ protected scopeConstructor(): new (key: string) => ConnectionWebhooks;
304
+ /** The full catalog, so an unscoped catch-all subscribes to everything. */
305
+ protected catalog(): readonly string[];
306
+ /** The connection-carrying subset, for a catch-all scoped to a connection. */
307
+ protected connectionCatalog(): readonly string[];
308
+ /** Narrows the block's scope to the typed registry. */
309
+ connection(key: string, block: (scope: ConnectionWebhooks) => unknown): this;
310
+ /** Registers a handler for `connection.created`. */
311
+ connectionCreated(handler: Handler<ConnectionCreatedPayload>): this;
312
+ /** Registers a handler for `connection.deleted`. */
313
+ connectionDeleted(handler: Handler<ConnectionDeletedPayload>): this;
314
+ /** Registers a handler for `connection.oauth.created`. */
315
+ connectionOauthCreated(handler: Handler<ConnectionOAuthCreatedPayload>): this;
316
+ /** Registers a handler for `member.added`. */
317
+ memberAdded(handler: Handler<MemberAddedPayload>): this;
318
+ /** Registers a handler for `member.removed`. */
319
+ memberRemoved(handler: Handler<MemberRemovedPayload>): this;
320
+ /** Registers a handler for `member.role_updated`. */
321
+ memberRoleUpdated(handler: Handler<MemberRoleUpdatedPayload>): this;
322
+ /** Registers a handler for `oauth.failed`. */
323
+ oauthFailed(handler: Handler<OAuthFailedPayload>): this;
324
+ /** Registers a handler for `oauth.refreshed`. */
325
+ oauthRefreshed(handler: Handler<OAuthRefreshedPayload>): this;
326
+ /** Registers a handler for `organization.created`. */
327
+ organizationCreated(handler: Handler<OrganizationCreatedPayload>): this;
328
+ /** Registers a handler for `organization.deleted`. */
329
+ organizationDeleted(handler: Handler<OrganizationDeletedPayload>): this;
330
+ /** Registers a handler for `organization.updated`. */
331
+ organizationUpdated(handler: Handler<OrganizationUpdatedPayload>): this;
332
+ /** Registers a handler for `passthrough.executed`. */
333
+ passthroughExecuted(handler: Handler<PassthroughExecutedPayload>): this;
334
+ /** Registers a handler for `project.created`. */
335
+ projectCreated(handler: Handler<ProjectCreatedPayload>): this;
336
+ /** Registers a handler for `project.deleted`. */
337
+ projectDeleted(handler: Handler<ProjectDeletedPayload>): this;
338
+ /** Registers a handler for `project.updated`. */
339
+ projectUpdated(handler: Handler<ProjectUpdatedPayload>): this;
340
+ }
341
+ /**
342
+ * Handlers scoped to one connection. Only the events that carry a connection
343
+ * are offered here, so a scoped handler can never be one that could not fire.
344
+ */
345
+ declare class ConnectionWebhooks extends ConnectionWebhooksBase {
346
+ /** Registers a handler for `connection.created` on this connection. */
347
+ connectionCreated(handler: Handler<ConnectionCreatedPayload>): this;
348
+ /** Registers a handler for `connection.deleted` on this connection. */
349
+ connectionDeleted(handler: Handler<ConnectionDeletedPayload>): this;
350
+ /** Registers a handler for `connection.oauth.created` on this connection. */
351
+ connectionOauthCreated(handler: Handler<ConnectionOAuthCreatedPayload>): this;
352
+ /** Registers a handler for `oauth.failed` on this connection. */
353
+ oauthFailed(handler: Handler<OAuthFailedPayload>): this;
354
+ /** Registers a handler for `oauth.refreshed` on this connection. */
355
+ oauthRefreshed(handler: Handler<OAuthRefreshedPayload>): this;
356
+ /** Registers a handler for `passthrough.executed` on this connection. */
357
+ passthroughExecuted(handler: Handler<PassthroughExecutedPayload>): this;
358
+ }
359
+
5
360
  /** Per-call overrides, passed as the second arg to `run`. */
6
361
  interface CallOptions {
7
362
  /** Retry policy for this call, overriding the client default. */
@@ -22,6 +377,15 @@ interface Hooks {
22
377
  onSuccess?: (result: CallResult) => void;
23
378
  onError?: (error: OneError) => void;
24
379
  }
380
+ interface CompletedCall<T> {
381
+ output: T;
382
+ result: CallResult;
383
+ }
384
+ declare class CallLifecycle {
385
+ private readonly hooks;
386
+ constructor(hooks: Hooks);
387
+ execute<Native, Output>(call: () => Promise<Native>, complete: (value: Native) => CompletedCall<Output>): Promise<Output>;
388
+ }
25
389
  /**
26
390
  * The One SDK client. A thin typed facade over the native Rust client — HTTP,
27
391
  * retries, and route templating live in Rust; this routes calls, supplies the
@@ -29,19 +393,26 @@ interface Hooks {
29
393
  */
30
394
  declare class One {
31
395
  private readonly native;
32
- private readonly hooks;
396
+ private readonly lifecycle;
33
397
  constructor(secret: string, options?: ClientOptions);
34
398
  /** Creates a client from the environment (`ONE_SECRET`, `ONE_BASE_URL`). */
35
399
  static fromEnv(): One;
36
400
  /** Scopes subsequent calls to a connection by its key. */
37
401
  connection(key: string): ConnectionScope;
402
+ /**
403
+ * Starts building a webhook listener, verified with the subscription's
404
+ * signing secret.
405
+ */
406
+ webhooks(options: WebhookOptions): Webhooks;
407
+ /** @internal Registers a webhook listener; the route and request shape live in Rust. */
408
+ registerWebhook(key: string, url: string, events: string[], secret: string): Promise<JsonValue>;
38
409
  }
39
410
  /** A connection-scoped runner. Run typed actions against one connection. */
40
411
  declare class ConnectionScope {
41
412
  private readonly native;
42
413
  private readonly key;
43
- private readonly hooks;
44
- constructor(native: One$1, key: string, hooks: Hooks);
414
+ private readonly lifecycle;
415
+ constructor(native: One$1, key: string, lifecycle: CallLifecycle);
45
416
  /** Runs a typed action and resolves with the response, typed as the action's
46
417
  * default output. `options` sets per-call retry and timeout. */
47
418
  run<Out = JsonValue>(action: Action<Out>, options?: CallOptions): Promise<CallResult<Out>>;
@@ -59,44 +430,21 @@ declare class ConnectionScope {
59
430
  * upstream response headers (rate-limit, request-id, `Link`, …).
60
431
  */
61
432
  declare class CallResult<Out = JsonValue> {
62
- /** HTTP status returned by the upstream platform. */
433
+ /** HTTP status returned by the request. */
63
434
  readonly status: number;
64
435
  /** Response headers, name → value. */
65
436
  readonly headers: Record<string, string>;
66
- /** Response body, typed as the action's default output. */
437
+ /** Response body, typed as the call's default output. */
67
438
  readonly data: Out;
68
439
  constructor(
69
- /** HTTP status returned by the upstream platform. */
440
+ /** HTTP status returned by the request. */
70
441
  status: number,
71
442
  /** Response headers, name → value. */
72
443
  headers: Record<string, string>,
73
- /** Response body, typed as the action's default output. */
444
+ /** Response body, typed as the call's default output. */
74
445
  data: Out);
75
446
  /** Decodes the body into a caller type — overrides the action's default output. */
76
447
  json<T>(): T;
77
448
  }
78
- /**
79
- * A failed call. Carries the upstream HTTP `status` and `body` and whether the
80
- * failure is `retryable` — the typed mirror of the Rust `one::Error`.
81
- */
82
- declare class OneError extends Error {
83
- /** Stable error kind (`status`, `timeout`, `transport`, `missing_secret`, …). */
84
- readonly code: string;
85
- /** Upstream HTTP status, for an upstream (`status`) failure. */
86
- readonly status: number | undefined;
87
- /** Upstream response body, for an upstream (`status`) failure. */
88
- readonly body: string | undefined;
89
- /** Whether retrying the call might succeed. */
90
- readonly retryable: boolean;
91
- constructor(
92
- /** Stable error kind (`status`, `timeout`, `transport`, `missing_secret`, …). */
93
- code: string, message: string,
94
- /** Upstream HTTP status, for an upstream (`status`) failure. */
95
- status: number | undefined,
96
- /** Upstream response body, for an upstream (`status`) failure. */
97
- body: string | undefined,
98
- /** Whether retrying the call might succeed. */
99
- retryable: boolean);
100
- }
101
449
 
102
- export { Action, ActionCall, type CallOptions, CallResult, type ClientOptions, ConnectionScope, One, OneError };
450
+ export { Action, ActionCall, type CallOptions, CallResult, type ClientOptions, ConnectionScope, ConnectionWebhooks, type Delivery, type Handler, One, OneError, type WebhookEventType, type WebhookOptions, type WebhookPayloads, Webhooks };