weifuwu 0.17.1 → 0.17.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/README.md +6 -1
- package/dist/index.js +17 -5
- package/dist/tsx-context.d.ts +1 -0
- package/dist/types.d.ts +1 -0
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1173,11 +1173,13 @@ app.use(preferences({
|
|
|
1173
1173
|
}))
|
|
1174
1174
|
|
|
1175
1175
|
// In handlers: ctx.t('greeting') → "Hello"
|
|
1176
|
+
// ctx.t('tools.uppercase.title') → "Uppercase" (nested key)
|
|
1176
1177
|
// ctx.locale → "en"
|
|
1177
1178
|
// ctx.theme → "light"
|
|
1178
1179
|
// ctx.prefs → { locale: 'en', theme: 'light' }
|
|
1179
1180
|
// ctx.setPref('locale', 'zh') → 302 + cookie
|
|
1180
1181
|
// ctx.setPref('flash', '{"type":"success","message":"Done"}') → flash message
|
|
1182
|
+
// ctx.env → { WEIFUWU_PUBLIC_API_URL: '...' } (public env vars)
|
|
1181
1183
|
|
|
1182
1184
|
// In tsx components:
|
|
1183
1185
|
const { t, locale, theme } = useCtx()
|
|
@@ -1187,6 +1189,9 @@ Locale detection priority: cookie → `Accept-Language` → default.
|
|
|
1187
1189
|
Theme detection: cookie → default (`'system'`).
|
|
1188
1190
|
Flash messages: set via `ctx.setPref('flash', ...)` → auto-read from cookie → cleared after rendering.
|
|
1189
1191
|
|
|
1192
|
+
`ctx.t()` supports dot-path nested keys: `t('tools.uppercase.title')` traverses the JSON structure.
|
|
1193
|
+
`ctx.env` exposes `WEIFUWU_PUBLIC_*` environment variables on both server and client (via `useCtx().env`).
|
|
1194
|
+
|
|
1190
1195
|
---
|
|
1191
1196
|
|
|
1192
1197
|
## Email
|
|
@@ -1234,7 +1239,7 @@ app.get('/stream', (req, ctx) => createSSEStream(events()))
|
|
|
1234
1239
|
|
|
1235
1240
|
| Hook / Component | Description |
|
|
1236
1241
|
|-----------------|-------------|
|
|
1237
|
-
| `useCtx()` | Unified context — `{ prefs, locale, theme, t, params, query }` (requires `preferences` middleware) |
|
|
1242
|
+
| `useCtx()` | Unified context — `{ prefs, locale, theme, t, params, query, env }` (requires `preferences` middleware) |
|
|
1238
1243
|
| `createStore(initial)` | Zustand-compatible shared state — `getState`, `setState`, `subscribe` |
|
|
1239
1244
|
| `useData(url, opts?)` | SWR-style data fetching — cache, dedup, mutate, fallback |
|
|
1240
1245
|
| `useQueryState(key, default)` | URL query param sync — `?page=1` via `useSyncExternalStore` |
|
package/dist/index.js
CHANGED
|
@@ -889,7 +889,8 @@ var TsxInstance = class {
|
|
|
889
889
|
prefs: ctx.prefs,
|
|
890
890
|
locale: ctx.locale,
|
|
891
891
|
theme: ctx.theme,
|
|
892
|
-
t: ctx.t
|
|
892
|
+
t: ctx.t,
|
|
893
|
+
env: ctx.env
|
|
893
894
|
}
|
|
894
895
|
}, createElement(NfComponent, { params: ctx.params, query: ctx.query }));
|
|
895
896
|
for (let i = rootLayouts.length - 1; i >= 0; i--) {
|
|
@@ -1063,7 +1064,8 @@ ${src}`;
|
|
|
1063
1064
|
prefs: ctx.prefs,
|
|
1064
1065
|
locale: ctx.locale,
|
|
1065
1066
|
theme: ctx.theme,
|
|
1066
|
-
t: ctx.t
|
|
1067
|
+
t: ctx.t,
|
|
1068
|
+
env: ctx.env
|
|
1067
1069
|
}
|
|
1068
1070
|
}, createElement(Component, allProps))
|
|
1069
1071
|
);
|
|
@@ -1349,6 +1351,15 @@ function buildHeadPayload(opts) {
|
|
|
1349
1351
|
if (ctx.prefs) ctxData.prefs = ctx.prefs;
|
|
1350
1352
|
if (ctx.locale) ctxData.locale = ctx.locale;
|
|
1351
1353
|
if (ctx.theme) ctxData.theme = ctx.theme;
|
|
1354
|
+
const publicEnv = {};
|
|
1355
|
+
for (const key of Object.keys(process.env)) {
|
|
1356
|
+
if (key.startsWith("WEIFUWU_PUBLIC_")) {
|
|
1357
|
+
publicEnv[key] = process.env[key];
|
|
1358
|
+
}
|
|
1359
|
+
}
|
|
1360
|
+
if (Object.keys(publicEnv).length > 0) {
|
|
1361
|
+
ctxData.env = publicEnv;
|
|
1362
|
+
}
|
|
1352
1363
|
result += `<script>window.__WEIFUWU_CTX=${JSON.stringify(ctxData)}</script>
|
|
1353
1364
|
`;
|
|
1354
1365
|
return result;
|
|
@@ -6780,9 +6791,10 @@ var defaults = {
|
|
|
6780
6791
|
theme: { default: "system", cookie: "theme" }
|
|
6781
6792
|
};
|
|
6782
6793
|
function translate(msgs, key, params) {
|
|
6783
|
-
const msg =
|
|
6784
|
-
if (
|
|
6785
|
-
|
|
6794
|
+
const msg = key.split(".").reduce((o, k) => o?.[k], msgs);
|
|
6795
|
+
if (msg === void 0 || msg === null) return key;
|
|
6796
|
+
if (!params) return String(msg);
|
|
6797
|
+
let result = String(msg);
|
|
6786
6798
|
for (const [k, v] of Object.entries(params)) {
|
|
6787
6799
|
result = result.replace(`{${k}}`, v);
|
|
6788
6800
|
}
|
package/dist/tsx-context.d.ts
CHANGED
package/dist/types.d.ts
CHANGED
|
@@ -10,6 +10,7 @@ export interface Context {
|
|
|
10
10
|
prefs?: Record<string, string>;
|
|
11
11
|
theme?: string;
|
|
12
12
|
setPref?: (name: string, value: string) => Response;
|
|
13
|
+
env?: Record<string, string>;
|
|
13
14
|
}
|
|
14
15
|
export type Handler = (req: Request, ctx: Context) => Response | Promise<Response>;
|
|
15
16
|
export type Middleware = (req: Request, ctx: Context, next: Handler) => Response | Promise<Response>;
|