@buildspacestudio/sdk 0.2.2 → 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/dist/auth/index.cjs +259 -0
- package/dist/auth/index.cjs.map +1 -0
- package/dist/auth/{server.d.ts → index.d.cts} +9 -6
- package/dist/auth/index.d.ts +118 -5
- package/dist/auth/index.js +255 -2
- package/dist/auth/index.js.map +1 -1
- package/dist/client/index.cjs +428 -0
- package/dist/client/index.cjs.map +1 -0
- package/dist/client/index.d.cts +85 -0
- package/dist/client/index.d.ts +8 -7
- package/dist/client/index.js +416 -105
- package/dist/client/index.js.map +1 -1
- package/dist/{auth/client.d.ts → client-BH7LbrKM.d.ts} +7 -5
- package/dist/client-C67hy1kt.d.cts +58 -0
- package/dist/{events/client.d.ts → client-Dlif1JBf.d.ts} +5 -3
- package/dist/{auth/client.js → client-DqWXAwCr.d.cts} +27 -30
- package/dist/events/index.cjs +131 -0
- package/dist/events/index.cjs.map +1 -0
- package/dist/events/{server.d.ts → index.d.cts} +7 -4
- package/dist/events/index.d.ts +59 -4
- package/dist/events/index.js +127 -2
- package/dist/events/index.js.map +1 -1
- package/dist/http-U-zzKmFF.d.cts +100 -0
- package/dist/http-U-zzKmFF.d.ts +100 -0
- package/dist/index.cjs +801 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +87 -0
- package/dist/index.d.ts +16 -15
- package/dist/index.js +785 -106
- package/dist/index.js.map +1 -1
- package/dist/notifications/index.cjs +56 -0
- package/dist/notifications/index.cjs.map +1 -0
- package/dist/notifications/{server.d.ts → index.d.cts} +8 -6
- package/dist/notifications/index.d.ts +79 -3
- package/dist/notifications/index.js +53 -1
- package/dist/notifications/index.js.map +1 -1
- package/dist/storage/index.cjs +213 -0
- package/dist/storage/index.cjs.map +1 -0
- package/dist/storage/index.d.cts +195 -0
- package/dist/storage/index.d.ts +195 -5
- package/dist/storage/index.js +209 -2
- package/dist/storage/index.js.map +1 -1
- package/package.json +75 -16
- package/dist/auth/client.d.ts.map +0 -1
- package/dist/auth/client.js.map +0 -1
- package/dist/auth/index.d.ts.map +0 -1
- package/dist/auth/server.d.ts.map +0 -1
- package/dist/auth/server.js +0 -148
- package/dist/auth/server.js.map +0 -1
- package/dist/client/index.d.ts.map +0 -1
- package/dist/config.d.ts +0 -40
- package/dist/config.d.ts.map +0 -1
- package/dist/config.js +0 -27
- package/dist/config.js.map +0 -1
- package/dist/errors.d.ts +0 -31
- package/dist/errors.d.ts.map +0 -1
- package/dist/errors.js +0 -30
- package/dist/errors.js.map +0 -1
- package/dist/events/client.d.ts.map +0 -1
- package/dist/events/client.js +0 -97
- package/dist/events/client.js.map +0 -1
- package/dist/events/index.d.ts.map +0 -1
- package/dist/events/server.d.ts.map +0 -1
- package/dist/events/server.js +0 -65
- package/dist/events/server.js.map +0 -1
- package/dist/http.d.ts +0 -39
- package/dist/http.d.ts.map +0 -1
- package/dist/http.js +0 -74
- package/dist/http.js.map +0 -1
- package/dist/index.d.ts.map +0 -1
- package/dist/notifications/index.d.ts.map +0 -1
- package/dist/notifications/server.d.ts.map +0 -1
- package/dist/notifications/server.js +0 -73
- package/dist/notifications/server.js.map +0 -1
- package/dist/storage/client.d.ts +0 -91
- package/dist/storage/client.d.ts.map +0 -1
- package/dist/storage/client.js +0 -117
- package/dist/storage/client.js.map +0 -1
- package/dist/storage/index.d.ts.map +0 -1
- package/dist/storage/server.d.ts +0 -104
- package/dist/storage/server.d.ts.map +0 -1
- package/dist/storage/server.js +0 -104
- package/dist/storage/server.js.map +0 -1
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import { H as HttpTransport } from './http-U-zzKmFF.cjs';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Client-side event tracking with automatic batching.
|
|
5
|
+
*
|
|
6
|
+
* Events are queued in memory and flushed to the API in batches,
|
|
7
|
+
* either on a timer or when the batch size limit is reached.
|
|
8
|
+
*
|
|
9
|
+
* Access via `buildspace.events` on the client SDK.
|
|
10
|
+
*
|
|
11
|
+
* @example
|
|
12
|
+
* ```ts
|
|
13
|
+
* // Fire and forget — events are queued automatically
|
|
14
|
+
* buildspace.events.track("button.clicked", { label: "upgrade" });
|
|
15
|
+
*
|
|
16
|
+
* // Flush before page unload
|
|
17
|
+
* await buildspace.events.flush();
|
|
18
|
+
*
|
|
19
|
+
* // Graceful shutdown (clears timer, flushes remaining)
|
|
20
|
+
* await buildspace.events.shutdown();
|
|
21
|
+
* ```
|
|
22
|
+
*/
|
|
23
|
+
declare class EventsClientNamespace {
|
|
24
|
+
private readonly queue;
|
|
25
|
+
private readonly transport;
|
|
26
|
+
private timer;
|
|
27
|
+
constructor(transport: HttpTransport, config?: {
|
|
28
|
+
flushInterval?: number;
|
|
29
|
+
maxBatchSize?: number;
|
|
30
|
+
});
|
|
31
|
+
private readonly maxBatchSize;
|
|
32
|
+
/**
|
|
33
|
+
* Queue an event for batched delivery.
|
|
34
|
+
*
|
|
35
|
+
* This method is synchronous and returns immediately. Events are
|
|
36
|
+
* automatically flushed when the batch size is reached or on the
|
|
37
|
+
* configured interval.
|
|
38
|
+
*
|
|
39
|
+
* @param event - Event name, e.g. `"button.clicked"`.
|
|
40
|
+
* @param properties - Arbitrary key-value data to attach.
|
|
41
|
+
* @param actorId - Identifier of the user or entity that triggered the event.
|
|
42
|
+
*/
|
|
43
|
+
track(event: string, properties?: Record<string, unknown>, actorId?: string): void;
|
|
44
|
+
/**
|
|
45
|
+
* Send all queued events to the API immediately.
|
|
46
|
+
*
|
|
47
|
+
* If the flush fails, events are re-queued for the next attempt.
|
|
48
|
+
*/
|
|
49
|
+
flush(): Promise<void>;
|
|
50
|
+
/**
|
|
51
|
+
* Stop the automatic flush timer and send any remaining events.
|
|
52
|
+
*
|
|
53
|
+
* Call this during app shutdown or cleanup to ensure no events are lost.
|
|
54
|
+
*/
|
|
55
|
+
shutdown(): Promise<void>;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
export { EventsClientNamespace as E };
|
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import
|
|
1
|
+
import { H as HttpTransport } from './http-U-zzKmFF.js';
|
|
2
|
+
|
|
2
3
|
/**
|
|
3
4
|
* Client-side event tracking with automatic batching.
|
|
4
5
|
*
|
|
@@ -19,7 +20,7 @@ import type { HttpTransport } from "../http";
|
|
|
19
20
|
* await buildspace.events.shutdown();
|
|
20
21
|
* ```
|
|
21
22
|
*/
|
|
22
|
-
|
|
23
|
+
declare class EventsClientNamespace {
|
|
23
24
|
private readonly queue;
|
|
24
25
|
private readonly transport;
|
|
25
26
|
private timer;
|
|
@@ -53,4 +54,5 @@ export declare class EventsClientNamespace {
|
|
|
53
54
|
*/
|
|
54
55
|
shutdown(): Promise<void>;
|
|
55
56
|
}
|
|
56
|
-
|
|
57
|
+
|
|
58
|
+
export { EventsClientNamespace as E };
|
|
@@ -1,3 +1,23 @@
|
|
|
1
|
+
import { H as HttpTransport } from './http-U-zzKmFF.cjs';
|
|
2
|
+
|
|
3
|
+
/** Options for generating a sign-in URL. */
|
|
4
|
+
interface SignInUrlOptions {
|
|
5
|
+
/** App slug to scope the login to a specific creator app. */
|
|
6
|
+
appSlug?: string;
|
|
7
|
+
/** Environment to authenticate against. */
|
|
8
|
+
env?: "dev" | "prod";
|
|
9
|
+
/** URL to redirect the user to after authentication completes. */
|
|
10
|
+
redirectUri: string;
|
|
11
|
+
}
|
|
12
|
+
/** Options for generating a sign-up URL. */
|
|
13
|
+
interface SignUpUrlOptions {
|
|
14
|
+
/** App slug to scope the registration to a specific creator app. */
|
|
15
|
+
appSlug?: string;
|
|
16
|
+
/** Environment to authenticate against. */
|
|
17
|
+
env?: "dev" | "prod";
|
|
18
|
+
/** URL to redirect the user to after registration completes. */
|
|
19
|
+
redirectUri: string;
|
|
20
|
+
}
|
|
1
21
|
/**
|
|
2
22
|
* Client-side authentication methods for generating login URLs.
|
|
3
23
|
*
|
|
@@ -18,11 +38,9 @@
|
|
|
18
38
|
* window.location.href = signInUrl;
|
|
19
39
|
* ```
|
|
20
40
|
*/
|
|
21
|
-
|
|
22
|
-
transport;
|
|
23
|
-
constructor(transport)
|
|
24
|
-
this.transport = transport;
|
|
25
|
-
}
|
|
41
|
+
declare class AuthClientNamespace {
|
|
42
|
+
private readonly transport;
|
|
43
|
+
constructor(transport: HttpTransport);
|
|
26
44
|
/**
|
|
27
45
|
* Build a URL that redirects the user to the Buildspace sign-in page.
|
|
28
46
|
*
|
|
@@ -33,18 +51,7 @@ export class AuthClientNamespace {
|
|
|
33
51
|
* @param opts - Sign-in URL options.
|
|
34
52
|
* @returns The full sign-in URL as a string.
|
|
35
53
|
*/
|
|
36
|
-
getSignInUrl(opts)
|
|
37
|
-
const url = new URL("/sign-in", this.transport.loginUrl);
|
|
38
|
-
if (opts.appSlug) {
|
|
39
|
-
url.searchParams.set("app", opts.appSlug);
|
|
40
|
-
}
|
|
41
|
-
url.searchParams.set("redirect_uri", opts.redirectUri);
|
|
42
|
-
url.searchParams.set("client_id", this.transport.key);
|
|
43
|
-
if (opts.env) {
|
|
44
|
-
url.searchParams.set("env", opts.env);
|
|
45
|
-
}
|
|
46
|
-
return url.toString();
|
|
47
|
-
}
|
|
54
|
+
getSignInUrl(opts: SignInUrlOptions): string;
|
|
48
55
|
/**
|
|
49
56
|
* Build a URL that redirects the user to the Buildspace sign-up page.
|
|
50
57
|
*
|
|
@@ -54,17 +61,7 @@ export class AuthClientNamespace {
|
|
|
54
61
|
* @param opts - Sign-up URL options.
|
|
55
62
|
* @returns The full sign-up URL as a string.
|
|
56
63
|
*/
|
|
57
|
-
getSignUpUrl(opts)
|
|
58
|
-
const url = new URL("/sign-up", this.transport.loginUrl);
|
|
59
|
-
if (opts.appSlug) {
|
|
60
|
-
url.searchParams.set("app", opts.appSlug);
|
|
61
|
-
}
|
|
62
|
-
url.searchParams.set("redirect_uri", opts.redirectUri);
|
|
63
|
-
url.searchParams.set("client_id", this.transport.key);
|
|
64
|
-
if (opts.env) {
|
|
65
|
-
url.searchParams.set("env", opts.env);
|
|
66
|
-
}
|
|
67
|
-
return url.toString();
|
|
68
|
-
}
|
|
64
|
+
getSignUpUrl(opts: SignUpUrlOptions): string;
|
|
69
65
|
}
|
|
70
|
-
|
|
66
|
+
|
|
67
|
+
export { AuthClientNamespace as A, type SignInUrlOptions as S, type SignUpUrlOptions as a };
|
|
@@ -0,0 +1,131 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
// src/events/client.ts
|
|
4
|
+
var EventsClientNamespace = class {
|
|
5
|
+
queue = [];
|
|
6
|
+
transport;
|
|
7
|
+
timer = null;
|
|
8
|
+
constructor(transport, config = {}) {
|
|
9
|
+
this.transport = transport;
|
|
10
|
+
const flushInterval = config.flushInterval ?? 5e3;
|
|
11
|
+
this.maxBatchSize = config.maxBatchSize ?? 20;
|
|
12
|
+
this.timer = setInterval(() => {
|
|
13
|
+
this.flush().catch(() => {
|
|
14
|
+
});
|
|
15
|
+
}, flushInterval);
|
|
16
|
+
}
|
|
17
|
+
maxBatchSize;
|
|
18
|
+
/**
|
|
19
|
+
* Queue an event for batched delivery.
|
|
20
|
+
*
|
|
21
|
+
* This method is synchronous and returns immediately. Events are
|
|
22
|
+
* automatically flushed when the batch size is reached or on the
|
|
23
|
+
* configured interval.
|
|
24
|
+
*
|
|
25
|
+
* @param event - Event name, e.g. `"button.clicked"`.
|
|
26
|
+
* @param properties - Arbitrary key-value data to attach.
|
|
27
|
+
* @param actorId - Identifier of the user or entity that triggered the event.
|
|
28
|
+
*/
|
|
29
|
+
track(event, properties, actorId) {
|
|
30
|
+
this.queue.push({
|
|
31
|
+
event,
|
|
32
|
+
properties,
|
|
33
|
+
actor_id: actorId,
|
|
34
|
+
timestamp: (/* @__PURE__ */ new Date()).toISOString()
|
|
35
|
+
});
|
|
36
|
+
if (this.queue.length >= this.maxBatchSize) {
|
|
37
|
+
this.flush().catch(() => {
|
|
38
|
+
});
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* Send all queued events to the API immediately.
|
|
43
|
+
*
|
|
44
|
+
* If the flush fails, events are re-queued for the next attempt.
|
|
45
|
+
*/
|
|
46
|
+
async flush() {
|
|
47
|
+
if (this.queue.length === 0) {
|
|
48
|
+
return;
|
|
49
|
+
}
|
|
50
|
+
const batch = this.queue.splice(0, this.queue.length);
|
|
51
|
+
try {
|
|
52
|
+
await this.transport.request({
|
|
53
|
+
service: "events",
|
|
54
|
+
path: "/v1/events/batch",
|
|
55
|
+
method: "POST",
|
|
56
|
+
keepalive: true,
|
|
57
|
+
body: { events: batch }
|
|
58
|
+
});
|
|
59
|
+
} catch {
|
|
60
|
+
this.queue.unshift(...batch);
|
|
61
|
+
throw new Error("Failed to flush events batch.");
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
/**
|
|
65
|
+
* Stop the automatic flush timer and send any remaining events.
|
|
66
|
+
*
|
|
67
|
+
* Call this during app shutdown or cleanup to ensure no events are lost.
|
|
68
|
+
*/
|
|
69
|
+
async shutdown() {
|
|
70
|
+
if (this.timer) {
|
|
71
|
+
clearInterval(this.timer);
|
|
72
|
+
this.timer = null;
|
|
73
|
+
}
|
|
74
|
+
await this.flush();
|
|
75
|
+
}
|
|
76
|
+
};
|
|
77
|
+
|
|
78
|
+
// src/events/server.ts
|
|
79
|
+
var EventsServerNamespace = class {
|
|
80
|
+
transport;
|
|
81
|
+
constructor(transport) {
|
|
82
|
+
this.transport = transport;
|
|
83
|
+
}
|
|
84
|
+
/**
|
|
85
|
+
* Track a single event.
|
|
86
|
+
*
|
|
87
|
+
* @param event - Event name, e.g. `"user.signed_up"`.
|
|
88
|
+
* @param properties - Arbitrary key-value data to attach to the event.
|
|
89
|
+
* @param actorId - Identifier of the user or entity that triggered the event.
|
|
90
|
+
* @returns The ID of the created event.
|
|
91
|
+
*/
|
|
92
|
+
track(event, properties, actorId) {
|
|
93
|
+
return this.transport.request({
|
|
94
|
+
service: "events",
|
|
95
|
+
path: "/v1/events",
|
|
96
|
+
method: "POST",
|
|
97
|
+
body: {
|
|
98
|
+
event,
|
|
99
|
+
properties,
|
|
100
|
+
actor_id: actorId,
|
|
101
|
+
timestamp: (/* @__PURE__ */ new Date()).toISOString()
|
|
102
|
+
}
|
|
103
|
+
});
|
|
104
|
+
}
|
|
105
|
+
/**
|
|
106
|
+
* Track multiple events in a single request.
|
|
107
|
+
*
|
|
108
|
+
* Events without a `timestamp` are automatically timestamped.
|
|
109
|
+
*
|
|
110
|
+
* @param events - Array of events to track.
|
|
111
|
+
* @returns The count and IDs of created events.
|
|
112
|
+
*/
|
|
113
|
+
batchTrack(events) {
|
|
114
|
+
return this.transport.request({
|
|
115
|
+
service: "events",
|
|
116
|
+
path: "/v1/events/batch",
|
|
117
|
+
method: "POST",
|
|
118
|
+
body: {
|
|
119
|
+
events: events.map((event) => ({
|
|
120
|
+
...event,
|
|
121
|
+
timestamp: event.timestamp ?? (/* @__PURE__ */ new Date()).toISOString()
|
|
122
|
+
}))
|
|
123
|
+
}
|
|
124
|
+
});
|
|
125
|
+
}
|
|
126
|
+
};
|
|
127
|
+
|
|
128
|
+
exports.EventsClientNamespace = EventsClientNamespace;
|
|
129
|
+
exports.EventsServerNamespace = EventsServerNamespace;
|
|
130
|
+
//# sourceMappingURL=index.cjs.map
|
|
131
|
+
//# sourceMappingURL=index.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/events/client.ts","../../src/events/server.ts"],"names":[],"mappings":";;;AA6BO,IAAM,wBAAN,MAA4B;AAAA,EAChB,QAAuB,EAAC;AAAA,EACxB,SAAA;AAAA,EACT,KAAA,GAA+C,IAAA;AAAA,EAEvD,WAAA,CACE,SAAA,EACA,MAAA,GAA4D,EAAC,EAC7D;AACA,IAAA,IAAA,CAAK,SAAA,GAAY,SAAA;AACjB,IAAA,MAAM,aAAA,GAAgB,OAAO,aAAA,IAAiB,GAAA;AAC9C,IAAA,IAAA,CAAK,YAAA,GAAe,OAAO,YAA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AAA,CAAI,CAAC,KAAA,MAAW;AAAA,UAC7B,GAAG,KAAA;AAAA,UACH,WAAW,KAAA,CAAM,SAAA,IAAA,iBAAa,IAAI,IAAA,IAAO,WAAA;AAAY,SACvD,CAAE;AAAA;AACJ,KACD,CAAA;AAAA,EACH;AACF","file":"index.cjs","sourcesContent":["import type { HttpTransport } from \"../http\";\n\ninterface QueuedEvent {\n actor_id?: string;\n event: string;\n properties?: Record<string, unknown>;\n timestamp: string;\n}\n\n/**\n * Client-side event tracking with automatic batching.\n *\n * Events are queued in memory and flushed to the API in batches,\n * either on a timer or when the batch size limit is reached.\n *\n * Access via `buildspace.events` on the client SDK.\n *\n * @example\n * ```ts\n * // Fire and forget — events are queued automatically\n * buildspace.events.track(\"button.clicked\", { label: \"upgrade\" });\n *\n * // Flush before page unload\n * await buildspace.events.flush();\n *\n * // Graceful shutdown (clears timer, flushes remaining)\n * await buildspace.events.shutdown();\n * ```\n */\nexport class EventsClientNamespace {\n private readonly queue: QueuedEvent[] = [];\n private readonly transport: HttpTransport;\n private timer: ReturnType<typeof setInterval> | null = null;\n\n constructor(\n transport: HttpTransport,\n config: { flushInterval?: number; maxBatchSize?: number } = {}\n ) {\n this.transport = transport;\n const flushInterval = config.flushInterval ?? 5000;\n this.maxBatchSize = config.maxBatchSize ?? 20;\n this.timer = setInterval(() => {\n this.flush().catch(() => {\n // best effort flush for background timer\n });\n }, flushInterval);\n }\n\n private readonly maxBatchSize: number;\n\n /**\n * Queue an event for batched delivery.\n *\n * This method is synchronous and returns immediately. Events are\n * automatically flushed when the batch size is reached or on the\n * configured interval.\n *\n * @param event - Event name, e.g. `\"button.clicked\"`.\n * @param properties - Arbitrary key-value data to attach.\n * @param actorId - Identifier of the user or entity that triggered the event.\n */\n track(event: string, properties?: Record<string, unknown>, actorId?: string): void {\n this.queue.push({\n event,\n properties,\n actor_id: actorId,\n timestamp: new Date().toISOString(),\n });\n\n if (this.queue.length >= this.maxBatchSize) {\n this.flush().catch(() => {\n // best effort flush on max batch size\n });\n }\n }\n\n /**\n * Send all queued events to the API immediately.\n *\n * If the flush fails, events are re-queued for the next attempt.\n */\n async flush(): Promise<void> {\n if (this.queue.length === 0) {\n return;\n }\n\n const batch = this.queue.splice(0, this.queue.length);\n\n try {\n await this.transport.request<void>({\n service: \"events\",\n path: \"/v1/events/batch\",\n method: \"POST\",\n keepalive: true,\n body: { events: batch },\n });\n } catch {\n this.queue.unshift(...batch);\n throw new Error(\"Failed to flush events batch.\");\n }\n }\n\n /**\n * Stop the automatic flush timer and send any remaining events.\n *\n * Call this during app shutdown or cleanup to ensure no events are lost.\n */\n async shutdown(): Promise<void> {\n if (this.timer) {\n clearInterval(this.timer);\n this.timer = null;\n }\n\n await this.flush();\n }\n}\n","import type { HttpTransport } from \"../http\";\n\n/** An event to include in a batch tracking call. */\nexport interface ServerTrackedEvent {\n /** Identifier of the user or entity that triggered the event. */\n actor_id?: string;\n /** Event name, e.g. `\"user.signed_up\"` or `\"page.viewed\"`. */\n event: string;\n /** Arbitrary key-value data attached to the event. */\n properties?: Record<string, unknown>;\n /** ISO 8601 timestamp. Auto-filled with `new Date().toISOString()` if omitted. */\n timestamp?: string;\n}\n\n/**\n * Server-side event tracking methods.\n *\n * Access via `buildspace.events` on the server SDK.\n *\n * @example\n * ```ts\n * // Track a single event\n * await buildspace.events.track(\"user.signed_up\", { plan: \"pro\" }, userId);\n *\n * // Batch track multiple events\n * await buildspace.events.batchTrack([\n * { event: \"page.viewed\", properties: { path: \"/home\" }, actor_id: userId },\n * ]);\n * ```\n */\nexport class EventsServerNamespace {\n private readonly transport: HttpTransport;\n\n constructor(transport: HttpTransport) {\n this.transport = transport;\n }\n\n /**\n * Track a single event.\n *\n * @param event - Event name, e.g. `\"user.signed_up\"`.\n * @param properties - Arbitrary key-value data to attach to the event.\n * @param actorId - Identifier of the user or entity that triggered the event.\n * @returns The ID of the created event.\n */\n track(\n event: string,\n properties?: Record<string, unknown>,\n actorId?: string\n ): Promise<{ event_id: string }> {\n return this.transport.request<{ event_id: string }>({\n service: \"events\",\n path: \"/v1/events\",\n method: \"POST\",\n body: {\n event,\n properties,\n actor_id: actorId,\n timestamp: new Date().toISOString(),\n },\n });\n }\n\n /**\n * Track multiple events in a single request.\n *\n * Events without a `timestamp` are automatically timestamped.\n *\n * @param events - Array of events to track.\n * @returns The count and IDs of created events.\n */\n batchTrack(events: ServerTrackedEvent[]): Promise<{ count: number; event_ids: string[] }> {\n return this.transport.request<{ count: number; event_ids: string[] }>({\n service: \"events\",\n path: \"/v1/events/batch\",\n method: \"POST\",\n body: {\n events: events.map((event) => ({\n ...event,\n timestamp: event.timestamp ?? new Date().toISOString(),\n })),\n },\n });\n }\n}\n"]}
|
|
@@ -1,6 +1,8 @@
|
|
|
1
|
-
|
|
1
|
+
export { E as EventsClientNamespace } from '../client-C67hy1kt.cjs';
|
|
2
|
+
import { H as HttpTransport } from '../http-U-zzKmFF.cjs';
|
|
3
|
+
|
|
2
4
|
/** An event to include in a batch tracking call. */
|
|
3
|
-
|
|
5
|
+
interface ServerTrackedEvent {
|
|
4
6
|
/** Identifier of the user or entity that triggered the event. */
|
|
5
7
|
actor_id?: string;
|
|
6
8
|
/** Event name, e.g. `"user.signed_up"` or `"page.viewed"`. */
|
|
@@ -26,7 +28,7 @@ export interface ServerTrackedEvent {
|
|
|
26
28
|
* ]);
|
|
27
29
|
* ```
|
|
28
30
|
*/
|
|
29
|
-
|
|
31
|
+
declare class EventsServerNamespace {
|
|
30
32
|
private readonly transport;
|
|
31
33
|
constructor(transport: HttpTransport);
|
|
32
34
|
/**
|
|
@@ -53,4 +55,5 @@ export declare class EventsServerNamespace {
|
|
|
53
55
|
event_ids: string[];
|
|
54
56
|
}>;
|
|
55
57
|
}
|
|
56
|
-
|
|
58
|
+
|
|
59
|
+
export { EventsServerNamespace, type ServerTrackedEvent };
|
package/dist/events/index.d.ts
CHANGED
|
@@ -1,4 +1,59 @@
|
|
|
1
|
-
export { EventsClientNamespace } from
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
1
|
+
export { E as EventsClientNamespace } from '../client-Dlif1JBf.js';
|
|
2
|
+
import { H as HttpTransport } from '../http-U-zzKmFF.js';
|
|
3
|
+
|
|
4
|
+
/** An event to include in a batch tracking call. */
|
|
5
|
+
interface ServerTrackedEvent {
|
|
6
|
+
/** Identifier of the user or entity that triggered the event. */
|
|
7
|
+
actor_id?: string;
|
|
8
|
+
/** Event name, e.g. `"user.signed_up"` or `"page.viewed"`. */
|
|
9
|
+
event: string;
|
|
10
|
+
/** Arbitrary key-value data attached to the event. */
|
|
11
|
+
properties?: Record<string, unknown>;
|
|
12
|
+
/** ISO 8601 timestamp. Auto-filled with `new Date().toISOString()` if omitted. */
|
|
13
|
+
timestamp?: string;
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* Server-side event tracking methods.
|
|
17
|
+
*
|
|
18
|
+
* Access via `buildspace.events` on the server SDK.
|
|
19
|
+
*
|
|
20
|
+
* @example
|
|
21
|
+
* ```ts
|
|
22
|
+
* // Track a single event
|
|
23
|
+
* await buildspace.events.track("user.signed_up", { plan: "pro" }, userId);
|
|
24
|
+
*
|
|
25
|
+
* // Batch track multiple events
|
|
26
|
+
* await buildspace.events.batchTrack([
|
|
27
|
+
* { event: "page.viewed", properties: { path: "/home" }, actor_id: userId },
|
|
28
|
+
* ]);
|
|
29
|
+
* ```
|
|
30
|
+
*/
|
|
31
|
+
declare class EventsServerNamespace {
|
|
32
|
+
private readonly transport;
|
|
33
|
+
constructor(transport: HttpTransport);
|
|
34
|
+
/**
|
|
35
|
+
* Track a single event.
|
|
36
|
+
*
|
|
37
|
+
* @param event - Event name, e.g. `"user.signed_up"`.
|
|
38
|
+
* @param properties - Arbitrary key-value data to attach to the event.
|
|
39
|
+
* @param actorId - Identifier of the user or entity that triggered the event.
|
|
40
|
+
* @returns The ID of the created event.
|
|
41
|
+
*/
|
|
42
|
+
track(event: string, properties?: Record<string, unknown>, actorId?: string): Promise<{
|
|
43
|
+
event_id: string;
|
|
44
|
+
}>;
|
|
45
|
+
/**
|
|
46
|
+
* Track multiple events in a single request.
|
|
47
|
+
*
|
|
48
|
+
* Events without a `timestamp` are automatically timestamped.
|
|
49
|
+
*
|
|
50
|
+
* @param events - Array of events to track.
|
|
51
|
+
* @returns The count and IDs of created events.
|
|
52
|
+
*/
|
|
53
|
+
batchTrack(events: ServerTrackedEvent[]): Promise<{
|
|
54
|
+
count: number;
|
|
55
|
+
event_ids: string[];
|
|
56
|
+
}>;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
export { EventsServerNamespace, type ServerTrackedEvent };
|
package/dist/events/index.js
CHANGED
|
@@ -1,3 +1,128 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
1
|
+
// src/events/client.ts
|
|
2
|
+
var EventsClientNamespace = class {
|
|
3
|
+
queue = [];
|
|
4
|
+
transport;
|
|
5
|
+
timer = null;
|
|
6
|
+
constructor(transport, config = {}) {
|
|
7
|
+
this.transport = transport;
|
|
8
|
+
const flushInterval = config.flushInterval ?? 5e3;
|
|
9
|
+
this.maxBatchSize = config.maxBatchSize ?? 20;
|
|
10
|
+
this.timer = setInterval(() => {
|
|
11
|
+
this.flush().catch(() => {
|
|
12
|
+
});
|
|
13
|
+
}, flushInterval);
|
|
14
|
+
}
|
|
15
|
+
maxBatchSize;
|
|
16
|
+
/**
|
|
17
|
+
* Queue an event for batched delivery.
|
|
18
|
+
*
|
|
19
|
+
* This method is synchronous and returns immediately. Events are
|
|
20
|
+
* automatically flushed when the batch size is reached or on the
|
|
21
|
+
* configured interval.
|
|
22
|
+
*
|
|
23
|
+
* @param event - Event name, e.g. `"button.clicked"`.
|
|
24
|
+
* @param properties - Arbitrary key-value data to attach.
|
|
25
|
+
* @param actorId - Identifier of the user or entity that triggered the event.
|
|
26
|
+
*/
|
|
27
|
+
track(event, properties, actorId) {
|
|
28
|
+
this.queue.push({
|
|
29
|
+
event,
|
|
30
|
+
properties,
|
|
31
|
+
actor_id: actorId,
|
|
32
|
+
timestamp: (/* @__PURE__ */ new Date()).toISOString()
|
|
33
|
+
});
|
|
34
|
+
if (this.queue.length >= this.maxBatchSize) {
|
|
35
|
+
this.flush().catch(() => {
|
|
36
|
+
});
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* Send all queued events to the API immediately.
|
|
41
|
+
*
|
|
42
|
+
* If the flush fails, events are re-queued for the next attempt.
|
|
43
|
+
*/
|
|
44
|
+
async flush() {
|
|
45
|
+
if (this.queue.length === 0) {
|
|
46
|
+
return;
|
|
47
|
+
}
|
|
48
|
+
const batch = this.queue.splice(0, this.queue.length);
|
|
49
|
+
try {
|
|
50
|
+
await this.transport.request({
|
|
51
|
+
service: "events",
|
|
52
|
+
path: "/v1/events/batch",
|
|
53
|
+
method: "POST",
|
|
54
|
+
keepalive: true,
|
|
55
|
+
body: { events: batch }
|
|
56
|
+
});
|
|
57
|
+
} catch {
|
|
58
|
+
this.queue.unshift(...batch);
|
|
59
|
+
throw new Error("Failed to flush events batch.");
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
/**
|
|
63
|
+
* Stop the automatic flush timer and send any remaining events.
|
|
64
|
+
*
|
|
65
|
+
* Call this during app shutdown or cleanup to ensure no events are lost.
|
|
66
|
+
*/
|
|
67
|
+
async shutdown() {
|
|
68
|
+
if (this.timer) {
|
|
69
|
+
clearInterval(this.timer);
|
|
70
|
+
this.timer = null;
|
|
71
|
+
}
|
|
72
|
+
await this.flush();
|
|
73
|
+
}
|
|
74
|
+
};
|
|
75
|
+
|
|
76
|
+
// src/events/server.ts
|
|
77
|
+
var EventsServerNamespace = class {
|
|
78
|
+
transport;
|
|
79
|
+
constructor(transport) {
|
|
80
|
+
this.transport = transport;
|
|
81
|
+
}
|
|
82
|
+
/**
|
|
83
|
+
* Track a single event.
|
|
84
|
+
*
|
|
85
|
+
* @param event - Event name, e.g. `"user.signed_up"`.
|
|
86
|
+
* @param properties - Arbitrary key-value data to attach to the event.
|
|
87
|
+
* @param actorId - Identifier of the user or entity that triggered the event.
|
|
88
|
+
* @returns The ID of the created event.
|
|
89
|
+
*/
|
|
90
|
+
track(event, properties, actorId) {
|
|
91
|
+
return this.transport.request({
|
|
92
|
+
service: "events",
|
|
93
|
+
path: "/v1/events",
|
|
94
|
+
method: "POST",
|
|
95
|
+
body: {
|
|
96
|
+
event,
|
|
97
|
+
properties,
|
|
98
|
+
actor_id: actorId,
|
|
99
|
+
timestamp: (/* @__PURE__ */ new Date()).toISOString()
|
|
100
|
+
}
|
|
101
|
+
});
|
|
102
|
+
}
|
|
103
|
+
/**
|
|
104
|
+
* Track multiple events in a single request.
|
|
105
|
+
*
|
|
106
|
+
* Events without a `timestamp` are automatically timestamped.
|
|
107
|
+
*
|
|
108
|
+
* @param events - Array of events to track.
|
|
109
|
+
* @returns The count and IDs of created events.
|
|
110
|
+
*/
|
|
111
|
+
batchTrack(events) {
|
|
112
|
+
return this.transport.request({
|
|
113
|
+
service: "events",
|
|
114
|
+
path: "/v1/events/batch",
|
|
115
|
+
method: "POST",
|
|
116
|
+
body: {
|
|
117
|
+
events: events.map((event) => ({
|
|
118
|
+
...event,
|
|
119
|
+
timestamp: event.timestamp ?? (/* @__PURE__ */ new Date()).toISOString()
|
|
120
|
+
}))
|
|
121
|
+
}
|
|
122
|
+
});
|
|
123
|
+
}
|
|
124
|
+
};
|
|
125
|
+
|
|
126
|
+
export { EventsClientNamespace, EventsServerNamespace };
|
|
127
|
+
//# sourceMappingURL=index.js.map
|
|
3
128
|
//# sourceMappingURL=index.js.map
|
package/dist/events/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"
|
|
1
|
+
{"version":3,"sources":["../../src/events/client.ts","../../src/events/server.ts"],"names":[],"mappings":";AA6BO,IAAM,wBAAN,MAA4B;AAAA,EAChB,QAAuB,EAAC;AAAA,EACxB,SAAA;AAAA,EACT,KAAA,GAA+C,IAAA;AAAA,EAEvD,WAAA,CACE,SAAA,EAC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K,UAAU,OAAA,CAAgD;AAAA,MACpE,OAAA,EAAS,QAAA;AAAA,MACT,IAAA,EAAM,kBAAA;AAAA,MACN,MAAA,EAAQ,MAAA;AAAA,MACR,IAAA,EAAM;AAAA,QACJ,MAAA,EAAQ,MAAA,CAAO,GAAA,CAAI,CAAC,KAAA,MAAW;AAAA,UAC7B,GAAG,KAAA;AAAA,UACH,WAAW,KAAA,CAAM,SAAA,IAAA,iBAAa,IAAI,IAAA,IAAO,WAAA;AAAY,SACvD,CAAE;AAAA;AACJ,KACD,CAAA;AAAA,EACH;AACF","file":"index.js","sourcesContent":["import type { HttpTransport } from \"../http\";\n\ninterface QueuedEvent {\n actor_id?: string;\n event: string;\n properties?: Record<string, unknown>;\n timestamp: string;\n}\n\n/**\n * Client-side event tracking with automatic batching.\n *\n * Events are queued in memory and flushed to the API in batches,\n * either on a timer or when the batch size limit is reached.\n *\n * Access via `buildspace.events` on the client SDK.\n *\n * @example\n * ```ts\n * // Fire and forget — events are queued automatically\n * buildspace.events.track(\"button.clicked\", { label: \"upgrade\" });\n *\n * // Flush before page unload\n * await buildspace.events.flush();\n *\n * // Graceful shutdown (clears timer, flushes remaining)\n * await buildspace.events.shutdown();\n * ```\n */\nexport class EventsClientNamespace {\n private readonly queue: QueuedEvent[] = [];\n private readonly transport: HttpTransport;\n private timer: ReturnType<typeof setInterval> | null = null;\n\n constructor(\n transport: HttpTransport,\n config: { flushInterval?: number; maxBatchSize?: number } = {}\n ) {\n this.transport = transport;\n const flushInterval = config.flushInterval ?? 5000;\n this.maxBatchSize = config.maxBatchSize ?? 20;\n this.timer = setInterval(() => {\n this.flush().catch(() => {\n // best effort flush for background timer\n });\n }, flushInterval);\n }\n\n private readonly maxBatchSize: number;\n\n /**\n * Queue an event for batched delivery.\n *\n * This method is synchronous and returns immediately. Events are\n * automatically flushed when the batch size is reached or on the\n * configured interval.\n *\n * @param event - Event name, e.g. `\"button.clicked\"`.\n * @param properties - Arbitrary key-value data to attach.\n * @param actorId - Identifier of the user or entity that triggered the event.\n */\n track(event: string, properties?: Record<string, unknown>, actorId?: string): void {\n this.queue.push({\n event,\n properties,\n actor_id: actorId,\n timestamp: new Date().toISOString(),\n });\n\n if (this.queue.length >= this.maxBatchSize) {\n this.flush().catch(() => {\n // best effort flush on max batch size\n });\n }\n }\n\n /**\n * Send all queued events to the API immediately.\n *\n * If the flush fails, events are re-queued for the next attempt.\n */\n async flush(): Promise<void> {\n if (this.queue.length === 0) {\n return;\n }\n\n const batch = this.queue.splice(0, this.queue.length);\n\n try {\n await this.transport.request<void>({\n service: \"events\",\n path: \"/v1/events/batch\",\n method: \"POST\",\n keepalive: true,\n body: { events: batch },\n });\n } catch {\n this.queue.unshift(...batch);\n throw new Error(\"Failed to flush events batch.\");\n }\n }\n\n /**\n * Stop the automatic flush timer and send any remaining events.\n *\n * Call this during app shutdown or cleanup to ensure no events are lost.\n */\n async shutdown(): Promise<void> {\n if (this.timer) {\n clearInterval(this.timer);\n this.timer = null;\n }\n\n await this.flush();\n }\n}\n","import type { HttpTransport } from \"../http\";\n\n/** An event to include in a batch tracking call. */\nexport interface ServerTrackedEvent {\n /** Identifier of the user or entity that triggered the event. */\n actor_id?: string;\n /** Event name, e.g. `\"user.signed_up\"` or `\"page.viewed\"`. */\n event: string;\n /** Arbitrary key-value data attached to the event. */\n properties?: Record<string, unknown>;\n /** ISO 8601 timestamp. Auto-filled with `new Date().toISOString()` if omitted. */\n timestamp?: string;\n}\n\n/**\n * Server-side event tracking methods.\n *\n * Access via `buildspace.events` on the server SDK.\n *\n * @example\n * ```ts\n * // Track a single event\n * await buildspace.events.track(\"user.signed_up\", { plan: \"pro\" }, userId);\n *\n * // Batch track multiple events\n * await buildspace.events.batchTrack([\n * { event: \"page.viewed\", properties: { path: \"/home\" }, actor_id: userId },\n * ]);\n * ```\n */\nexport class EventsServerNamespace {\n private readonly transport: HttpTransport;\n\n constructor(transport: HttpTransport) {\n this.transport = transport;\n }\n\n /**\n * Track a single event.\n *\n * @param event - Event name, e.g. `\"user.signed_up\"`.\n * @param properties - Arbitrary key-value data to attach to the event.\n * @param actorId - Identifier of the user or entity that triggered the event.\n * @returns The ID of the created event.\n */\n track(\n event: string,\n properties?: Record<string, unknown>,\n actorId?: string\n ): Promise<{ event_id: string }> {\n return this.transport.request<{ event_id: string }>({\n service: \"events\",\n path: \"/v1/events\",\n method: \"POST\",\n body: {\n event,\n properties,\n actor_id: actorId,\n timestamp: new Date().toISOString(),\n },\n });\n }\n\n /**\n * Track multiple events in a single request.\n *\n * Events without a `timestamp` are automatically timestamped.\n *\n * @param events - Array of events to track.\n * @returns The count and IDs of created events.\n */\n batchTrack(events: ServerTrackedEvent[]): Promise<{ count: number; event_ids: string[] }> {\n return this.transport.request<{ count: number; event_ids: string[] }>({\n service: \"events\",\n path: \"/v1/events/batch\",\n method: \"POST\",\n body: {\n events: events.map((event) => ({\n ...event,\n timestamp: event.timestamp ?? new Date().toISOString(),\n })),\n },\n });\n }\n}\n"]}
|
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
type ClientMode = "server" | "client";
|
|
2
|
+
/** Configuration options for the server SDK (`new Buildspace()`). */
|
|
3
|
+
interface BuildspaceConfig {
|
|
4
|
+
/** Base URL of the Buildspace API. Defaults to `"https://api.buildspace.studio"`. */
|
|
5
|
+
baseUrl?: string;
|
|
6
|
+
/** Custom `fetch` implementation (useful for testing or edge runtimes). */
|
|
7
|
+
fetch?: typeof fetch;
|
|
8
|
+
/** Base URL of the Buildspace login service. Defaults to `"https://login.buildspace.studio"`. */
|
|
9
|
+
loginUrl?: string;
|
|
10
|
+
/** API version string sent via the `X-Buildspace-Version` header. Defaults to `"2025-06-01"`. */
|
|
11
|
+
version?: string;
|
|
12
|
+
}
|
|
13
|
+
/** Configuration for client-side event batching behavior. */
|
|
14
|
+
interface EventsBatchingConfig {
|
|
15
|
+
/** How often queued events are flushed to the API, in milliseconds. Defaults to `5000`. */
|
|
16
|
+
flushInterval?: number;
|
|
17
|
+
/** Maximum number of events to queue before an automatic flush. Defaults to `20`. */
|
|
18
|
+
maxBatchSize?: number;
|
|
19
|
+
}
|
|
20
|
+
/** Configuration options for the client SDK (`createClient()`). */
|
|
21
|
+
interface BuildspaceClientConfig extends BuildspaceConfig {
|
|
22
|
+
/** Options for client-side event batching. */
|
|
23
|
+
events?: EventsBatchingConfig;
|
|
24
|
+
}
|
|
25
|
+
interface ResolvedConfig {
|
|
26
|
+
baseUrl: string;
|
|
27
|
+
fetch: typeof fetch;
|
|
28
|
+
loginUrl: string;
|
|
29
|
+
mode: ClientMode;
|
|
30
|
+
version: string;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/** Services available in the Buildspace platform. */
|
|
34
|
+
type BuildspaceService = "auth" | "events" | "notifications" | "storage";
|
|
35
|
+
/**
|
|
36
|
+
* Error thrown by all Buildspace SDK methods on failure.
|
|
37
|
+
*
|
|
38
|
+
* @example
|
|
39
|
+
* ```ts
|
|
40
|
+
* try {
|
|
41
|
+
* await buildspace.auth.getSession(token);
|
|
42
|
+
* } catch (err) {
|
|
43
|
+
* if (err instanceof BuildspaceError) {
|
|
44
|
+
* console.error(err.service, err.code, err.status, err.message);
|
|
45
|
+
* }
|
|
46
|
+
* }
|
|
47
|
+
* ```
|
|
48
|
+
*/
|
|
49
|
+
declare class BuildspaceError extends Error {
|
|
50
|
+
/** Machine-readable error code, e.g. `"auth/invalid-token"`. */
|
|
51
|
+
readonly code: string;
|
|
52
|
+
/** Which service produced the error. */
|
|
53
|
+
readonly service: BuildspaceService;
|
|
54
|
+
/** HTTP status code from the API. */
|
|
55
|
+
readonly status: number;
|
|
56
|
+
constructor({ code, message, service, status, }: {
|
|
57
|
+
code: string;
|
|
58
|
+
message: string;
|
|
59
|
+
service: BuildspaceService;
|
|
60
|
+
status: number;
|
|
61
|
+
});
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
interface RequestOptions {
|
|
65
|
+
body?: unknown;
|
|
66
|
+
headers?: Record<string, string>;
|
|
67
|
+
keepalive?: boolean;
|
|
68
|
+
method?: string;
|
|
69
|
+
path: string;
|
|
70
|
+
query?: Record<string, string | number | boolean | undefined>;
|
|
71
|
+
service: BuildspaceService;
|
|
72
|
+
}
|
|
73
|
+
/**
|
|
74
|
+
* Low-level HTTP transport used internally by all SDK namespaces.
|
|
75
|
+
*
|
|
76
|
+
* Handles authentication headers, API versioning, session tokens,
|
|
77
|
+
* and consistent error mapping to {@link BuildspaceError}.
|
|
78
|
+
*/
|
|
79
|
+
declare class HttpTransport {
|
|
80
|
+
readonly baseUrl: string;
|
|
81
|
+
readonly fetcher: typeof fetch;
|
|
82
|
+
readonly key: string;
|
|
83
|
+
readonly loginUrl: string;
|
|
84
|
+
readonly mode: ClientMode;
|
|
85
|
+
readonly version: string;
|
|
86
|
+
private sessionToken;
|
|
87
|
+
constructor({ key, resolvedConfig }: {
|
|
88
|
+
key: string;
|
|
89
|
+
resolvedConfig: ResolvedConfig;
|
|
90
|
+
});
|
|
91
|
+
/** Remove the stored session token. */
|
|
92
|
+
clearSession(): void;
|
|
93
|
+
/** Return the current session token, or `null` if none is set. */
|
|
94
|
+
getSessionToken(): string | null;
|
|
95
|
+
/** Store a session token that will be sent as `X-Session-Token` on subsequent requests. */
|
|
96
|
+
setSession(sessionToken: string): void;
|
|
97
|
+
request<T>({ service, path, method, query, body, headers, keepalive, }: RequestOptions): Promise<T>;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
export { type BuildspaceConfig as B, HttpTransport as H, type BuildspaceClientConfig as a, BuildspaceError as b, type BuildspaceService as c };
|