@withone/sdk 0.0.4 → 0.3.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 +14 -2
- package/dist/index.d.mts +449 -33
- package/dist/index.d.ts +449 -33
- package/dist/index.js +422 -42
- package/dist/index.mjs +423 -43
- package/native.d.ts +52 -3
- package/native.js +1 -0
- package/package.json +8 -8
package/dist/index.mjs
CHANGED
|
@@ -4,9 +4,370 @@ import {
|
|
|
4
4
|
|
|
5
5
|
// src/client.ts
|
|
6
6
|
import { One as NativeOne } from "../native.js";
|
|
7
|
+
|
|
8
|
+
// src/errors.ts
|
|
9
|
+
var OneError = class extends Error {
|
|
10
|
+
constructor(code, message, status, body, retryable) {
|
|
11
|
+
super(message);
|
|
12
|
+
this.code = code;
|
|
13
|
+
this.status = status;
|
|
14
|
+
this.body = body;
|
|
15
|
+
this.retryable = retryable;
|
|
16
|
+
this.name = "OneError";
|
|
17
|
+
}
|
|
18
|
+
code;
|
|
19
|
+
status;
|
|
20
|
+
body;
|
|
21
|
+
retryable;
|
|
22
|
+
};
|
|
23
|
+
function toOneError(error) {
|
|
24
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
25
|
+
try {
|
|
26
|
+
const envelope = JSON.parse(message);
|
|
27
|
+
return new OneError(envelope.code, envelope.message, envelope.status, envelope.body, envelope.retryable);
|
|
28
|
+
} catch {
|
|
29
|
+
return new OneError("unknown", message, void 0, void 0, false);
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
// src/webhooks.ts
|
|
34
|
+
import { verifyWebhook } from "../native.js";
|
|
35
|
+
var WebhooksBase = class {
|
|
36
|
+
constructor(client, options) {
|
|
37
|
+
this.client = client;
|
|
38
|
+
this.options = options;
|
|
39
|
+
}
|
|
40
|
+
client;
|
|
41
|
+
options;
|
|
42
|
+
handlers = /* @__PURE__ */ new Map();
|
|
43
|
+
anyHandlers = [];
|
|
44
|
+
/** @internal Registers a typed handler; generated methods wrap this. */
|
|
45
|
+
register(event, connection, run) {
|
|
46
|
+
const existing = this.handlers.get(event) ?? [];
|
|
47
|
+
existing.push({ connection, run });
|
|
48
|
+
this.handlers.set(event, existing);
|
|
49
|
+
return this;
|
|
50
|
+
}
|
|
51
|
+
/**
|
|
52
|
+
* Registers handlers scoped to one connection: they fire only for deliveries
|
|
53
|
+
* carrying that connection's key.
|
|
54
|
+
*
|
|
55
|
+
* The block keeps the scope lexical, so a reader never has to scan backwards
|
|
56
|
+
* to work out which connection a handler belongs to, and only the
|
|
57
|
+
* connection-carrying events are offered inside it.
|
|
58
|
+
*/
|
|
59
|
+
connection(key, block) {
|
|
60
|
+
const scope = new (this.scopeConstructor())(key);
|
|
61
|
+
block(scope);
|
|
62
|
+
for (const [event, registered] of scope.drain()) {
|
|
63
|
+
const existing = this.handlers.get(event) ?? [];
|
|
64
|
+
existing.push(...registered);
|
|
65
|
+
this.handlers.set(event, existing);
|
|
66
|
+
}
|
|
67
|
+
this.anyHandlers.push(...scope.drainAny());
|
|
68
|
+
return this;
|
|
69
|
+
}
|
|
70
|
+
/** @internal The generated subclass supplies its own scope type. */
|
|
71
|
+
scopeConstructor() {
|
|
72
|
+
return ConnectionWebhooksBase;
|
|
73
|
+
}
|
|
74
|
+
/**
|
|
75
|
+
* Registers a handler fired for every verified delivery, including event
|
|
76
|
+
* types this SDK build does not recognize. Runs after event-specific
|
|
77
|
+
* handlers.
|
|
78
|
+
*/
|
|
79
|
+
onAny(handler) {
|
|
80
|
+
this.anyHandlers.push({ connection: null, run: handler });
|
|
81
|
+
return this;
|
|
82
|
+
}
|
|
83
|
+
/**
|
|
84
|
+
* The event types `listen` subscribes to.
|
|
85
|
+
*
|
|
86
|
+
* An unscoped catch-all subscribes to this build's whole catalog; the server
|
|
87
|
+
* matches exact event strings, so events added after this build are not
|
|
88
|
+
* covered. A catch-all scoped to a connection adds only the
|
|
89
|
+
* connection-carrying events, since the server cannot filter by connection
|
|
90
|
+
* and the rest would just be discarded on arrival.
|
|
91
|
+
*/
|
|
92
|
+
events() {
|
|
93
|
+
if (this.anyHandlers.some((handler) => handler.connection === null)) return [...this.catalog()];
|
|
94
|
+
const events = new Set(this.handlers.keys());
|
|
95
|
+
if (this.anyHandlers.length > 0) {
|
|
96
|
+
for (const event of this.connectionCatalog()) events.add(event);
|
|
97
|
+
}
|
|
98
|
+
return [...events].sort();
|
|
99
|
+
}
|
|
100
|
+
/** @internal The generated subclass supplies the full event catalog. */
|
|
101
|
+
catalog() {
|
|
102
|
+
return [];
|
|
103
|
+
}
|
|
104
|
+
/** @internal The generated subclass supplies the connection-carrying subset. */
|
|
105
|
+
connectionCatalog() {
|
|
106
|
+
return [];
|
|
107
|
+
}
|
|
108
|
+
/**
|
|
109
|
+
* Verifies the signature and freshness of a delivery without dispatching.
|
|
110
|
+
* Use it when you want to route deliveries yourself.
|
|
111
|
+
*
|
|
112
|
+
* `rawBody` must be the exact bytes One sent. A JSON body parser that has
|
|
113
|
+
* already re-serialized the request will fail verification.
|
|
114
|
+
*/
|
|
115
|
+
verify(rawBody, signature) {
|
|
116
|
+
try {
|
|
117
|
+
const verified = verifyWebhook(rawBody, signature, this.options.secret, this.options.toleranceMs);
|
|
118
|
+
return {
|
|
119
|
+
event: verified.event,
|
|
120
|
+
id: verified.eventId,
|
|
121
|
+
timestamp: new Date(verified.timestamp),
|
|
122
|
+
payload: verified.payload,
|
|
123
|
+
metadata: verified.metadata ?? null,
|
|
124
|
+
connection: verified.connection ?? null,
|
|
125
|
+
isTest: verified.isTest
|
|
126
|
+
};
|
|
127
|
+
} catch (error) {
|
|
128
|
+
throw toOneError(error);
|
|
129
|
+
}
|
|
130
|
+
}
|
|
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
|
+
async handle(rawBody, signature) {
|
|
137
|
+
const delivery = this.verify(rawBody, signature);
|
|
138
|
+
const accepts = (registered) => registered.connection === null || registered.connection === delivery.connection;
|
|
139
|
+
let failure;
|
|
140
|
+
const run = async (registered) => {
|
|
141
|
+
try {
|
|
142
|
+
await registered.run(delivery);
|
|
143
|
+
} catch (error) {
|
|
144
|
+
failure ??= { error };
|
|
145
|
+
}
|
|
146
|
+
};
|
|
147
|
+
if (!delivery.isTest) {
|
|
148
|
+
for (const registered of (this.handlers.get(delivery.event) ?? []).filter(accepts)) {
|
|
149
|
+
await run(registered);
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
for (const registered of this.anyHandlers.filter(accepts)) {
|
|
153
|
+
await run(registered);
|
|
154
|
+
}
|
|
155
|
+
if (failure) throw failure.error;
|
|
156
|
+
return delivery;
|
|
157
|
+
}
|
|
158
|
+
/**
|
|
159
|
+
* Registers this listener with One under the stable name `key`, pointing at
|
|
160
|
+
* `url` and subscribing to exactly the events handlers were attached for.
|
|
161
|
+
*
|
|
162
|
+
* Idempotent: calling it on every boot reconciles the one listener named
|
|
163
|
+
* `key` rather than accumulating duplicates.
|
|
164
|
+
*/
|
|
165
|
+
async listen(key, url) {
|
|
166
|
+
return this.client.registerWebhook(key, url, this.events(), this.options.secret);
|
|
167
|
+
}
|
|
168
|
+
};
|
|
169
|
+
var ConnectionWebhooksBase = class {
|
|
170
|
+
constructor(key) {
|
|
171
|
+
this.key = key;
|
|
172
|
+
}
|
|
173
|
+
key;
|
|
174
|
+
handlers = /* @__PURE__ */ new Map();
|
|
175
|
+
anyHandlers = [];
|
|
176
|
+
/** @internal Registers a typed handler; generated methods wrap this. */
|
|
177
|
+
register(event, run) {
|
|
178
|
+
const existing = this.handlers.get(event) ?? [];
|
|
179
|
+
existing.push({ connection: this.key, run });
|
|
180
|
+
this.handlers.set(event, existing);
|
|
181
|
+
return this;
|
|
182
|
+
}
|
|
183
|
+
/** Registers a handler for every event concerning this connection. */
|
|
184
|
+
onAny(handler) {
|
|
185
|
+
this.anyHandlers.push({ connection: this.key, run: handler });
|
|
186
|
+
return this;
|
|
187
|
+
}
|
|
188
|
+
/** @internal Hands the collected handlers to the parent registry. */
|
|
189
|
+
drain() {
|
|
190
|
+
return this.handlers;
|
|
191
|
+
}
|
|
192
|
+
/** @internal Hands the collected catch-all handlers to the parent registry. */
|
|
193
|
+
drainAny() {
|
|
194
|
+
return this.anyHandlers;
|
|
195
|
+
}
|
|
196
|
+
};
|
|
197
|
+
|
|
198
|
+
// src/webhooks.generated.ts
|
|
199
|
+
var ALL_WEBHOOK_EVENTS = [
|
|
200
|
+
"api_key.created",
|
|
201
|
+
"api_key.revoked",
|
|
202
|
+
"connection.created",
|
|
203
|
+
"connection.deleted",
|
|
204
|
+
"connection.oauth.created",
|
|
205
|
+
"connection.updated",
|
|
206
|
+
"member.added",
|
|
207
|
+
"member.removed",
|
|
208
|
+
"member.role_updated",
|
|
209
|
+
"oauth.failed",
|
|
210
|
+
"oauth.refreshed",
|
|
211
|
+
"organization.created",
|
|
212
|
+
"organization.deleted",
|
|
213
|
+
"organization.updated",
|
|
214
|
+
"passthrough.executed",
|
|
215
|
+
"project.created",
|
|
216
|
+
"project.deleted",
|
|
217
|
+
"project.updated"
|
|
218
|
+
];
|
|
219
|
+
var CONNECTION_SCOPED_EVENTS = [
|
|
220
|
+
"connection.created",
|
|
221
|
+
"connection.deleted",
|
|
222
|
+
"connection.oauth.created",
|
|
223
|
+
"connection.updated",
|
|
224
|
+
"oauth.failed",
|
|
225
|
+
"oauth.refreshed",
|
|
226
|
+
"passthrough.executed"
|
|
227
|
+
];
|
|
228
|
+
var Webhooks = class extends WebhooksBase {
|
|
229
|
+
/** Scopes to the generated connection registry, so a `connection(..)` block
|
|
230
|
+
* offers the typed per-event methods rather than the bare base. */
|
|
231
|
+
scopeConstructor() {
|
|
232
|
+
return ConnectionWebhooks;
|
|
233
|
+
}
|
|
234
|
+
/** The full catalog, so an unscoped catch-all subscribes to everything. */
|
|
235
|
+
catalog() {
|
|
236
|
+
return ALL_WEBHOOK_EVENTS;
|
|
237
|
+
}
|
|
238
|
+
/** The connection-carrying subset, for a catch-all scoped to a connection. */
|
|
239
|
+
connectionCatalog() {
|
|
240
|
+
return CONNECTION_SCOPED_EVENTS;
|
|
241
|
+
}
|
|
242
|
+
/** Narrows the block's scope to the typed registry. */
|
|
243
|
+
connection(key, block) {
|
|
244
|
+
return super.connection(key, block);
|
|
245
|
+
}
|
|
246
|
+
/** Registers a handler for `api_key.created`. */
|
|
247
|
+
apiKeyCreated(handler) {
|
|
248
|
+
return this.register("api_key.created", null, handler);
|
|
249
|
+
}
|
|
250
|
+
/** Registers a handler for `api_key.revoked`. */
|
|
251
|
+
apiKeyRevoked(handler) {
|
|
252
|
+
return this.register("api_key.revoked", null, handler);
|
|
253
|
+
}
|
|
254
|
+
/** Registers a handler for `connection.created`. */
|
|
255
|
+
connectionCreated(handler) {
|
|
256
|
+
return this.register("connection.created", null, handler);
|
|
257
|
+
}
|
|
258
|
+
/** Registers a handler for `connection.deleted`. */
|
|
259
|
+
connectionDeleted(handler) {
|
|
260
|
+
return this.register("connection.deleted", null, handler);
|
|
261
|
+
}
|
|
262
|
+
/** Registers a handler for `connection.oauth.created`. */
|
|
263
|
+
connectionOauthCreated(handler) {
|
|
264
|
+
return this.register("connection.oauth.created", null, handler);
|
|
265
|
+
}
|
|
266
|
+
/** Registers a handler for `connection.updated`. */
|
|
267
|
+
connectionUpdated(handler) {
|
|
268
|
+
return this.register("connection.updated", null, handler);
|
|
269
|
+
}
|
|
270
|
+
/** Registers a handler for `member.added`. */
|
|
271
|
+
memberAdded(handler) {
|
|
272
|
+
return this.register("member.added", null, handler);
|
|
273
|
+
}
|
|
274
|
+
/** Registers a handler for `member.removed`. */
|
|
275
|
+
memberRemoved(handler) {
|
|
276
|
+
return this.register("member.removed", null, handler);
|
|
277
|
+
}
|
|
278
|
+
/** Registers a handler for `member.role_updated`. */
|
|
279
|
+
memberRoleUpdated(handler) {
|
|
280
|
+
return this.register("member.role_updated", null, handler);
|
|
281
|
+
}
|
|
282
|
+
/** Registers a handler for `oauth.failed`. */
|
|
283
|
+
oauthFailed(handler) {
|
|
284
|
+
return this.register("oauth.failed", null, handler);
|
|
285
|
+
}
|
|
286
|
+
/** Registers a handler for `oauth.refreshed`. */
|
|
287
|
+
oauthRefreshed(handler) {
|
|
288
|
+
return this.register("oauth.refreshed", null, handler);
|
|
289
|
+
}
|
|
290
|
+
/** Registers a handler for `organization.created`. */
|
|
291
|
+
organizationCreated(handler) {
|
|
292
|
+
return this.register("organization.created", null, handler);
|
|
293
|
+
}
|
|
294
|
+
/** Registers a handler for `organization.deleted`. */
|
|
295
|
+
organizationDeleted(handler) {
|
|
296
|
+
return this.register("organization.deleted", null, handler);
|
|
297
|
+
}
|
|
298
|
+
/** Registers a handler for `organization.updated`. */
|
|
299
|
+
organizationUpdated(handler) {
|
|
300
|
+
return this.register("organization.updated", null, handler);
|
|
301
|
+
}
|
|
302
|
+
/** Registers a handler for `passthrough.executed`. */
|
|
303
|
+
passthroughExecuted(handler) {
|
|
304
|
+
return this.register("passthrough.executed", null, handler);
|
|
305
|
+
}
|
|
306
|
+
/** Registers a handler for `project.created`. */
|
|
307
|
+
projectCreated(handler) {
|
|
308
|
+
return this.register("project.created", null, handler);
|
|
309
|
+
}
|
|
310
|
+
/** Registers a handler for `project.deleted`. */
|
|
311
|
+
projectDeleted(handler) {
|
|
312
|
+
return this.register("project.deleted", null, handler);
|
|
313
|
+
}
|
|
314
|
+
/** Registers a handler for `project.updated`. */
|
|
315
|
+
projectUpdated(handler) {
|
|
316
|
+
return this.register("project.updated", null, handler);
|
|
317
|
+
}
|
|
318
|
+
};
|
|
319
|
+
var ConnectionWebhooks = class extends ConnectionWebhooksBase {
|
|
320
|
+
/** Registers a handler for `connection.created` on this connection. */
|
|
321
|
+
connectionCreated(handler) {
|
|
322
|
+
return this.register("connection.created", handler);
|
|
323
|
+
}
|
|
324
|
+
/** Registers a handler for `connection.deleted` on this connection. */
|
|
325
|
+
connectionDeleted(handler) {
|
|
326
|
+
return this.register("connection.deleted", handler);
|
|
327
|
+
}
|
|
328
|
+
/** Registers a handler for `connection.oauth.created` on this connection. */
|
|
329
|
+
connectionOauthCreated(handler) {
|
|
330
|
+
return this.register("connection.oauth.created", handler);
|
|
331
|
+
}
|
|
332
|
+
/** Registers a handler for `connection.updated` on this connection. */
|
|
333
|
+
connectionUpdated(handler) {
|
|
334
|
+
return this.register("connection.updated", handler);
|
|
335
|
+
}
|
|
336
|
+
/** Registers a handler for `oauth.failed` on this connection. */
|
|
337
|
+
oauthFailed(handler) {
|
|
338
|
+
return this.register("oauth.failed", handler);
|
|
339
|
+
}
|
|
340
|
+
/** Registers a handler for `oauth.refreshed` on this connection. */
|
|
341
|
+
oauthRefreshed(handler) {
|
|
342
|
+
return this.register("oauth.refreshed", handler);
|
|
343
|
+
}
|
|
344
|
+
/** Registers a handler for `passthrough.executed` on this connection. */
|
|
345
|
+
passthroughExecuted(handler) {
|
|
346
|
+
return this.register("passthrough.executed", handler);
|
|
347
|
+
}
|
|
348
|
+
};
|
|
349
|
+
|
|
350
|
+
// src/client.ts
|
|
351
|
+
var CallLifecycle = class {
|
|
352
|
+
constructor(hooks) {
|
|
353
|
+
this.hooks = hooks;
|
|
354
|
+
}
|
|
355
|
+
hooks;
|
|
356
|
+
async execute(call, complete) {
|
|
357
|
+
try {
|
|
358
|
+
const completed = complete(await call());
|
|
359
|
+
this.hooks.onSuccess?.(completed.result);
|
|
360
|
+
return completed.output;
|
|
361
|
+
} catch (error) {
|
|
362
|
+
const err = toOneError(error);
|
|
363
|
+
this.hooks.onError?.(err);
|
|
364
|
+
throw err;
|
|
365
|
+
}
|
|
366
|
+
}
|
|
367
|
+
};
|
|
7
368
|
var One = class _One {
|
|
8
369
|
native;
|
|
9
|
-
|
|
370
|
+
lifecycle;
|
|
10
371
|
constructor(secret, options) {
|
|
11
372
|
const { onSuccess, onError, ...native } = options ?? {};
|
|
12
373
|
try {
|
|
@@ -14,34 +375,77 @@ var One = class _One {
|
|
|
14
375
|
} catch (error) {
|
|
15
376
|
throw toOneError(error);
|
|
16
377
|
}
|
|
17
|
-
this.
|
|
378
|
+
this.lifecycle = new CallLifecycle({ onSuccess, onError });
|
|
18
379
|
}
|
|
19
|
-
/**
|
|
380
|
+
/**
|
|
381
|
+
* Creates a client that authenticates on an end user's OAuth grant.
|
|
382
|
+
*
|
|
383
|
+
* The counterpart to the constructor, as a factory because a class can only
|
|
384
|
+
* have one constructor and the two credentials are alternatives: a client
|
|
385
|
+
* authenticates as an agency with its secret key, or on a user's grant with
|
|
386
|
+
* their token, never both. What the token may reach is the scopes the user
|
|
387
|
+
* consented to, so a call outside them fails at the API, not here.
|
|
388
|
+
*/
|
|
389
|
+
static withAccessToken(accessToken, options) {
|
|
390
|
+
const { onSuccess, onError, ...native } = options ?? {};
|
|
391
|
+
return _One.wrapping(() => NativeOne.withAccessToken(accessToken, native), { onSuccess, onError });
|
|
392
|
+
}
|
|
393
|
+
/**
|
|
394
|
+
* Creates a client from the environment: `ONE_SECRET` or `ONE_ACCESS_TOKEN`
|
|
395
|
+
* for the credential, plus the optional `ONE_BASE_URL`. `ONE_SECRET` wins
|
|
396
|
+
* when both are set.
|
|
397
|
+
*/
|
|
20
398
|
static fromEnv() {
|
|
399
|
+
return _One.wrapping(() => NativeOne.fromEnv(), {});
|
|
400
|
+
}
|
|
401
|
+
/**
|
|
402
|
+
* Builds a facade around a native client a factory produced, bypassing the
|
|
403
|
+
* constructor because that one takes a secret key. One place assigns the
|
|
404
|
+
* fields, so a new factory cannot forget the lifecycle and leave hooks
|
|
405
|
+
* silently unfired.
|
|
406
|
+
*/
|
|
407
|
+
static wrapping(build, hooks) {
|
|
21
408
|
const client = Object.create(_One.prototype);
|
|
22
409
|
const fields = client;
|
|
23
410
|
try {
|
|
24
|
-
fields.native =
|
|
411
|
+
fields.native = build();
|
|
25
412
|
} catch (error) {
|
|
26
413
|
throw toOneError(error);
|
|
27
414
|
}
|
|
28
|
-
fields.
|
|
415
|
+
fields.lifecycle = new CallLifecycle(hooks);
|
|
29
416
|
return client;
|
|
30
417
|
}
|
|
31
418
|
/** Scopes subsequent calls to a connection by its key. */
|
|
32
419
|
connection(key) {
|
|
33
|
-
return new ConnectionScope(this.native, key, this.
|
|
420
|
+
return new ConnectionScope(this.native, key, this.lifecycle);
|
|
421
|
+
}
|
|
422
|
+
/**
|
|
423
|
+
* Starts building a webhook listener, verified with the subscription's
|
|
424
|
+
* signing secret.
|
|
425
|
+
*/
|
|
426
|
+
webhooks(options) {
|
|
427
|
+
return new Webhooks(this, options);
|
|
428
|
+
}
|
|
429
|
+
/** @internal Registers a webhook listener; the route and request shape live in Rust. */
|
|
430
|
+
async registerWebhook(key, url, events, secret) {
|
|
431
|
+
return this.lifecycle.execute(
|
|
432
|
+
() => this.native.registerWebhook(key, url, events, secret),
|
|
433
|
+
(result) => {
|
|
434
|
+
const out = new CallResult(result.status, result.headers, result.data);
|
|
435
|
+
return { output: result.data, result: out };
|
|
436
|
+
}
|
|
437
|
+
);
|
|
34
438
|
}
|
|
35
439
|
};
|
|
36
440
|
var ConnectionScope = class {
|
|
37
|
-
constructor(native, key,
|
|
441
|
+
constructor(native, key, lifecycle) {
|
|
38
442
|
this.native = native;
|
|
39
443
|
this.key = key;
|
|
40
|
-
this.
|
|
444
|
+
this.lifecycle = lifecycle;
|
|
41
445
|
}
|
|
42
446
|
native;
|
|
43
447
|
key;
|
|
44
|
-
|
|
448
|
+
lifecycle;
|
|
45
449
|
/** Runs a typed action and resolves with the response, typed as the action's
|
|
46
450
|
* default output. `options` sets per-call retry and timeout. */
|
|
47
451
|
async run(action, options) {
|
|
@@ -56,16 +460,13 @@ var ConnectionScope = class {
|
|
|
56
460
|
return this.execute({ connectionKey: this.key, ...request }, options);
|
|
57
461
|
}
|
|
58
462
|
async execute(request, options) {
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
this.hooks.onError?.(err);
|
|
67
|
-
throw err;
|
|
68
|
-
}
|
|
463
|
+
return this.lifecycle.execute(
|
|
464
|
+
() => this.native.run({ ...request, ...options ?? {} }),
|
|
465
|
+
(result) => {
|
|
466
|
+
const out = new CallResult(result.status, result.headers, result.data);
|
|
467
|
+
return { output: out, result: out };
|
|
468
|
+
}
|
|
469
|
+
);
|
|
69
470
|
}
|
|
70
471
|
};
|
|
71
472
|
var CallResult = class {
|
|
@@ -82,33 +483,12 @@ var CallResult = class {
|
|
|
82
483
|
return this.data;
|
|
83
484
|
}
|
|
84
485
|
};
|
|
85
|
-
var OneError = class extends Error {
|
|
86
|
-
constructor(code, message, status, body, retryable) {
|
|
87
|
-
super(message);
|
|
88
|
-
this.code = code;
|
|
89
|
-
this.status = status;
|
|
90
|
-
this.body = body;
|
|
91
|
-
this.retryable = retryable;
|
|
92
|
-
this.name = "OneError";
|
|
93
|
-
}
|
|
94
|
-
code;
|
|
95
|
-
status;
|
|
96
|
-
body;
|
|
97
|
-
retryable;
|
|
98
|
-
};
|
|
99
|
-
function toOneError(error) {
|
|
100
|
-
const message = error instanceof Error ? error.message : String(error);
|
|
101
|
-
try {
|
|
102
|
-
const envelope = JSON.parse(message);
|
|
103
|
-
return new OneError(envelope.code, envelope.message, envelope.status, envelope.body, envelope.retryable);
|
|
104
|
-
} catch {
|
|
105
|
-
return new OneError("unknown", message, void 0, void 0, false);
|
|
106
|
-
}
|
|
107
|
-
}
|
|
108
486
|
export {
|
|
109
487
|
Action,
|
|
110
488
|
CallResult,
|
|
111
489
|
ConnectionScope,
|
|
490
|
+
ConnectionWebhooks,
|
|
112
491
|
One,
|
|
113
|
-
OneError
|
|
492
|
+
OneError,
|
|
493
|
+
Webhooks
|
|
114
494
|
};
|
package/native.d.ts
CHANGED
|
@@ -6,10 +6,28 @@ export type JsonValue = string | number | boolean | null | { [key: string]: Json
|
|
|
6
6
|
export declare class One {
|
|
7
7
|
/** Creates a client from a One secret key and optional configuration. */
|
|
8
8
|
constructor(secret: string, options?: ClientOptions | undefined | null)
|
|
9
|
-
/**
|
|
9
|
+
/**
|
|
10
|
+
* Creates a client from an OAuth access token and optional configuration.
|
|
11
|
+
*
|
|
12
|
+
* The counterpart to the constructor, as a factory because a class can
|
|
13
|
+
* only have one constructor and the two credentials are alternatives: a
|
|
14
|
+
* client authenticates as an agency with its secret key, or on an end
|
|
15
|
+
* user's grant with their token, never both.
|
|
16
|
+
*/
|
|
17
|
+
static withAccessToken(accessToken: string, options?: ClientOptions | undefined | null): One
|
|
18
|
+
/**
|
|
19
|
+
* Creates a client from the environment: `ONE_SECRET` or
|
|
20
|
+
* `ONE_ACCESS_TOKEN` for the credential, and optional `ONE_BASE_URL`.
|
|
21
|
+
*/
|
|
10
22
|
static fromEnv(): One
|
|
11
23
|
/** Runs an action against a connection and resolves with the response. */
|
|
12
24
|
run(request: ActionRequest): Promise<CallResult>
|
|
25
|
+
/**
|
|
26
|
+
* Registers (or reconciles) a webhook listener under a stable name.
|
|
27
|
+
*
|
|
28
|
+
* The route and request shape live in the Rust client; this only marshals.
|
|
29
|
+
*/
|
|
30
|
+
registerWebhook(key: string, url: string, events: Array<string>, secret: string): Promise<CallResult>
|
|
13
31
|
}
|
|
14
32
|
|
|
15
33
|
/**
|
|
@@ -59,9 +77,9 @@ export interface ActionRequest {
|
|
|
59
77
|
timeoutMs?: number
|
|
60
78
|
}
|
|
61
79
|
|
|
62
|
-
/** The result of a successful (2xx)
|
|
80
|
+
/** The result of a successful (2xx) SDK call. */
|
|
63
81
|
export interface CallResult {
|
|
64
|
-
/** HTTP status code returned by the
|
|
82
|
+
/** HTTP status code returned by the request. */
|
|
65
83
|
status: number
|
|
66
84
|
/** Response headers (rate-limit, request-id, `ETag`, `Link`, …). */
|
|
67
85
|
headers: Record<string, string>
|
|
@@ -88,3 +106,34 @@ export interface RetryConfig {
|
|
|
88
106
|
/** Maximum backoff delay in milliseconds (default 1000). */
|
|
89
107
|
maxDelayMs?: number
|
|
90
108
|
}
|
|
109
|
+
|
|
110
|
+
/** A verified webhook delivery handed back to the TS registry for dispatch. */
|
|
111
|
+
export interface VerifiedDelivery {
|
|
112
|
+
/** On-the-wire event string (`connection.created`, ...). */
|
|
113
|
+
event: string
|
|
114
|
+
/** Stable delivery id, identical on every retry. Deduplicate on it. */
|
|
115
|
+
eventId: string
|
|
116
|
+
/** RFC 3339 timestamp of when One emitted the event. */
|
|
117
|
+
timestamp: string
|
|
118
|
+
/** The event-specific payload. */
|
|
119
|
+
payload: JsonValue
|
|
120
|
+
/** Subscription metadata echoed back on delivery, if any was configured. */
|
|
121
|
+
metadata?: JsonValue | null
|
|
122
|
+
/** The connection this delivery concerns, for events that carry one. */
|
|
123
|
+
connection?: string
|
|
124
|
+
/**
|
|
125
|
+
* Whether this is a test delivery, decided by the native client so both
|
|
126
|
+
* SDKs share one definition of the marker.
|
|
127
|
+
*/
|
|
128
|
+
isTest: boolean
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
/**
|
|
132
|
+
* Verifies a webhook delivery's signature and freshness, returning the parsed
|
|
133
|
+
* delivery or throwing an `ErrorEnvelope`.
|
|
134
|
+
*
|
|
135
|
+
* Signature checking, the tolerance window, and envelope parsing live in Rust
|
|
136
|
+
* so the two SDKs cannot drift on them; only handler dispatch is TS, because
|
|
137
|
+
* handlers are closures and those should not cross the boundary.
|
|
138
|
+
*/
|
|
139
|
+
export declare function verifyWebhook(body: Buffer, signature: string, secret: string, toleranceMs?: number | undefined | null): VerifiedDelivery
|
package/native.js
CHANGED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@withone/sdk",
|
|
3
|
-
"version": "0.0
|
|
3
|
+
"version": "0.3.0",
|
|
4
4
|
"description": "The One SDK for Node — typed client over the One API.",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"module": "dist/index.mjs",
|
|
@@ -206,17 +206,17 @@
|
|
|
206
206
|
"native.d.ts"
|
|
207
207
|
],
|
|
208
208
|
"optionalDependencies": {
|
|
209
|
-
"@withone/sdk-linux-x64-gnu": "0.0
|
|
210
|
-
"@withone/sdk-linux-arm64-gnu": "0.0
|
|
211
|
-
"@withone/sdk-linux-x64-musl": "0.0
|
|
212
|
-
"@withone/sdk-darwin-x64": "0.0
|
|
213
|
-
"@withone/sdk-darwin-arm64": "0.0
|
|
214
|
-
"@withone/sdk-win32-x64-msvc": "0.0
|
|
209
|
+
"@withone/sdk-linux-x64-gnu": "0.3.0",
|
|
210
|
+
"@withone/sdk-linux-arm64-gnu": "0.3.0",
|
|
211
|
+
"@withone/sdk-linux-x64-musl": "0.3.0",
|
|
212
|
+
"@withone/sdk-darwin-x64": "0.3.0",
|
|
213
|
+
"@withone/sdk-darwin-arm64": "0.3.0",
|
|
214
|
+
"@withone/sdk-win32-x64-msvc": "0.3.0"
|
|
215
215
|
},
|
|
216
216
|
"scripts": {
|
|
217
217
|
"build:native": "napi build --platform --release --js native.js --dts native.d.ts --dts-header \"export type JsonValue = string | number | boolean | null | { [key: string]: JsonValue } | JsonValue[];\n\"",
|
|
218
218
|
"manifest": "cargo run -p one-codegen --bin one-codegen-manifest -- ../../actions actions.manifest.json",
|
|
219
|
-
"gen": "pnpm run manifest && tsx scripts/gen-actions.ts",
|
|
219
|
+
"gen": "pnpm run manifest && tsx scripts/gen-actions.ts && tsx scripts/gen-webhooks.ts",
|
|
220
220
|
"build:ts": "tsup",
|
|
221
221
|
"build": "pnpm run build:native && pnpm run gen && pnpm run build:ts",
|
|
222
222
|
"pretest": "pnpm run build:native && pnpm run gen",
|