@pablozaiden/webapp 0.6.2 → 0.6.4
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/docs/server.md
CHANGED
|
@@ -106,6 +106,31 @@ Route context is user-aware:
|
|
|
106
106
|
|
|
107
107
|
Use `ctx.filterOwned(records, getUserId)` and `ctx.requireOwned(record, getUserId)` when app records use a different ownership field. Return 404 for other-user resources so route responses do not reveal whether another user's id exists.
|
|
108
108
|
|
|
109
|
+
## Supplying a validated runtime config
|
|
110
|
+
|
|
111
|
+
`createWebAppServer` reads and validates `{PREFIX}_*` environment variables by
|
|
112
|
+
default. An application that has already resolved a `RuntimeConfig` can pass it
|
|
113
|
+
through `runtimeConfig` instead:
|
|
114
|
+
|
|
115
|
+
```ts
|
|
116
|
+
import { createWebAppServer, readRuntimeConfig } from "@pablozaiden/webapp/server";
|
|
117
|
+
|
|
118
|
+
const runtimeConfig = readRuntimeConfig({ appName: "My App", envPrefix: "MY_APP" });
|
|
119
|
+
const app = createWebAppServer({
|
|
120
|
+
appName: "My App",
|
|
121
|
+
envPrefix: "MY_APP",
|
|
122
|
+
runtimeConfig,
|
|
123
|
+
routes,
|
|
124
|
+
});
|
|
125
|
+
```
|
|
126
|
+
|
|
127
|
+
The supplied config is used during every framework initialization step,
|
|
128
|
+
including store selection, logging, authentication, document generation,
|
|
129
|
+
request handling, and lifecycle setup. Its `appName` and `envPrefix` must match
|
|
130
|
+
the constructor inputs. Do not mutate `app.config` after construction to
|
|
131
|
+
replace constructor options; omit `runtimeConfig` when the framework should read
|
|
132
|
+
the environment itself.
|
|
133
|
+
|
|
109
134
|
Built-in endpoints include:
|
|
110
135
|
|
|
111
136
|
| Endpoint | Purpose |
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@pablozaiden/webapp",
|
|
3
|
-
"version": "0.6.
|
|
3
|
+
"version": "0.6.4",
|
|
4
4
|
"description": "Opinionated Bun + React webapp framework for single-server apps",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"type": "module",
|
|
@@ -33,13 +33,15 @@
|
|
|
33
33
|
"dev": "bun run --cwd examples/notes-todo dev",
|
|
34
34
|
"dev:kitchen-sink": "bun run --cwd examples/kitchen-sink dev",
|
|
35
35
|
"dev:notes-todo": "bun run --cwd examples/notes-todo dev",
|
|
36
|
-
"build": "bun run
|
|
36
|
+
"build": "bun run typecheck && bun run build:examples",
|
|
37
|
+
"build:examples": "bun run --cwd examples/kitchen-sink build && bun run --cwd examples/notes-todo build",
|
|
37
38
|
"build:kitchen-sink": "bun run --cwd examples/kitchen-sink build",
|
|
38
39
|
"build:notes-todo": "bun run --cwd examples/notes-todo build",
|
|
39
40
|
"test": "bun run test:build-binary && bun run test:remaining",
|
|
40
41
|
"test:build-binary": "bun test tests/build-binary.test.ts",
|
|
41
42
|
"test:remaining": "bun test --path-ignore-patterns=**/build-binary.test.ts",
|
|
42
|
-
"
|
|
43
|
+
"typecheck": "mkdir -p .cache/tsc && bunx tsc --noEmit --pretty false --incremental --tsBuildInfoFile .cache/tsc/build.tsbuildinfo",
|
|
44
|
+
"tsc": "bun run typecheck"
|
|
43
45
|
},
|
|
44
46
|
"dependencies": {
|
|
45
47
|
"@simplewebauthn/browser": "13.3.0",
|
|
@@ -41,7 +41,10 @@ function canUseSpaFallback(req: Request): boolean {
|
|
|
41
41
|
}
|
|
42
42
|
|
|
43
43
|
export function createWebAppServer<TEvent = unknown>(input: WebAppServerConfig<TEvent>): WebAppServer<TEvent> {
|
|
44
|
-
const config = readRuntimeConfig({ appName: input.appName, envPrefix: input.envPrefix });
|
|
44
|
+
const config = input.runtimeConfig ?? readRuntimeConfig({ appName: input.appName, envPrefix: input.envPrefix });
|
|
45
|
+
if (config.appName !== input.appName || config.envPrefix !== input.envPrefix) {
|
|
46
|
+
throw new Error("runtimeConfig appName and envPrefix must match the createWebAppServer inputs");
|
|
47
|
+
}
|
|
45
48
|
const store = input.store ?? sqliteWebAppStore({ dataDir: config.dataDir });
|
|
46
49
|
store.initialize();
|
|
47
50
|
const savedLogLevel = store.getLogLevelPreference();
|
package/src/web/theme.tsx
CHANGED
|
@@ -43,10 +43,10 @@ function readSystemTheme(): ResolvedTheme {
|
|
|
43
43
|
}
|
|
44
44
|
|
|
45
45
|
function parseThemeResponse(value: unknown): ThemePreference {
|
|
46
|
-
if (!isRecord(value) || !isThemePreference(value
|
|
46
|
+
if (!isRecord(value) || !isThemePreference(value["theme"])) {
|
|
47
47
|
throw new Error("Theme preference response was invalid.");
|
|
48
48
|
}
|
|
49
|
-
return value
|
|
49
|
+
return value["theme"];
|
|
50
50
|
}
|
|
51
51
|
|
|
52
52
|
function toError(value: unknown): Error {
|
|
@@ -97,8 +97,8 @@ export function ThemeProvider({ userId, children }: { userId?: string; children:
|
|
|
97
97
|
const root = document.documentElement;
|
|
98
98
|
root.classList.toggle("dark", resolvedTheme === "dark");
|
|
99
99
|
root.style.colorScheme = resolvedTheme;
|
|
100
|
-
root.dataset
|
|
101
|
-
root.dataset
|
|
100
|
+
root.dataset["theme"] = preference;
|
|
101
|
+
root.dataset["resolvedTheme"] = resolvedTheme;
|
|
102
102
|
window.localStorage.setItem(THEME_STORAGE_KEY, preference);
|
|
103
103
|
}, [preference, resolvedTheme]);
|
|
104
104
|
|
|
@@ -21,11 +21,11 @@ function isLogLevelName(value: unknown): boolean {
|
|
|
21
21
|
|
|
22
22
|
function isCurrentUser(value: unknown): boolean {
|
|
23
23
|
return isRecord(value)
|
|
24
|
-
&& typeof value
|
|
25
|
-
&& typeof value
|
|
26
|
-
&& (value
|
|
27
|
-
&& typeof value
|
|
28
|
-
&& typeof value
|
|
24
|
+
&& typeof value["id"] === "string"
|
|
25
|
+
&& typeof value["username"] === "string"
|
|
26
|
+
&& (value["role"] === "owner" || value["role"] === "admin" || value["role"] === "user")
|
|
27
|
+
&& typeof value["isOwner"] === "boolean"
|
|
28
|
+
&& typeof value["isAdmin"] === "boolean";
|
|
29
29
|
}
|
|
30
30
|
|
|
31
31
|
function hasBooleanFields(value: unknown, fields: readonly string[]): boolean {
|
|
@@ -34,10 +34,10 @@ function hasBooleanFields(value: unknown, fields: readonly string[]): boolean {
|
|
|
34
34
|
|
|
35
35
|
function isWebAppConfigResponse(value: unknown): value is WebAppConfigResponse {
|
|
36
36
|
return isRecord(value)
|
|
37
|
-
&& typeof value
|
|
38
|
-
&& typeof value
|
|
39
|
-
&& (value
|
|
40
|
-
&& hasBooleanFields(value
|
|
37
|
+
&& typeof value["appName"] === "string"
|
|
38
|
+
&& typeof value["version"] === "string"
|
|
39
|
+
&& (value["currentUser"] === undefined || isCurrentUser(value["currentUser"]))
|
|
40
|
+
&& hasBooleanFields(value["passkeyAuth"], [
|
|
41
41
|
"enabled",
|
|
42
42
|
"passkeyConfigured",
|
|
43
43
|
"passkeyDisabled",
|
|
@@ -46,12 +46,12 @@ function isWebAppConfigResponse(value: unknown): value is WebAppConfigResponse {
|
|
|
46
46
|
"bootstrapRequired",
|
|
47
47
|
"ownerPasskeySetupRequired",
|
|
48
48
|
])
|
|
49
|
-
&& hasBooleanFields(value
|
|
50
|
-
&& isRecord(value
|
|
51
|
-
&& isLogLevelName(value
|
|
52
|
-
&& typeof value
|
|
53
|
-
&& hasBooleanFields(value
|
|
54
|
-
&& hasBooleanFields(value
|
|
49
|
+
&& hasBooleanFields(value["userManagement"], ["enabled", "canManageUsers"])
|
|
50
|
+
&& isRecord(value["logLevel"])
|
|
51
|
+
&& isLogLevelName(value["logLevel"]["level"])
|
|
52
|
+
&& typeof value["logLevel"]["fromEnv"] === "boolean"
|
|
53
|
+
&& hasBooleanFields(value["deviceAuth"], ["enabled"])
|
|
54
|
+
&& hasBooleanFields(value["apiKeys"], ["enabled"]);
|
|
55
55
|
}
|
|
56
56
|
|
|
57
57
|
export function parseWebAppConfigResponse(value: unknown): WebAppConfigResponse {
|