@buildspacestudio/sdk 0.1.0 → 0.1.2

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.
Files changed (2) hide show
  1. package/README.md +237 -0
  2. package/package.json +3 -2
package/README.md ADDED
@@ -0,0 +1,237 @@
1
+ # @buildspacestudio/sdk
2
+
3
+ The official JavaScript/TypeScript SDK for [Buildspace](https://buildspace.studio) — auth, events, storage, and notifications for your app.
4
+
5
+ ## Installation
6
+
7
+ ```bash
8
+ npm install @buildspacestudio/sdk
9
+ # or
10
+ bun add @buildspacestudio/sdk
11
+ ```
12
+
13
+ ## Quick start
14
+
15
+ The SDK has two entrypoints depending on where your code runs:
16
+
17
+ | Entrypoint | Key type | Use in |
18
+ |---|---|---|
19
+ | `@buildspacestudio/sdk` | Secret key (`bs_sec_*`) | Server / Node.js / Edge functions |
20
+ | `@buildspacestudio/sdk/client` | Publishable key (`bs_pub_*`) | Browser / React / client components |
21
+
22
+ ### Server SDK
23
+
24
+ ```ts
25
+ import Buildspace from "@buildspacestudio/sdk";
26
+
27
+ const buildspace = new Buildspace(process.env.BUILDSPACE_SECRET_KEY!);
28
+ ```
29
+
30
+ ### Client SDK
31
+
32
+ ```ts
33
+ import { createClient } from "@buildspacestudio/sdk/client";
34
+
35
+ const buildspace = createClient(process.env.NEXT_PUBLIC_BUILDSPACE_KEY!);
36
+ ```
37
+
38
+ ---
39
+
40
+ ## Auth
41
+
42
+ ### Server — handle OAuth callback
43
+
44
+ ```ts
45
+ // In your callback route handler:
46
+ const tokens = await buildspace.auth.handleCallback(request);
47
+ // tokens.access_token, tokens.user
48
+
49
+ // Verify a session
50
+ const session = await buildspace.auth.getSession(sessionToken);
51
+ if (!session) {
52
+ // invalid or expired
53
+ }
54
+
55
+ // Revoke a session (sign out)
56
+ await buildspace.auth.revokeSession(sessionToken);
57
+ ```
58
+
59
+ ### Client — generate sign-in / sign-up URLs
60
+
61
+ ```ts
62
+ const signInUrl = buildspace.auth.getSignInUrl({
63
+ redirectUri: "https://yourapp.com/auth/callback",
64
+ appSlug: "my-app", // optional
65
+ });
66
+
67
+ const signUpUrl = buildspace.auth.getSignUpUrl({
68
+ redirectUri: "https://yourapp.com/auth/callback",
69
+ });
70
+ ```
71
+
72
+ ---
73
+
74
+ ## Events
75
+
76
+ Track user and system events for analytics.
77
+
78
+ ### Server
79
+
80
+ ```ts
81
+ // Single event
82
+ await buildspace.events.track("user.signed_up", { plan: "pro" }, userId);
83
+
84
+ // Batch
85
+ await buildspace.events.batchTrack([
86
+ { event: "page.viewed", properties: { path: "/dashboard" }, actor_id: userId },
87
+ { event: "feature.used", properties: { feature: "export" }, actor_id: userId },
88
+ ]);
89
+ ```
90
+
91
+ ### Client (batched, auto-flushed)
92
+
93
+ ```ts
94
+ // Fire and forget — events are queued and flushed in batches
95
+ buildspace.events.track("button.clicked", { label: "upgrade" });
96
+
97
+ // Flush immediately (e.g. before page unload)
98
+ await buildspace.events.flush();
99
+
100
+ // Graceful shutdown
101
+ await buildspace.events.shutdown();
102
+ ```
103
+
104
+ Configure batching behavior:
105
+
106
+ ```ts
107
+ const buildspace = createClient(key, {
108
+ events: {
109
+ flushInterval: 3000, // ms, default 5000
110
+ maxBatchSize: 50, // default 20
111
+ },
112
+ });
113
+ ```
114
+
115
+ ---
116
+
117
+ ## Storage
118
+
119
+ Upload and manage files.
120
+
121
+ ### Server — get a pre-signed upload URL
122
+
123
+ ```ts
124
+ const { upload_url, key } = await buildspace.storage.getUploadUrl({
125
+ key: "avatars/user-123.png",
126
+ contentType: "image/png",
127
+ size: file.size,
128
+ });
129
+
130
+ // Upload directly to the signed URL
131
+ await fetch(upload_url, { method: "PUT", body: file });
132
+
133
+ // Get a signed download URL
134
+ const { url } = await buildspace.storage.getSignedUrl(key, { expiresIn: 3600 });
135
+
136
+ // List objects
137
+ const { objects } = await buildspace.storage.list("avatars/");
138
+
139
+ // Delete
140
+ await buildspace.storage.delete(key);
141
+
142
+ // Check usage
143
+ const { storageBytes, objectCount } = await buildspace.storage.getUsage();
144
+ ```
145
+
146
+ ### Client — upload a File/Blob directly
147
+
148
+ ```ts
149
+ const { key, url } = await buildspace.storage.upload(file, {
150
+ path: "avatars/user-123.png",
151
+ contentType: "image/png", // optional, inferred from File if omitted
152
+ });
153
+ ```
154
+
155
+ ---
156
+
157
+ ## Notifications
158
+
159
+ Send transactional emails. **Server only.**
160
+
161
+ ```ts
162
+ // Send a custom email
163
+ await buildspace.notifications.send({
164
+ to: "user@example.com",
165
+ subject: "Welcome to Buildspace",
166
+ html: "<h1>You're in!</h1>",
167
+ text: "You're in!", // optional plain-text fallback
168
+ replyTo: "support@yourapp.com",
169
+ });
170
+
171
+ // Send from a saved template
172
+ await buildspace.notifications.sendTemplate("welcome-email", {
173
+ to: "user@example.com",
174
+ variables: { firstName: "Alex", plan: "Pro" },
175
+ });
176
+ ```
177
+
178
+ ---
179
+
180
+ ## Sessions
181
+
182
+ Attach a session token to scope requests to a specific user:
183
+
184
+ ```ts
185
+ buildspace.setSession(sessionToken);
186
+
187
+ // All subsequent requests carry the session
188
+ const session = await buildspace.auth.getSession(sessionToken);
189
+
190
+ // Clear when done
191
+ buildspace.clearSession();
192
+ ```
193
+
194
+ ---
195
+
196
+ ## Error handling
197
+
198
+ All errors throw a `BuildspaceError`:
199
+
200
+ ```ts
201
+ import { BuildspaceError } from "@buildspacestudio/sdk";
202
+
203
+ try {
204
+ await buildspace.auth.getSession(token);
205
+ } catch (err) {
206
+ if (err instanceof BuildspaceError) {
207
+ console.error(err.service, err.code, err.status, err.message);
208
+ }
209
+ }
210
+ ```
211
+
212
+ ---
213
+
214
+ ## Configuration
215
+
216
+ Both `new Buildspace()` and `createClient()` accept an optional config object:
217
+
218
+ ```ts
219
+ const buildspace = new Buildspace(secretKey, {
220
+ baseUrl: "https://api.buildspace.studio", // default
221
+ loginUrl: "https://login.buildspace.studio", // default
222
+ version: "2025-06-01", // API version
223
+ fetch: customFetch, // inject a custom fetch implementation
224
+ });
225
+ ```
226
+
227
+ ---
228
+
229
+ ## TypeScript
230
+
231
+ The SDK is written in TypeScript and ships full type definitions. No `@types` package needed.
232
+
233
+ ---
234
+
235
+ ## License
236
+
237
+ MIT
package/package.json CHANGED
@@ -1,6 +1,7 @@
1
1
  {
2
2
  "name": "@buildspacestudio/sdk",
3
- "version": "0.1.0",
3
+ "version": "0.1.2",
4
+ "type": "module",
4
5
  "main": "./dist/index.js",
5
6
  "types": "./dist/index.d.ts",
6
7
  "sideEffects": false,
@@ -42,6 +43,6 @@
42
43
  },
43
44
  "devDependencies": {
44
45
  "typescript": "^5",
45
- "vitest": "^4.0.18"
46
+ "vitest": "^4.1.0"
46
47
  }
47
48
  }