@brokr/sdk 1.0.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.js +175 -0
- package/dist/auth.mjs +151 -0
- package/dist/brokr_runtime.py +333 -0
- package/dist/index.d.ts +17 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +568 -0
- package/dist/index.mjs +558 -0
- package/dist/management.js +92 -0
- package/dist/management.mjs +66 -0
- package/dist/react.js +264 -0
- package/dist/react.mjs +225 -0
- package/dist/runtime.js +512 -0
- package/dist/runtime.mjs +501 -0
- package/dist/src/auth.d.ts +70 -0
- package/dist/src/auth.d.ts.map +1 -0
- package/dist/src/management.d.ts +68 -0
- package/dist/src/management.d.ts.map +1 -0
- package/dist/src/react/auth.d.ts +50 -0
- package/dist/src/react/auth.d.ts.map +1 -0
- package/dist/src/runtime.d.ts +217 -0
- package/dist/src/runtime.d.ts.map +1 -0
- package/dist/tsconfig.tsbuildinfo +1 -0
- package/dist/types.d.ts +135 -0
- package/dist/types.d.ts.map +1 -0
- package/package.json +79 -0
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,558 @@
|
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
3
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
4
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
5
|
+
var __require = /* @__PURE__ */ ((x) => typeof require !== "undefined" ? require : typeof Proxy !== "undefined" ? new Proxy(x, {
|
|
6
|
+
get: (a, b) => (typeof require !== "undefined" ? require : a)[b]
|
|
7
|
+
}) : x)(function(x) {
|
|
8
|
+
if (typeof require !== "undefined") return require.apply(this, arguments);
|
|
9
|
+
throw Error('Dynamic require of "' + x + '" is not supported');
|
|
10
|
+
});
|
|
11
|
+
var __esm = (fn, res) => function __init() {
|
|
12
|
+
return fn && (res = (0, fn[__getOwnPropNames(fn)[0]])(fn = 0)), res;
|
|
13
|
+
};
|
|
14
|
+
var __export = (target, all) => {
|
|
15
|
+
for (var name in all)
|
|
16
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
17
|
+
};
|
|
18
|
+
var __copyProps = (to, from, except, desc) => {
|
|
19
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
20
|
+
for (let key of __getOwnPropNames(from))
|
|
21
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
22
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
23
|
+
}
|
|
24
|
+
return to;
|
|
25
|
+
};
|
|
26
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
27
|
+
|
|
28
|
+
// src/auth.ts
|
|
29
|
+
var auth_exports = {};
|
|
30
|
+
__export(auth_exports, {
|
|
31
|
+
BrokrAuthClient: () => BrokrAuthClient,
|
|
32
|
+
authMiddleware: () => authMiddleware
|
|
33
|
+
});
|
|
34
|
+
function parseCookies(cookieHeader) {
|
|
35
|
+
const cookies = /* @__PURE__ */ new Map();
|
|
36
|
+
for (const pair of cookieHeader.split(";")) {
|
|
37
|
+
const eqIdx = pair.indexOf("=");
|
|
38
|
+
if (eqIdx === -1) continue;
|
|
39
|
+
const name = pair.slice(0, eqIdx).trim();
|
|
40
|
+
const value = pair.slice(eqIdx + 1).trim();
|
|
41
|
+
if (name) cookies.set(name, value);
|
|
42
|
+
}
|
|
43
|
+
return cookies;
|
|
44
|
+
}
|
|
45
|
+
function authMiddleware(options) {
|
|
46
|
+
const { protectedRoutes = [], publicOnlyRoutes = [] } = options;
|
|
47
|
+
return async function middleware(request) {
|
|
48
|
+
const url = new URL(request.url);
|
|
49
|
+
const pathname = url.pathname;
|
|
50
|
+
const isProtected = protectedRoutes.some((route) => pathname.startsWith(route));
|
|
51
|
+
const isPublicOnly = publicOnlyRoutes.some((route) => pathname.startsWith(route));
|
|
52
|
+
if (!isProtected && !isPublicOnly) return void 0;
|
|
53
|
+
const cookieHeader = request.headers.get("cookie") ?? "";
|
|
54
|
+
const cookies = parseCookies(cookieHeader);
|
|
55
|
+
const hasSession = cookies.has("better-auth.session_token");
|
|
56
|
+
if (isProtected && !hasSession) {
|
|
57
|
+
return Response.redirect(new URL("/sign-in", request.url).toString(), 302);
|
|
58
|
+
}
|
|
59
|
+
if (isPublicOnly && hasSession) {
|
|
60
|
+
return Response.redirect(new URL("/", request.url).toString(), 302);
|
|
61
|
+
}
|
|
62
|
+
return void 0;
|
|
63
|
+
};
|
|
64
|
+
}
|
|
65
|
+
var BrokrAuthClient;
|
|
66
|
+
var init_auth = __esm({
|
|
67
|
+
"src/auth.ts"() {
|
|
68
|
+
"use strict";
|
|
69
|
+
init_runtime();
|
|
70
|
+
BrokrAuthClient = class {
|
|
71
|
+
constructor(token, gatewayUrl, appUrl) {
|
|
72
|
+
this._token = token;
|
|
73
|
+
this._gatewayUrl = gatewayUrl;
|
|
74
|
+
this._appUrl = appUrl ?? (typeof process !== "undefined" ? process.env.BETTER_AUTH_URL : void 0);
|
|
75
|
+
}
|
|
76
|
+
/**
|
|
77
|
+
* Get current user from an incoming request's cookies.
|
|
78
|
+
* Calls the local Better Auth API (runs inside your app).
|
|
79
|
+
*/
|
|
80
|
+
async currentUser(request) {
|
|
81
|
+
const session = await this.getSession(request);
|
|
82
|
+
return session?.user ?? null;
|
|
83
|
+
}
|
|
84
|
+
/**
|
|
85
|
+
* Get the full session (user + metadata) from an incoming request.
|
|
86
|
+
* Calls the local Better Auth API.
|
|
87
|
+
*/
|
|
88
|
+
async getSession(request) {
|
|
89
|
+
const appUrl = this._appUrl;
|
|
90
|
+
if (!appUrl) {
|
|
91
|
+
throw new BrokrError(
|
|
92
|
+
"[brokr] BETTER_AUTH_URL is not set. Auth may not be provisioned.",
|
|
93
|
+
"AUTH_NOT_CONFIGURED",
|
|
94
|
+
"auth"
|
|
95
|
+
);
|
|
96
|
+
}
|
|
97
|
+
const cookieHeader = request.headers.get("cookie") ?? "";
|
|
98
|
+
if (!cookieHeader) return null;
|
|
99
|
+
const res = await fetch(`${appUrl}/api/auth/get-session`, {
|
|
100
|
+
method: "GET",
|
|
101
|
+
headers: { cookie: cookieHeader }
|
|
102
|
+
});
|
|
103
|
+
if (!res.ok) return null;
|
|
104
|
+
const data = await res.json();
|
|
105
|
+
if (!data?.user || !data?.session) return null;
|
|
106
|
+
return {
|
|
107
|
+
user: {
|
|
108
|
+
id: data.user.id,
|
|
109
|
+
email: data.user.email,
|
|
110
|
+
name: data.user.name ?? null,
|
|
111
|
+
avatarUrl: data.user.image ?? null,
|
|
112
|
+
emailVerified: data.user.emailVerified ? /* @__PURE__ */ new Date() : null
|
|
113
|
+
},
|
|
114
|
+
sessionId: data.session.id,
|
|
115
|
+
expiresAt: new Date(data.session.expiresAt)
|
|
116
|
+
};
|
|
117
|
+
}
|
|
118
|
+
/**
|
|
119
|
+
* Generate an OAuth authorization URL.
|
|
120
|
+
*/
|
|
121
|
+
async getOAuthUrl(provider, options) {
|
|
122
|
+
const appUrl = this._appUrl;
|
|
123
|
+
if (!appUrl) {
|
|
124
|
+
throw new BrokrError("[brokr] BETTER_AUTH_URL is not set.", "AUTH_NOT_CONFIGURED", "auth");
|
|
125
|
+
}
|
|
126
|
+
const redirectTo = options?.redirectTo ?? "/";
|
|
127
|
+
if (!redirectTo.startsWith("/") || redirectTo.startsWith("//")) {
|
|
128
|
+
throw new BrokrError("[brokr] redirectTo must be a relative path (start with /)", "INVALID_REDIRECT", "auth");
|
|
129
|
+
}
|
|
130
|
+
const params = new URLSearchParams({ callbackURL: redirectTo });
|
|
131
|
+
return { redirectUrl: `${appUrl}/api/auth/sign-in/social?provider=${provider}&${params}` };
|
|
132
|
+
}
|
|
133
|
+
/**
|
|
134
|
+
* Send a magic link email (requires email capability).
|
|
135
|
+
*/
|
|
136
|
+
async sendMagicLink(email, options) {
|
|
137
|
+
const appUrl = this._appUrl;
|
|
138
|
+
if (!appUrl) {
|
|
139
|
+
throw new BrokrError("[brokr] BETTER_AUTH_URL is not set.", "AUTH_NOT_CONFIGURED", "auth");
|
|
140
|
+
}
|
|
141
|
+
const res = await fetch(`${appUrl}/api/auth/magic-link/send`, {
|
|
142
|
+
method: "POST",
|
|
143
|
+
headers: { "Content-Type": "application/json" },
|
|
144
|
+
body: JSON.stringify({ email, callbackURL: options?.redirectTo ?? "/" })
|
|
145
|
+
});
|
|
146
|
+
if (!res.ok) {
|
|
147
|
+
const data = await res.json().catch(() => ({}));
|
|
148
|
+
throw new BrokrError(
|
|
149
|
+
data.message ?? `[brokr] Failed to send magic link (HTTP ${res.status})`,
|
|
150
|
+
"AUTH_MAGIC_LINK_FAILED",
|
|
151
|
+
"auth"
|
|
152
|
+
);
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
};
|
|
156
|
+
}
|
|
157
|
+
});
|
|
158
|
+
|
|
159
|
+
// src/runtime.ts
|
|
160
|
+
function resolveToken() {
|
|
161
|
+
return typeof process !== "undefined" ? process.env.BROKR_TOKEN : void 0;
|
|
162
|
+
}
|
|
163
|
+
function assertToken(token, capability) {
|
|
164
|
+
if (!token) {
|
|
165
|
+
let hint = "brokr env pull --stack <name>";
|
|
166
|
+
try {
|
|
167
|
+
if (typeof process !== "undefined") {
|
|
168
|
+
const fs = __require("fs");
|
|
169
|
+
const path = __require("path");
|
|
170
|
+
const brokrFile = path.join(process.cwd(), ".brokr");
|
|
171
|
+
if (fs.existsSync(brokrFile)) {
|
|
172
|
+
const data = JSON.parse(fs.readFileSync(brokrFile, "utf8"));
|
|
173
|
+
if (data?.stackName) hint = `brokr env pull --stack ${data.stackName}`;
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
} catch {
|
|
177
|
+
}
|
|
178
|
+
throw new BrokrAuthError(
|
|
179
|
+
`[brokr] BROKR_TOKEN is not set.
|
|
180
|
+
Run: ${hint}`,
|
|
181
|
+
"BROKR_TOKEN_MISSING"
|
|
182
|
+
);
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
async function gatewayFetch(gatewayUrl, token, path, body, capability) {
|
|
186
|
+
let res;
|
|
187
|
+
try {
|
|
188
|
+
res = await fetch(`${gatewayUrl}${path}`, {
|
|
189
|
+
method: "POST",
|
|
190
|
+
headers: { "Content-Type": "application/json", Authorization: `Bearer ${token}` },
|
|
191
|
+
body: JSON.stringify(body)
|
|
192
|
+
});
|
|
193
|
+
} catch (err) {
|
|
194
|
+
throw new BrokrNetworkError(
|
|
195
|
+
`[brokr] Could not reach Brokr gateway. Check your network.
|
|
196
|
+
${err instanceof Error ? err.message : String(err)}`,
|
|
197
|
+
capability
|
|
198
|
+
);
|
|
199
|
+
}
|
|
200
|
+
if (res.status === 429) {
|
|
201
|
+
const retryAfter = parseInt(res.headers.get("Retry-After") ?? "60", 10);
|
|
202
|
+
const data = await res.json().catch(() => ({}));
|
|
203
|
+
throw new BrokrRateLimitError(
|
|
204
|
+
data.error ?? `[brokr] Rate limited (retry after ${retryAfter}s)`,
|
|
205
|
+
retryAfter,
|
|
206
|
+
capability
|
|
207
|
+
);
|
|
208
|
+
}
|
|
209
|
+
if (res.status === 401) {
|
|
210
|
+
throw new BrokrAuthError("[brokr] Invalid or expired BROKR_TOKEN.", "BROKR_TOKEN_INVALID");
|
|
211
|
+
}
|
|
212
|
+
if (!res.ok) {
|
|
213
|
+
const data = await res.json().catch(() => ({}));
|
|
214
|
+
throw new BrokrError(
|
|
215
|
+
data.error ?? `[brokr] ${capability} request failed (HTTP ${res.status})`,
|
|
216
|
+
data.code ?? `${capability.toUpperCase()}_FAILED`,
|
|
217
|
+
capability
|
|
218
|
+
);
|
|
219
|
+
}
|
|
220
|
+
return res.json();
|
|
221
|
+
}
|
|
222
|
+
function createBrokr(options) {
|
|
223
|
+
return new BrokrRuntime(options);
|
|
224
|
+
}
|
|
225
|
+
var GATEWAY_URL, BrokrError, BrokrAuthError, BrokrRateLimitError, BrokrNetworkError, BrokrAIClient, BrokrStorageClient, BrokrEmailClient, BrokrRuntime;
|
|
226
|
+
var init_runtime = __esm({
|
|
227
|
+
"src/runtime.ts"() {
|
|
228
|
+
"use strict";
|
|
229
|
+
GATEWAY_URL = "https://api.brokr.sh";
|
|
230
|
+
BrokrError = class extends Error {
|
|
231
|
+
constructor(message, code, capability, retryable = false) {
|
|
232
|
+
super(message);
|
|
233
|
+
this.code = code;
|
|
234
|
+
this.capability = capability;
|
|
235
|
+
this.retryable = retryable;
|
|
236
|
+
this.name = "BrokrError";
|
|
237
|
+
}
|
|
238
|
+
};
|
|
239
|
+
BrokrAuthError = class extends BrokrError {
|
|
240
|
+
constructor(message, code) {
|
|
241
|
+
super(message, code, "auth", false);
|
|
242
|
+
this.name = "BrokrAuthError";
|
|
243
|
+
}
|
|
244
|
+
};
|
|
245
|
+
BrokrRateLimitError = class extends BrokrError {
|
|
246
|
+
constructor(message, retryAfter, capability) {
|
|
247
|
+
super(message, "RATE_LIMITED", capability, true);
|
|
248
|
+
this.retryAfter = retryAfter;
|
|
249
|
+
this.name = "BrokrRateLimitError";
|
|
250
|
+
}
|
|
251
|
+
};
|
|
252
|
+
BrokrNetworkError = class extends BrokrError {
|
|
253
|
+
constructor(message, capability) {
|
|
254
|
+
super(message, "NETWORK_ERROR", capability, true);
|
|
255
|
+
this.name = "BrokrNetworkError";
|
|
256
|
+
}
|
|
257
|
+
};
|
|
258
|
+
BrokrAIClient = class {
|
|
259
|
+
constructor(_token, _gatewayUrl) {
|
|
260
|
+
this._token = _token;
|
|
261
|
+
this._gatewayUrl = _gatewayUrl;
|
|
262
|
+
}
|
|
263
|
+
/**
|
|
264
|
+
* Send a chat completion request.
|
|
265
|
+
*
|
|
266
|
+
* @example
|
|
267
|
+
* ```typescript
|
|
268
|
+
* const reply = await brokr.ai.chat([
|
|
269
|
+
* { role: 'user', content: 'Explain quantum computing in one sentence.' }
|
|
270
|
+
* ]);
|
|
271
|
+
* console.log(reply.content);
|
|
272
|
+
* ```
|
|
273
|
+
*/
|
|
274
|
+
async chat(messages, options) {
|
|
275
|
+
assertToken(this._token, "ai");
|
|
276
|
+
const data = await gatewayFetch(this._gatewayUrl, this._token, "/v1/chat/completions", {
|
|
277
|
+
messages,
|
|
278
|
+
model: options?.model,
|
|
279
|
+
max_tokens: options?.maxTokens,
|
|
280
|
+
temperature: options?.temperature
|
|
281
|
+
}, "ai");
|
|
282
|
+
return {
|
|
283
|
+
content: data.choices?.[0]?.message?.content ?? "",
|
|
284
|
+
model: data.model ?? "",
|
|
285
|
+
usage: {
|
|
286
|
+
promptTokens: data.usage?.prompt_tokens ?? 0,
|
|
287
|
+
completionTokens: data.usage?.completion_tokens ?? 0
|
|
288
|
+
}
|
|
289
|
+
};
|
|
290
|
+
}
|
|
291
|
+
/**
|
|
292
|
+
* Stream a chat completion. Yields text strings directly.
|
|
293
|
+
*
|
|
294
|
+
* @example
|
|
295
|
+
* ```typescript
|
|
296
|
+
* for await (const text of brokr.ai.stream(messages)) {
|
|
297
|
+
* process.stdout.write(text);
|
|
298
|
+
* }
|
|
299
|
+
* ```
|
|
300
|
+
*/
|
|
301
|
+
async *stream(messages, options) {
|
|
302
|
+
assertToken(this._token, "ai");
|
|
303
|
+
let res;
|
|
304
|
+
try {
|
|
305
|
+
res = await fetch(`${this._gatewayUrl}/v1/chat/completions`, {
|
|
306
|
+
method: "POST",
|
|
307
|
+
headers: { "Content-Type": "application/json", Authorization: `Bearer ${this._token}` },
|
|
308
|
+
body: JSON.stringify({ messages, stream: true, model: options?.model, max_tokens: options?.maxTokens })
|
|
309
|
+
});
|
|
310
|
+
} catch (err) {
|
|
311
|
+
throw new BrokrNetworkError(
|
|
312
|
+
`[brokr] Could not reach Brokr gateway.
|
|
313
|
+
${err instanceof Error ? err.message : String(err)}`,
|
|
314
|
+
"ai"
|
|
315
|
+
);
|
|
316
|
+
}
|
|
317
|
+
if (res.status === 429) {
|
|
318
|
+
const retryAfter = parseInt(res.headers.get("Retry-After") ?? "60", 10);
|
|
319
|
+
throw new BrokrRateLimitError("[brokr] AI rate limited.", retryAfter, "ai");
|
|
320
|
+
}
|
|
321
|
+
if (res.status === 401) {
|
|
322
|
+
throw new BrokrAuthError("[brokr] Invalid or expired BROKR_TOKEN.", "BROKR_TOKEN_INVALID");
|
|
323
|
+
}
|
|
324
|
+
if (!res.ok || !res.body) {
|
|
325
|
+
throw new BrokrError(`[brokr] AI stream failed (HTTP ${res.status})`, "AI_STREAM_FAILED", "ai");
|
|
326
|
+
}
|
|
327
|
+
const reader = res.body.getReader();
|
|
328
|
+
const decoder = new TextDecoder();
|
|
329
|
+
let buffer = "";
|
|
330
|
+
while (true) {
|
|
331
|
+
const { done, value } = await reader.read();
|
|
332
|
+
if (done) break;
|
|
333
|
+
buffer += decoder.decode(value, { stream: true });
|
|
334
|
+
const lines = buffer.split("\n");
|
|
335
|
+
buffer = lines.pop() ?? "";
|
|
336
|
+
for (const line of lines) {
|
|
337
|
+
if (!line.startsWith("data: ")) continue;
|
|
338
|
+
const payload = line.slice(6).trim();
|
|
339
|
+
if (payload === "[DONE]") return;
|
|
340
|
+
try {
|
|
341
|
+
const parsed = JSON.parse(payload);
|
|
342
|
+
const delta = parsed.choices?.[0]?.delta?.content ?? "";
|
|
343
|
+
if (delta) yield delta;
|
|
344
|
+
} catch {
|
|
345
|
+
}
|
|
346
|
+
}
|
|
347
|
+
}
|
|
348
|
+
}
|
|
349
|
+
/**
|
|
350
|
+
* OpenAI-SDK compatible base URL.
|
|
351
|
+
*
|
|
352
|
+
* @example
|
|
353
|
+
* ```typescript
|
|
354
|
+
* const openai = new OpenAI({ baseURL: brokr.ai.baseURL, apiKey: brokr.ai.apiKey });
|
|
355
|
+
* ```
|
|
356
|
+
*/
|
|
357
|
+
get baseURL() {
|
|
358
|
+
return `${this._gatewayUrl}/v1`;
|
|
359
|
+
}
|
|
360
|
+
/** Use as `apiKey` with the official OpenAI SDK to route through Brokr's gateway. */
|
|
361
|
+
get apiKey() {
|
|
362
|
+
assertToken(this._token, "ai");
|
|
363
|
+
return this._token;
|
|
364
|
+
}
|
|
365
|
+
};
|
|
366
|
+
BrokrStorageClient = class {
|
|
367
|
+
constructor(_token, _gatewayUrl) {
|
|
368
|
+
this._token = _token;
|
|
369
|
+
this._gatewayUrl = _gatewayUrl;
|
|
370
|
+
}
|
|
371
|
+
/**
|
|
372
|
+
* Get a presigned upload URL for browser-direct or streaming uploads.
|
|
373
|
+
*
|
|
374
|
+
* @example
|
|
375
|
+
* ```typescript
|
|
376
|
+
* const { url, key } = await brokr.storage.getUploadUrl('avatar.png', 'image/png');
|
|
377
|
+
* await fetch(url, { method: 'PUT', body: file });
|
|
378
|
+
* ```
|
|
379
|
+
*/
|
|
380
|
+
async getUploadUrl(filename, contentType = "application/octet-stream") {
|
|
381
|
+
assertToken(this._token, "storage");
|
|
382
|
+
return gatewayFetch(
|
|
383
|
+
this._gatewayUrl,
|
|
384
|
+
this._token,
|
|
385
|
+
"/v1/storage/sign-upload",
|
|
386
|
+
{ filename, contentType },
|
|
387
|
+
"storage"
|
|
388
|
+
);
|
|
389
|
+
}
|
|
390
|
+
/**
|
|
391
|
+
* Upload data to R2. Returns the stable object key.
|
|
392
|
+
*
|
|
393
|
+
* @example
|
|
394
|
+
* ```typescript
|
|
395
|
+
* const { key } = await brokr.storage.upload(fileBuffer, 'photo.jpg', 'image/jpeg');
|
|
396
|
+
* ```
|
|
397
|
+
*/
|
|
398
|
+
async upload(data, filename, contentType = "application/octet-stream") {
|
|
399
|
+
const { url, key } = await this.getUploadUrl(filename, contentType);
|
|
400
|
+
const putRes = await fetch(url, {
|
|
401
|
+
method: "PUT",
|
|
402
|
+
headers: { "Content-Type": contentType },
|
|
403
|
+
body: data
|
|
404
|
+
});
|
|
405
|
+
if (!putRes.ok) {
|
|
406
|
+
throw new BrokrError(`[brokr] Upload failed (HTTP ${putRes.status})`, "STORAGE_UPLOAD_FAILED", "storage");
|
|
407
|
+
}
|
|
408
|
+
return { key };
|
|
409
|
+
}
|
|
410
|
+
/**
|
|
411
|
+
* Get a presigned download URL for a stored object.
|
|
412
|
+
*
|
|
413
|
+
* @example
|
|
414
|
+
* ```typescript
|
|
415
|
+
* const { url } = await brokr.storage.url('photos/avatar.jpg');
|
|
416
|
+
* // use url in <img src={url} /> or redirect
|
|
417
|
+
* ```
|
|
418
|
+
*/
|
|
419
|
+
async url(key, options) {
|
|
420
|
+
assertToken(this._token, "storage");
|
|
421
|
+
return gatewayFetch(
|
|
422
|
+
this._gatewayUrl,
|
|
423
|
+
this._token,
|
|
424
|
+
"/v1/storage/sign-download",
|
|
425
|
+
{ key, expiresIn: options?.expiresIn },
|
|
426
|
+
"storage"
|
|
427
|
+
);
|
|
428
|
+
}
|
|
429
|
+
/** @deprecated Use `url()` instead. */
|
|
430
|
+
async getUrl(key, options) {
|
|
431
|
+
return this.url(key, options);
|
|
432
|
+
}
|
|
433
|
+
};
|
|
434
|
+
BrokrEmailClient = class {
|
|
435
|
+
constructor(_token, _gatewayUrl) {
|
|
436
|
+
this._token = _token;
|
|
437
|
+
this._gatewayUrl = _gatewayUrl;
|
|
438
|
+
}
|
|
439
|
+
/**
|
|
440
|
+
* Send an email. The from address and API credentials are resolved server-side.
|
|
441
|
+
*
|
|
442
|
+
* @example
|
|
443
|
+
* ```typescript
|
|
444
|
+
* await brokr.email.send({
|
|
445
|
+
* to: 'user@example.com',
|
|
446
|
+
* subject: 'Welcome!',
|
|
447
|
+
* html: '<h1>Welcome to the app</h1>',
|
|
448
|
+
* });
|
|
449
|
+
* ```
|
|
450
|
+
*/
|
|
451
|
+
async send(params) {
|
|
452
|
+
assertToken(this._token, "email");
|
|
453
|
+
return gatewayFetch(
|
|
454
|
+
this._gatewayUrl,
|
|
455
|
+
this._token,
|
|
456
|
+
"/v1/email/send",
|
|
457
|
+
params,
|
|
458
|
+
"email"
|
|
459
|
+
);
|
|
460
|
+
}
|
|
461
|
+
};
|
|
462
|
+
BrokrRuntime = class {
|
|
463
|
+
constructor(options) {
|
|
464
|
+
this._token = options?.token ?? resolveToken();
|
|
465
|
+
this._gatewayUrl = options?.gatewayUrl ?? GATEWAY_URL;
|
|
466
|
+
this.ai = new BrokrAIClient(this._token, this._gatewayUrl);
|
|
467
|
+
this.storage = new BrokrStorageClient(this._token, this._gatewayUrl);
|
|
468
|
+
this.email = new BrokrEmailClient(this._token, this._gatewayUrl);
|
|
469
|
+
}
|
|
470
|
+
/** Auth client — lazily initialized to avoid pulling in auth deps when not needed. */
|
|
471
|
+
get auth() {
|
|
472
|
+
if (!this._auth) {
|
|
473
|
+
const { BrokrAuthClient: BrokrAuthClient2 } = (init_auth(), __toCommonJS(auth_exports));
|
|
474
|
+
this._auth = new BrokrAuthClient2(this._token, this._gatewayUrl);
|
|
475
|
+
}
|
|
476
|
+
return this._auth;
|
|
477
|
+
}
|
|
478
|
+
};
|
|
479
|
+
}
|
|
480
|
+
});
|
|
481
|
+
|
|
482
|
+
// index.ts
|
|
483
|
+
init_runtime();
|
|
484
|
+
init_auth();
|
|
485
|
+
|
|
486
|
+
// src/management.ts
|
|
487
|
+
async function trpcRequest(config, procedure, input) {
|
|
488
|
+
const url = new URL(`${config.apiUrl}/api/trpc/${procedure}`);
|
|
489
|
+
const response = await fetch(url.toString(), {
|
|
490
|
+
method: "POST",
|
|
491
|
+
headers: {
|
|
492
|
+
"Content-Type": "application/json",
|
|
493
|
+
"Authorization": `Bearer ${config.accessToken}`,
|
|
494
|
+
...config.headers
|
|
495
|
+
},
|
|
496
|
+
body: JSON.stringify(input),
|
|
497
|
+
signal: config.timeout ? AbortSignal.timeout(config.timeout) : void 0
|
|
498
|
+
});
|
|
499
|
+
if (!response.ok) {
|
|
500
|
+
const error = await response.json().catch(() => ({ message: "Request failed" }));
|
|
501
|
+
throw new Error(error.message || `HTTP ${response.status}`);
|
|
502
|
+
}
|
|
503
|
+
const result = await response.json();
|
|
504
|
+
return result.data;
|
|
505
|
+
}
|
|
506
|
+
var BrokrClient = class {
|
|
507
|
+
constructor(config) {
|
|
508
|
+
this.config = config;
|
|
509
|
+
}
|
|
510
|
+
/** Create a new stack with default providers. */
|
|
511
|
+
async create(input) {
|
|
512
|
+
return trpcRequest(this.config, "v1.stack.create", input);
|
|
513
|
+
}
|
|
514
|
+
/** List all stacks for the current user. */
|
|
515
|
+
async listStacks() {
|
|
516
|
+
return trpcRequest(this.config, "v1.stack.list", {});
|
|
517
|
+
}
|
|
518
|
+
/** Get a specific stack by ID. */
|
|
519
|
+
async getStack(id) {
|
|
520
|
+
return trpcRequest(this.config, "v1.stack.get", { id });
|
|
521
|
+
}
|
|
522
|
+
/** Update a stack. */
|
|
523
|
+
async updateStack(id, input) {
|
|
524
|
+
return trpcRequest(this.config, "v1.stack.update", { id, ...input });
|
|
525
|
+
}
|
|
526
|
+
/** Delete a stack. */
|
|
527
|
+
async deleteStack(id) {
|
|
528
|
+
return trpcRequest(this.config, "v1.stack.delete", { id });
|
|
529
|
+
}
|
|
530
|
+
/** Add a provider to a stack. */
|
|
531
|
+
async add(provider, input) {
|
|
532
|
+
return trpcRequest(this.config, "v1.provider.add", { provider, ...input });
|
|
533
|
+
}
|
|
534
|
+
/** Refresh the access token. */
|
|
535
|
+
async refreshToken(refreshToken) {
|
|
536
|
+
return trpcRequest(this.config, "v1.auth.refresh", { refreshToken });
|
|
537
|
+
}
|
|
538
|
+
/** Update the access token. */
|
|
539
|
+
setAccessToken(accessToken) {
|
|
540
|
+
this.config.accessToken = accessToken;
|
|
541
|
+
}
|
|
542
|
+
};
|
|
543
|
+
function createBrokrClient(config) {
|
|
544
|
+
return new BrokrClient(config);
|
|
545
|
+
}
|
|
546
|
+
var create = createBrokrClient;
|
|
547
|
+
export {
|
|
548
|
+
BrokrAIClient,
|
|
549
|
+
BrokrClient,
|
|
550
|
+
BrokrEmailClient,
|
|
551
|
+
BrokrError,
|
|
552
|
+
BrokrRuntime,
|
|
553
|
+
BrokrStorageClient,
|
|
554
|
+
authMiddleware,
|
|
555
|
+
create,
|
|
556
|
+
createBrokr,
|
|
557
|
+
createBrokrClient
|
|
558
|
+
};
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// src/management.ts
|
|
21
|
+
var management_exports = {};
|
|
22
|
+
__export(management_exports, {
|
|
23
|
+
BrokrClient: () => BrokrClient,
|
|
24
|
+
create: () => create,
|
|
25
|
+
default: () => createBrokrClient
|
|
26
|
+
});
|
|
27
|
+
module.exports = __toCommonJS(management_exports);
|
|
28
|
+
async function trpcRequest(config, procedure, input) {
|
|
29
|
+
const url = new URL(`${config.apiUrl}/api/trpc/${procedure}`);
|
|
30
|
+
const response = await fetch(url.toString(), {
|
|
31
|
+
method: "POST",
|
|
32
|
+
headers: {
|
|
33
|
+
"Content-Type": "application/json",
|
|
34
|
+
"Authorization": `Bearer ${config.accessToken}`,
|
|
35
|
+
...config.headers
|
|
36
|
+
},
|
|
37
|
+
body: JSON.stringify(input),
|
|
38
|
+
signal: config.timeout ? AbortSignal.timeout(config.timeout) : void 0
|
|
39
|
+
});
|
|
40
|
+
if (!response.ok) {
|
|
41
|
+
const error = await response.json().catch(() => ({ message: "Request failed" }));
|
|
42
|
+
throw new Error(error.message || `HTTP ${response.status}`);
|
|
43
|
+
}
|
|
44
|
+
const result = await response.json();
|
|
45
|
+
return result.data;
|
|
46
|
+
}
|
|
47
|
+
var BrokrClient = class {
|
|
48
|
+
constructor(config) {
|
|
49
|
+
this.config = config;
|
|
50
|
+
}
|
|
51
|
+
/** Create a new stack with default providers. */
|
|
52
|
+
async create(input) {
|
|
53
|
+
return trpcRequest(this.config, "v1.stack.create", input);
|
|
54
|
+
}
|
|
55
|
+
/** List all stacks for the current user. */
|
|
56
|
+
async listStacks() {
|
|
57
|
+
return trpcRequest(this.config, "v1.stack.list", {});
|
|
58
|
+
}
|
|
59
|
+
/** Get a specific stack by ID. */
|
|
60
|
+
async getStack(id) {
|
|
61
|
+
return trpcRequest(this.config, "v1.stack.get", { id });
|
|
62
|
+
}
|
|
63
|
+
/** Update a stack. */
|
|
64
|
+
async updateStack(id, input) {
|
|
65
|
+
return trpcRequest(this.config, "v1.stack.update", { id, ...input });
|
|
66
|
+
}
|
|
67
|
+
/** Delete a stack. */
|
|
68
|
+
async deleteStack(id) {
|
|
69
|
+
return trpcRequest(this.config, "v1.stack.delete", { id });
|
|
70
|
+
}
|
|
71
|
+
/** Add a provider to a stack. */
|
|
72
|
+
async add(provider, input) {
|
|
73
|
+
return trpcRequest(this.config, "v1.provider.add", { provider, ...input });
|
|
74
|
+
}
|
|
75
|
+
/** Refresh the access token. */
|
|
76
|
+
async refreshToken(refreshToken) {
|
|
77
|
+
return trpcRequest(this.config, "v1.auth.refresh", { refreshToken });
|
|
78
|
+
}
|
|
79
|
+
/** Update the access token. */
|
|
80
|
+
setAccessToken(accessToken) {
|
|
81
|
+
this.config.accessToken = accessToken;
|
|
82
|
+
}
|
|
83
|
+
};
|
|
84
|
+
function createBrokrClient(config) {
|
|
85
|
+
return new BrokrClient(config);
|
|
86
|
+
}
|
|
87
|
+
var create = createBrokrClient;
|
|
88
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
89
|
+
0 && (module.exports = {
|
|
90
|
+
BrokrClient,
|
|
91
|
+
create
|
|
92
|
+
});
|