scaffolds.ts86 lines · main
1/**
2 * Framework scaffold snippets for briven-engine (Phase 8 pack).
3 * Copy-paste helpers — not runtime imports required by the client.
4 */
5
6import { brivenEngineProxyTarget } from './index.js';
7
8export const BRIVEN_ENGINE_SCAFFOLDS = {
9 engine: 'briven-engine' as const,
10
11 nextAppRouterProxy: `
12// app/api/auth/[...path]/route.ts
13import { brivenEngineNextHandler } from '@briven/auth/engine';
14
15const handler = brivenEngineNextHandler({
16 apiOrigin: process.env.BRIVEN_API_ORIGIN ?? 'https://api.briven.tech',
17 projectId: process.env.BRIVEN_PROJECT_ID, // optional default
18});
19
20export const GET = handler;
21export const POST = handler;
22export const PUT = handler;
23export const DELETE = handler;
24export const PATCH = handler;
25`.trim(),
26
27 nextClientInit: `
28// lib/auth.ts
29import { createBrivenEngineClient } from '@briven/auth/engine';
30
31export const auth = createBrivenEngineClient({
32 projectId: process.env.NEXT_PUBLIC_BRIVEN_PROJECT_ID!,
33 apiBasePath: '/api/auth', // first-party proxy — cookies on your domain
34});
35`.trim(),
36
37 expressProxy: `
38// Express first-party proxy → briven-engine FDI
39import express from 'express';
40
41const TARGET = '${brivenEngineProxyTarget('https://api.briven.tech')}';
42const app = express();
43
44app.use('/api/auth', async (req, res) => {
45 const dest = TARGET + req.url;
46 const headers = { ...req.headers, host: undefined, 'x-briven-engine': 'briven-engine' };
47 const r = await fetch(dest, {
48 method: req.method,
49 headers: headers as HeadersInit,
50 body: ['GET', 'HEAD'].includes(req.method) ? undefined : req,
51 duplex: 'half',
52 } as RequestInit);
53 res.status(r.status);
54 r.headers.forEach((v, k) => res.setHeader(k, v));
55 const buf = Buffer.from(await r.arrayBuffer());
56 res.send(buf);
57});
58`.trim(),
59
60 vanillaSignIn: `
61import { createBrivenEngineClient } from '@briven/auth/engine';
62
63const auth = createBrivenEngineClient({
64 projectId: 'p_YOUR_PROJECT',
65 apiBasePath: '/api/auth',
66});
67
68await auth.signInEmailPassword({
69 email: 'you@example.com',
70 password: '…',
71});
72`.trim(),
73
74 passwordlessSms: `
75// SMS OTP is included in briven-engine
76const code = await auth.createPasswordlessCode({
77 phoneNumber: '+15551234567',
78});
79// show user the SMS, then:
80// await auth.consumePasswordlessCode({ preAuthSessionId, deviceId, userInputCode });
81`.trim(),
82} as const;
83
84export function listBrivenEngineScaffolds(): string[] {
85 return Object.keys(BRIVEN_ENGINE_SCAFFOLDS).filter((k) => k !== 'engine');
86}