@jgb-solutions/kit 0.2.3 → 0.4.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.
Files changed (37) hide show
  1. package/CHANGELOG.md +10 -0
  2. package/README.md +163 -2
  3. package/dist/admin/blocks/ActivityFeed.d.ts +20 -0
  4. package/dist/admin/blocks/BarChart.d.ts +26 -0
  5. package/dist/admin/blocks/ChartPoints.d.ts +24 -0
  6. package/dist/admin/blocks/DataTable.d.ts +58 -0
  7. package/dist/admin/blocks/Filters.d.ts +12 -0
  8. package/dist/admin/blocks/Hero.d.ts +22 -0
  9. package/dist/admin/blocks/LineChart.d.ts +24 -0
  10. package/dist/admin/blocks/Pagination.d.ts +30 -0
  11. package/dist/admin/blocks/Segmented.d.ts +15 -0
  12. package/dist/admin/blocks/Stat.d.ts +35 -0
  13. package/dist/admin/blocks/Status.d.ts +22 -0
  14. package/dist/admin/blocks/Tile.d.ts +20 -0
  15. package/dist/admin/blocks/sorting.d.ts +29 -0
  16. package/dist/admin/contrast.d.ts +12 -0
  17. package/dist/admin/forms/ConfirmDialog.d.ts +25 -0
  18. package/dist/admin/forms/Field.d.ts +48 -0
  19. package/dist/admin/forms/People.d.ts +28 -0
  20. package/dist/admin/forms/Toast.d.ts +17 -0
  21. package/dist/admin/icons.d.ts +31 -0
  22. package/dist/admin/index.d.ts +33 -0
  23. package/dist/admin/shell/AdminShell.d.ts +31 -0
  24. package/dist/admin/shell/Dock.d.ts +8 -0
  25. package/dist/admin/shell/PageHeader.d.ts +19 -0
  26. package/dist/admin/shell/Sidebar.d.ts +27 -0
  27. package/dist/admin/shell/ThemeToggle.d.ts +10 -0
  28. package/dist/admin/shell/nav.d.ts +33 -0
  29. package/dist/admin/styles.css +1677 -0
  30. package/dist/admin/theme.d.ts +30 -0
  31. package/dist/admin/ui/Avatar.d.ts +14 -0
  32. package/dist/admin/ui/Button.d.ts +19 -0
  33. package/dist/admin.d.ts +2 -0
  34. package/dist/admin.js +2168 -0
  35. package/dist/i18n.d.ts +104 -0
  36. package/dist/i18n.js +218 -0
  37. package/package.json +40 -7
package/dist/i18n.d.ts ADDED
@@ -0,0 +1,104 @@
1
+ /**
2
+ * kit/i18n: four languages, one set of rules, for websites and Expo apps alike.
3
+ * No imports and no runtime APIs beyond `Intl`, so it runs in browsers, Workers, Bun and Hermes.
4
+ *
5
+ * - Locales and paths: English has no prefix, the others live under /fr, /es and /ht.
6
+ * - Negotiation: from an Accept-Language header, to offer (never force) a language.
7
+ * - Messages: nested catalogs, `{name}` placeholders, plurals, typed keys and typed params.
8
+ */
9
+ export declare const LOCALES: readonly ["en", "fr", "es", "ht"];
10
+ export type Locale = (typeof LOCALES)[number];
11
+ export declare const DEFAULT_LOCALE: Locale;
12
+ /** Each language in its own words. */
13
+ export declare const LOCALE_NAMES: Record<Locale, string>;
14
+ /** Name of the cookie that remembers a visitor's choice. */
15
+ export declare const LOCALE_COOKIE = "locale";
16
+ export declare function isLocale(value: unknown): value is Locale;
17
+ /** The language of a path: its first segment when that is /fr, /es or /ht, else English. */
18
+ export declare function localeFromPath(path: string): Locale;
19
+ /** The path without its /fr, /es or /ht prefix (query and hash kept). Always starts with "/". */
20
+ export declare function stripLocale(path: string): string;
21
+ /** The path in another language: any prefix is replaced; English has none. Query and hash kept. */
22
+ export declare function localizedPath(path: string, locale: Locale): string;
23
+ /** hreflang alternates for a page's <head>: one per language, then x-default (English). */
24
+ export declare function alternates(path: string, origin: string): {
25
+ hreflang: string;
26
+ href: string;
27
+ }[];
28
+ /** The supported language an Accept-Language header prefers (q-values; ties keep header order). */
29
+ export declare function negotiate(acceptLanguage: string | null | undefined): Locale;
30
+ /**
31
+ * The language a "view this page in X?" bar should offer, or null for no bar: the visitor already
32
+ * chose (the cookie holds a valid locale), or their browser's language is the page's.
33
+ */
34
+ export declare function suggestLocale(opts: {
35
+ acceptLanguage: string | null | undefined;
36
+ current: Locale;
37
+ chosen: string | null | undefined;
38
+ }): Locale | null;
39
+ /** A plural leaf: `one` and `other` always, the other CLDR categories when the language needs them. */
40
+ export type PluralMessage = {
41
+ one: string;
42
+ other: string;
43
+ zero?: string;
44
+ two?: string;
45
+ few?: string;
46
+ many?: string;
47
+ };
48
+ /** A catalog: nested plain objects whose leaves are strings or plural objects. */
49
+ export type Messages = {
50
+ readonly [key: string]: string | PluralMessage | Messages;
51
+ };
52
+ /**
53
+ * The shape of a translation, from the English source: same keys, `string` leaves.
54
+ * Plural leaves become full `PluralMessage`s, so a language may add categories English lacks
55
+ * (French `many`). A missing key is a type error, and so is an extra key in an object literal
56
+ * (TypeScript's excess property check; see `defineCatalog`).
57
+ */
58
+ export type Catalog<M> = {
59
+ -readonly [K in keyof M]: M[K] extends string ? string : M[K] extends {
60
+ readonly other: string;
61
+ } ? PluralMessage : Catalog<M[K]>;
62
+ };
63
+ type Join<P extends string, K extends string> = P extends '' ? K : `${P}.${K}`;
64
+ /** The dotted paths to every leaf: 'home.title' | 'items.count'. */
65
+ export type MessageKey<M, P extends string = ''> = {
66
+ [K in keyof M & string]: M[K] extends string | {
67
+ readonly other: string;
68
+ } ? Join<P, K> : MessageKey<M[K], Join<P, K>>;
69
+ }[keyof M & string];
70
+ type PlaceholderNames<S extends string> = S extends `${string}{${infer N}}${infer Rest}` ? N | PlaceholderNames<Rest> : never;
71
+ /** The params a source string needs: each `{name}` required; none (undefined) when it has no placeholder. */
72
+ export type Params<S> = S extends string ? [PlaceholderNames<S>] extends [never] ? undefined : {
73
+ [N in PlaceholderNames<S>]: string | number;
74
+ } : S extends {
75
+ readonly other: string;
76
+ } ? {
77
+ count: number;
78
+ } & {
79
+ [N in Exclude<PlaceholderNames<Extract<S[keyof S], string>>, 'count'>]: string | number;
80
+ } : undefined;
81
+ type ValueAt<M, K extends string> = K extends `${infer Head}.${infer Tail}` ? Head extends keyof M ? ValueAt<M[Head], Tail> : never : K extends keyof M ? M[K] : never;
82
+ type TranslateArgs<M, K extends string> = undefined extends Params<ValueAt<M, K>> ? [params?: Params<ValueAt<M, K>>] : [params: Params<ValueAt<M, K>>];
83
+ /** `t(key, params?)`: the key must exist in the source catalog, and params are required exactly when it needs them. */
84
+ export type Translator<M> = <K extends MessageKey<M>>(key: K, ...args: TranslateArgs<M, K>) => string;
85
+ /**
86
+ * Types a translation against the English source and rejects extra keys:
87
+ * `export const fr = defineCatalog<typeof en>()({ … })`. A plain `const fr: Catalog<typeof en> = { … }`
88
+ * does the same for object literals; this helper just names the intent and keeps the typed result.
89
+ */
90
+ export declare function defineCatalog<M>(): (catalog: Catalog<M>) => Catalog<M>;
91
+ export declare function createTranslator<M extends Messages>(locale: Locale, messages: M, fallback?: M): Translator<M>;
92
+ /**
93
+ * A translation typed by its source: `createTranslator<typeof en>('fr', fr)` (a website sends only the
94
+ * active language's catalog, so there may be no English fallback in the browser), or
95
+ * `createTranslator('fr', fr, en)` with English as the fallback for missing or empty strings.
96
+ */
97
+ export declare function createTranslator<M extends Messages>(locale: Locale, messages: Catalog<NoInfer<M>>, fallback?: M): Translator<M>;
98
+ /**
99
+ * Problems in a translation, as readable strings naming the dotted key: missing keys, extra keys,
100
+ * empty strings, plural objects without `other`, and placeholder sets that differ from the source's
101
+ * (for plurals, per category: the source `other`'s placeholders, plus `count`). Empty list means fine.
102
+ */
103
+ export declare function checkCatalog(source: object, translation: object): string[];
104
+ export {};
package/dist/i18n.js ADDED
@@ -0,0 +1,218 @@
1
+ // src/i18n.ts
2
+ var LOCALES = ["en", "fr", "es", "ht"];
3
+ var DEFAULT_LOCALE = "en";
4
+ var LOCALE_NAMES = {
5
+ en: "English",
6
+ fr: "Français",
7
+ es: "Español",
8
+ ht: "Kreyòl ayisyen"
9
+ };
10
+ var LOCALE_COOKIE = "locale";
11
+ function isLocale(value) {
12
+ return typeof value === "string" && LOCALES.some((locale) => locale === value);
13
+ }
14
+ function prefixed(segment) {
15
+ return segment !== undefined && segment !== DEFAULT_LOCALE && isLocale(segment);
16
+ }
17
+ function splitPath(path) {
18
+ const cut = path.search(/[?#]/);
19
+ const pathname = cut === -1 ? path : path.slice(0, cut);
20
+ const rest = cut === -1 ? "" : path.slice(cut);
21
+ return { pathname: pathname.startsWith("/") ? pathname : `/${pathname}`, rest };
22
+ }
23
+ function firstSegment(pathname) {
24
+ return pathname.split("/")[1];
25
+ }
26
+ function localeFromPath(path) {
27
+ const segment = firstSegment(splitPath(path).pathname);
28
+ return prefixed(segment) ? segment : DEFAULT_LOCALE;
29
+ }
30
+ function stripLocale(path) {
31
+ const { pathname, rest } = splitPath(path);
32
+ const segment = firstSegment(pathname);
33
+ if (!prefixed(segment))
34
+ return pathname + rest;
35
+ return (pathname.slice(segment.length + 1) || "/") + rest;
36
+ }
37
+ function localizedPath(path, locale) {
38
+ const stripped = stripLocale(path);
39
+ if (locale === DEFAULT_LOCALE)
40
+ return stripped;
41
+ const { pathname, rest } = splitPath(stripped);
42
+ return `/${locale}${pathname === "/" ? "" : pathname}${rest}`;
43
+ }
44
+ function alternates(path, origin) {
45
+ const base = origin.replace(/\/+$/, "");
46
+ return [
47
+ ...LOCALES.map((locale) => ({ hreflang: locale, href: base + localizedPath(path, locale) })),
48
+ { hreflang: "x-default", href: base + localizedPath(path, DEFAULT_LOCALE) }
49
+ ];
50
+ }
51
+ var LANGUAGE_TAG = /^[A-Za-z]{1,8}(?:-[A-Za-z0-9]{1,8})*$/;
52
+ function negotiate(acceptLanguage) {
53
+ if (!acceptLanguage)
54
+ return DEFAULT_LOCALE;
55
+ let best = DEFAULT_LOCALE;
56
+ let bestQuality = 0;
57
+ for (const part of acceptLanguage.split(",")) {
58
+ const [tag = "", ...params] = part.split(";").map((piece) => piece.trim());
59
+ if (!LANGUAGE_TAG.test(tag))
60
+ continue;
61
+ let quality = 1;
62
+ let malformed = false;
63
+ for (const param of params) {
64
+ const match = /^q=(\d(?:\.\d{0,3})?)$/i.exec(param);
65
+ const value = match?.[1] === undefined ? Number.NaN : Number(match[1]);
66
+ if (Number.isNaN(value) || value > 1)
67
+ malformed = true;
68
+ else
69
+ quality = value;
70
+ }
71
+ if (malformed || quality <= 0)
72
+ continue;
73
+ const primary = tag.split("-")[0]?.toLowerCase();
74
+ if (isLocale(primary) && quality > bestQuality) {
75
+ best = primary;
76
+ bestQuality = quality;
77
+ }
78
+ }
79
+ return best;
80
+ }
81
+ function suggestLocale(opts) {
82
+ if (isLocale(opts.chosen))
83
+ return null;
84
+ const wanted = negotiate(opts.acceptLanguage);
85
+ return wanted === opts.current ? null : wanted;
86
+ }
87
+ var PLURAL_CATEGORIES = ["zero", "one", "two", "few", "many", "other"];
88
+ function defineCatalog() {
89
+ return (catalog) => catalog;
90
+ }
91
+ function isRecord(value) {
92
+ return typeof value === "object" && value !== null && !Array.isArray(value);
93
+ }
94
+ function isPlural(value) {
95
+ return isRecord(value) && typeof value.other === "string";
96
+ }
97
+ function leafAt(messages, key) {
98
+ let node = messages;
99
+ for (const part of key.split(".")) {
100
+ if (!isRecord(node))
101
+ return;
102
+ node = node[part];
103
+ }
104
+ if (typeof node === "string")
105
+ return node || undefined;
106
+ if (isPlural(node) && node.other)
107
+ return node;
108
+ return;
109
+ }
110
+ function createTranslator(locale, messages, fallback) {
111
+ const rules = new Intl.PluralRules(locale);
112
+ const numbers = new Intl.NumberFormat(locale);
113
+ const fill = (template, params, formatNumbers) => template.replace(/\{(\w+)\}/g, (placeholder, name) => {
114
+ const value = params?.[name];
115
+ if (value === undefined)
116
+ return placeholder;
117
+ return typeof value === "number" && formatNumbers ? numbers.format(value) : String(value);
118
+ });
119
+ return (key, params) => {
120
+ const leaf = leafAt(messages, key) ?? leafAt(fallback, key);
121
+ if (leaf === undefined)
122
+ return key;
123
+ if (typeof leaf === "string")
124
+ return fill(leaf, params, false);
125
+ const count = params?.count;
126
+ const category = typeof count === "number" ? rules.select(count) : "other";
127
+ const chosen = leaf[category];
128
+ return fill(typeof chosen === "string" && chosen ? chosen : leaf.other === undefined ? key : String(leaf.other), params, true);
129
+ };
130
+ }
131
+ var placeholdersOf = (text) => new Set([...text.matchAll(/\{(\w+)\}/g)].map((match) => match[1] ?? ""));
132
+ var list = (names) => names.size === 0 ? "none" : [...names].sort().map((name) => `{${name}}`).join(", ");
133
+ var sameSet = (a, b) => a.size === b.size && [...a].every((name) => b.has(name));
134
+ function describe(value) {
135
+ if (typeof value === "string")
136
+ return "a string";
137
+ if (isPlural(value))
138
+ return "a plural object";
139
+ if (isRecord(value))
140
+ return "a group of messages";
141
+ return typeof value;
142
+ }
143
+ function compare(source, translation, path, problems) {
144
+ const sourceIsGroup = isRecord(source) && !isPlural(source);
145
+ if (sourceIsGroup) {
146
+ if (!isRecord(translation) || isPlural(translation)) {
147
+ problems.push(`${path}: expected a group of messages, found ${describe(translation)}`);
148
+ return;
149
+ }
150
+ for (const key of Object.keys(source)) {
151
+ const child = path ? `${path}.${key}` : key;
152
+ if (!(key in translation))
153
+ problems.push(`${child}: missing key`);
154
+ else
155
+ compare(source[key], translation[key], child, problems);
156
+ }
157
+ for (const key of Object.keys(translation))
158
+ if (!(key in source))
159
+ problems.push(`${path ? `${path}.${key}` : key}: extra key`);
160
+ return;
161
+ }
162
+ if (typeof source === "string") {
163
+ if (typeof translation !== "string")
164
+ return void problems.push(`${path}: expected a string, found ${describe(translation)}`);
165
+ if (translation.trim() === "")
166
+ return void problems.push(`${path}: empty string`);
167
+ const wanted = placeholdersOf(source);
168
+ const found = placeholdersOf(translation);
169
+ if (!sameSet(wanted, found))
170
+ problems.push(`${path}: placeholders differ (source ${list(wanted)}, translation ${list(found)})`);
171
+ return;
172
+ }
173
+ if (isPlural(source)) {
174
+ if (!isRecord(translation) || typeof translation.other !== "string") {
175
+ problems.push(`${path}: plural object must have an "other" string, found ${describe(translation)}`);
176
+ return;
177
+ }
178
+ const required = placeholdersOf(source.other);
179
+ required.delete("count");
180
+ const allowed = new Set([...required, "count"]);
181
+ for (const [category, text] of Object.entries(translation)) {
182
+ const at = `${path}.${category}`;
183
+ if (!PLURAL_CATEGORIES.some((name) => name === category))
184
+ problems.push(`${at}: unknown plural category`);
185
+ else if (typeof text !== "string" || text.trim() === "")
186
+ problems.push(`${at}: empty string`);
187
+ else {
188
+ const found = placeholdersOf(text);
189
+ const missing = [...required].filter((name) => !found.has(name));
190
+ const unknown = [...found].filter((name) => !allowed.has(name));
191
+ if (missing.length || unknown.length)
192
+ problems.push(`${at}: placeholders differ (source allows ${list(allowed)}, translation has ${list(found)})`);
193
+ }
194
+ }
195
+ return;
196
+ }
197
+ }
198
+ function checkCatalog(source, translation) {
199
+ const problems = [];
200
+ compare(source, translation, "", problems);
201
+ return problems;
202
+ }
203
+ export {
204
+ DEFAULT_LOCALE,
205
+ LOCALES,
206
+ LOCALE_COOKIE,
207
+ LOCALE_NAMES,
208
+ alternates,
209
+ checkCatalog,
210
+ createTranslator,
211
+ defineCatalog,
212
+ isLocale,
213
+ localeFromPath,
214
+ localizedPath,
215
+ negotiate,
216
+ stripLocale,
217
+ suggestLocale
218
+ };
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@jgb-solutions/kit",
3
- "version": "0.2.3",
4
- "description": "Generic plumbing for web apps on Bun and Cloudflare Workers: small, typed, tree-shakable modules (Cloudflare Access, Better Auth).",
3
+ "version": "0.4.0",
4
+ "description": "One package for JGB Solutions apps, in tree-shakable namespaces: Cloudflare Access, Better Auth (kit/access, kit/auth\u2026) and the admin panel kit (kit/admin, React 19). An app bundles only the subpaths it imports.",
5
5
  "license": "MIT",
6
6
  "author": "JGB Solutions",
7
7
  "repository": {
@@ -19,10 +19,17 @@
19
19
  "better-auth",
20
20
  "passkeys",
21
21
  "orpc",
22
- "expo"
22
+ "expo",
23
+ "admin",
24
+ "dashboard",
25
+ "react",
26
+ "design-system",
27
+ "accessible"
23
28
  ],
24
29
  "type": "module",
25
- "sideEffects": false,
30
+ "sideEffects": [
31
+ "**/*.css"
32
+ ],
26
33
  "files": [
27
34
  "dist",
28
35
  "README.md",
@@ -54,6 +61,15 @@
54
61
  "types": "./dist/auth/expo.d.ts",
55
62
  "import": "./dist/auth/expo.js"
56
63
  },
64
+ "./i18n": {
65
+ "types": "./dist/i18n.d.ts",
66
+ "import": "./dist/i18n.js"
67
+ },
68
+ "./admin": {
69
+ "types": "./dist/admin.d.ts",
70
+ "import": "./dist/admin.js"
71
+ },
72
+ "./admin/styles.css": "./dist/admin/styles.css",
57
73
  "./package.json": "./package.json"
58
74
  },
59
75
  "engines": {
@@ -67,12 +83,15 @@
67
83
  "scripts": {
68
84
  "build": "bun scripts/build.ts",
69
85
  "typecheck": "bun check",
70
- "test": "bun test",
86
+ "test": "bun test src",
71
87
  "format": "bun fmt .",
72
88
  "format:check": "bun fmt --check .",
73
89
  "check:package": "bun scripts/check-package.ts",
74
90
  "prepublishOnly": "bun run build && bun run check:package",
75
- "check:install": "bun scripts/check-install.ts"
91
+ "check:install": "bun scripts/check-install.ts",
92
+ "test:e2e": "bun --bun playwright test",
93
+ "playground": "bun playground/serve.ts",
94
+ "screenshots": "bun --bun playwright test e2e/screenshots.spec.ts"
76
95
  },
77
96
  "dependencies": {
78
97
  "jose": "6.2.18"
@@ -80,7 +99,9 @@
80
99
  "peerDependencies": {
81
100
  "better-auth": ">=1.7.22 <2",
82
101
  "@better-auth/passkey": ">=1.7.22 <2",
83
- "@orpc/server": ">=2.0.0-beta.52 <3"
102
+ "@orpc/server": ">=2.0.0-beta.52 <3",
103
+ "react": ">=19 <20",
104
+ "react-dom": ">=19 <20"
84
105
  },
85
106
  "peerDependenciesMeta": {
86
107
  "better-auth": {
@@ -91,13 +112,25 @@
91
112
  },
92
113
  "@orpc/server": {
93
114
  "optional": true
115
+ },
116
+ "react": {
117
+ "optional": true
118
+ },
119
+ "react-dom": {
120
+ "optional": true
94
121
  }
95
122
  },
96
123
  "devDependencies": {
124
+ "@axe-core/playwright": "4.14.0",
97
125
  "@better-auth/passkey": "1.7.27",
98
126
  "@orpc/server": "2.0.0-beta.52",
127
+ "@playwright/test": "1.64.1",
99
128
  "@types/bun": "1.4.5",
129
+ "@types/react": "19.3.0",
130
+ "@types/react-dom": "19.3.0",
100
131
  "better-auth": "1.7.27",
132
+ "react": "19.3.1",
133
+ "react-dom": "19.3.1",
101
134
  "typescript": "6.0.3"
102
135
  }
103
136
  }