@timeback/webhooks 0.1.1-beta.20260219190739

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 ADDED
@@ -0,0 +1,226 @@
1
+ # @timeback/webhooks
2
+
3
+ TypeScript client for Timeback Webhooks — manage webhook registrations and event filters for real-time notifications.
4
+
5
+ ## Installation
6
+
7
+ ```bash
8
+ bun add @timeback/webhooks
9
+ ```
10
+
11
+ ## Quick Start
12
+
13
+ ```typescript
14
+ import { WebhooksClient } from '@timeback/webhooks'
15
+
16
+ const client = new WebhooksClient({
17
+ env: 'staging', // or 'production'
18
+ auth: {
19
+ clientId: 'your-client-id',
20
+ clientSecret: 'your-client-secret',
21
+ },
22
+ })
23
+
24
+ // Create a webhook
25
+ const webhook = await client.webhooks.create({
26
+ name: 'My Webhook',
27
+ targetUrl: 'https://myapp.example.com/events',
28
+ secret: 'my-shared-secret',
29
+ active: true,
30
+ })
31
+
32
+ // Add a filter to only receive ActivityEvents
33
+ await client.webhookFilters.create({
34
+ webhookId: webhook.id,
35
+ filterKey: 'type',
36
+ filterValue: 'ActivityEvent',
37
+ filterType: 'string',
38
+ filterOperator: 'eq',
39
+ active: true,
40
+ })
41
+
42
+ // List all webhooks
43
+ const webhooks = await client.webhooks.list()
44
+ ```
45
+
46
+ ## API Design
47
+
48
+ ### Standalone vs Composed
49
+
50
+ ```typescript
51
+ // Composed
52
+ import { TimebackClient } from '@timeback/core'
53
+
54
+ // Standalone
55
+ const client = new WebhooksClient({ env: 'staging', auth })
56
+
57
+ const timeback = new TimebackClient({ env: 'staging', auth })
58
+ timeback.webhooks.webhooks.list()
59
+ ```
60
+
61
+ ### Client Structure
62
+
63
+ ```typescript
64
+ const client = new WebhooksClient(options)
65
+
66
+ // Webhooks
67
+ client.webhooks.list(sensor?) // List webhooks (optionally filter by sensor)
68
+ client.webhooks.get(id) // Get webhook by ID
69
+ client.webhooks.create(input) // Create a webhook
70
+ client.webhooks.update(id, input) // Update a webhook
71
+ client.webhooks.delete(id) // Delete a webhook
72
+ client.webhooks.activate(id) // Activate a webhook
73
+ client.webhooks.deactivate(id) // Deactivate a webhook
74
+
75
+ // Webhook Filters
76
+ client.webhookFilters.list() // List all filters
77
+ client.webhookFilters.get(id) // Get filter by ID
78
+ client.webhookFilters.listByWebhook(id) // List filters for a webhook
79
+ client.webhookFilters.create(input) // Create a filter
80
+ client.webhookFilters.update(id, input) // Update a filter
81
+ client.webhookFilters.delete(id) // Delete a filter
82
+ ```
83
+
84
+ ### Managing Webhooks
85
+
86
+ ```typescript
87
+ // Create a webhook for Caliper event notifications
88
+ const webhook = await client.webhooks.create({
89
+ name: 'Activity Tracker',
90
+ targetUrl: 'https://myapp.example.com/caliper-events',
91
+ secret: 'my-shared-secret',
92
+ active: true,
93
+ })
94
+
95
+ // Temporarily disable a webhook
96
+ await client.webhooks.deactivate(webhook.id)
97
+
98
+ // Re-enable it
99
+ await client.webhooks.activate(webhook.id)
100
+
101
+ // Update the target URL
102
+ await client.webhooks.update(webhook.id, {
103
+ name: 'Activity Tracker',
104
+ targetUrl: 'https://new-url.example.com/events',
105
+ secret: 'my-shared-secret',
106
+ active: true,
107
+ })
108
+
109
+ // List webhooks filtered by sensor
110
+ const webhooks = await client.webhooks.list('https://example.edu/sensors/1')
111
+
112
+ // Delete a webhook
113
+ await client.webhooks.delete(webhook.id)
114
+ ```
115
+
116
+ ### Webhook Filters
117
+
118
+ Filters control which events trigger delivery to a webhook:
119
+
120
+ ```typescript
121
+ // Only receive ActivityEvent types
122
+ const filter = await client.webhookFilters.create({
123
+ webhookId: webhook.id,
124
+ filterKey: 'type',
125
+ filterValue: 'ActivityEvent',
126
+ filterType: 'string',
127
+ filterOperator: 'eq',
128
+ active: true,
129
+ })
130
+
131
+ // Update the filter to match a different event type
132
+ await client.webhookFilters.update(filter.id, {
133
+ webhookId: webhook.id,
134
+ filterKey: 'type',
135
+ filterValue: 'TimeSpentEvent',
136
+ filterType: 'string',
137
+ filterOperator: 'eq',
138
+ active: true,
139
+ })
140
+
141
+ // List all filters for a specific webhook
142
+ const filters = await client.webhookFilters.listByWebhook(webhook.id)
143
+
144
+ // Delete a filter
145
+ await client.webhookFilters.delete(filter.id)
146
+ ```
147
+
148
+ ### Authentication
149
+
150
+ The client handles OAuth2 token management automatically:
151
+
152
+ ```typescript
153
+ // Environment mode (recommended for Timeback APIs)
154
+ const client = new WebhooksClient({
155
+ env: 'staging', // or 'production'
156
+ auth: {
157
+ clientId: 'xxx',
158
+ clientSecret: 'xxx',
159
+ },
160
+ })
161
+
162
+ // Explicit mode (custom API)
163
+ const client = new WebhooksClient({
164
+ baseUrl: 'https://webhooks.example.com',
165
+ auth: {
166
+ clientId: 'xxx',
167
+ clientSecret: 'xxx',
168
+ authUrl: 'https://auth.example.com/oauth2/token',
169
+ },
170
+ })
171
+ ```
172
+
173
+ Tokens are cached and refreshed automatically before expiry.
174
+
175
+ ### Error Handling
176
+
177
+ ```typescript
178
+ try {
179
+ await client.webhooks.get('invalid-id')
180
+ } catch (error) {
181
+ if (error instanceof Error) {
182
+ console.log(error.message)
183
+ }
184
+ }
185
+ ```
186
+
187
+ ## Configuration
188
+
189
+ ```typescript
190
+ new WebhooksClient({
191
+ // Environment mode (Timeback APIs)
192
+ env: 'staging' | 'production',
193
+ auth: {
194
+ clientId: string,
195
+ clientSecret: string,
196
+ },
197
+
198
+ // OR Explicit mode (custom API)
199
+ baseUrl: string,
200
+ auth: {
201
+ clientId: string,
202
+ clientSecret: string,
203
+ authUrl: string,
204
+ },
205
+
206
+ // Internal (for composition with @timeback/core)
207
+ transport?: WebhooksTransportLike, // Custom transport
208
+ })
209
+ ```
210
+
211
+ ## Debug Mode
212
+
213
+ Enable debug logging by setting `DEBUG=1` or `DEBUG=true`:
214
+
215
+ ```bash
216
+ DEBUG=1 bun run my-script.ts
217
+ ```
218
+
219
+ This outputs detailed logs for HTTP requests and authentication:
220
+
221
+ ```bash
222
+ [2025-01-15T10:30:00.000Z] DEBUG [webhooks:auth] Fetching new access token...
223
+ [2025-01-15T10:30:00.500Z] DEBUG [webhooks:auth] Token acquired (500ms, expires in 3600s)
224
+ [2025-01-15T10:30:00.501Z] DEBUG [webhooks:http] → POST https://api.example.com/webhooks
225
+ [2025-01-15T10:30:00.800Z] DEBUG [webhooks:http] ← 201 Created (299ms)
226
+ ```
@@ -0,0 +1,70 @@
1
+ /**
2
+ * Webhooks Client
3
+ *
4
+ * Main entry point for the Webhooks management SDK.
5
+ */
6
+ /**
7
+ * Webhooks API client.
8
+ *
9
+ * Provides methods to create, list, update, delete, activate, and
10
+ * deactivate webhooks, as well as manage webhook filters.
11
+ *
12
+ * @example
13
+ * ```typescript
14
+ * // Environment mode (Timeback APIs)
15
+ * const client = new WebhooksClient({
16
+ * env: 'staging',
17
+ * auth: {
18
+ * clientId: 'your-client-id',
19
+ * clientSecret: 'your-client-secret',
20
+ * },
21
+ * })
22
+ * ```
23
+ *
24
+ * @example
25
+ * ```typescript
26
+ * // Provider mode (shared tokens)
27
+ * import { TimebackProvider } from '@timeback/internal-client-infra'
28
+ *
29
+ * const provider = new TimebackProvider({
30
+ * platform: 'BEYOND_AI',
31
+ * env: 'staging',
32
+ * auth: { clientId: '...', clientSecret: '...' },
33
+ * })
34
+ *
35
+ * const client = new WebhooksClient({ provider })
36
+ * ```
37
+ *
38
+ * @example
39
+ * ```typescript
40
+ * // Managing webhooks
41
+ * const webhook = await client.webhooks.create({
42
+ * name: 'My Webhook',
43
+ * targetUrl: 'https://myapp.example.com/events',
44
+ * secret: 'my-shared-secret',
45
+ * active: true,
46
+ * })
47
+ *
48
+ * // Add a filter
49
+ * await client.webhookFilters.create({
50
+ * webhookId: webhook.id,
51
+ * filterKey: 'type',
52
+ * filterValue: 'ActivityEvent',
53
+ * filterType: 'string',
54
+ * filterOperator: 'eq',
55
+ * active: true,
56
+ * })
57
+ * ```
58
+ */
59
+ export declare const WebhooksClient: {
60
+ new (config?: import("./types").WebhooksClientConfig): {
61
+ readonly transport: import("./types").WebhooksTransportLike;
62
+ readonly _provider?: import("@timeback/internal-client-infra").TimebackProvider | undefined;
63
+ readonly webhooks: import("./resources").WebhooksResource;
64
+ readonly webhookFilters: import("./resources").WebhookFiltersResource;
65
+ getTransport(): import("./types").WebhooksTransportLike;
66
+ checkAuth(): Promise<import("@timeback/internal-client-infra").AuthCheckResult>;
67
+ };
68
+ };
69
+ export type { WebhooksClientInstance } from './types';
70
+ //# sourceMappingURL=client.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"client.d.ts","sourceRoot":"","sources":["../src/client.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAIH;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAoDG;AACH,eAAO,MAAM,cAAc;;;;;;;;;CAAyB,CAAA;AAEpD,YAAY,EAAE,sBAAsB,EAAE,MAAM,SAAS,CAAA"}
@@ -0,0 +1,29 @@
1
+ /**
2
+ * Webhooks Constants
3
+ *
4
+ * Configuration constants for the Webhooks client.
5
+ */
6
+ import type { ClientUrlMaps, Platform } from '@timeback/internal-client-infra';
7
+ /**
8
+ * Environment variable names for Webhooks configuration.
9
+ *
10
+ * Supports fallback chains - tries each env var in order until one is defined:
11
+ * - `TIMEBACK_API_*` (canonical/preferred)
12
+ * - `TIMEBACK_*` (unified Timeback shorthand)
13
+ * - `WEBHOOKS_*` (service-specific)
14
+ */
15
+ export declare const WEBHOOKS_ENV_VARS: {
16
+ readonly baseUrl: readonly ["TIMEBACK_API_BASE_URL", "TIMEBACK_BASE_URL", "WEBHOOKS_BASE_URL"];
17
+ readonly authUrl: readonly ["TIMEBACK_API_AUTH_URL", "TIMEBACK_AUTH_URL", "WEBHOOKS_TOKEN_URL"];
18
+ readonly clientId: readonly ["TIMEBACK_API_CLIENT_ID", "TIMEBACK_CLIENT_ID", "WEBHOOKS_CLIENT_ID"];
19
+ readonly clientSecret: readonly ["TIMEBACK_API_CLIENT_SECRET", "TIMEBACK_CLIENT_SECRET", "WEBHOOKS_CLIENT_SECRET"];
20
+ };
21
+ /**
22
+ * Get URL maps for a specific platform.
23
+ * Webhooks share the same base URL as the Caliper API.
24
+ *
25
+ * @param platform - Platform name
26
+ * @returns Client URL maps for the platform
27
+ */
28
+ export declare function getUrlMapsForPlatform(platform?: Platform): ClientUrlMaps;
29
+ //# sourceMappingURL=constants.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"constants.d.ts","sourceRoot":"","sources":["../src/constants.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAIH,OAAO,KAAK,EAAE,aAAa,EAAE,QAAQ,EAAE,MAAM,iCAAiC,CAAA;AAE9E;;;;;;;GAOG;AACH,eAAO,MAAM,iBAAiB;;;;;CASpB,CAAA;AAEV;;;;;;GAMG;AACH,wBAAgB,qBAAqB,CAAC,QAAQ,GAAE,QAA2B,GAAG,aAAa,CAM1F"}
@@ -0,0 +1,38 @@
1
+ /**
2
+ * Webhooks Client Factory
3
+ *
4
+ * Creates WebhooksClient classes bound to specific provider registries.
5
+ */
6
+ import { WebhookFiltersResource, WebhooksResource } from './resources';
7
+ import type { AuthCheckResult, ProviderRegistry, TimebackProvider } from '@timeback/internal-client-infra';
8
+ import type { WebhooksClientConfig, WebhooksTransportLike } from './types';
9
+ /**
10
+ * Create a WebhooksClient class bound to a specific provider registry.
11
+ *
12
+ * @param registry - Provider registry to use (defaults to all Timeback platforms)
13
+ * @returns WebhooksClient class bound to the registry
14
+ */
15
+ export declare function createWebhooksClient(registry?: ProviderRegistry): {
16
+ new (config?: WebhooksClientConfig): {
17
+ /** @internal */
18
+ readonly transport: WebhooksTransportLike;
19
+ /** @internal */
20
+ readonly _provider?: TimebackProvider | undefined;
21
+ /** Manage webhook registrations */
22
+ readonly webhooks: WebhooksResource;
23
+ /** Manage filters that control which events trigger webhook delivery */
24
+ readonly webhookFilters: WebhookFiltersResource;
25
+ /**
26
+ * Get the underlying transport for advanced use cases.
27
+ * @returns The transport instance used by this client
28
+ */
29
+ getTransport(): WebhooksTransportLike;
30
+ /**
31
+ * Verify that OAuth authentication is working.
32
+ * @returns Auth check result
33
+ * @throws {Error} If client was initialized with custom transport (no provider)
34
+ */
35
+ checkAuth(): Promise<AuthCheckResult>;
36
+ };
37
+ };
38
+ //# sourceMappingURL=factory.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"factory.d.ts","sourceRoot":"","sources":["../src/factory.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAMH,OAAO,EAAE,sBAAsB,EAAE,gBAAgB,EAAE,MAAM,aAAa,CAAA;AAGtE,OAAO,KAAK,EACX,eAAe,EACf,gBAAgB,EAChB,gBAAgB,EAChB,MAAM,iCAAiC,CAAA;AACxC,OAAO,KAAK,EAAE,oBAAoB,EAAE,qBAAqB,EAAE,MAAM,SAAS,CAAA;AAE1E;;;;;GAKG;AACH,wBAAgB,oBAAoB,CAAC,QAAQ,GAAE,gBAA4C;;QAQzF,gBAAgB;;QAGhB,gBAAgB;;QAGhB,mCAAmC;;QAEnC,wEAAwE;;QA0CxE;;;WAGG;;QAKH;;;;WAIG;;;EASJ"}