@buildspacestudio/sdk 0.2.2 → 0.4.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 +126 -0
- package/dist/auth/index.cjs +259 -0
- package/dist/auth/index.cjs.map +1 -0
- package/dist/auth/index.d.cts +3 -0
- package/dist/auth/index.d.ts +3 -5
- package/dist/auth/index.js +255 -2
- package/dist/auth/index.js.map +1 -1
- package/dist/billing/index.cjs +226 -0
- package/dist/billing/index.cjs.map +1 -0
- package/dist/billing/index.d.cts +60 -0
- package/dist/billing/index.d.ts +60 -0
- package/dist/billing/index.js +222 -0
- package/dist/billing/index.js.map +1 -0
- package/dist/client/index.cjs +558 -0
- package/dist/client/index.cjs.map +1 -0
- package/dist/client/index.d.cts +89 -0
- package/dist/client/index.d.ts +12 -7
- package/dist/client/index.js +546 -105
- package/dist/client/index.js.map +1 -1
- package/dist/client-BYUWUiGZ.d.cts +143 -0
- package/dist/{auth/client.d.ts → client-ByNR5EZz.d.ts} +7 -5
- package/dist/client-D0vypxWb.d.cts +58 -0
- package/dist/{auth/client.js → client-D7bqvGJv.d.cts} +27 -30
- package/dist/{events/client.d.ts → client-DbGRRMt7.d.ts} +5 -3
- package/dist/client-d7kX5WfR.d.ts +143 -0
- 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-D2gXpNpr.d.cts +100 -0
- package/dist/http-D2gXpNpr.d.ts +100 -0
- package/dist/index.cjs +1029 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +92 -0
- package/dist/index.d.ts +21 -15
- package/dist/index.js +1010 -106
- package/dist/index.js.map +1 -1
- package/dist/next/index.cjs +183 -0
- package/dist/next/index.cjs.map +1 -0
- package/dist/next/index.d.cts +164 -0
- package/dist/next/index.d.ts +164 -0
- package/dist/next/index.js +172 -0
- package/dist/next/index.js.map +1 -0
- 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/react/index.cjs +120 -0
- package/dist/react/index.cjs.map +1 -0
- package/dist/react/index.d.cts +567 -0
- package/dist/react/index.d.ts +567 -0
- package/dist/react/index.js +92 -0
- package/dist/react/index.js.map +1 -0
- package/dist/server-CoPDzSUP.d.cts +117 -0
- package/dist/{auth/server.d.ts → server-Suq3tZZC.d.ts} +8 -6
- 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 +124 -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
package/dist/auth/index.js
CHANGED
|
@@ -1,3 +1,256 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
1
|
+
// src/auth/client.ts
|
|
2
|
+
var AuthClientNamespace = class {
|
|
3
|
+
transport;
|
|
4
|
+
constructor(transport) {
|
|
5
|
+
this.transport = transport;
|
|
6
|
+
}
|
|
7
|
+
/**
|
|
8
|
+
* Build a URL that redirects the user to the Buildspace sign-in page.
|
|
9
|
+
*
|
|
10
|
+
* After the user authenticates, they are redirected back to `redirectUri`
|
|
11
|
+
* with a `?code=` parameter that can be exchanged for tokens via
|
|
12
|
+
* `buildspace.auth.handleCallback()` on the server.
|
|
13
|
+
*
|
|
14
|
+
* @param opts - Sign-in URL options.
|
|
15
|
+
* @returns The full sign-in URL as a string.
|
|
16
|
+
*/
|
|
17
|
+
getSignInUrl(opts) {
|
|
18
|
+
const url = new URL("/sign-in", this.transport.loginUrl);
|
|
19
|
+
if (opts.appSlug) {
|
|
20
|
+
url.searchParams.set("app", opts.appSlug);
|
|
21
|
+
}
|
|
22
|
+
url.searchParams.set("redirect_uri", opts.redirectUri);
|
|
23
|
+
url.searchParams.set("client_id", this.transport.key);
|
|
24
|
+
if (opts.env) {
|
|
25
|
+
url.searchParams.set("env", opts.env);
|
|
26
|
+
}
|
|
27
|
+
return url.toString();
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* Build a URL that redirects the user to the Buildspace sign-up page.
|
|
31
|
+
*
|
|
32
|
+
* Works identically to {@link getSignInUrl} but directs the user to the
|
|
33
|
+
* registration flow instead.
|
|
34
|
+
*
|
|
35
|
+
* @param opts - Sign-up URL options.
|
|
36
|
+
* @returns The full sign-up URL as a string.
|
|
37
|
+
*/
|
|
38
|
+
getSignUpUrl(opts) {
|
|
39
|
+
const url = new URL("/sign-up", this.transport.loginUrl);
|
|
40
|
+
if (opts.appSlug) {
|
|
41
|
+
url.searchParams.set("app", opts.appSlug);
|
|
42
|
+
}
|
|
43
|
+
url.searchParams.set("redirect_uri", opts.redirectUri);
|
|
44
|
+
url.searchParams.set("client_id", this.transport.key);
|
|
45
|
+
if (opts.env) {
|
|
46
|
+
url.searchParams.set("env", opts.env);
|
|
47
|
+
}
|
|
48
|
+
return url.toString();
|
|
49
|
+
}
|
|
50
|
+
};
|
|
51
|
+
|
|
52
|
+
// src/errors.ts
|
|
53
|
+
var BuildspaceError = class extends Error {
|
|
54
|
+
/** Machine-readable error code, e.g. `"auth/invalid-token"`. */
|
|
55
|
+
code;
|
|
56
|
+
/** Which service produced the error. */
|
|
57
|
+
service;
|
|
58
|
+
/** HTTP status code from the API. */
|
|
59
|
+
status;
|
|
60
|
+
constructor({
|
|
61
|
+
code,
|
|
62
|
+
message,
|
|
63
|
+
service,
|
|
64
|
+
status
|
|
65
|
+
}) {
|
|
66
|
+
super(message);
|
|
67
|
+
this.name = "BuildspaceError";
|
|
68
|
+
this.code = code;
|
|
69
|
+
this.service = service;
|
|
70
|
+
this.status = status;
|
|
71
|
+
}
|
|
72
|
+
};
|
|
73
|
+
|
|
74
|
+
// src/auth/server.ts
|
|
75
|
+
var LOOPBACK_HOSTS = /* @__PURE__ */ new Set(["127.0.0.1", "localhost", "::1", "0.0.0.0"]);
|
|
76
|
+
var FORWARDED_HOST_RE = /host="?([^;"]+)"?/i;
|
|
77
|
+
var FORWARDED_PROTO_RE = /proto="?([^;"]+)"?/i;
|
|
78
|
+
function getFirstForwardedValue(value) {
|
|
79
|
+
if (!value) {
|
|
80
|
+
return null;
|
|
81
|
+
}
|
|
82
|
+
return value.split(",").map((part) => part.trim()).find(Boolean) ?? null;
|
|
83
|
+
}
|
|
84
|
+
function stripPort(host) {
|
|
85
|
+
if (host.startsWith("[") && host.includes("]")) {
|
|
86
|
+
return host.slice(1, host.indexOf("]"));
|
|
87
|
+
}
|
|
88
|
+
const colonIndex = host.indexOf(":");
|
|
89
|
+
return colonIndex >= 0 ? host.slice(0, colonIndex) : host;
|
|
90
|
+
}
|
|
91
|
+
function isPrivateIpv4(hostname) {
|
|
92
|
+
const parts = hostname.split(".").map((part) => Number(part));
|
|
93
|
+
if (parts.length !== 4 || parts.some((part) => Number.isNaN(part) || part < 0 || part > 255)) {
|
|
94
|
+
return false;
|
|
95
|
+
}
|
|
96
|
+
return parts[0] === 10 || parts[0] === 172 && parts[1] >= 16 && parts[1] <= 31 || parts[0] === 192 && parts[1] === 168;
|
|
97
|
+
}
|
|
98
|
+
function isInternalHostname(hostname) {
|
|
99
|
+
const normalized = hostname.toLowerCase();
|
|
100
|
+
return LOOPBACK_HOSTS.has(normalized) || normalized.endsWith(".local") || normalized.endsWith(".internal") || isPrivateIpv4(normalized);
|
|
101
|
+
}
|
|
102
|
+
function getForwardedOrigin(request) {
|
|
103
|
+
const forwarded = getFirstForwardedValue(request.headers.get("forwarded"));
|
|
104
|
+
if (forwarded) {
|
|
105
|
+
const forwardedHost2 = forwarded.match(FORWARDED_HOST_RE)?.[1]?.trim();
|
|
106
|
+
const forwardedProto2 = forwarded.match(FORWARDED_PROTO_RE)?.[1]?.trim();
|
|
107
|
+
if (forwardedHost2 && forwardedProto2) {
|
|
108
|
+
return `${forwardedProto2}://${forwardedHost2}`;
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
const forwardedHost = getFirstForwardedValue(request.headers.get("x-forwarded-host"));
|
|
112
|
+
const forwardedProto = getFirstForwardedValue(request.headers.get("x-forwarded-proto"));
|
|
113
|
+
if (forwardedHost && forwardedProto) {
|
|
114
|
+
return `${forwardedProto}://${forwardedHost}`;
|
|
115
|
+
}
|
|
116
|
+
return null;
|
|
117
|
+
}
|
|
118
|
+
function resolveDefaultRedirectUri(request, url) {
|
|
119
|
+
if (!(request instanceof Request)) {
|
|
120
|
+
return `${url.origin}${url.pathname}`;
|
|
121
|
+
}
|
|
122
|
+
const forwardedOrigin = getForwardedOrigin(request);
|
|
123
|
+
if (forwardedOrigin) {
|
|
124
|
+
return `${forwardedOrigin}${url.pathname}`;
|
|
125
|
+
}
|
|
126
|
+
const hostHeader = request.headers.get("host")?.trim();
|
|
127
|
+
if (hostHeader && !isInternalHostname(stripPort(hostHeader))) {
|
|
128
|
+
return `${url.protocol}//${hostHeader}${url.pathname}`;
|
|
129
|
+
}
|
|
130
|
+
if (!isInternalHostname(url.hostname)) {
|
|
131
|
+
return `${url.origin}${url.pathname}`;
|
|
132
|
+
}
|
|
133
|
+
return `${url.origin}${url.pathname}`;
|
|
134
|
+
}
|
|
135
|
+
var AuthServerNamespace = class {
|
|
136
|
+
transport;
|
|
137
|
+
constructor(transport) {
|
|
138
|
+
this.transport = transport;
|
|
139
|
+
}
|
|
140
|
+
/**
|
|
141
|
+
* Retrieve the session associated with a token.
|
|
142
|
+
*
|
|
143
|
+
* Returns the {@link AuthSession} if valid, or `null` if the token is
|
|
144
|
+
* expired, revoked, or invalid (401/404).
|
|
145
|
+
*
|
|
146
|
+
* @throws {BuildspaceError} On unexpected server errors (5xx, network issues).
|
|
147
|
+
*/
|
|
148
|
+
async getSession(sessionToken) {
|
|
149
|
+
try {
|
|
150
|
+
return await this.transport.request({
|
|
151
|
+
service: "auth",
|
|
152
|
+
path: "/v1/auth/session",
|
|
153
|
+
headers: { "X-Session-Token": sessionToken }
|
|
154
|
+
});
|
|
155
|
+
} catch (error) {
|
|
156
|
+
if (error instanceof BuildspaceError && (error.status === 401 || error.status === 404)) {
|
|
157
|
+
return null;
|
|
158
|
+
}
|
|
159
|
+
throw error;
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
/**
|
|
163
|
+
* Exchange an OAuth authorization code for tokens.
|
|
164
|
+
*
|
|
165
|
+
* Extracts the `code` query parameter from the callback URL and exchanges
|
|
166
|
+
* it with the Buildspace API for an access token and user info.
|
|
167
|
+
*
|
|
168
|
+
* @param request - The incoming callback request. Accepts a `Request` object,
|
|
169
|
+
* a `URL`, or a raw URL string containing the `?code=` parameter.
|
|
170
|
+
* @param opts - Optional settings.
|
|
171
|
+
* @param opts.redirectUri - Override the redirect URI sent to the token endpoint.
|
|
172
|
+
* Defaults to the origin + pathname of the callback URL.
|
|
173
|
+
* @returns The token response containing `access_token`, `expires_in`, and `user`.
|
|
174
|
+
*
|
|
175
|
+
* @throws {BuildspaceError} If the code exchange fails (invalid code, expired, etc.).
|
|
176
|
+
*
|
|
177
|
+
* @example
|
|
178
|
+
* ```ts
|
|
179
|
+
* // Next.js Route Handler
|
|
180
|
+
* export async function GET(request: Request) {
|
|
181
|
+
* const { access_token, user } = await buildspace.auth.handleCallback(request);
|
|
182
|
+
* // Store access_token as a session cookie
|
|
183
|
+
* }
|
|
184
|
+
* ```
|
|
185
|
+
*/
|
|
186
|
+
handleCallback(request, opts) {
|
|
187
|
+
let url;
|
|
188
|
+
if (typeof request === "string") {
|
|
189
|
+
url = new URL(request);
|
|
190
|
+
} else if (request instanceof URL) {
|
|
191
|
+
url = request;
|
|
192
|
+
} else {
|
|
193
|
+
url = new URL(request.url);
|
|
194
|
+
}
|
|
195
|
+
const code = url.searchParams.get("code");
|
|
196
|
+
if (!code) {
|
|
197
|
+
throw new BuildspaceError({
|
|
198
|
+
service: "auth",
|
|
199
|
+
status: 400,
|
|
200
|
+
code: "auth/missing-code",
|
|
201
|
+
message: "No auth code found in callback URL. Expected ?code= parameter."
|
|
202
|
+
});
|
|
203
|
+
}
|
|
204
|
+
const redirectUri = opts?.redirectUri ?? resolveDefaultRedirectUri(request, url);
|
|
205
|
+
return this.transport.request({
|
|
206
|
+
service: "auth",
|
|
207
|
+
path: "/v1/auth/token",
|
|
208
|
+
method: "POST",
|
|
209
|
+
body: {
|
|
210
|
+
code,
|
|
211
|
+
redirect_uri: redirectUri
|
|
212
|
+
}
|
|
213
|
+
});
|
|
214
|
+
}
|
|
215
|
+
/**
|
|
216
|
+
* Revoke a specific session token.
|
|
217
|
+
*
|
|
218
|
+
* @param sessionToken - The token to revoke.
|
|
219
|
+
* @throws {BuildspaceError} If the revocation fails.
|
|
220
|
+
*/
|
|
221
|
+
async revokeSession(sessionToken) {
|
|
222
|
+
await this.transport.request({
|
|
223
|
+
service: "auth",
|
|
224
|
+
path: "/v1/auth/session",
|
|
225
|
+
method: "DELETE",
|
|
226
|
+
headers: { "X-Session-Token": sessionToken }
|
|
227
|
+
});
|
|
228
|
+
}
|
|
229
|
+
/**
|
|
230
|
+
* Sign the user out by revoking the session and clearing the SDK's stored token.
|
|
231
|
+
*
|
|
232
|
+
* If the session is already expired or not found, the error is silently
|
|
233
|
+
* ignored and the local session is still cleared.
|
|
234
|
+
*
|
|
235
|
+
* @param sessionToken - Token to revoke. If omitted, uses the token
|
|
236
|
+
* previously set via `buildspace.setSession()`.
|
|
237
|
+
*/
|
|
238
|
+
async signOut(sessionToken) {
|
|
239
|
+
const token = sessionToken ?? this.transport.getSessionToken();
|
|
240
|
+
try {
|
|
241
|
+
if (token) {
|
|
242
|
+
await this.revokeSession(token);
|
|
243
|
+
}
|
|
244
|
+
} catch (error) {
|
|
245
|
+
if (!(error instanceof BuildspaceError && error.service === "auth" && (error.status === 401 || error.status === 404))) {
|
|
246
|
+
throw error;
|
|
247
|
+
}
|
|
248
|
+
} finally {
|
|
249
|
+
this.transport.clearSession();
|
|
250
|
+
}
|
|
251
|
+
}
|
|
252
|
+
};
|
|
253
|
+
|
|
254
|
+
export { AuthClientNamespace, AuthServerNamespace };
|
|
255
|
+
//# sourceMappingURL=index.js.map
|
|
3
256
|
//# sourceMappingURL=index.js.map
|
package/dist/auth/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/auth/index.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,mBAAmB,EAAE,MAAM,UAAU,CAAC;AAE/C,OAAO,EAAE,mBAAmB,EAAE,MAAM,UAAU,CAAC"}
|
|
1
|
+
{"version":3,"sources":["../../src/auth/client.ts","../../src/errors.ts","../../src/auth/server.ts"],"names":["forwardedHost","forwardedProto"],"mappings":";AA0CO,IAAM,sBAAN,MAA0B;AAAA,EACd,SAAA;AAAA,EAEjB,YAAY,SAAA,EAA0B;AACpC,IAAA,IAAA,CAAK,SAAA,GAAY,SAAA;AAAA,EACnB;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAYA,aAAa,IAAA,EAAgC;AAC3C,IAAA,MAAM,MAAM,IAAI,GAAA,CAAI,UAAA,EAAY,IAAA,CAAK,UAAU,QAAQ,CAAA;AAEvD,IAAA,IAAI,KAAK,OAAA,EAAS;AAChB,MAAA,GAAA,CAAI,YAAA,CAAa,GAAA,CAAI,KAAA,EAAO,IAAA,CAAK,OAAO,CAAA;AAAA,IAC1C;AAEA,IAAA,GAAA,CAAI,YAAA,CAAa,GAAA,CAAI,cAAA,EAAgB,IAAA,CAAK,WAAW,CAAA;AACrD,IAAA,GAAA,CAAI,YAAA,CAAa,GAAA,CAAI,WAAA,EAAa,IAAA,CAAK,UAAU,GAAG,CAAA;AAEpD,IAAA,IAAI,KAAK,GAAA,EAAK;AACZ,MAAA,GAAA,CAAI,YAAA,CAAa,GAAA,CAAI,KAAA,EAAO,IAAA,CAAK,GAAG,CAAA;AAAA,IACtC;AAEA,IAAA,OAAO,IAAI,QAAA,EAAS;AAAA,EACtB;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAWA,aAAa,IAAA,EAAgC;AAC3C,IAAA,MAAM,MAAM,IAAI,GAAA,CAAI,UAAA,EAAY,IAAA,CAAK,UAAU,QAAQ,CAAA;AAEvD,I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A;AAAA;AAAA;AAAA;AAAA,EAQA,MAAM,cAAc,YAAA,EAAqC;AACvD,IAAA,MAAM,IAAA,CAAK,UAAU,OAAA,CAAc;AAAA,MACjC,OAAA,EAAS,MAAA;AAAA,MACT,IAAA,EAAM,kBAAA;AAAA,MACN,MAAA,EAAQ,QAAA;AAAA,MACR,OAAA,EAAS,EAAE,iBAAA,EAAmB,YAAA;AAAa,KAC5C,CAAA;AAAA,EACH;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAWA,MAAM,QAAQ,YAAA,EAAsC;AAClD,IAAA,MAAM,KAAA,GAAQ,YAAA,IAAgB,IAAA,CAAK,SAAA,CAAU,eAAA,EAAgB;AAE7D,IAAA,IAAI;AACF,MAAA,IAAI,KAAA,EAAO;AACT,QAAA,MAAM,IAAA,CAAK,cAAc,KAAK,CAAA;AAAA,MAChC;AAAA,IACF,SAAS,KAAA,EAAO;AACd,MAAA,IACE,EACE,KAAA,YAAiB,eAAA,IACjB,KAAA,CAAM,OAAA,KAAY,MAAA,KACjB,KAAA,CAAM,MAAA,KAAW,GAAA,IAAO,KAAA,CAAM,MAAA,KAAW,GAAA,CAAA,CAAA,EAE5C;AACA,QAAA,MAAM,KAAA;AAAA,MACR;AAAA,IACF,CAAA,SAAE;AACA,MAAA,IAAA,CAAK,UAAU,YAAA,EAAa;AAAA,IAC9B;AAAA,EACF;AACF","file":"index.js","sourcesContent":["import type { HttpTransport } from \"../http\";\n\n/** Options for generating a sign-in URL. */\nexport interface SignInUrlOptions {\n /** App slug to scope the login to a specific creator app. */\n appSlug?: string;\n /** Environment to authenticate against. */\n env?: \"dev\" | \"prod\";\n /** URL to redirect the user to after authentication completes. */\n redirectUri: string;\n}\n\n/** Options for generating a sign-up URL. */\nexport interface SignUpUrlOptions {\n /** App slug to scope the registration to a specific creator app. */\n appSlug?: string;\n /** Environment to authenticate against. */\n env?: \"dev\" | \"prod\";\n /** URL to redirect the user to after registration completes. */\n redirectUri: string;\n}\n\n/**\n * Client-side authentication methods for generating login URLs.\n *\n * Access via `buildspace.auth` on the client SDK, or `buildspace.authClient`\n * on the server SDK when you need to generate URLs from server-side code.\n *\n * @example\n * ```ts\n * import { createClient } from \"@buildspacestudio/sdk/client\";\n *\n * const buildspace = createClient(process.env.NEXT_PUBLIC_BUILDSPACE_KEY!);\n *\n * const signInUrl = buildspace.auth.getSignInUrl({\n * redirectUri: \"https://yourapp.com/auth/callback\",\n * });\n *\n * // Use in a link or redirect\n * window.location.href = signInUrl;\n * ```\n */\nexport class AuthClientNamespace {\n private readonly transport: HttpTransport;\n\n constructor(transport: HttpTransport) {\n this.transport = transport;\n }\n\n /**\n * Build a URL that redirects the user to the Buildspace sign-in page.\n *\n * After the user authenticates, they are redirected back to `redirectUri`\n * with a `?code=` parameter that can be exchanged for tokens via\n * `buildspace.auth.handleCallback()` on the server.\n *\n * @param opts - Sign-in URL options.\n * @returns The full sign-in URL as a string.\n */\n getSignInUrl(opts: SignInUrlOptions): string {\n const url = new URL(\"/sign-in\", this.transport.loginUrl);\n\n if (opts.appSlug) {\n url.searchParams.set(\"app\", opts.appSlug);\n }\n\n url.searchParams.set(\"redirect_uri\", opts.redirectUri);\n url.searchParams.set(\"client_id\", this.transport.key);\n\n if (opts.env) {\n url.searchParams.set(\"env\", opts.env);\n }\n\n return url.toString();\n }\n\n /**\n * Build a URL that redirects the user to the Buildspace sign-up page.\n *\n * Works identically to {@link getSignInUrl} but directs the user to the\n * registration flow instead.\n *\n * @param opts - Sign-up URL options.\n * @returns The full sign-up URL as a string.\n */\n getSignUpUrl(opts: SignUpUrlOptions): string {\n const url = new URL(\"/sign-up\", this.transport.loginUrl);\n\n if (opts.appSlug) {\n url.searchParams.set(\"app\", opts.appSlug);\n }\n\n url.searchParams.set(\"redirect_uri\", opts.redirectUri);\n url.searchParams.set(\"client_id\", this.transport.key);\n\n if (opts.env) {\n url.searchParams.set(\"env\", opts.env);\n }\n\n return url.toString();\n }\n}\n","/** Services available in the Buildspace platform. */\nexport type BuildspaceService = \"auth\" | \"billing\" | \"events\" | \"notifications\" | \"storage\";\n\n/**\n * Error thrown by all Buildspace SDK methods on failure.\n *\n * @example\n * ```ts\n * try {\n * await buildspace.auth.getSession(token);\n * } catch (err) {\n * if (err instanceof BuildspaceError) {\n * console.error(err.service, err.code, err.status, err.message);\n * }\n * }\n * ```\n */\nexport class BuildspaceError extends Error {\n /** Machine-readable error code, e.g. `\"auth/invalid-token\"`. */\n readonly code: string;\n /** Which service produced the error. */\n readonly service: BuildspaceService;\n /** HTTP status code from the API. */\n readonly status: number;\n\n constructor({\n code,\n message,\n service,\n status,\n }: {\n code: string;\n message: string;\n service: BuildspaceService;\n status: number;\n }) {\n super(message);\n this.name = \"BuildspaceError\";\n this.code = code;\n this.service = service;\n this.status = status;\n }\n}\n","import { BuildspaceError } from \"../errors\";\nimport type { HttpTransport } from \"../http\";\n\n/** A Buildspace user returned from auth endpoints. */\nexport interface AuthUser {\n /** The user's email address. */\n email: string;\n /** Unique user identifier. */\n id: string;\n /** The user's display name, or `null` if not set. */\n name: string | null;\n}\n\n/**\n * An active session for an authenticated user.\n *\n * Returned by {@link AuthServerNamespace.getSession}.\n */\nexport interface AuthSession {\n /** The app this session belongs to, or `null` for platform-level sessions. */\n appId: string | null;\n /** The authenticated user. */\n user: AuthUser;\n}\n\n/**\n * Token response returned after a successful OAuth callback exchange.\n *\n * Returned by {@link AuthServerNamespace.handleCallback}.\n */\nexport interface TokenResponse {\n /** The access token to use for authenticated requests. */\n access_token: string;\n /** Token lifetime in seconds. */\n expires_in: number;\n /** Always `\"bearer\"`. */\n token_type: \"bearer\";\n /** The authenticated user. */\n user: AuthUser;\n}\n\nconst LOOPBACK_HOSTS = new Set([\"127.0.0.1\", \"localhost\", \"::1\", \"0.0.0.0\"]);\nconst FORWARDED_HOST_RE = /host=\"?([^;\"]+)\"?/i;\nconst FORWARDED_PROTO_RE = /proto=\"?([^;\"]+)\"?/i;\n\nfunction getFirstForwardedValue(value: string | null): string | null {\n if (!value) {\n return null;\n }\n\n return (\n value\n .split(\",\")\n .map((part) => part.trim())\n .find(Boolean) ?? null\n );\n}\n\nfunction stripPort(host: string): string {\n if (host.startsWith(\"[\") && host.includes(\"]\")) {\n return host.slice(1, host.indexOf(\"]\"));\n }\n\n const colonIndex = host.indexOf(\":\");\n return colonIndex >= 0 ? host.slice(0, colonIndex) : host;\n}\n\nfunction isPrivateIpv4(hostname: string): boolean {\n const parts = hostname.split(\".\").map((part) => Number(part));\n if (parts.length !== 4 || parts.some((part) => Number.isNaN(part) || part < 0 || part > 255)) {\n return false;\n }\n\n return (\n parts[0] === 10 ||\n (parts[0] === 172 && parts[1] >= 16 && parts[1] <= 31) ||\n (parts[0] === 192 && parts[1] === 168)\n );\n}\n\nfunction isInternalHostname(hostname: string): boolean {\n const normalized = hostname.toLowerCase();\n\n return (\n LOOPBACK_HOSTS.has(normalized) ||\n normalized.endsWith(\".local\") ||\n normalized.endsWith(\".internal\") ||\n isPrivateIpv4(normalized)\n );\n}\n\nfunction getForwardedOrigin(request: Request): string | null {\n const forwarded = getFirstForwardedValue(request.headers.get(\"forwarded\"));\n if (forwarded) {\n const forwardedHost = forwarded.match(FORWARDED_HOST_RE)?.[1]?.trim();\n const forwardedProto = forwarded.match(FORWARDED_PROTO_RE)?.[1]?.trim();\n\n if (forwardedHost && forwardedProto) {\n return `${forwardedProto}://${forwardedHost}`;\n }\n }\n\n const forwardedHost = getFirstForwardedValue(request.headers.get(\"x-forwarded-host\"));\n const forwardedProto = getFirstForwardedValue(request.headers.get(\"x-forwarded-proto\"));\n\n if (forwardedHost && forwardedProto) {\n return `${forwardedProto}://${forwardedHost}`;\n }\n\n return null;\n}\n\nfunction resolveDefaultRedirectUri(request: Request | URL | string, url: URL): string {\n if (!(request instanceof Request)) {\n return `${url.origin}${url.pathname}`;\n }\n\n const forwardedOrigin = getForwardedOrigin(request);\n if (forwardedOrigin) {\n return `${forwardedOrigin}${url.pathname}`;\n }\n\n const hostHeader = request.headers.get(\"host\")?.trim();\n if (hostHeader && !isInternalHostname(stripPort(hostHeader))) {\n return `${url.protocol}//${hostHeader}${url.pathname}`;\n }\n\n if (!isInternalHostname(url.hostname)) {\n return `${url.origin}${url.pathname}`;\n }\n\n return `${url.origin}${url.pathname}`;\n}\n\n/**\n * Server-side authentication methods.\n *\n * Access via `buildspace.auth` on the server SDK.\n *\n * @example\n * ```ts\n * import Buildspace from \"@buildspacestudio/sdk\";\n *\n * const buildspace = new Buildspace(process.env.BUILDSPACE_SECRET_KEY!);\n *\n * // Handle OAuth callback\n * const tokens = await buildspace.auth.handleCallback(request);\n *\n * // Verify a session\n * const session = await buildspace.auth.getSession(sessionToken);\n *\n * // Sign out\n * await buildspace.auth.signOut(sessionToken);\n * ```\n */\nexport class AuthServerNamespace {\n private readonly transport: HttpTransport;\n\n constructor(transport: HttpTransport) {\n this.transport = transport;\n }\n\n /**\n * Retrieve the session associated with a token.\n *\n * Returns the {@link AuthSession} if valid, or `null` if the token is\n * expired, revoked, or invalid (401/404).\n *\n * @throws {BuildspaceError} On unexpected server errors (5xx, network issues).\n */\n async getSession(sessionToken: string): Promise<AuthSession | null> {\n try {\n return await this.transport.request<AuthSession>({\n service: \"auth\",\n path: \"/v1/auth/session\",\n headers: { \"X-Session-Token\": sessionToken },\n });\n } catch (error) {\n if (error instanceof BuildspaceError && (error.status === 401 || error.status === 404)) {\n return null;\n }\n throw error;\n }\n }\n\n /**\n * Exchange an OAuth authorization code for tokens.\n *\n * Extracts the `code` query parameter from the callback URL and exchanges\n * it with the Buildspace API for an access token and user info.\n *\n * @param request - The incoming callback request. Accepts a `Request` object,\n * a `URL`, or a raw URL string containing the `?code=` parameter.\n * @param opts - Optional settings.\n * @param opts.redirectUri - Override the redirect URI sent to the token endpoint.\n * Defaults to the origin + pathname of the callback URL.\n * @returns The token response containing `access_token`, `expires_in`, and `user`.\n *\n * @throws {BuildspaceError} If the code exchange fails (invalid code, expired, etc.).\n *\n * @example\n * ```ts\n * // Next.js Route Handler\n * export async function GET(request: Request) {\n * const { access_token, user } = await buildspace.auth.handleCallback(request);\n * // Store access_token as a session cookie\n * }\n * ```\n */\n handleCallback(\n request: Request | URL | string,\n opts?: { redirectUri?: string }\n ): Promise<TokenResponse> {\n let url: URL;\n if (typeof request === \"string\") {\n url = new URL(request);\n } else if (request instanceof URL) {\n url = request;\n } else {\n url = new URL(request.url);\n }\n\n const code = url.searchParams.get(\"code\");\n if (!code) {\n throw new BuildspaceError({\n service: \"auth\",\n status: 400,\n code: \"auth/missing-code\",\n message: \"No auth code found in callback URL. Expected ?code= parameter.\",\n });\n }\n\n const redirectUri = opts?.redirectUri ?? resolveDefaultRedirectUri(request, url);\n\n return this.transport.request<TokenResponse>({\n service: \"auth\",\n path: \"/v1/auth/token\",\n method: \"POST\",\n body: {\n code,\n redirect_uri: redirectUri,\n },\n });\n }\n\n /**\n * Revoke a specific session token.\n *\n * @param sessionToken - The token to revoke.\n * @throws {BuildspaceError} If the revocation fails.\n */\n async revokeSession(sessionToken: string): Promise<void> {\n await this.transport.request<void>({\n service: \"auth\",\n path: \"/v1/auth/session\",\n method: \"DELETE\",\n headers: { \"X-Session-Token\": sessionToken },\n });\n }\n\n /**\n * Sign the user out by revoking the session and clearing the SDK's stored token.\n *\n * If the session is already expired or not found, the error is silently\n * ignored and the local session is still cleared.\n *\n * @param sessionToken - Token to revoke. If omitted, uses the token\n * previously set via `buildspace.setSession()`.\n */\n async signOut(sessionToken?: string): Promise<void> {\n const token = sessionToken ?? this.transport.getSessionToken();\n\n try {\n if (token) {\n await this.revokeSession(token);\n }\n } catch (error) {\n if (\n !(\n error instanceof BuildspaceError &&\n error.service === \"auth\" &&\n (error.status === 401 || error.status === 404)\n )\n ) {\n throw error;\n }\n } finally {\n this.transport.clearSession();\n }\n }\n}\n"]}
|
|
@@ -0,0 +1,226 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
// src/billing/shared.ts
|
|
4
|
+
function shouldShowTestBanner(status) {
|
|
5
|
+
return status.testMode;
|
|
6
|
+
}
|
|
7
|
+
function billingIdentityQuery(opts) {
|
|
8
|
+
return { app_user_id: opts?.appUserId, user_id: opts?.userId };
|
|
9
|
+
}
|
|
10
|
+
function checkoutRequestBody(opts) {
|
|
11
|
+
return {
|
|
12
|
+
app_user_id: "appUserId" in opts ? opts.appUserId : void 0,
|
|
13
|
+
cancel_url: opts.cancelUrl,
|
|
14
|
+
checkout_request_id: opts.checkoutRequestId,
|
|
15
|
+
lookup_key: opts.lookupKey,
|
|
16
|
+
metadata: opts.metadata,
|
|
17
|
+
price_id: opts.priceId,
|
|
18
|
+
quantity: opts.quantity,
|
|
19
|
+
success_url: opts.successUrl,
|
|
20
|
+
user_id: "userId" in opts ? opts.userId : void 0
|
|
21
|
+
};
|
|
22
|
+
}
|
|
23
|
+
function portalRequestBody(opts) {
|
|
24
|
+
return {
|
|
25
|
+
app_user_id: "appUserId" in opts ? opts.appUserId : void 0,
|
|
26
|
+
return_url: opts.returnUrl,
|
|
27
|
+
user_id: "userId" in opts ? opts.userId : void 0
|
|
28
|
+
};
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
// src/billing/client.ts
|
|
32
|
+
function redirectTo(url) {
|
|
33
|
+
const browser = globalThis;
|
|
34
|
+
if (!browser.window?.location) {
|
|
35
|
+
throw new Error("redirectToCheckout can only be used in a browser environment");
|
|
36
|
+
}
|
|
37
|
+
browser.window.location.href = url;
|
|
38
|
+
}
|
|
39
|
+
var BillingClientNamespace = class {
|
|
40
|
+
transport;
|
|
41
|
+
constructor(transport) {
|
|
42
|
+
this.transport = transport;
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
* Read whether billing is enabled for the app environment tied to this
|
|
46
|
+
* publishable key, and whether the connected Stripe account is in test or
|
|
47
|
+
* live mode.
|
|
48
|
+
*
|
|
49
|
+
* Render a "Test mode" banner in your app whenever `testMode` is true.
|
|
50
|
+
*/
|
|
51
|
+
getStatus() {
|
|
52
|
+
return this.transport.request({
|
|
53
|
+
service: "billing",
|
|
54
|
+
path: "/v1/billing/status"
|
|
55
|
+
});
|
|
56
|
+
}
|
|
57
|
+
/**
|
|
58
|
+
* List the active billing products for the app environment tied to this
|
|
59
|
+
* publishable key.
|
|
60
|
+
*/
|
|
61
|
+
listProducts() {
|
|
62
|
+
return this.transport.request({
|
|
63
|
+
service: "billing",
|
|
64
|
+
path: "/v1/billing/products"
|
|
65
|
+
});
|
|
66
|
+
}
|
|
67
|
+
/**
|
|
68
|
+
* List the active billing prices for the app environment tied to this
|
|
69
|
+
* publishable key.
|
|
70
|
+
*/
|
|
71
|
+
listPrices() {
|
|
72
|
+
return this.transport.request({
|
|
73
|
+
service: "billing",
|
|
74
|
+
path: "/v1/billing/prices"
|
|
75
|
+
});
|
|
76
|
+
}
|
|
77
|
+
/**
|
|
78
|
+
* Create a Checkout Session for the signed-in browser user and redirect to
|
|
79
|
+
* the Stripe-hosted Checkout page.
|
|
80
|
+
*
|
|
81
|
+
* This method depends on a Buildspace session being available through
|
|
82
|
+
* `setSession()` or your app's authenticated request context.
|
|
83
|
+
*/
|
|
84
|
+
async redirectToCheckout(opts) {
|
|
85
|
+
const checkout = await this.transport.request({
|
|
86
|
+
service: "billing",
|
|
87
|
+
path: "/v1/billing/checkout",
|
|
88
|
+
method: "POST",
|
|
89
|
+
body: checkoutRequestBody(opts)
|
|
90
|
+
});
|
|
91
|
+
redirectTo(checkout.url);
|
|
92
|
+
}
|
|
93
|
+
/**
|
|
94
|
+
* Open the Stripe-hosted customer portal for the signed-in browser user.
|
|
95
|
+
*/
|
|
96
|
+
async redirectToPortal(opts) {
|
|
97
|
+
const portal = await this.transport.request({
|
|
98
|
+
service: "billing",
|
|
99
|
+
path: "/v1/billing/portal",
|
|
100
|
+
method: "POST",
|
|
101
|
+
body: portalRequestBody(opts)
|
|
102
|
+
});
|
|
103
|
+
redirectTo(portal.url);
|
|
104
|
+
}
|
|
105
|
+
/**
|
|
106
|
+
* Discoverable alias for redirectToPortal — opens the Stripe-hosted customer
|
|
107
|
+
* portal so the signed-in user can manage their subscription, payment method,
|
|
108
|
+
* and invoices.
|
|
109
|
+
*/
|
|
110
|
+
manageSubscription(opts) {
|
|
111
|
+
return this.redirectToPortal(opts);
|
|
112
|
+
}
|
|
113
|
+
/**
|
|
114
|
+
* Read the current subscription state for the signed-in browser user.
|
|
115
|
+
*/
|
|
116
|
+
getSubscription() {
|
|
117
|
+
return this.transport.request({
|
|
118
|
+
service: "billing",
|
|
119
|
+
path: "/v1/billing/subscription"
|
|
120
|
+
});
|
|
121
|
+
}
|
|
122
|
+
/**
|
|
123
|
+
* Read the current paid entitlement state for the signed-in browser user.
|
|
124
|
+
*/
|
|
125
|
+
getEntitlements() {
|
|
126
|
+
return this.transport.request({
|
|
127
|
+
service: "billing",
|
|
128
|
+
path: "/v1/billing/entitlements"
|
|
129
|
+
});
|
|
130
|
+
}
|
|
131
|
+
};
|
|
132
|
+
|
|
133
|
+
// src/billing/server.ts
|
|
134
|
+
var BillingServerNamespace = class {
|
|
135
|
+
transport;
|
|
136
|
+
constructor(transport) {
|
|
137
|
+
this.transport = transport;
|
|
138
|
+
}
|
|
139
|
+
/**
|
|
140
|
+
* Read whether billing is enabled for the app environment tied to this API
|
|
141
|
+
* key, and whether the connected Stripe account is in test or live mode.
|
|
142
|
+
*
|
|
143
|
+
* Render a "Test mode" banner in your app whenever `testMode` is true.
|
|
144
|
+
*/
|
|
145
|
+
getStatus() {
|
|
146
|
+
return this.transport.request({
|
|
147
|
+
service: "billing",
|
|
148
|
+
path: "/v1/billing/status"
|
|
149
|
+
});
|
|
150
|
+
}
|
|
151
|
+
/**
|
|
152
|
+
* List the active billing products configured for the app environment tied to
|
|
153
|
+
* this API key.
|
|
154
|
+
*/
|
|
155
|
+
listProducts() {
|
|
156
|
+
return this.transport.request({
|
|
157
|
+
service: "billing",
|
|
158
|
+
path: "/v1/billing/products"
|
|
159
|
+
});
|
|
160
|
+
}
|
|
161
|
+
/**
|
|
162
|
+
* List the active billing prices configured for the app environment tied to
|
|
163
|
+
* this API key.
|
|
164
|
+
*/
|
|
165
|
+
listPrices() {
|
|
166
|
+
return this.transport.request({
|
|
167
|
+
service: "billing",
|
|
168
|
+
path: "/v1/billing/prices"
|
|
169
|
+
});
|
|
170
|
+
}
|
|
171
|
+
/**
|
|
172
|
+
* Create a Stripe Checkout Session on the creator's connected Stripe account.
|
|
173
|
+
*
|
|
174
|
+
* Use `userId` for Buildspace-authenticated users, or `appUserId` when your
|
|
175
|
+
* app uses its own auth system and you want Buildspace to track billing state
|
|
176
|
+
* against an app-defined user id.
|
|
177
|
+
*/
|
|
178
|
+
createCheckout(opts) {
|
|
179
|
+
return this.transport.request({
|
|
180
|
+
service: "billing",
|
|
181
|
+
path: "/v1/billing/checkout",
|
|
182
|
+
method: "POST",
|
|
183
|
+
body: checkoutRequestBody(opts)
|
|
184
|
+
});
|
|
185
|
+
}
|
|
186
|
+
/**
|
|
187
|
+
* Create a Stripe-hosted customer portal session for the specified billing
|
|
188
|
+
* identity.
|
|
189
|
+
*/
|
|
190
|
+
createPortalSession(opts) {
|
|
191
|
+
return this.transport.request({
|
|
192
|
+
service: "billing",
|
|
193
|
+
path: "/v1/billing/portal",
|
|
194
|
+
method: "POST",
|
|
195
|
+
body: portalRequestBody(opts)
|
|
196
|
+
});
|
|
197
|
+
}
|
|
198
|
+
/**
|
|
199
|
+
* Read the current subscription state for a Buildspace user or app-defined
|
|
200
|
+
* billing identity.
|
|
201
|
+
*/
|
|
202
|
+
getSubscription(opts) {
|
|
203
|
+
return this.transport.request({
|
|
204
|
+
service: "billing",
|
|
205
|
+
path: "/v1/billing/subscription",
|
|
206
|
+
query: billingIdentityQuery(opts)
|
|
207
|
+
});
|
|
208
|
+
}
|
|
209
|
+
/**
|
|
210
|
+
* Read the current paid entitlement state for a Buildspace user or app-defined
|
|
211
|
+
* billing identity.
|
|
212
|
+
*/
|
|
213
|
+
getEntitlements(opts) {
|
|
214
|
+
return this.transport.request({
|
|
215
|
+
service: "billing",
|
|
216
|
+
path: "/v1/billing/entitlements",
|
|
217
|
+
query: billingIdentityQuery(opts)
|
|
218
|
+
});
|
|
219
|
+
}
|
|
220
|
+
};
|
|
221
|
+
|
|
222
|
+
exports.BillingClientNamespace = BillingClientNamespace;
|
|
223
|
+
exports.BillingServerNamespace = BillingServerNamespace;
|
|
224
|
+
exports.shouldShowTestBanner = shouldShowTestBanner;
|
|
225
|
+
//# sourceMappingURL=index.cjs.map
|
|
226
|
+
//# sourceMappingURL=index.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/billing/shared.ts","../../src/billing/client.ts","../../src/billing/server.ts"],"names":[],"mappings":";;;AAoFO,SAAS,qBAAqB,MAAA,EAAwC;AAC3E,EAAA,OAAO,MAAA,CAAO,QAAA;AAChB;AAYO,SAAS,qBAAqB,IAAA,EAAgD;AACnF,EAAA,OAAO,EAAE,WAAA,EAAa,IAAA,EAAM,SAAA,EAAW,OAAA,EAAS,MAAM,MAAA,EAAO;AAC/D;AAEO,SAAS,oBACd,IAAA,EACA;AACA,EAAA,OAAO;AAAA,IACL,WAAA,EAAa,WAAA,IAAe,IAAA,GAAO,IAAA,CAAK,SAAA,GAAY,MAAA;AAAA,IACpD,YAAY,IAAA,CAAK,SAAA;AAAA,IACjB,qBAAqB,IAAA,CAAK,iBAAA;AAAA,IAC1B,YAAY,IAAA,CAAK,SAAA;AAAA,IACjB,UAAU,IAAA,CAAK,QAAA;AAAA,IACf,UAAU,IAAA,CAAK,OAAA;AAAA,IACf,UAAU,IAAA,CAAK,QAAA;AAAA,IACf,aAAa,IAAA,CAAK,UAAA;AAAA,IAClB,OAAA,EAAS,QAAA,IAAY,IAAA,GAAO,IAAA,CAAK,MAAA,GAAS;AAAA,GAC5C;AACF;AAEO,SAAS,kBACd,IAAA,EACA;AACA,EAAA,OAAO;AAAA,IACL,WAAA,EAAa,WAAA,IAAe,IAAA,GAAO,IAAA,CAAK,SAA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C,CAAA;AAAA,EACH;AAAA;AAAA;AAAA;AAAA;AAAA,EAMA,gBAAgB,IAAA,EAGyB;AACvC,IAAA,OAAO,IAAA,CAAK,UAAU,OAAA,CAAqC;AAAA,MACzD,OAAA,EAAS,SAAA;AAAA,MACT,IAAA,EAAM,0BAAA;AAAA,MACN,KAAA,EAAO,qBAAqB,IAAI;AAAA,KACjC,CAAA;AAAA,EACH;AACF","file":"index.cjs","sourcesContent":["export interface BillingProduct {\n active: boolean;\n createdAt: string;\n description: string | null;\n id: string;\n name: string;\n}\n\nexport interface BillingPrice {\n active: boolean;\n amountCents: number | null;\n createdAt: string;\n currency: string;\n id: string;\n interval: string | null;\n lookupKey: string | null;\n productId: string;\n productName: string;\n type: \"one_time\" | \"recurring\" | \"metered\";\n}\n\nexport interface CreateServerCheckoutOptions {\n appUserId?: string;\n cancelUrl: string;\n checkoutRequestId?: string;\n lookupKey?: string;\n metadata?: Record<string, string>;\n priceId?: string;\n quantity?: number;\n successUrl: string;\n userId?: string;\n}\n\nexport interface CreateClientCheckoutOptions {\n cancelUrl: string;\n checkoutRequestId?: string;\n lookupKey?: string;\n metadata?: Record<string, string>;\n priceId?: string;\n quantity?: number;\n successUrl: string;\n}\n\nexport interface BillingCheckoutResponse {\n id?: string;\n stripe_checkout_session_id: string;\n url: string;\n}\n\nexport interface BillingPortalResponse {\n url: string;\n}\n\nexport interface BillingSubscriptionResponse {\n subscription: {\n cancelAtPeriodEnd: boolean;\n currentPeriodEnd: string | null;\n id: string;\n status: string;\n } | null;\n}\n\nexport interface BillingEntitlementsResponse {\n active: boolean;\n subscription: { currentPeriodEnd: string | null; id: string; status: string } | null;\n}\n\nexport type BillingMode = \"test\" | \"live\";\n\nexport type BillingConfigStatus = \"disabled\" | \"setup_required\" | \"active\" | \"paused\";\n\nexport interface BillingStatusResponse {\n enabled: boolean;\n mode: BillingMode | null;\n status: BillingConfigStatus | null;\n testMode: boolean;\n}\n\n/**\n * True when the app environment is wired to a Stripe test-mode account.\n *\n * Use this to render a \"Test mode\" banner in your app — payments aren't real\n * charges, and end-buyers should not be expected to enter real card details.\n */\nexport function shouldShowTestBanner(status: BillingStatusResponse): boolean {\n return status.testMode;\n}\n\nexport interface CreateServerPortalSessionOptions {\n appUserId?: string;\n returnUrl: string;\n userId?: string;\n}\n\nexport interface CreateClientPortalSessionOptions {\n returnUrl: string;\n}\n\nexport function billingIdentityQuery(opts?: { appUserId?: string; userId?: string }) {\n return { app_user_id: opts?.appUserId, user_id: opts?.userId };\n}\n\nexport function checkoutRequestBody(\n opts: CreateServerCheckoutOptions | CreateClientCheckoutOptions\n) {\n return {\n app_user_id: \"appUserId\" in opts ? opts.appUserId : undefined,\n cancel_url: opts.cancelUrl,\n checkout_request_id: opts.checkoutRequestId,\n lookup_key: opts.lookupKey,\n metadata: opts.metadata,\n price_id: opts.priceId,\n quantity: opts.quantity,\n success_url: opts.successUrl,\n user_id: \"userId\" in opts ? opts.userId : undefined,\n };\n}\n\nexport function portalRequestBody(\n opts: CreateServerPortalSessionOptions | CreateClientPortalSessionOptions\n) {\n return {\n app_user_id: \"appUserId\" in opts ? opts.appUserId : undefined,\n return_url: opts.returnUrl,\n user_id: \"userId\" in opts ? opts.userId : undefined,\n };\n}\n","import type { HttpTransport } from \"../http\";\nimport {\n type BillingEntitlementsResponse,\n type BillingPortalResponse,\n type BillingPrice,\n type BillingProduct,\n type BillingStatusResponse,\n type BillingSubscriptionResponse,\n type CreateClientCheckoutOptions,\n type CreateClientPortalSessionOptions,\n checkoutRequestBody,\n portalRequestBody,\n} from \"./shared\";\n\nfunction redirectTo(url: string): void {\n const browser = globalThis as typeof globalThis & {\n window?: { location: { href: string } };\n };\n if (!browser.window?.location) {\n throw new Error(\"redirectToCheckout can only be used in a browser environment\");\n }\n browser.window.location.href = url;\n}\n\nexport class BillingClientNamespace {\n private readonly transport: HttpTransport;\n\n constructor(transport: HttpTransport) {\n this.transport = transport;\n }\n\n /**\n * Read whether billing is enabled for the app environment tied to this\n * publishable key, and whether the connected Stripe account is in test or\n * live mode.\n *\n * Render a \"Test mode\" banner in your app whenever `testMode` is true.\n */\n getStatus(): Promise<BillingStatusResponse> {\n return this.transport.request<BillingStatusResponse>({\n service: \"billing\",\n path: \"/v1/billing/status\",\n });\n }\n\n /**\n * List the active billing products for the app environment tied to this\n * publishable key.\n */\n listProducts(): Promise<{ products: BillingProduct[] }> {\n return this.transport.request<{ products: BillingProduct[] }>({\n service: \"billing\",\n path: \"/v1/billing/products\",\n });\n }\n\n /**\n * List the active billing prices for the app environment tied to this\n * publishable key.\n */\n listPrices(): Promise<{ prices: BillingPrice[] }> {\n return this.transport.request<{ prices: BillingPrice[] }>({\n service: \"billing\",\n path: \"/v1/billing/prices\",\n });\n }\n\n /**\n * Create a Checkout Session for the signed-in browser user and redirect to\n * the Stripe-hosted Checkout page.\n *\n * This method depends on a Buildspace session being available through\n * `setSession()` or your app's authenticated request context.\n */\n async redirectToCheckout(opts: CreateClientCheckoutOptions): Promise<void> {\n const checkout = await this.transport.request<{ url: string }>({\n service: \"billing\",\n path: \"/v1/billing/checkout\",\n method: \"POST\",\n body: checkoutRequestBody(opts),\n });\n redirectTo(checkout.url);\n }\n\n /**\n * Open the Stripe-hosted customer portal for the signed-in browser user.\n */\n async redirectToPortal(opts: CreateClientPortalSessionOptions): Promise<void> {\n const portal = await this.transport.request<BillingPortalResponse>({\n service: \"billing\",\n path: \"/v1/billing/portal\",\n method: \"POST\",\n body: portalRequestBody(opts),\n });\n redirectTo(portal.url);\n }\n\n /**\n * Discoverable alias for redirectToPortal — opens the Stripe-hosted customer\n * portal so the signed-in user can manage their subscription, payment method,\n * and invoices.\n */\n manageSubscription(opts: CreateClientPortalSessionOptions): Promise<void> {\n return this.redirectToPortal(opts);\n }\n\n /**\n * Read the current subscription state for the signed-in browser user.\n */\n getSubscription(): Promise<BillingSubscriptionResponse> {\n return this.transport.request<BillingSubscriptionResponse>({\n service: \"billing\",\n path: \"/v1/billing/subscription\",\n });\n }\n\n /**\n * Read the current paid entitlement state for the signed-in browser user.\n */\n getEntitlements(): Promise<BillingEntitlementsResponse> {\n return this.transport.request<BillingEntitlementsResponse>({\n service: \"billing\",\n path: \"/v1/billing/entitlements\",\n });\n }\n}\n","import type { HttpTransport } from \"../http\";\nimport {\n type BillingCheckoutResponse,\n type BillingEntitlementsResponse,\n type BillingPortalResponse,\n type BillingPrice,\n type BillingProduct,\n type BillingStatusResponse,\n type BillingSubscriptionResponse,\n billingIdentityQuery,\n type CreateServerCheckoutOptions,\n type CreateServerPortalSessionOptions,\n checkoutRequestBody,\n portalRequestBody,\n} from \"./shared\";\n\nexport type {\n BillingCheckoutResponse,\n BillingEntitlementsResponse,\n BillingPortalResponse,\n BillingPrice,\n BillingProduct,\n BillingStatusResponse,\n BillingSubscriptionResponse,\n CreateServerCheckoutOptions,\n CreateServerPortalSessionOptions,\n} from \"./shared\";\nexport { shouldShowTestBanner } from \"./shared\";\n\nexport class BillingServerNamespace {\n private readonly transport: HttpTransport;\n\n constructor(transport: HttpTransport) {\n this.transport = transport;\n }\n\n /**\n * Read whether billing is enabled for the app environment tied to this API\n * key, and whether the connected Stripe account is in test or live mode.\n *\n * Render a \"Test mode\" banner in your app whenever `testMode` is true.\n */\n getStatus(): Promise<BillingStatusResponse> {\n return this.transport.request<BillingStatusResponse>({\n service: \"billing\",\n path: \"/v1/billing/status\",\n });\n }\n\n /**\n * List the active billing products configured for the app environment tied to\n * this API key.\n */\n listProducts(): Promise<{ products: BillingProduct[] }> {\n return this.transport.request<{ products: BillingProduct[] }>({\n service: \"billing\",\n path: \"/v1/billing/products\",\n });\n }\n\n /**\n * List the active billing prices configured for the app environment tied to\n * this API key.\n */\n listPrices(): Promise<{ prices: BillingPrice[] }> {\n return this.transport.request<{ prices: BillingPrice[] }>({\n service: \"billing\",\n path: \"/v1/billing/prices\",\n });\n }\n\n /**\n * Create a Stripe Checkout Session on the creator's connected Stripe account.\n *\n * Use `userId` for Buildspace-authenticated users, or `appUserId` when your\n * app uses its own auth system and you want Buildspace to track billing state\n * against an app-defined user id.\n */\n createCheckout(opts: CreateServerCheckoutOptions): Promise<BillingCheckoutResponse> {\n return this.transport.request<BillingCheckoutResponse>({\n service: \"billing\",\n path: \"/v1/billing/checkout\",\n method: \"POST\",\n body: checkoutRequestBody(opts),\n });\n }\n\n /**\n * Create a Stripe-hosted customer portal session for the specified billing\n * identity.\n */\n createPortalSession(opts: CreateServerPortalSessionOptions): Promise<BillingPortalResponse> {\n return this.transport.request<BillingPortalResponse>({\n service: \"billing\",\n path: \"/v1/billing/portal\",\n method: \"POST\",\n body: portalRequestBody(opts),\n });\n }\n\n /**\n * Read the current subscription state for a Buildspace user or app-defined\n * billing identity.\n */\n getSubscription(opts?: {\n appUserId?: string;\n userId?: string;\n }): Promise<BillingSubscriptionResponse> {\n return this.transport.request<BillingSubscriptionResponse>({\n service: \"billing\",\n path: \"/v1/billing/subscription\",\n query: billingIdentityQuery(opts),\n });\n }\n\n /**\n * Read the current paid entitlement state for a Buildspace user or app-defined\n * billing identity.\n */\n getEntitlements(opts?: {\n appUserId?: string;\n userId?: string;\n }): Promise<BillingEntitlementsResponse> {\n return this.transport.request<BillingEntitlementsResponse>({\n service: \"billing\",\n path: \"/v1/billing/entitlements\",\n query: billingIdentityQuery(opts),\n });\n }\n}\n"]}
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
import { f as BillingStatusResponse, e as BillingProduct, d as BillingPrice, C as CreateServerCheckoutOptions, a as BillingCheckoutResponse, h as CreateServerPortalSessionOptions, c as BillingPortalResponse, g as BillingSubscriptionResponse, b as BillingEntitlementsResponse } from '../client-BYUWUiGZ.cjs';
|
|
2
|
+
export { B as BillingClientNamespace, s as shouldShowTestBanner } from '../client-BYUWUiGZ.cjs';
|
|
3
|
+
import { H as HttpTransport } from '../http-D2gXpNpr.cjs';
|
|
4
|
+
|
|
5
|
+
declare class BillingServerNamespace {
|
|
6
|
+
private readonly transport;
|
|
7
|
+
constructor(transport: HttpTransport);
|
|
8
|
+
/**
|
|
9
|
+
* Read whether billing is enabled for the app environment tied to this API
|
|
10
|
+
* key, and whether the connected Stripe account is in test or live mode.
|
|
11
|
+
*
|
|
12
|
+
* Render a "Test mode" banner in your app whenever `testMode` is true.
|
|
13
|
+
*/
|
|
14
|
+
getStatus(): Promise<BillingStatusResponse>;
|
|
15
|
+
/**
|
|
16
|
+
* List the active billing products configured for the app environment tied to
|
|
17
|
+
* this API key.
|
|
18
|
+
*/
|
|
19
|
+
listProducts(): Promise<{
|
|
20
|
+
products: BillingProduct[];
|
|
21
|
+
}>;
|
|
22
|
+
/**
|
|
23
|
+
* List the active billing prices configured for the app environment tied to
|
|
24
|
+
* this API key.
|
|
25
|
+
*/
|
|
26
|
+
listPrices(): Promise<{
|
|
27
|
+
prices: BillingPrice[];
|
|
28
|
+
}>;
|
|
29
|
+
/**
|
|
30
|
+
* Create a Stripe Checkout Session on the creator's connected Stripe account.
|
|
31
|
+
*
|
|
32
|
+
* Use `userId` for Buildspace-authenticated users, or `appUserId` when your
|
|
33
|
+
* app uses its own auth system and you want Buildspace to track billing state
|
|
34
|
+
* against an app-defined user id.
|
|
35
|
+
*/
|
|
36
|
+
createCheckout(opts: CreateServerCheckoutOptions): Promise<BillingCheckoutResponse>;
|
|
37
|
+
/**
|
|
38
|
+
* Create a Stripe-hosted customer portal session for the specified billing
|
|
39
|
+
* identity.
|
|
40
|
+
*/
|
|
41
|
+
createPortalSession(opts: CreateServerPortalSessionOptions): Promise<BillingPortalResponse>;
|
|
42
|
+
/**
|
|
43
|
+
* Read the current subscription state for a Buildspace user or app-defined
|
|
44
|
+
* billing identity.
|
|
45
|
+
*/
|
|
46
|
+
getSubscription(opts?: {
|
|
47
|
+
appUserId?: string;
|
|
48
|
+
userId?: string;
|
|
49
|
+
}): Promise<BillingSubscriptionResponse>;
|
|
50
|
+
/**
|
|
51
|
+
* Read the current paid entitlement state for a Buildspace user or app-defined
|
|
52
|
+
* billing identity.
|
|
53
|
+
*/
|
|
54
|
+
getEntitlements(opts?: {
|
|
55
|
+
appUserId?: string;
|
|
56
|
+
userId?: string;
|
|
57
|
+
}): Promise<BillingEntitlementsResponse>;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
export { BillingCheckoutResponse, BillingEntitlementsResponse, BillingPortalResponse, BillingPrice, BillingProduct, BillingServerNamespace, BillingStatusResponse, BillingSubscriptionResponse, CreateServerCheckoutOptions, CreateServerPortalSessionOptions };
|