create-terreno-app 57.21.0 → 58.0.0-beta.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.
- package/CHANGELOG.md +89 -0
- package/dist/generate.d.ts +7 -0
- package/dist/generate.d.ts.map +1 -1
- package/dist/generate.js +245 -548
- package/dist/generate.js.map +1 -1
- package/package.json +2 -2
package/dist/generate.js
CHANGED
|
@@ -7,6 +7,15 @@ export const MONGODB_VERSION = "7.2.0";
|
|
|
7
7
|
export const BSON_VERSION = "7.2.0";
|
|
8
8
|
export const PACKAGE_VERSION = packageJson.version;
|
|
9
9
|
const TERRENO_DEP_VERSION = `^${PACKAGE_VERSION}`;
|
|
10
|
+
/** Better Auth client + Expo plugin, pinned together (the monorepo overrides both to 1.6.25). */
|
|
11
|
+
export const BETTER_AUTH_VERSION = "^1.6.25";
|
|
12
|
+
/**
|
|
13
|
+
* Regenerates `store/apiSdk.ts` (typed hooks for every non-synced route) from the running
|
|
14
|
+
* backend's OpenAPI spec with the `@terreno/syncdb` codegen.
|
|
15
|
+
*/
|
|
16
|
+
export const API_SDK_SCRIPT =
|
|
17
|
+
// biome-ignore lint/suspicious/noTemplateCurlyInString: shell default expansion in a package.json script
|
|
18
|
+
"terreno-syncdb-codegen --schema ${OPENAPI_URL:-http://localhost:4000/openapi.json} --api-out ./store/apiSdk.ts";
|
|
10
19
|
const generateCursorRules = (args) => {
|
|
11
20
|
const { appDisplayName } = args;
|
|
12
21
|
return `# ${appDisplayName}
|
|
@@ -15,7 +24,7 @@ A full-stack application built with the Terreno framework.
|
|
|
15
24
|
|
|
16
25
|
## Project Structure
|
|
17
26
|
|
|
18
|
-
- **frontend/** - Expo/React Native frontend using @terreno/ui
|
|
27
|
+
- **frontend/** - Expo/React Native frontend using @terreno/ui and @terreno/syncdb (data, request client, generated API hooks, Better Auth session)
|
|
19
28
|
- **backend/** - Express/Mongoose backend using @terreno/api
|
|
20
29
|
|
|
21
30
|
## Development
|
|
@@ -28,7 +37,7 @@ cd backend && bun run dev # Start backend on port 4000
|
|
|
28
37
|
|
|
29
38
|
# Frontend
|
|
30
39
|
cd frontend && bun run web # Start web frontend
|
|
31
|
-
cd frontend && bun run sdk # Regenerate
|
|
40
|
+
cd frontend && bun run sdk # Regenerate store/apiSdk.ts after backend changes
|
|
32
41
|
\`\`\`
|
|
33
42
|
|
|
34
43
|
## Code Style
|
|
@@ -53,7 +62,7 @@ cd frontend && bun run sdk # Regenerate SDK after backend changes
|
|
|
53
62
|
- Use functional components with \`React.FC\` type
|
|
54
63
|
- Add explanatory comment above each \`useEffect\`
|
|
55
64
|
- Wrap callbacks in \`useCallback\`
|
|
56
|
-
- Use
|
|
65
|
+
- Use React context, local state, or \`useStoredState\` for app state (no Redux)
|
|
57
66
|
|
|
58
67
|
### Backend Conventions
|
|
59
68
|
- Use \`modelRouter\` for CRUD endpoints
|
|
@@ -63,9 +72,10 @@ cd frontend && bun run sdk # Regenerate SDK after backend changes
|
|
|
63
72
|
|
|
64
73
|
### Frontend Conventions
|
|
65
74
|
- Use \`@terreno/syncdb/react\` hooks (\`useQuery\`, \`useMutate\`) for synced collection CRUD
|
|
66
|
-
- Use generated
|
|
75
|
+
- Use generated hooks from \`@/store/apiSdk\` (\`useGetAuthMeQuery\`, \`usePatchAuthMeMutation\`, ...) for non-synced routes only
|
|
76
|
+
- Read the session with \`useSession()\` from \`@terreno/syncdb/auth\` (\`userId\`, \`isLoading\`, \`logout\`, \`refresh\`)
|
|
67
77
|
- Use @terreno/ui components (Box, Page, Button, TextField, etc.)
|
|
68
|
-
- Never modify \`
|
|
78
|
+
- Never modify \`apiSdk.ts\` manually - regenerate with \`bun run sdk\`
|
|
69
79
|
|
|
70
80
|
## AI Assistance
|
|
71
81
|
|
|
@@ -136,7 +146,7 @@ Full-stack Terreno app generated by [\`create-terreno-app\`](https://www.npmjs.c
|
|
|
136
146
|
## Project structure
|
|
137
147
|
|
|
138
148
|
- **backend/** — Express/Mongoose API (\`@terreno/api\`, Better Auth, admin, sync)
|
|
139
|
-
- **frontend/** — Expo universal app (\`@terreno/ui\`, \`@terreno/syncdb
|
|
149
|
+
- **frontend/** — Expo universal app (\`@terreno/ui\`, \`@terreno/syncdb\`)
|
|
140
150
|
|
|
141
151
|
## Local development
|
|
142
152
|
|
|
@@ -169,7 +179,7 @@ cd backend && bun run dev
|
|
|
169
179
|
cd frontend && bun run web
|
|
170
180
|
\`\`\`
|
|
171
181
|
|
|
172
|
-
After backend route changes, regenerate the
|
|
182
|
+
After backend route changes, regenerate the typed API hooks in \`frontend/store/apiSdk.ts\` (the backend must be running):
|
|
173
183
|
|
|
174
184
|
\`\`\`bash
|
|
175
185
|
cd frontend && bun run sdk
|
|
@@ -287,7 +297,7 @@ cd backend && bun run dev
|
|
|
287
297
|
# Start frontend (port 8082)
|
|
288
298
|
cd frontend && bun run web
|
|
289
299
|
|
|
290
|
-
# Regenerate
|
|
300
|
+
# Regenerate frontend/store/apiSdk.ts after backend changes (backend running)
|
|
291
301
|
cd frontend && bun run sdk
|
|
292
302
|
\`\`\`
|
|
293
303
|
|
|
@@ -296,7 +306,7 @@ cd frontend && bun run sdk
|
|
|
296
306
|
1. Create model in \`backend/src/models/\`
|
|
297
307
|
2. Create route in \`backend/src/api/\`
|
|
298
308
|
3. Register route in \`backend/src/server.ts\`
|
|
299
|
-
4. Regenerate
|
|
309
|
+
4. Regenerate API hooks: \`cd frontend && bun run sdk\` (writes \`store/apiSdk.ts\`)
|
|
300
310
|
5. Create screens in \`frontend/app/\`
|
|
301
311
|
|
|
302
312
|
## Code Style
|
|
@@ -514,6 +524,7 @@ export const start = async (skipListen = false): Promise<express.Application> =>
|
|
|
514
524
|
organizations: {
|
|
515
525
|
settingsSchema: organizationSettingsSchema,
|
|
516
526
|
},
|
|
527
|
+
rateLimit: isDeployed ? {trustProxy: 1} : false,
|
|
517
528
|
skipListen,
|
|
518
529
|
// noExplicitAny: User model type mismatch
|
|
519
530
|
// biome-ignore lint/suspicious/noExplicitAny: User model type mismatch
|
|
@@ -1211,37 +1222,36 @@ const generateFrontendPackageJson = (args) => {
|
|
|
1211
1222
|
const { appName } = args;
|
|
1212
1223
|
return JSON.stringify({
|
|
1213
1224
|
dependencies: {
|
|
1225
|
+
"@better-auth/expo": BETTER_AUTH_VERSION,
|
|
1226
|
+
"@better-auth/passkey": BETTER_AUTH_VERSION,
|
|
1214
1227
|
"@expo/vector-icons": "^15.1.1",
|
|
1228
|
+
"@modelcontextprotocol/client": "^2.0.0",
|
|
1215
1229
|
"@react-native-async-storage/async-storage": "2.2.0",
|
|
1216
|
-
"@reduxjs/toolkit": "^2.11.1",
|
|
1217
1230
|
"@sentry/react": "^10.29.0",
|
|
1218
1231
|
"@terreno/admin-frontend": TERRENO_DEP_VERSION,
|
|
1219
|
-
"@terreno/rtk": TERRENO_DEP_VERSION,
|
|
1220
1232
|
"@terreno/syncdb": TERRENO_DEP_VERSION,
|
|
1221
1233
|
"@terreno/ui": TERRENO_DEP_VERSION,
|
|
1222
|
-
|
|
1223
|
-
|
|
1224
|
-
"expo-
|
|
1225
|
-
"expo-
|
|
1234
|
+
"better-auth": BETTER_AUTH_VERSION,
|
|
1235
|
+
expo: "~58.0.14",
|
|
1236
|
+
"expo-constants": "~58.0.12",
|
|
1237
|
+
"expo-network": "~58.0.3",
|
|
1238
|
+
"expo-router": "~58.0.23",
|
|
1239
|
+
"expo-secure-store": "~58.0.2",
|
|
1240
|
+
"expo-sqlite": "~58.0.13",
|
|
1226
1241
|
lodash: "^4.18.1",
|
|
1227
1242
|
luxon: "^3.7.2",
|
|
1228
|
-
react: "19.
|
|
1229
|
-
"react-dom": "19.
|
|
1230
|
-
"react-native": "0.
|
|
1231
|
-
"react-native-reanimated": "4.
|
|
1232
|
-
"react-redux": "^9.2.0",
|
|
1233
|
-
"redux-persist": "^6.0.0",
|
|
1243
|
+
react: "19.3.0",
|
|
1244
|
+
"react-dom": "19.3.0",
|
|
1245
|
+
"react-native": "0.88.0",
|
|
1246
|
+
"react-native-reanimated": "4.7.1",
|
|
1234
1247
|
},
|
|
1235
1248
|
devDependencies: {
|
|
1236
1249
|
"@biomejs/biome": "^2.3.6",
|
|
1237
1250
|
"@playwright/test": "^1.58.2",
|
|
1238
|
-
"@rtk-query/codegen-openapi": "^2.2.0",
|
|
1239
1251
|
"@types/bun": "^1.4.0",
|
|
1240
1252
|
"@types/lodash": "^4.17.17",
|
|
1241
1253
|
"@types/luxon": "^3.7.1",
|
|
1242
|
-
"@types/react": "~19.
|
|
1243
|
-
"ts-node": "^10.9.2",
|
|
1244
|
-
tsx: "^4.23.12",
|
|
1254
|
+
"@types/react": "~19.3.0",
|
|
1245
1255
|
typescript: "~6.0.3",
|
|
1246
1256
|
},
|
|
1247
1257
|
main: "expo-router/entry",
|
|
@@ -1249,12 +1259,13 @@ const generateFrontendPackageJson = (args) => {
|
|
|
1249
1259
|
private: true,
|
|
1250
1260
|
scripts: {
|
|
1251
1261
|
android: "EXPO_UNSTABLE_MCP_SERVER=1 bun expo start --android --port 8082",
|
|
1262
|
+
"api-sdk": API_SDK_SCRIPT,
|
|
1252
1263
|
compile: "tsc",
|
|
1253
1264
|
ios: "EXPO_UNSTABLE_MCP_SERVER=1 bun expo start --ios --port 8082",
|
|
1254
1265
|
lint: "bun biome check .",
|
|
1255
1266
|
"lint:fix": "bun biome check --write .",
|
|
1256
1267
|
"lint:unsafefix": "biome check --write . --unsafe",
|
|
1257
|
-
sdk: "bun
|
|
1268
|
+
sdk: "bun run api-sdk",
|
|
1258
1269
|
start: "EXPO_UNSTABLE_MCP_SERVER=1 bun expo start --port 8082",
|
|
1259
1270
|
test: "bun test",
|
|
1260
1271
|
web: "EXPO_UNSTABLE_MCP_SERVER=1 bun expo start --web --port 8082",
|
|
@@ -1298,7 +1309,6 @@ const generateFrontendTsConfig = () => {
|
|
|
1298
1309
|
paths: {
|
|
1299
1310
|
"@/*": ["./*"],
|
|
1300
1311
|
"@components/*": ["./components/*"],
|
|
1301
|
-
"@store": ["./store/index"],
|
|
1302
1312
|
"@store/*": ["./store/*"],
|
|
1303
1313
|
"@utils": ["./utils/index"],
|
|
1304
1314
|
"@utils/*": ["./utils/*"],
|
|
@@ -1348,17 +1358,6 @@ config.resolver.resolveRequest = (context, moduleName, platform) => {
|
|
|
1348
1358
|
module.exports = config;
|
|
1349
1359
|
`;
|
|
1350
1360
|
};
|
|
1351
|
-
const generateFrontendTsConfigCodegen = () => {
|
|
1352
|
-
return JSON.stringify({
|
|
1353
|
-
compilerOptions: {
|
|
1354
|
-
module: "commonjs",
|
|
1355
|
-
moduleResolution: "bundler",
|
|
1356
|
-
skipLibCheck: true,
|
|
1357
|
-
strict: true,
|
|
1358
|
-
target: "es2022",
|
|
1359
|
-
},
|
|
1360
|
-
}, null, 2);
|
|
1361
|
-
};
|
|
1362
1361
|
const generateFrontendBiomeJsonc = () => {
|
|
1363
1362
|
return `{
|
|
1364
1363
|
"$schema": "./node_modules/@biomejs/biome/configuration_schema.json",
|
|
@@ -1442,126 +1441,146 @@ const generateFrontendBiomeJsonc = () => {
|
|
|
1442
1441
|
}
|
|
1443
1442
|
`;
|
|
1444
1443
|
};
|
|
1445
|
-
const
|
|
1446
|
-
return
|
|
1444
|
+
const generateFrontendApiClient = () => {
|
|
1445
|
+
return `/**
|
|
1446
|
+
* Request client for every non-synced backend route (\`/auth/me\`, modelRouter actions, admin).
|
|
1447
|
+
* Synced collections go through \`syncDb\` instead. Generated hooks in \`store/apiSdk.ts\` use the
|
|
1448
|
+
* client that \`ApiClientProvider\` (in \`app/_layout.tsx\`) puts in context.
|
|
1449
|
+
*/
|
|
1450
|
+
import {type ApiClient, createApiClient} from "@terreno/syncdb/api";
|
|
1451
|
+
import {appHeaders, baseUrl} from "@terreno/syncdb/config";
|
|
1452
|
+
import {authProvider} from "@/store/syncdb";
|
|
1447
1453
|
|
|
1448
|
-
|
|
1449
|
-
|
|
1450
|
-
apiImport: "emptySplitApi",
|
|
1451
|
-
argSuffix: "Args",
|
|
1452
|
-
exportName: "openapi",
|
|
1453
|
-
flattenArg: true,
|
|
1454
|
-
hooks: true,
|
|
1455
|
-
outputFile: "./store/openApiSdk.ts",
|
|
1456
|
-
responseSuffix: "Res",
|
|
1457
|
-
schemaFile: process.env.OPENAPI_URL ?? "http://localhost:4000/openapi.json",
|
|
1458
|
-
tag: true,
|
|
1459
|
-
};
|
|
1454
|
+
/** Failures callers already handle (signed out, missing rows) are not worth a warning. */
|
|
1455
|
+
const QUIET_STATUSES = new Set([401, 404]);
|
|
1460
1456
|
|
|
1461
|
-
export
|
|
1457
|
+
export const apiClient: ApiClient = createApiClient({
|
|
1458
|
+
authProvider,
|
|
1459
|
+
baseUrl,
|
|
1460
|
+
credentials: "include",
|
|
1461
|
+
headers: appHeaders,
|
|
1462
|
+
onError: (error, args): void => {
|
|
1463
|
+
if (QUIET_STATUSES.has(error.status)) {
|
|
1464
|
+
return;
|
|
1465
|
+
}
|
|
1466
|
+
console.warn(\`\${args.method ?? "GET"} \${args.url} failed: \${error.message}\`);
|
|
1467
|
+
},
|
|
1468
|
+
});
|
|
1469
|
+
|
|
1470
|
+
/** Bearer header for \`@terreno/admin-frontend\`'s \`AdminProvider\` fetch client. */
|
|
1471
|
+
export const getAdminAuthHeaders = async (): Promise<Record<string, string>> => {
|
|
1472
|
+
const token = await authProvider.getToken();
|
|
1473
|
+
if (!token) {
|
|
1474
|
+
return {};
|
|
1475
|
+
}
|
|
1476
|
+
return {Authorization: \`Bearer \${token}\`};
|
|
1477
|
+
};
|
|
1462
1478
|
`;
|
|
1463
1479
|
};
|
|
1464
|
-
|
|
1465
|
-
|
|
1466
|
-
|
|
1467
|
-
|
|
1468
|
-
|
|
1469
|
-
|
|
1470
|
-
|
|
1471
|
-
|
|
1472
|
-
|
|
1473
|
-
|
|
1474
|
-
|
|
1475
|
-
|
|
1476
|
-
|
|
1477
|
-
|
|
1478
|
-
|
|
1479
|
-
|
|
1480
|
-
|
|
1481
|
-
|
|
1482
|
-
|
|
1483
|
-
|
|
1484
|
-
|
|
1485
|
-
|
|
1486
|
-
|
|
1487
|
-
|
|
1488
|
-
|
|
1489
|
-
|
|
1490
|
-
|
|
1491
|
-
|
|
1492
|
-
|
|
1493
|
-
|
|
1494
|
-
|
|
1495
|
-
|
|
1496
|
-
|
|
1497
|
-
|
|
1498
|
-
|
|
1499
|
-
|
|
1500
|
-
|
|
1480
|
+
/**
|
|
1481
|
+
* Starter `store/apiSdk.ts`: the `/auth/me` hooks the profile screen uses, in the exact shape
|
|
1482
|
+
* `terreno-syncdb-codegen --api-out` emits. `bun run sdk` replaces it with hooks for every
|
|
1483
|
+
* non-synced route the running backend serves.
|
|
1484
|
+
*/
|
|
1485
|
+
const generateFrontendApiSdk = () => {
|
|
1486
|
+
return `// GENERATED by @terreno/syncdb (terreno-syncdb-codegen --api-out). Do not edit.
|
|
1487
|
+
// Typed request hooks for every non-synced @terreno/api route. Synced collections use the
|
|
1488
|
+
// collection hooks from the sync SDK instead. Run \`bun run sdk\` with the backend running to
|
|
1489
|
+
// regenerate this file from its OpenAPI spec.
|
|
1490
|
+
// biome-ignore-all lint/suspicious/noConfusingVoidType: no-argument operations type their argument as void so callers can omit it
|
|
1491
|
+
|
|
1492
|
+
import type {ApiRequestArgs} from "@terreno/syncdb/api";
|
|
1493
|
+
import {
|
|
1494
|
+
type UseApiMutationOptions,
|
|
1495
|
+
type UseApiMutationResult,
|
|
1496
|
+
type UseApiQueryOptions,
|
|
1497
|
+
type UseApiQueryResult,
|
|
1498
|
+
type UseLazyApiQueryOptions,
|
|
1499
|
+
type UseLazyApiQueryResult,
|
|
1500
|
+
useApiMutation,
|
|
1501
|
+
useApiQuery,
|
|
1502
|
+
useLazyApiQuery,
|
|
1503
|
+
} from "@terreno/syncdb/react";
|
|
1504
|
+
|
|
1505
|
+
type ApiSdkQueryOptions<T> = Omit<UseApiQueryOptions<T, T>, "providesTags" | "transformResponse">;
|
|
1506
|
+
type ApiSdkLazyOptions<T> = Omit<UseLazyApiQueryOptions<T, never>, "providesTags" | "toRequest">;
|
|
1507
|
+
|
|
1508
|
+
export type GetAuthMeArgs = void;
|
|
1509
|
+
|
|
1510
|
+
export type GetAuthMeRes = {
|
|
1511
|
+
admin?: boolean;
|
|
1512
|
+
email: string;
|
|
1513
|
+
name?: string;
|
|
1514
|
+
_id: string;
|
|
1515
|
+
id: string;
|
|
1516
|
+
created?: string;
|
|
1517
|
+
updated?: string;
|
|
1501
1518
|
};
|
|
1502
1519
|
|
|
1503
|
-
|
|
1504
|
-
|
|
1505
|
-
|
|
1506
|
-
}
|
|
1520
|
+
export const getAuthMeRequest = (): ApiRequestArgs => ({
|
|
1521
|
+
method: "GET",
|
|
1522
|
+
url: \`/auth/me\`,
|
|
1523
|
+
});
|
|
1507
1524
|
|
|
1508
|
-
|
|
1509
|
-
|
|
1510
|
-
|
|
1511
|
-
|
|
1512
|
-
|
|
1513
|
-
|
|
1514
|
-
|
|
1515
|
-
|
|
1516
|
-
|
|
1517
|
-
|
|
1518
|
-
|
|
1519
|
-
|
|
1525
|
+
/** Get the current user */
|
|
1526
|
+
export const useGetAuthMeQuery = (
|
|
1527
|
+
_arg?: GetAuthMeArgs,
|
|
1528
|
+
options: ApiSdkQueryOptions<GetAuthMeRes> = {}
|
|
1529
|
+
): UseApiQueryResult<GetAuthMeRes> =>
|
|
1530
|
+
useApiQuery<GetAuthMeRes>(getAuthMeRequest(), {...options, providesTags: ["auth"]});
|
|
1531
|
+
|
|
1532
|
+
export const useLazyGetAuthMeQuery = (
|
|
1533
|
+
options: ApiSdkLazyOptions<GetAuthMeRes> = {}
|
|
1534
|
+
): UseLazyApiQueryResult<GetAuthMeRes, GetAuthMeArgs> =>
|
|
1535
|
+
useLazyApiQuery<GetAuthMeRes, GetAuthMeArgs>({
|
|
1536
|
+
...options,
|
|
1537
|
+
providesTags: ["auth"],
|
|
1538
|
+
toRequest: (): ApiRequestArgs => getAuthMeRequest(),
|
|
1539
|
+
});
|
|
1520
1540
|
|
|
1521
|
-
|
|
1522
|
-
|
|
1523
|
-
|
|
1524
|
-
|
|
1525
|
-
|
|
1526
|
-
"// biome-ignore-all lint/suspicious/noExplicitAny: types are generated from backend OpenAPI schemas\\n" +
|
|
1527
|
-
content;
|
|
1528
|
-
}
|
|
1529
|
-
writeFileSync(sdkPath, content, "utf8");
|
|
1530
|
-
}
|
|
1541
|
+
export type PatchAuthMeArgs = {
|
|
1542
|
+
currentPassword?: string;
|
|
1543
|
+
email?: string;
|
|
1544
|
+
name?: string;
|
|
1545
|
+
};
|
|
1531
1546
|
|
|
1532
|
-
|
|
1533
|
-
|
|
1534
|
-
|
|
1535
|
-
|
|
1536
|
-
|
|
1537
|
-
|
|
1538
|
-
|
|
1539
|
-
|
|
1540
|
-
|
|
1541
|
-
|
|
1542
|
-
|
|
1543
|
-
|
|
1544
|
-
|
|
1547
|
+
export type PatchAuthMeRes = GetAuthMeRes;
|
|
1548
|
+
|
|
1549
|
+
export const patchAuthMeRequest = (arg: PatchAuthMeArgs): ApiRequestArgs => ({
|
|
1550
|
+
body: arg,
|
|
1551
|
+
method: "PATCH",
|
|
1552
|
+
url: \`/auth/me\`,
|
|
1553
|
+
});
|
|
1554
|
+
|
|
1555
|
+
/** Update the current user */
|
|
1556
|
+
export const usePatchAuthMeMutation = (
|
|
1557
|
+
options: UseApiMutationOptions<PatchAuthMeArgs, PatchAuthMeRes> = {}
|
|
1558
|
+
): UseApiMutationResult<PatchAuthMeArgs, PatchAuthMeRes> =>
|
|
1559
|
+
useApiMutation<PatchAuthMeArgs, PatchAuthMeRes>(patchAuthMeRequest, {
|
|
1560
|
+
invalidatesTags: ["auth"],
|
|
1561
|
+
...options,
|
|
1562
|
+
});
|
|
1545
1563
|
`;
|
|
1546
1564
|
};
|
|
1547
1565
|
const generateFrontendRootLayout = (_args) => {
|
|
1548
|
-
return `import {
|
|
1566
|
+
return `import {SessionProvider, useSession} from "@terreno/syncdb/auth";
|
|
1567
|
+
import {baseUrl} from "@terreno/syncdb/config";
|
|
1568
|
+
import {ApiClientProvider, SyncDbProvider} from "@terreno/syncdb/react";
|
|
1569
|
+
import {Spinner, TerrenoProvider} from "@terreno/ui";
|
|
1549
1570
|
import {Stack} from "expo-router";
|
|
1571
|
+
import {DefaultTheme, ThemeProvider} from "expo-router/react-navigation";
|
|
1572
|
+
import type React from "react";
|
|
1550
1573
|
import {useEffect} from "react";
|
|
1551
1574
|
import "react-native-reanimated";
|
|
1552
|
-
import {
|
|
1553
|
-
import {
|
|
1554
|
-
import {
|
|
1555
|
-
import {Spinner, TerrenoProvider} from "@terreno/ui";
|
|
1556
|
-
import {Provider, useSelector} from "react-redux";
|
|
1557
|
-
import {PersistGate} from "redux-persist/integration/react";
|
|
1558
|
-
import store, {persistor, syncBetterAuthSession} from "@/store/index";
|
|
1575
|
+
import {installDevConsoleLogger} from "@terreno/syncdb/app";
|
|
1576
|
+
import {betterAuthClient} from "@/lib/betterAuth";
|
|
1577
|
+
import {apiClient} from "@/store/apiClient";
|
|
1559
1578
|
import {setSyncDbReady, syncDb} from "@/store/syncdb";
|
|
1560
1579
|
|
|
1561
1580
|
// Dev-only: forwards console errors/warnings and uncaught errors to the backend's
|
|
1562
1581
|
// POST /__terreno/browser-logs so terreno-mcp-local \`read_logs\` / \`last_error\` can read them.
|
|
1563
1582
|
// No-op outside __DEV__.
|
|
1564
|
-
|
|
1583
|
+
installDevConsoleLogger({baseUrl});
|
|
1565
1584
|
|
|
1566
1585
|
export {ErrorBoundary} from "expo-router";
|
|
1567
1586
|
|
|
@@ -1569,30 +1588,34 @@ export const unstable_settings = {
|
|
|
1569
1588
|
initialRouteName: "(tabs)",
|
|
1570
1589
|
};
|
|
1571
1590
|
|
|
1591
|
+
// Logging out signs syncDb out (stop + wipe) and drops cached API responses so the next
|
|
1592
|
+
// user never sees the previous user's data.
|
|
1593
|
+
const SYNC_DBS = [syncDb];
|
|
1594
|
+
|
|
1572
1595
|
// TerrenoProvider loads the Nunito/Titillium Web families @terreno/ui renders with, so this
|
|
1573
1596
|
// app ships no font assets of its own. Add \`useFonts\` here only for extra custom faces.
|
|
1574
|
-
|
|
1597
|
+
// SessionProvider owns the Better Auth session (\`useSession()\`); ApiClientProvider gives the
|
|
1598
|
+
// generated hooks in store/apiSdk.ts their request client. No Redux store.
|
|
1599
|
+
export default function RootLayout(): React.ReactElement {
|
|
1575
1600
|
return (
|
|
1576
|
-
<
|
|
1577
|
-
<
|
|
1578
|
-
<
|
|
1601
|
+
<TerrenoProvider openAPISpecUrl={\`\${baseUrl}/openapi.json\`}>
|
|
1602
|
+
<ApiClientProvider client={apiClient}>
|
|
1603
|
+
<SessionProvider
|
|
1604
|
+
authClient={betterAuthClient}
|
|
1605
|
+
onLogout={apiClient.reset}
|
|
1606
|
+
syncDbs={SYNC_DBS}
|
|
1607
|
+
>
|
|
1579
1608
|
<RootLayoutNav />
|
|
1580
|
-
</
|
|
1581
|
-
</
|
|
1582
|
-
</
|
|
1609
|
+
</SessionProvider>
|
|
1610
|
+
</ApiClientProvider>
|
|
1611
|
+
</TerrenoProvider>
|
|
1583
1612
|
);
|
|
1584
1613
|
}
|
|
1585
1614
|
|
|
1586
1615
|
function RootLayoutNav(): React.ReactElement {
|
|
1587
|
-
const userId =
|
|
1588
|
-
const isAuthLoading = useSelector(selectBetterAuthIsLoading);
|
|
1589
|
-
|
|
1590
|
-
// Hydrate Better Auth session into Redux on startup.
|
|
1591
|
-
useEffect(() => {
|
|
1592
|
-
void syncBetterAuthSession(store.dispatch);
|
|
1593
|
-
}, []);
|
|
1616
|
+
const {userId, isLoading: isAuthLoading} = useSession();
|
|
1594
1617
|
|
|
1595
|
-
// Start syncdb after login; stop on logout.
|
|
1618
|
+
// Start syncdb after login; stop on logout or user switch.
|
|
1596
1619
|
useEffect(() => {
|
|
1597
1620
|
if (!userId) {
|
|
1598
1621
|
setSyncDbReady(false);
|
|
@@ -1644,12 +1667,11 @@ function RootLayoutNav(): React.ReactElement {
|
|
|
1644
1667
|
`;
|
|
1645
1668
|
};
|
|
1646
1669
|
const generateFrontendLogin = () => {
|
|
1647
|
-
return `import {TerrenoLoginScreen} from "@terreno/
|
|
1670
|
+
return `import {TerrenoLoginScreen, useSession} from "@terreno/syncdb/auth";
|
|
1648
1671
|
import {useLocalSearchParams, useRouter} from "expo-router";
|
|
1649
1672
|
import type React from "react";
|
|
1650
1673
|
import {useCallback} from "react";
|
|
1651
1674
|
import {betterAuthClient, rememberedAccounts} from "@/lib/betterAuth";
|
|
1652
|
-
import {syncBetterAuthSession, useAppDispatch} from "@/store/index";
|
|
1653
1675
|
|
|
1654
1676
|
/**
|
|
1655
1677
|
* Renders only the sign-in methods the server's /terreno/methods turns on. A social sign-in
|
|
@@ -1658,13 +1680,14 @@ import {syncBetterAuthSession, useAppDispatch} from "@/store/index";
|
|
|
1658
1680
|
*/
|
|
1659
1681
|
const Login: React.FC = () => {
|
|
1660
1682
|
const router = useRouter();
|
|
1661
|
-
const
|
|
1683
|
+
const {refresh} = useSession();
|
|
1662
1684
|
const {error} = useLocalSearchParams<{error?: string}>();
|
|
1663
1685
|
|
|
1664
1686
|
const handleSignedIn = useCallback(async (): Promise<void> => {
|
|
1665
|
-
|
|
1687
|
+
// Resolve the new session now instead of waiting for the next session push.
|
|
1688
|
+
await refresh();
|
|
1666
1689
|
router.replace("/(tabs)");
|
|
1667
|
-
}, [
|
|
1690
|
+
}, [refresh, router]);
|
|
1668
1691
|
|
|
1669
1692
|
const handleSignUpPress = useCallback((): void => {
|
|
1670
1693
|
router.push("/signup");
|
|
@@ -1689,7 +1712,7 @@ export default Login;
|
|
|
1689
1712
|
`;
|
|
1690
1713
|
};
|
|
1691
1714
|
const generateFrontendSettingsSecurity = () => {
|
|
1692
|
-
return `import {TerrenoAccountSecurityScreen} from "@terreno/
|
|
1715
|
+
return `import {TerrenoAccountSecurityScreen} from "@terreno/syncdb/auth";
|
|
1693
1716
|
import {useLocalSearchParams} from "expo-router";
|
|
1694
1717
|
import type React from "react";
|
|
1695
1718
|
import {betterAuthClient} from "@/lib/betterAuth";
|
|
@@ -1714,16 +1737,16 @@ export default SecurityScreen;
|
|
|
1714
1737
|
`;
|
|
1715
1738
|
};
|
|
1716
1739
|
const generateFrontendSignup = () => {
|
|
1717
|
-
return `import {
|
|
1740
|
+
return `import {useSession} from "@terreno/syncdb/auth";
|
|
1741
|
+
import {SignUpScreen, simplePasswordRequirements} from "@terreno/ui";
|
|
1718
1742
|
import {useRouter} from "expo-router";
|
|
1719
1743
|
import type React from "react";
|
|
1720
1744
|
import {useCallback, useState} from "react";
|
|
1721
1745
|
import {betterAuthClient} from "@/lib/betterAuth";
|
|
1722
|
-
import {syncBetterAuthSession, useAppDispatch} from "@/store/index";
|
|
1723
1746
|
|
|
1724
1747
|
const SignUp: React.FC = () => {
|
|
1725
1748
|
const router = useRouter();
|
|
1726
|
-
const
|
|
1749
|
+
const {refresh} = useSession();
|
|
1727
1750
|
const [errorMessage, setErrorMessage] = useState<string | undefined>(undefined);
|
|
1728
1751
|
const [isSubmitting, setIsSubmitting] = useState(false);
|
|
1729
1752
|
|
|
@@ -1738,7 +1761,8 @@ const SignUp: React.FC = () => {
|
|
|
1738
1761
|
setErrorMessage(result.error.message ?? "Sign up failed.");
|
|
1739
1762
|
return;
|
|
1740
1763
|
}
|
|
1741
|
-
|
|
1764
|
+
// Resolve the new session now instead of waiting for the next session push.
|
|
1765
|
+
await refresh();
|
|
1742
1766
|
router.replace("/(tabs)");
|
|
1743
1767
|
} catch {
|
|
1744
1768
|
setErrorMessage("Sign up failed. Please try again.");
|
|
@@ -1746,7 +1770,7 @@ const SignUp: React.FC = () => {
|
|
|
1746
1770
|
setIsSubmitting(false);
|
|
1747
1771
|
}
|
|
1748
1772
|
},
|
|
1749
|
-
[
|
|
1773
|
+
[refresh, router]
|
|
1750
1774
|
);
|
|
1751
1775
|
|
|
1752
1776
|
return (
|
|
@@ -1775,11 +1799,16 @@ import {Tabs} from "expo-router";
|
|
|
1775
1799
|
import type React from "react";
|
|
1776
1800
|
import type {ColorValue} from "react-native";
|
|
1777
1801
|
|
|
1802
|
+
type IconColor = React.ComponentProps<typeof FontAwesome>["color"];
|
|
1803
|
+
|
|
1778
1804
|
const TabBarIcon: React.FC<{
|
|
1779
1805
|
name: React.ComponentProps<typeof FontAwesome>["name"];
|
|
1780
1806
|
color: ColorValue;
|
|
1781
1807
|
}> = ({name, color}) => {
|
|
1782
|
-
|
|
1808
|
+
// React Native 0.88's ColorValue also admits number and null; tab tints are theme strings.
|
|
1809
|
+
return (
|
|
1810
|
+
<FontAwesome color={color as IconColor} name={name} size={24} style={{marginBottom: -3}} />
|
|
1811
|
+
);
|
|
1783
1812
|
};
|
|
1784
1813
|
|
|
1785
1814
|
const TabLayout: React.FC = () => {
|
|
@@ -1845,26 +1874,23 @@ export default HomeScreen;
|
|
|
1845
1874
|
`;
|
|
1846
1875
|
};
|
|
1847
1876
|
const generateFrontendTabsProfile = () => {
|
|
1848
|
-
return `import {
|
|
1877
|
+
return `import {useSession} from "@terreno/syncdb/auth";
|
|
1878
|
+
import {Box, Button, Card, Heading, Page, Spinner, TapToEdit, Text, TextField} from "@terreno/ui";
|
|
1849
1879
|
import {useRouter} from "expo-router";
|
|
1850
1880
|
import type React from "react";
|
|
1851
1881
|
import {useCallback, useEffect, useMemo, useState} from "react";
|
|
1852
|
-
import {
|
|
1853
|
-
import {logout, syncBetterAuthSession, useAppDispatch} from "@/store/index";
|
|
1854
|
-
import {useGetMeQuery, usePatchMeMutation} from "@/store/sdk";
|
|
1882
|
+
import {useGetAuthMeQuery, usePatchAuthMeMutation} from "@/store/apiSdk";
|
|
1855
1883
|
|
|
1856
1884
|
const ProfileScreen: React.FC = () => {
|
|
1857
|
-
const
|
|
1885
|
+
const {logout} = useSession();
|
|
1858
1886
|
const router = useRouter();
|
|
1859
|
-
const {data:
|
|
1860
|
-
const [updateProfile] =
|
|
1887
|
+
const {data: user, isLoading} = useGetAuthMeQuery();
|
|
1888
|
+
const [updateProfile] = usePatchAuthMeMutation();
|
|
1861
1889
|
const [name, setName] = useState<string>("");
|
|
1862
1890
|
const [email, setEmail] = useState<string>("");
|
|
1863
1891
|
const [currentPassword, setCurrentPassword] = useState<string>("");
|
|
1864
1892
|
const [saveError, setSaveError] = useState<string | null>(null);
|
|
1865
1893
|
|
|
1866
|
-
const user = profile;
|
|
1867
|
-
|
|
1868
1894
|
// Copy the server name into local state without resetting an in-progress email edit.
|
|
1869
1895
|
useEffect(() => {
|
|
1870
1896
|
if (!user) {
|
|
@@ -1881,11 +1907,10 @@ const ProfileScreen: React.FC = () => {
|
|
|
1881
1907
|
setEmail(user.email || "");
|
|
1882
1908
|
}, [user?.email]);
|
|
1883
1909
|
|
|
1910
|
+
// logout signs out Better Auth, then syncDb, and clears cached API responses.
|
|
1884
1911
|
const handleLogout = useCallback(async (): Promise<void> => {
|
|
1885
|
-
await
|
|
1886
|
-
|
|
1887
|
-
await syncBetterAuthSession(dispatch);
|
|
1888
|
-
}, [dispatch]);
|
|
1912
|
+
await logout();
|
|
1913
|
+
}, [logout]);
|
|
1889
1914
|
|
|
1890
1915
|
const handleOpenSecurity = useCallback((): void => {
|
|
1891
1916
|
router.push("/settings/security");
|
|
@@ -1991,11 +2016,24 @@ export default ProfileScreen;
|
|
|
1991
2016
|
`;
|
|
1992
2017
|
};
|
|
1993
2018
|
const generateFrontendAdminLayout = () => {
|
|
1994
|
-
return `import {
|
|
2019
|
+
return `import {AdminProvider} from "@terreno/admin-frontend";
|
|
2020
|
+
import {baseUrl} from "@terreno/syncdb/config";
|
|
2021
|
+
import {Stack} from "expo-router";
|
|
1995
2022
|
import type React from "react";
|
|
2023
|
+
import {getAdminAuthHeaders} from "@/store/apiClient";
|
|
1996
2024
|
|
|
2025
|
+
// AdminProvider binds every admin request to the API origin and the Better Auth session.
|
|
1997
2026
|
const AdminLayout: React.FC = () => {
|
|
1998
|
-
return
|
|
2027
|
+
return (
|
|
2028
|
+
<AdminProvider
|
|
2029
|
+
apiBase="/admin"
|
|
2030
|
+
apiOrigin={baseUrl}
|
|
2031
|
+
getAuthHeaders={getAdminAuthHeaders}
|
|
2032
|
+
routeBase="/admin"
|
|
2033
|
+
>
|
|
2034
|
+
<Stack screenOptions={{headerShown: false}} />
|
|
2035
|
+
</AdminProvider>
|
|
2036
|
+
);
|
|
1999
2037
|
};
|
|
2000
2038
|
|
|
2001
2039
|
export default AdminLayout;
|
|
@@ -2004,16 +2042,9 @@ export default AdminLayout;
|
|
|
2004
2042
|
const generateFrontendAdminIndex = () => {
|
|
2005
2043
|
return `import {AdminModelList} from "@terreno/admin-frontend";
|
|
2006
2044
|
import type React from "react";
|
|
2007
|
-
import {adminTerrenoApi} from "@/store/sdk";
|
|
2008
2045
|
|
|
2009
2046
|
const AdminListScreen: React.FC = () => {
|
|
2010
|
-
return
|
|
2011
|
-
<AdminModelList
|
|
2012
|
-
api={adminTerrenoApi}
|
|
2013
|
-
baseUrl="/admin"
|
|
2014
|
-
configurationPath="/admin/configuration"
|
|
2015
|
-
/>
|
|
2016
|
-
);
|
|
2047
|
+
return <AdminModelList baseUrl="/admin" configurationPath="/admin/configuration" />;
|
|
2017
2048
|
};
|
|
2018
2049
|
|
|
2019
2050
|
export default AdminListScreen;
|
|
@@ -2022,10 +2053,9 @@ export default AdminListScreen;
|
|
|
2022
2053
|
const generateFrontendAdminConfiguration = () => {
|
|
2023
2054
|
return `import {ConfigurationScreen} from "@terreno/admin-frontend";
|
|
2024
2055
|
import type React from "react";
|
|
2025
|
-
import {adminTerrenoApi} from "@/store/sdk";
|
|
2026
2056
|
|
|
2027
2057
|
const ConfigurationPage: React.FC = () => {
|
|
2028
|
-
return <ConfigurationScreen
|
|
2058
|
+
return <ConfigurationScreen />;
|
|
2029
2059
|
};
|
|
2030
2060
|
|
|
2031
2061
|
export default ConfigurationPage;
|
|
@@ -2055,315 +2085,6 @@ const NotFoundScreen: React.FC = () => {
|
|
|
2055
2085
|
export default NotFoundScreen;
|
|
2056
2086
|
`;
|
|
2057
2087
|
};
|
|
2058
|
-
const generateFrontendStoreIndex = () => {
|
|
2059
|
-
return `import AsyncStorage from "@react-native-async-storage/async-storage";
|
|
2060
|
-
import {combineReducers, configureStore, type Store} from "@reduxjs/toolkit";
|
|
2061
|
-
import {
|
|
2062
|
-
type BetterAuthClientInterface,
|
|
2063
|
-
generateBetterAuthSlice,
|
|
2064
|
-
registerTerrenoDevStore,
|
|
2065
|
-
setTokenSource,
|
|
2066
|
-
} from "@terreno/rtk";
|
|
2067
|
-
import {DateTime} from "luxon";
|
|
2068
|
-
import {useDispatch} from "react-redux";
|
|
2069
|
-
import type {Storage} from "redux-persist";
|
|
2070
|
-
import {persistReducer, persistStore} from "redux-persist";
|
|
2071
|
-
import {betterAuthClient} from "@/lib/betterAuth";
|
|
2072
|
-
|
|
2073
|
-
import appState from "./appState";
|
|
2074
|
-
import {rtkQueryErrorMiddleware} from "./errors";
|
|
2075
|
-
import {terrenoApi} from "./sdk";
|
|
2076
|
-
import {authProvider} from "./syncdb";
|
|
2077
|
-
|
|
2078
|
-
// Registered before the store exists, so the first RTK request already reads the Better Auth
|
|
2079
|
-
// token instead of rtk's default JWT source.
|
|
2080
|
-
setTokenSource(authProvider);
|
|
2081
|
-
|
|
2082
|
-
const betterAuth = generateBetterAuthSlice({
|
|
2083
|
-
authClient: betterAuthClient as unknown as BetterAuthClientInterface,
|
|
2084
|
-
});
|
|
2085
|
-
|
|
2086
|
-
export const logout = betterAuth.actions.logout;
|
|
2087
|
-
export const syncBetterAuthSession = betterAuth.syncSession;
|
|
2088
|
-
|
|
2089
|
-
const createSafeStorage = (): Storage => {
|
|
2090
|
-
return {
|
|
2091
|
-
getItem: async (key: string): Promise<string | null> => {
|
|
2092
|
-
if (typeof window !== "undefined") {
|
|
2093
|
-
return AsyncStorage.getItem(key);
|
|
2094
|
-
}
|
|
2095
|
-
return null;
|
|
2096
|
-
},
|
|
2097
|
-
removeItem: async (key: string): Promise<void> => {
|
|
2098
|
-
if (typeof window !== "undefined") {
|
|
2099
|
-
return AsyncStorage.removeItem(key);
|
|
2100
|
-
}
|
|
2101
|
-
},
|
|
2102
|
-
setItem: async (key: string, value: string): Promise<void> => {
|
|
2103
|
-
if (typeof window !== "undefined") {
|
|
2104
|
-
return AsyncStorage.setItem(key, value);
|
|
2105
|
-
}
|
|
2106
|
-
},
|
|
2107
|
-
};
|
|
2108
|
-
};
|
|
2109
|
-
|
|
2110
|
-
const persistConfig = {
|
|
2111
|
-
blacklist: ["terreno-rtk", "betterAuth"],
|
|
2112
|
-
key: "root",
|
|
2113
|
-
storage: createSafeStorage(),
|
|
2114
|
-
timeout: 0,
|
|
2115
|
-
version: 1,
|
|
2116
|
-
};
|
|
2117
|
-
|
|
2118
|
-
const rootReducer = combineReducers({
|
|
2119
|
-
appState,
|
|
2120
|
-
betterAuth: betterAuth.reducer,
|
|
2121
|
-
"terreno-rtk": terrenoApi.reducer,
|
|
2122
|
-
});
|
|
2123
|
-
|
|
2124
|
-
const persistedReducer = persistReducer(persistConfig, rootReducer);
|
|
2125
|
-
|
|
2126
|
-
const store = configureStore({
|
|
2127
|
-
devTools: process.env.NODE_ENV !== "production" && {
|
|
2128
|
-
name: \`App-\${
|
|
2129
|
-
typeof window !== "undefined"
|
|
2130
|
-
// biome-ignore lint/suspicious/noAssignInExpressions: Window name assignment
|
|
2131
|
-
? window.name || ((window.name = \`Window-\${DateTime.now().toFormat("HH:mm:ss")}\`))
|
|
2132
|
-
: "Unknown"
|
|
2133
|
-
}\`,
|
|
2134
|
-
},
|
|
2135
|
-
middleware: (getDefaultMiddleware) => {
|
|
2136
|
-
return getDefaultMiddleware({
|
|
2137
|
-
immutableCheck: false,
|
|
2138
|
-
serializableCheck: false,
|
|
2139
|
-
thunk: true,
|
|
2140
|
-
}).concat([
|
|
2141
|
-
...betterAuth.middleware,
|
|
2142
|
-
// noExplicitAny: RTK Query middleware typing
|
|
2143
|
-
// biome-ignore lint/suspicious/noExplicitAny: RTK Query middleware typing
|
|
2144
|
-
terrenoApi.middleware as any,
|
|
2145
|
-
rtkQueryErrorMiddleware,
|
|
2146
|
-
// noExplicitAny: Middleware array typing
|
|
2147
|
-
// biome-ignore lint/suspicious/noExplicitAny: Middleware array typing
|
|
2148
|
-
]) as any;
|
|
2149
|
-
},
|
|
2150
|
-
reducer: persistedReducer,
|
|
2151
|
-
});
|
|
2152
|
-
|
|
2153
|
-
registerTerrenoDevStore(store as unknown as Store<Record<string, unknown>>);
|
|
2154
|
-
|
|
2155
|
-
export const persistor = persistStore(store);
|
|
2156
|
-
|
|
2157
|
-
export type RootState = ReturnType<typeof store.getState>;
|
|
2158
|
-
export type AppDispatch = typeof store.dispatch;
|
|
2159
|
-
|
|
2160
|
-
export const useAppDispatch: () => AppDispatch = useDispatch;
|
|
2161
|
-
|
|
2162
|
-
export default store;
|
|
2163
|
-
`;
|
|
2164
|
-
};
|
|
2165
|
-
const generateFrontendStoreAppState = () => {
|
|
2166
|
-
return `import {createSlice, type PayloadAction} from "@reduxjs/toolkit";
|
|
2167
|
-
import {type TypedUseSelectorHook, useSelector} from "react-redux";
|
|
2168
|
-
import type {RootState} from "./index";
|
|
2169
|
-
|
|
2170
|
-
export const useAppSelector: TypedUseSelectorHook<RootState> = useSelector;
|
|
2171
|
-
|
|
2172
|
-
export type AppState = {
|
|
2173
|
-
darkMode: boolean;
|
|
2174
|
-
language: string;
|
|
2175
|
-
};
|
|
2176
|
-
|
|
2177
|
-
const initialState: AppState = {
|
|
2178
|
-
darkMode: false,
|
|
2179
|
-
language: "en",
|
|
2180
|
-
};
|
|
2181
|
-
|
|
2182
|
-
export const appStateSlice = createSlice({
|
|
2183
|
-
initialState,
|
|
2184
|
-
name: "appState",
|
|
2185
|
-
reducers: {
|
|
2186
|
-
resetAppState: () => initialState,
|
|
2187
|
-
setDarkMode: (state, action: PayloadAction<boolean>) => {
|
|
2188
|
-
state.darkMode = action.payload;
|
|
2189
|
-
},
|
|
2190
|
-
setLanguage: (state, action: PayloadAction<string>) => {
|
|
2191
|
-
state.language = action.payload;
|
|
2192
|
-
},
|
|
2193
|
-
},
|
|
2194
|
-
});
|
|
2195
|
-
|
|
2196
|
-
export const {setDarkMode, setLanguage, resetAppState} = appStateSlice.actions;
|
|
2197
|
-
|
|
2198
|
-
export const useSelectDarkMode = (): boolean => {
|
|
2199
|
-
return useAppSelector((state) => state.appState.darkMode);
|
|
2200
|
-
};
|
|
2201
|
-
|
|
2202
|
-
export const useSelectLanguage = (): string => {
|
|
2203
|
-
return useAppSelector((state) => state.appState.language);
|
|
2204
|
-
};
|
|
2205
|
-
|
|
2206
|
-
export default appStateSlice.reducer;
|
|
2207
|
-
`;
|
|
2208
|
-
};
|
|
2209
|
-
const generateFrontendStoreErrors = () => {
|
|
2210
|
-
return `import type {Middleware} from "@reduxjs/toolkit";
|
|
2211
|
-
import {useToast} from "@terreno/ui";
|
|
2212
|
-
|
|
2213
|
-
const ignoredErrors = [
|
|
2214
|
-
"Password or username is incorrect",
|
|
2215
|
-
"Token refresh failed with 401",
|
|
2216
|
-
"Failed to refresh token",
|
|
2217
|
-
];
|
|
2218
|
-
|
|
2219
|
-
// noExplicitAny: Generic middleware
|
|
2220
|
-
// biome-ignore lint/suspicious/noExplicitAny: Generic middleware
|
|
2221
|
-
export const rtkQueryErrorMiddleware: Middleware = () => (next) => (action: any) => {
|
|
2222
|
-
if (action?.error && action?.payload) {
|
|
2223
|
-
const errorMessage =
|
|
2224
|
-
action.payload?.data?.title ??
|
|
2225
|
-
action.payload?.data?.message ??
|
|
2226
|
-
action.payload?.error ??
|
|
2227
|
-
JSON.stringify(action.payload);
|
|
2228
|
-
|
|
2229
|
-
let endpointInfo = "unknown endpoint";
|
|
2230
|
-
if (action.meta?.baseQueryMeta?.request?.method && action.meta?.baseQueryMeta?.request?.url) {
|
|
2231
|
-
endpointInfo = \`\${action.meta.baseQueryMeta.request.url} \${action.meta.baseQueryMeta.request.method}\`;
|
|
2232
|
-
} else if (action.meta?.arg?.endpointName) {
|
|
2233
|
-
endpointInfo = \`\${action.meta.arg.endpointName} rejected \${action.meta.arg.type || ""}\`;
|
|
2234
|
-
}
|
|
2235
|
-
|
|
2236
|
-
const message = \`\${endpointInfo.trim()}: \${errorMessage}\`;
|
|
2237
|
-
console.debug(message);
|
|
2238
|
-
|
|
2239
|
-
if (action.payload.status === 404 || action.payload.status === 401) {
|
|
2240
|
-
return next(action);
|
|
2241
|
-
}
|
|
2242
|
-
|
|
2243
|
-
const shouldIgnore = ignoredErrors.some((err) => errorMessage.includes(err));
|
|
2244
|
-
if (!shouldIgnore) {
|
|
2245
|
-
console.warn(message);
|
|
2246
|
-
}
|
|
2247
|
-
}
|
|
2248
|
-
|
|
2249
|
-
return next(action);
|
|
2250
|
-
};
|
|
2251
|
-
|
|
2252
|
-
export const useSentryAndToast = (): ((errorMessage: string) => void) => {
|
|
2253
|
-
const toast = useToast();
|
|
2254
|
-
return (error: string): void => {
|
|
2255
|
-
if (!error) {
|
|
2256
|
-
return;
|
|
2257
|
-
}
|
|
2258
|
-
toast.error(error);
|
|
2259
|
-
console.warn(\`Error: \${error}\`);
|
|
2260
|
-
};
|
|
2261
|
-
};
|
|
2262
|
-
`;
|
|
2263
|
-
};
|
|
2264
|
-
const generateFrontendStoreSdk = () => {
|
|
2265
|
-
return `import type {AdminScreenProps} from "@terreno/admin-frontend";
|
|
2266
|
-
import {generateTags} from "@terreno/rtk";
|
|
2267
|
-
import startCase from "lodash/startCase";
|
|
2268
|
-
|
|
2269
|
-
import {addTagTypes, openapi} from "./openApiSdk";
|
|
2270
|
-
|
|
2271
|
-
/** emptyApi unwraps the \`data\` envelope — profile fields are top-level on the response. */
|
|
2272
|
-
export interface ProfileResponse {
|
|
2273
|
-
_id: string;
|
|
2274
|
-
id: string;
|
|
2275
|
-
email: string;
|
|
2276
|
-
name: string;
|
|
2277
|
-
}
|
|
2278
|
-
|
|
2279
|
-
export interface UpdateProfileRequest {
|
|
2280
|
-
name?: string;
|
|
2281
|
-
/** The app User's email; the Better Auth sign-in email does not change. */
|
|
2282
|
-
email?: string;
|
|
2283
|
-
/** Required by the server when \`email\` changes the mailbox. */
|
|
2284
|
-
currentPassword?: string;
|
|
2285
|
-
}
|
|
2286
|
-
|
|
2287
|
-
export const terrenoApi = openapi
|
|
2288
|
-
.enhanceEndpoints({
|
|
2289
|
-
addTagTypes: ["profile"],
|
|
2290
|
-
})
|
|
2291
|
-
.injectEndpoints({
|
|
2292
|
-
endpoints: (builder) => ({
|
|
2293
|
-
getMe: builder.query<ProfileResponse, void>({
|
|
2294
|
-
providesTags: ["profile"],
|
|
2295
|
-
query: () => ({
|
|
2296
|
-
method: "GET",
|
|
2297
|
-
url: "/auth/me",
|
|
2298
|
-
}),
|
|
2299
|
-
}),
|
|
2300
|
-
patchMe: builder.mutation<ProfileResponse, UpdateProfileRequest>({
|
|
2301
|
-
invalidatesTags: ["profile"],
|
|
2302
|
-
query: (body) => ({
|
|
2303
|
-
body,
|
|
2304
|
-
method: "PATCH",
|
|
2305
|
-
url: "/auth/me",
|
|
2306
|
-
}),
|
|
2307
|
-
}),
|
|
2308
|
-
}),
|
|
2309
|
-
})
|
|
2310
|
-
.enhanceEndpoints({
|
|
2311
|
-
endpoints: {
|
|
2312
|
-
...generateTags(openapi, [...addTagTypes]),
|
|
2313
|
-
},
|
|
2314
|
-
});
|
|
2315
|
-
|
|
2316
|
-
/** Type-erased API instance for @terreno/admin-frontend screens. */
|
|
2317
|
-
export const adminTerrenoApi = terrenoApi as unknown as AdminScreenProps["api"];
|
|
2318
|
-
|
|
2319
|
-
export const {
|
|
2320
|
-
useGetMeQuery,
|
|
2321
|
-
usePatchMeMutation,
|
|
2322
|
-
} = terrenoApi;
|
|
2323
|
-
export * from "./openApiSdk";
|
|
2324
|
-
|
|
2325
|
-
type OpenApiEndpoints = Record<string, unknown>;
|
|
2326
|
-
|
|
2327
|
-
export const getSdkHook = (
|
|
2328
|
-
modelName: string,
|
|
2329
|
-
type: "list" | "read" | "create" | "update" | "remove"
|
|
2330
|
-
): Record<string, unknown> => {
|
|
2331
|
-
const modelPath = startCase(modelName).replace(/\\s/g, "");
|
|
2332
|
-
const endpoints = openapi.endpoints as OpenApiEndpoints;
|
|
2333
|
-
switch (type) {
|
|
2334
|
-
case "list":
|
|
2335
|
-
return endpoints[\`get\${modelPath}\`] as Record<string, unknown>;
|
|
2336
|
-
case "read":
|
|
2337
|
-
return endpoints[\`get\${modelPath}ById\`] as Record<string, unknown>;
|
|
2338
|
-
case "create":
|
|
2339
|
-
return endpoints[\`post\${modelPath}\`] as Record<string, unknown>;
|
|
2340
|
-
case "update":
|
|
2341
|
-
return endpoints[\`patch\${modelPath}ById\`] as Record<string, unknown>;
|
|
2342
|
-
case "remove":
|
|
2343
|
-
return endpoints[\`delete\${modelPath}ById\`] as Record<string, unknown>;
|
|
2344
|
-
default:
|
|
2345
|
-
throw new Error(\`Invalid SDK hook: \${modelName}/\${type}\`);
|
|
2346
|
-
}
|
|
2347
|
-
};
|
|
2348
|
-
`;
|
|
2349
|
-
};
|
|
2350
|
-
const generateFrontendStoreOpenApiSdk = () => {
|
|
2351
|
-
return `// THIS FILE IS AUTO-GENERATED. DO NOT EDIT.
|
|
2352
|
-
// Run "bun run sdk" to regenerate this file from the backend OpenAPI spec.
|
|
2353
|
-
|
|
2354
|
-
import {emptySplitApi as api} from "@terreno/rtk";
|
|
2355
|
-
export const addTagTypes = ["Users", "Auth", "profile"] as const;
|
|
2356
|
-
const injectedRtkApi = api
|
|
2357
|
-
.enhanceEndpoints({
|
|
2358
|
-
addTagTypes,
|
|
2359
|
-
})
|
|
2360
|
-
.injectEndpoints({
|
|
2361
|
-
endpoints: () => ({}),
|
|
2362
|
-
overrideExisting: false,
|
|
2363
|
-
});
|
|
2364
|
-
export {injectedRtkApi as openapi};
|
|
2365
|
-
`;
|
|
2366
|
-
};
|
|
2367
2088
|
const generateFrontendTheme = () => {
|
|
2368
2089
|
return `// Theme configuration for the app
|
|
2369
2090
|
// Override these values to customize the app's appearance
|
|
@@ -2540,7 +2261,8 @@ NODE_ENV=development
|
|
|
2540
2261
|
};
|
|
2541
2262
|
const generateFrontendBetterAuthLib = (args) => {
|
|
2542
2263
|
const { appName } = args;
|
|
2543
|
-
return `import {
|
|
2264
|
+
return `import {createBetterAuthClient, createRememberedAccountsStore} from "@terreno/syncdb/auth";
|
|
2265
|
+
import {baseUrl} from "@terreno/syncdb/config";
|
|
2544
2266
|
import Constants from "expo-constants";
|
|
2545
2267
|
|
|
2546
2268
|
const getAppScheme = (): string => {
|
|
@@ -2548,8 +2270,12 @@ const getAppScheme = (): string => {
|
|
|
2548
2270
|
return expoExtra?.scheme ?? "${appName}";
|
|
2549
2271
|
};
|
|
2550
2272
|
|
|
2551
|
-
|
|
2552
|
-
|
|
2273
|
+
/**
|
|
2274
|
+
* Expo-ready Better Auth client (SecureStore on native, AsyncStorage on web). Feeds
|
|
2275
|
+
* \`SessionProvider\` (app/_layout.tsx) and the shared auth provider in store/syncdb.ts.
|
|
2276
|
+
* The client features match the auth suite backend/src/utils/betterAuthConfig.ts turns on. The
|
|
2277
|
+
* login screen shows the two-factor step itself, so twoFactor needs no redirect.
|
|
2278
|
+
*/
|
|
2553
2279
|
export const betterAuthClient = createBetterAuthClient({
|
|
2554
2280
|
baseURL: baseUrl,
|
|
2555
2281
|
features: {
|
|
@@ -2567,56 +2293,33 @@ export const betterAuthClient = createBetterAuthClient({
|
|
|
2567
2293
|
* method is kept; pass \`rememberIdentity: true\` to also keep name, masked email and avatar.
|
|
2568
2294
|
*/
|
|
2569
2295
|
export const rememberedAccounts = createRememberedAccountsStore({rememberIdentity: false});
|
|
2570
|
-
|
|
2571
|
-
export const signOut = async (): Promise<void> => {
|
|
2572
|
-
await betterAuthClient.signOut();
|
|
2573
|
-
};
|
|
2574
2296
|
`;
|
|
2575
2297
|
};
|
|
2576
2298
|
const generateFrontendSyncDb = (args) => {
|
|
2577
2299
|
const { appName } = args;
|
|
2578
|
-
|
|
2579
|
-
return `import {baseUrl} from "@terreno/rtk";
|
|
2580
|
-
import {
|
|
2581
|
-
type BetterAuthClientLike,
|
|
2300
|
+
return `import {
|
|
2582
2301
|
betterAuthAdapter,
|
|
2302
|
+
bridgeBetterAuthReactClient,
|
|
2583
2303
|
createSyncDb,
|
|
2584
2304
|
type SyncDb,
|
|
2585
2305
|
} from "@terreno/syncdb";
|
|
2306
|
+
import {baseUrl} from "@terreno/syncdb/config";
|
|
2586
2307
|
import {betterAuthClient} from "@/lib/betterAuth";
|
|
2587
2308
|
|
|
2588
|
-
export const SYNC_DB_NAME = "${
|
|
2309
|
+
export const SYNC_DB_NAME = "${appName}";
|
|
2589
2310
|
|
|
2590
2311
|
/** Add synced collection names here as you register modelRouter sync scopes on the backend. */
|
|
2591
2312
|
export const SYNC_COLLECTIONS: string[] = [];
|
|
2592
2313
|
|
|
2593
2314
|
/**
|
|
2594
|
-
*
|
|
2595
|
-
*
|
|
2596
|
-
*
|
|
2597
|
-
*
|
|
2598
|
-
* atom to the shape the adapter expects so auth changes are event-driven instead.
|
|
2315
|
+
* Shared by the sync client and the request client (store/apiClient.ts) so both send the same
|
|
2316
|
+
* Better Auth session token and refresh the same way. bridgeBetterAuthReactClient turns the
|
|
2317
|
+
* Better Auth session atom into push updates; pollIntervalMs is only a fallback if that atom
|
|
2318
|
+
* is unavailable, so keep it slow.
|
|
2599
2319
|
*/
|
|
2600
|
-
|
|
2601
|
-
|
|
2602
|
-
|
|
2603
|
-
).$store?.atoms?.session;
|
|
2604
|
-
|
|
2605
|
-
const syncAuthClient: BetterAuthClientLike = {
|
|
2606
|
-
getSession: () => betterAuthClient.getSession(),
|
|
2607
|
-
...(sessionAtom
|
|
2608
|
-
? {useSession: {subscribe: (listener): (() => void) => sessionAtom.subscribe(listener)}}
|
|
2609
|
-
: {}),
|
|
2610
|
-
};
|
|
2611
|
-
|
|
2612
|
-
/**
|
|
2613
|
-
* The app's one token source: syncdb authenticates with it, and store/index.ts registers it
|
|
2614
|
-
* with @terreno/rtk so RTK Query requests read the same Better Auth token.
|
|
2615
|
-
*
|
|
2616
|
-
* pollIntervalMs is only used as a fallback if the session atom bridge above is unavailable
|
|
2617
|
-
* (e.g. a future Better Auth client shape change); keep it slow.
|
|
2618
|
-
*/
|
|
2619
|
-
export const authProvider = betterAuthAdapter(syncAuthClient, {pollIntervalMs: 60_000});
|
|
2320
|
+
export const authProvider = betterAuthAdapter(bridgeBetterAuthReactClient(betterAuthClient), {
|
|
2321
|
+
pollIntervalMs: 60_000,
|
|
2322
|
+
});
|
|
2620
2323
|
|
|
2621
2324
|
export const syncDb: SyncDb = createSyncDb({
|
|
2622
2325
|
authProvider,
|
|
@@ -2625,6 +2328,10 @@ export const syncDb: SyncDb = createSyncDb({
|
|
|
2625
2328
|
name: SYNC_DB_NAME,
|
|
2626
2329
|
});
|
|
2627
2330
|
|
|
2331
|
+
/**
|
|
2332
|
+
* Tracks whether \`syncDb.start()\` has resolved for the current login. Screens gate
|
|
2333
|
+
* \`mutate()\` on it: \`start()\` must resolve an authenticated user before writes are safe.
|
|
2334
|
+
*/
|
|
2628
2335
|
let syncDbReady = false;
|
|
2629
2336
|
const syncDbReadyListeners = new Set<() => void>();
|
|
2630
2337
|
|
|
@@ -2656,10 +2363,6 @@ const generateFrontendUtilsIndex = () => {
|
|
|
2656
2363
|
export const captureMessage = (message: string): void => {
|
|
2657
2364
|
console.warn("Captured message:", message);
|
|
2658
2365
|
};
|
|
2659
|
-
|
|
2660
|
-
export const createSentryReduxEnhancer = (): unknown => {
|
|
2661
|
-
return undefined;
|
|
2662
|
-
};
|
|
2663
2366
|
`;
|
|
2664
2367
|
};
|
|
2665
2368
|
const generateBackendCiWorkflow = (_args) => {
|
|
@@ -2840,11 +2543,8 @@ export const generateAllFiles = (args) => {
|
|
|
2840
2543
|
{ content: generateFrontendPackageJson(args), path: `${frontendDir}/package.json` },
|
|
2841
2544
|
{ content: generateFrontendAppJson(args), path: `${frontendDir}/app.json` },
|
|
2842
2545
|
{ content: generateFrontendTsConfig(), path: `${frontendDir}/tsconfig.json` },
|
|
2843
|
-
{ content: generateFrontendTsConfigCodegen(), path: `${frontendDir}/tsconfig.codegen.json` },
|
|
2844
2546
|
{ content: generateFrontendMetroConfig(), path: `${frontendDir}/metro.config.js` },
|
|
2845
2547
|
{ content: generateFrontendBiomeJsonc(), path: `${frontendDir}/biome.jsonc` },
|
|
2846
|
-
{ content: generateFrontendOpenApiConfig(), path: `${frontendDir}/openapi-config.ts` },
|
|
2847
|
-
{ content: generateFrontendGenerateSdk(), path: `${frontendDir}/scripts/generate-sdk.ts` },
|
|
2848
2548
|
{ content: generateFrontendRootLayout(args), path: `${frontendDir}/app/_layout.tsx` },
|
|
2849
2549
|
{ content: generateFrontendLogin(), path: `${frontendDir}/app/login.tsx` },
|
|
2850
2550
|
{ content: generateFrontendSignup(), path: `${frontendDir}/app/signup.tsx` },
|
|
@@ -2862,11 +2562,8 @@ export const generateAllFiles = (args) => {
|
|
|
2862
2562
|
content: generateFrontendAdminConfiguration(),
|
|
2863
2563
|
path: `${frontendDir}/app/(tabs)/admin/configuration.tsx`,
|
|
2864
2564
|
},
|
|
2865
|
-
{ content:
|
|
2866
|
-
{ content:
|
|
2867
|
-
{ content: generateFrontendStoreErrors(), path: `${frontendDir}/store/errors.ts` },
|
|
2868
|
-
{ content: generateFrontendStoreSdk(), path: `${frontendDir}/store/sdk.ts` },
|
|
2869
|
-
{ content: generateFrontendStoreOpenApiSdk(), path: `${frontendDir}/store/openApiSdk.ts` },
|
|
2565
|
+
{ content: generateFrontendApiClient(), path: `${frontendDir}/store/apiClient.ts` },
|
|
2566
|
+
{ content: generateFrontendApiSdk(), path: `${frontendDir}/store/apiSdk.ts` },
|
|
2870
2567
|
{ content: generateFrontendBetterAuthLib(args), path: `${frontendDir}/lib/betterAuth.ts` },
|
|
2871
2568
|
{ content: generateFrontendSyncDb(args), path: `${frontendDir}/store/syncdb.ts` },
|
|
2872
2569
|
{ content: generateFrontendTheme(), path: `${frontendDir}/constants/theme.ts` },
|