@rpcbase/db 0.95.0 → 0.97.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.
package/README.md CHANGED
@@ -15,14 +15,19 @@ const ZUser = z.object({
15
15
 
16
16
  ### `LocalizedString` / `zI18nString()`
17
17
 
18
- `zI18nString()` (alias of `zLocalizedString()`) describes a localized string as an object keyed by *language codes* (BCP47):
18
+ `zI18nString()` (alias of `zLocalizedString()`) describes a localized string as an object keyed by *language codes* (BCP47), where each locale stores the translated value and metadata:
19
19
 
20
20
  ```ts
21
21
  import { z, zI18nString } from "@rpcbase/db"
22
22
 
23
23
  const ZPost = z.object({
24
- title: zI18nString(), // { "en": "...", "fr": "...", "fr-FR": "..." }
24
+ title: zI18nString(),
25
25
  })
26
+
27
+ const title = {
28
+ en: { value: "Hello", updatedAt: new Date(), autoTranslated: false },
29
+ fr: { value: "Bonjour", updatedAt: new Date() },
30
+ }
26
31
  ```
27
32
 
28
33
  ### Mongoose re-export
@@ -77,7 +82,7 @@ Notes:
77
82
 
78
83
  ### Mongoose localized fallback (optional)
79
84
 
80
- When storing localized strings as plain objects, `mongooseLocalizedStringField()` adds a getter that returns a proxy with fallback behavior:
85
+ When storing localized strings as plain objects, `mongooseLocalizedStringField()` adds a getter that returns a proxy with fallback behavior. Fallback access returns the full locale entry:
81
86
 
82
87
  ```ts
83
88
  import { Schema, mongooseLocalizedStringField } from "@rpcbase/db"
@@ -85,6 +90,9 @@ import { Schema, mongooseLocalizedStringField } from "@rpcbase/db"
85
90
  const PostSchema = new Schema({
86
91
  title: mongooseLocalizedStringField(),
87
92
  })
93
+
94
+ const entry = doc.title.get("fr-CA")
95
+ const text = resolveLocalizedString(doc.title, "fr-CA")
88
96
  ```
89
97
 
90
98
  ### Search helpers
@@ -38,6 +38,9 @@ function buildLocaleFallbackChain(locale, fallbacks) {
38
38
  return output;
39
39
  }
40
40
  function resolveLocalizedString(value, locale, options) {
41
+ return resolveLocalizedStringEntry(value, locale, options)?.value;
42
+ }
43
+ function resolveLocalizedStringEntry(value, locale, options) {
41
44
  if (!value) return void 0;
42
45
  const chain = buildLocaleFallbackChain(locale, options?.fallbacks);
43
46
  if (chain.length === 0) return void 0;
@@ -59,17 +62,17 @@ function withLocalizedStringFallback(value) {
59
62
  }
60
63
  function withFallbackRecord(record) {
61
64
  const getExact = (key) => {
62
- const current = record[key];
63
- return typeof current === "string" ? current : void 0;
65
+ if (!hasExact(key)) return void 0;
66
+ return record[key];
64
67
  };
65
68
  const hasExact = (key) => Object.prototype.hasOwnProperty.call(record, key);
66
69
  return new Proxy(record, {
67
70
  get(target, prop) {
68
71
  if (prop === "getExact") return getExact;
69
72
  if (prop === "hasExact") return hasExact;
70
- if (prop === "get") return (key) => resolveLocalizedString(target, key);
73
+ if (prop === "get") return (key) => resolveLocalizedStringEntry(target, key);
71
74
  if (typeof prop === "string" && !(prop in target)) {
72
- return resolveLocalizedString(target, prop);
75
+ return resolveLocalizedStringEntry(target, prop);
73
76
  }
74
77
  return Reflect.get(target, prop);
75
78
  },
@@ -79,9 +82,15 @@ function withFallbackRecord(record) {
79
82
  });
80
83
  }
81
84
  const zLocalizedString = () => {
82
- const schema = z$1.record(z$1.string().regex(LANGUAGE_CODE_REGEX, {
85
+ const languageCodeSchema = z$1.string().regex(LANGUAGE_CODE_REGEX, {
83
86
  message: "Expected a language code (BCP 47, e.g. en or fr-FR)."
84
- }), z$1.string());
87
+ });
88
+ const entrySchema = z$1.object({
89
+ value: z$1.string(),
90
+ updatedAt: z$1.date(),
91
+ autoTranslated: z$1.boolean().optional()
92
+ }).passthrough();
93
+ const schema = z$1.record(languageCodeSchema, entrySchema);
85
94
  return schema;
86
95
  };
87
96
  const zI18nString = zLocalizedString;
@@ -130,4 +139,4 @@ export {
130
139
  withLocalizedStringFallback as w,
131
140
  z
132
141
  };
133
- //# sourceMappingURL=index-BQeL_LHp.js.map
142
+ //# sourceMappingURL=index-BNcI2Uw0.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index-BNcI2Uw0.js","sources":["../src/zod/localizedString.ts","../src/zod/e164Phone.ts","../src/zod/extension.ts","../src/zod/index.ts"],"sourcesContent":["import { z } from \"zod\"\n\n\nexport type LanguageCode = string\n\nexport type LocalizedStringEntry<TExtra extends Record<string, unknown> = Record<string, unknown>> = {\n value: string\n updatedAt: Date\n autoTranslated?: boolean\n} & TExtra\n\nexport type LocalizedString<TExtra extends Record<string, unknown> = Record<string, unknown>> = Record<\n LanguageCode,\n LocalizedStringEntry<TExtra>\n>\n\nexport type I18nStringRecord = LocalizedString\n\nexport type I18nString = LocalizedString\n\nexport type LocalizedStringWithFallback = LocalizedString & {\n get: (key: string) => LocalizedStringEntry | undefined\n getExact: (key: string) => LocalizedStringEntry | undefined\n hasExact: (key: string) => boolean\n}\n\nexport const LANGUAGE_CODE_REGEX = /^[a-z]{2,3}(?:-[A-Za-z0-9]{2,8})*$/\n\nconst LOCALIZED_STRING_PROXY_CACHE = new WeakMap<object, unknown>()\n\nfunction normalizeLocale(locale: string): string {\n const trimmed = locale.trim()\n if (!trimmed) return \"\"\n const getCanonicalLocales = (Intl as typeof Intl & {\n getCanonicalLocales?: (locales: string | readonly string[]) => string[]\n }).getCanonicalLocales\n if (typeof getCanonicalLocales !== \"function\") return trimmed\n try {\n return getCanonicalLocales(trimmed)[0] ?? trimmed\n } catch {\n return trimmed\n }\n}\n\nexport function buildLocaleFallbackChain(locale: string, fallbacks?: string | string[]): string[] {\n const base = locale.trim()\n const canonical = normalizeLocale(base)\n const extra = typeof fallbacks === \"string\" ? [fallbacks] : (fallbacks ?? [])\n\n const output: string[] = []\n const seen = new Set<string>()\n\n const push = (value: string) => {\n if (!value) return\n if (seen.has(value)) return\n seen.add(value)\n output.push(value)\n }\n\n const addChain = (value: string) => {\n if (!value) return\n const parts = value.split(\"-\").filter(Boolean)\n while (parts.length > 0) {\n push(parts.join(\"-\"))\n parts.pop()\n }\n }\n\n addChain(base)\n addChain(canonical)\n for (const fallback of extra) addChain(normalizeLocale(fallback))\n\n return output\n}\n\nexport function resolveLocalizedString(\n value: LocalizedString | null | undefined,\n locale: string,\n options?: { fallbacks?: string | string[] }\n): string | undefined {\n return resolveLocalizedStringEntry(value, locale, options)?.value\n}\n\nfunction resolveLocalizedStringEntry(\n value: LocalizedString | null | undefined,\n locale: string,\n options?: { fallbacks?: string | string[] }\n): LocalizedStringEntry | undefined {\n if (!value) return undefined\n const chain = buildLocaleFallbackChain(locale, options?.fallbacks)\n if (chain.length === 0) return undefined\n\n const record = value\n for (const key of chain) {\n if (!Object.prototype.hasOwnProperty.call(record, key)) continue\n return record[key] as LocalizedStringEntry\n }\n return undefined\n}\n\nexport function withLocalizedStringFallback<T>(value: T): T {\n if (!value || typeof value !== \"object\" || Array.isArray(value)) return value\n if (value instanceof Map) return value\n const cached = LOCALIZED_STRING_PROXY_CACHE.get(value)\n if (cached) return cached as T\n\n const proxy = withFallbackRecord(value as Record<string, unknown>)\n LOCALIZED_STRING_PROXY_CACHE.set(value, proxy)\n return proxy as T\n}\n\nfunction withFallbackRecord(record: Record<string, unknown>): LocalizedStringWithFallback {\n const getExact = (key: string): LocalizedStringEntry | undefined => {\n if (!hasExact(key)) return undefined\n return record[key] as LocalizedStringEntry\n }\n const hasExact = (key: string) => Object.prototype.hasOwnProperty.call(record, key)\n\n return new Proxy(record, {\n get(target, prop) {\n if (prop === \"getExact\") return getExact\n if (prop === \"hasExact\") return hasExact\n if (prop === \"get\") return (key: string) => resolveLocalizedStringEntry(target as LocalizedString, key)\n if (typeof prop === \"string\" && !(prop in target)) {\n return resolveLocalizedStringEntry(target as LocalizedString, prop)\n }\n\n return Reflect.get(target, prop)\n },\n set(target, prop, next) {\n return Reflect.set(target, prop, next)\n },\n }) as LocalizedStringWithFallback\n}\n\nexport const zLocalizedString = () => {\n const languageCodeSchema = z\n .string()\n .regex(LANGUAGE_CODE_REGEX, { message: \"Expected a language code (BCP 47, e.g. en or fr-FR).\" })\n const entrySchema = z.object({\n value: z.string(),\n updatedAt: z.date(),\n autoTranslated: z.boolean().optional(),\n }).passthrough()\n const schema = z.record(\n languageCodeSchema,\n entrySchema,\n )\n\n return schema\n}\n\nexport const zI18nString = zLocalizedString\n","import { z } from \"zod\"\n\n\nexport const E164_PHONE_REGEX = /^\\+[1-9]\\d{1,14}$/\nexport const E164_PHONE_OR_EMPTY_REGEX = /^(?:\\+[1-9]\\d{1,14})?$/\n\nexport type E164PhoneOptions = {\n allowEmpty?: boolean\n}\n\nexport const makeZE164Phone = (zod: typeof z, options?: E164PhoneOptions) => {\n const allowEmpty = options?.allowEmpty ?? false\n return zod.string().trim().regex(allowEmpty ? E164_PHONE_OR_EMPTY_REGEX : E164_PHONE_REGEX, {\n message: allowEmpty\n ? \"Expected an empty string or a phone number in E.164 format (e.g. +33608707197).\"\n : \"Expected a phone number in E.164 format (e.g. +33608707197).\",\n })\n}\n\nexport const zE164Phone = (options?: E164PhoneOptions) => makeZE164Phone(z, options)\n","import { z } from \"zod\"\n\nimport { type E164PhoneOptions, makeZE164Phone } from \"./e164Phone\"\n\n\ntype RpcbaseZodExtension = typeof z & {\n e164Phone?: (options?: E164PhoneOptions) => z.ZodString\n}\n\ntype ExtendableZodPrototype = {\n unique?: (this: unknown, arg?: boolean) => unknown\n sparse?: (this: unknown, arg?: boolean) => unknown\n}\n\ndeclare module \"zod\" {\n interface ZodString {\n unique(arg?: boolean): this\n sparse(arg?: boolean): this\n }\n\n interface ZodNumber {\n unique(arg?: boolean): this\n sparse(arg?: boolean): this\n }\n\n interface ZodDate {\n unique(arg?: boolean): this\n sparse(arg?: boolean): this\n }\n\n}\n\nlet zodPrototypesExtended = false\n\nexport function extendZod(zod: typeof z) {\n const zodWithExtension = zod as RpcbaseZodExtension\n if (Object.isExtensible(zodWithExtension) && typeof zodWithExtension.e164Phone !== \"function\") {\n zodWithExtension.e164Phone = (options?: E164PhoneOptions) => makeZE164Phone(zod, options)\n }\n\n if (zodPrototypesExtended) return\n zodPrototypesExtended = true\n\n const supported = [zod.ZodString, zod.ZodNumber, zod.ZodDate] as const\n for (const type of supported) {\n const proto = type?.prototype as ExtendableZodPrototype | undefined\n if (!proto) continue\n\n proto.unique = function unique(this: unknown, _flag = true) {\n return this\n }\n\n proto.sparse = function sparse(this: unknown, _flag = true) {\n return this\n }\n }\n}\n","import { z as baseZ, type ZodError, type ZodString, type ZodType } from \"zod\"\n\nimport { extendZod } from \"./extension\"\n\n\nexport * from \"./e164Phone\"\nexport * from \"./localizedString\"\n\nexport type RpcbaseZod = typeof baseZ & {\n e164Phone: (options?: import(\"./e164Phone\").E164PhoneOptions) => ZodString\n}\n\nexport const z = Object.create(baseZ) as RpcbaseZod\nextendZod(z)\n\nexport namespace z {\n export type infer<T> = import(\"zod\").infer<T>\n export type input<T> = import(\"zod\").input<T>\n export type output<T> = import(\"zod\").output<T>\n export type TypeOf<T> = import(\"zod\").TypeOf<T>\n export type Infer<T> = import(\"zod\").Infer<T>\n}\n\nexport { extendZod, ZodError, ZodType }\n"],"names":["LANGUAGE_CODE_REGEX","LOCALIZED_STRING_PROXY_CACHE","WeakMap","normalizeLocale","locale","trimmed","trim","getCanonicalLocales","Intl","buildLocaleFallbackChain","fallbacks","base","canonical","extra","output","seen","Set","push","value","has","add","addChain","parts","split","filter","Boolean","length","join","pop","fallback","resolveLocalizedString","options","resolveLocalizedStringEntry","undefined","chain","record","key","Object","prototype","hasOwnProperty","call","withLocalizedStringFallback","Array","isArray","Map","cached","get","proxy","withFallbackRecord","set","getExact","hasExact","Proxy","target","prop","Reflect","next","zLocalizedString","languageCodeSchema","z","string","regex","message","entrySchema","object","updatedAt","date","autoTranslated","boolean","optional","passthrough","schema","zI18nString","E164_PHONE_REGEX","E164_PHONE_OR_EMPTY_REGEX","makeZE164Phone","zod","allowEmpty","zE164Phone","zodPrototypesExtended","extendZod","zodWithExtension","isExtensible","e164Phone","supported","ZodString","ZodNumber","ZodDate","type","proto","unique","_flag","sparse","create","baseZ"],"mappings":";AA0BO,MAAMA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MC,SAAS,SAASA,OAAsBC,QAAQ,MAAM;AAC1D,aAAO;AAAA,IACT;AAEAF,UAAMG,SAAS,SAASA,OAAsBD,QAAQ,MAAM;AAC1D,aAAO;AAAA,IACT;AAAA,EACF;AACF;AC5CO,MAAMhC,IAAItB,OAAOwD,OAAOC,GAAK;AACpCd,UAAUrB,CAAC;"}
@@ -1,4 +1,4 @@
1
- import { E, a, L, b, e, m, r, w, z, c, d, f } from "./index-BQeL_LHp.js";
1
+ import { E, a, L, b, e, m, r, w, z, c, d, f } from "./index-BNcI2Uw0.js";
2
2
  export {
3
3
  E as E164_PHONE_OR_EMPTY_REGEX,
4
4
  a as E164_PHONE_REGEX,
package/dist/index.js CHANGED
@@ -4,8 +4,8 @@ import mongoose, { Schema as Schema$1, Types } from "mongoose";
4
4
  import { default as default2 } from "mongoose";
5
5
  import { z } from "zod";
6
6
  import { timingSafeEqual, createHmac } from "node:crypto";
7
- import { w as withLocalizedStringFallback } from "./index-BQeL_LHp.js";
8
- import { E, a as a2, L, b as b2, e as e2, m, r, z as z2, c as c2, d as d2, f as f2 } from "./index-BQeL_LHp.js";
7
+ import { w as withLocalizedStringFallback } from "./index-BNcI2Uw0.js";
8
+ import { E, a as a2, L, b as b2, e as e2, m, r, z as z2, c as c2, d as d2, f as f2 } from "./index-BNcI2Uw0.js";
9
9
  import assert from "assert";
10
10
  import { getMongoUrl } from "./mongo.js";
11
11
  import { accessibleBy, accessibleRecordsPlugin } from "@casl/mongoose";
@@ -1,11 +1,16 @@
1
1
  import { z } from 'zod';
2
2
  export type LanguageCode = string;
3
- export type LocalizedString = Record<LanguageCode, string>;
3
+ export type LocalizedStringEntry<TExtra extends Record<string, unknown> = Record<string, unknown>> = {
4
+ value: string;
5
+ updatedAt: Date;
6
+ autoTranslated?: boolean;
7
+ } & TExtra;
8
+ export type LocalizedString<TExtra extends Record<string, unknown> = Record<string, unknown>> = Record<LanguageCode, LocalizedStringEntry<TExtra>>;
4
9
  export type I18nStringRecord = LocalizedString;
5
10
  export type I18nString = LocalizedString;
6
11
  export type LocalizedStringWithFallback = LocalizedString & {
7
- get: (key: string) => string | undefined;
8
- getExact: (key: string) => string | undefined;
12
+ get: (key: string) => LocalizedStringEntry | undefined;
13
+ getExact: (key: string) => LocalizedStringEntry | undefined;
9
14
  hasExact: (key: string) => boolean;
10
15
  };
11
16
  export declare const LANGUAGE_CODE_REGEX: RegExp;
@@ -14,6 +19,14 @@ export declare function resolveLocalizedString(value: LocalizedString | null | u
14
19
  fallbacks?: string | string[];
15
20
  }): string | undefined;
16
21
  export declare function withLocalizedStringFallback<T>(value: T): T;
17
- export declare const zLocalizedString: () => z.ZodRecord<z.ZodString, z.ZodString>;
18
- export declare const zI18nString: () => z.ZodRecord<z.ZodString, z.ZodString>;
22
+ export declare const zLocalizedString: () => z.ZodRecord<z.ZodString, z.ZodObject<{
23
+ value: z.ZodString;
24
+ updatedAt: z.ZodDate;
25
+ autoTranslated: z.ZodOptional<z.ZodBoolean>;
26
+ }, z.core.$loose>>;
27
+ export declare const zI18nString: () => z.ZodRecord<z.ZodString, z.ZodObject<{
28
+ value: z.ZodString;
29
+ updatedAt: z.ZodDate;
30
+ autoTranslated: z.ZodOptional<z.ZodBoolean>;
31
+ }, z.core.$loose>>;
19
32
  //# sourceMappingURL=localizedString.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"localizedString.d.ts","sourceRoot":"","sources":["../../src/zod/localizedString.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAA;AAGvB,MAAM,MAAM,YAAY,GAAG,MAAM,CAAA;AAEjC,MAAM,MAAM,eAAe,GAAG,MAAM,CAAC,YAAY,EAAE,MAAM,CAAC,CAAA;AAE1D,MAAM,MAAM,gBAAgB,GAAG,eAAe,CAAA;AAE9C,MAAM,MAAM,UAAU,GAAG,eAAe,CAAA;AAExC,MAAM,MAAM,2BAA2B,GAAG,eAAe,GAAG;IAC1D,GAAG,EAAE,CAAC,GAAG,E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MAAM,EAAE,OAAO,CAAC,IAAI,MAAM,CACpG,YAAY,EACZ,oBAAoB,CAAC,MAAM,CAAC,CAC7B,CAAA;AAED,MAAM,MAAM,gBAAgB,GAAG,eAAe,CAAA;AAE9C,MAAM,MAAM,UAAU,GAAG,eAAe,CAAA;AAExC,MAAM,MAAM,2BAA2B,GAAG,eAAe,GAAG;IAC1D,GAAG,EAAE,CAAC,GAAG,EAAE,MAAM,KAAK,oBAAoB,GAAG,SAAS,CAAA;IACtD,QAAQ,EAAE,CAAC,GAAG,EAAE,MAAM,KAAK,oBAAoB,GAAG,SAAS,CAAA;IAC3D,QAAQ,EAAE,CAAC,GAAG,EAAE,MAAM,KAAK,OAAO,CAAA;CACnC,CAAA;AAED,eAAO,MAAM,mBAAmB,QAAuC,CAAA;AAkBvE,wBAAgB,wBAAwB,CAAC,MAAM,EAAE,MAAM,EAAE,SAAS,CAAC,EAAE,MAAM,GAAG,MAAM,EAAE,GAAG,MAAM,EAAE,CA6BhG;AAED,wBAAgB,sBAAsB,CACpC,KAAK,EAAE,eAAe,GAAG,IAAI,GAAG,SAAS,EACzC,MAAM,EAAE,MAAM,EACd,OAAO,CAAC,EAAE;IAAE,SAAS,CAAC,EAAE,MAAM,GAAG,MAAM,EAAE,CAAA;CAAE,GAC1C,MAAM,GAAG,SAAS,CAEpB;AAmBD,wBAAgB,2BAA2B,CAAC,CAAC,EAAE,KAAK,EAAE,CAAC,GAAG,CAAC,CAS1D;AA0BD,eAAO,MAAM,gBAAgB;;;;kBAe5B,CAAA;AAED,eAAO,MAAM,WAAW;;;;kBAAmB,CAAA"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@rpcbase/db",
3
- "version": "0.95.0",
3
+ "version": "0.97.0",
4
4
  "type": "module",
5
5
  "files": [
6
6
  "dist"
@@ -52,7 +52,11 @@
52
52
  },
53
53
  "build-watch": {
54
54
  "command": "../../node_modules/.bin/vite build --watch",
55
- "service": true
55
+ "service": {
56
+ "readyWhen": {
57
+ "lineMatches": "^built in .*\\.$"
58
+ }
59
+ }
56
60
  },
57
61
  "release": {
58
62
  "command": "../../scripts/publish.js",
@@ -1 +0,0 @@
1
- {"version":3,"file":"index-BQeL_LHp.js","sources":["../src/zod/localizedString.ts","../src/zod/e164Phone.ts","../src/zod/extension.ts","../src/zod/index.ts"],"sourcesContent":["import { z } from \"zod\"\n\n\nexport type LanguageCode = string\n\nexport type LocalizedString = Record<LanguageCode, string>\n\nexport type I18nStringRecord = LocalizedString\n\nexport type I18nString = LocalizedString\n\nexport type LocalizedStringWithFallback = LocalizedString & {\n get: (key: string) => string | undefined\n getExact: (key: string) => string | undefined\n hasExact: (key: string) => boolean\n}\n\nexport const LANGUAGE_CODE_REGEX = /^[a-z]{2,3}(?:-[A-Za-z0-9]{2,8})*$/\n\nconst LOCALIZED_STRING_PROXY_CACHE = new WeakMap<object, unknown>()\n\nfunction normalizeLocale(locale: string): string {\n const trimmed = locale.trim()\n if (!trimmed) return \"\"\n const getCanonicalLocales = (Intl as typeof Intl & {\n getCanonicalLocales?: (locales: string | readonly string[]) => string[]\n }).getCanonicalLocales\n if (typeof getCanonicalLocales !== \"function\") return trimmed\n try {\n return getCanonicalLocales(trimmed)[0] ?? trimmed\n } catch {\n return trimmed\n }\n}\n\nexport function buildLocaleFallbackChain(locale: string, fallbacks?: string | string[]): string[] {\n const base = locale.trim()\n const canonical = normalizeLocale(base)\n const extra = typeof fallbacks === \"string\" ? [fallbacks] : (fallbacks ?? [])\n\n const output: string[] = []\n const seen = new Set<string>()\n\n const push = (value: string) => {\n if (!value) return\n if (seen.has(value)) return\n seen.add(value)\n output.push(value)\n }\n\n const addChain = (value: string) => {\n if (!value) return\n const parts = value.split(\"-\").filter(Boolean)\n while (parts.length > 0) {\n push(parts.join(\"-\"))\n parts.pop()\n }\n }\n\n addChain(base)\n addChain(canonical)\n for (const fallback of extra) addChain(normalizeLocale(fallback))\n\n return output\n}\n\nexport function resolveLocalizedString(\n value: LocalizedString | null | undefined,\n locale: string,\n options?: { fallbacks?: string | string[] }\n): string | undefined {\n if (!value) return undefined\n const chain = buildLocaleFallbackChain(locale, options?.fallbacks)\n if (chain.length === 0) return undefined\n\n const record = value\n for (const key of chain) {\n if (!Object.prototype.hasOwnProperty.call(record, key)) continue\n return record[key]\n }\n return undefined\n}\n\nexport function withLocalizedStringFallback<T>(value: T): T {\n if (!value || typeof value !== \"object\" || Array.isArray(value)) return value\n if (value instanceof Map) return value\n const cached = LOCALIZED_STRING_PROXY_CACHE.get(value)\n if (cached) return cached as T\n\n const proxy = withFallbackRecord(value as Record<string, unknown>)\n LOCALIZED_STRING_PROXY_CACHE.set(value, proxy)\n return proxy as T\n}\n\nfunction withFallbackRecord(record: Record<string, unknown>): LocalizedStringWithFallback {\n const getExact = (key: string): string | undefined => {\n const current = record[key]\n return typeof current === \"string\" ? current : undefined\n }\n const hasExact = (key: string) => Object.prototype.hasOwnProperty.call(record, key)\n\n return new Proxy(record, {\n get(target, prop) {\n if (prop === \"getExact\") return getExact\n if (prop === \"hasExact\") return hasExact\n if (prop === \"get\") return (key: string) => resolveLocalizedString(target as LocalizedString, key)\n if (typeof prop === \"string\" && !(prop in target)) {\n return resolveLocalizedString(target as LocalizedString, prop)\n }\n\n return Reflect.get(target, prop)\n },\n set(target, prop, next) {\n return Reflect.set(target, prop, next)\n },\n }) as LocalizedStringWithFallback\n}\n\nexport const zLocalizedString = () => {\n const schema = z.record(\n z\n .string()\n .regex(LANGUAGE_CODE_REGEX, { message: \"Expected a language code (BCP 47, e.g. en or fr-FR).\" }),\n z.string()\n ) as z.ZodRecord<z.ZodString, z.ZodString>\n\n return schema\n}\n\nexport const zI18nString = zLocalizedString\n","import { z } from \"zod\"\n\n\nexport const E164_PHONE_REGEX = /^\\+[1-9]\\d{1,14}$/\nexport const E164_PHONE_OR_EMPTY_REGEX = /^(?:\\+[1-9]\\d{1,14})?$/\n\nexport type E164PhoneOptions = {\n allowEmpty?: boolean\n}\n\nexport const makeZE164Phone = (zod: typeof z, options?: E164PhoneOptions) => {\n const allowEmpty = options?.allowEmpty ?? false\n return zod.string().trim().regex(allowEmpty ? E164_PHONE_OR_EMPTY_REGEX : E164_PHONE_REGEX, {\n message: allowEmpty\n ? \"Expected an empty string or a phone number in E.164 format (e.g. +33608707197).\"\n : \"Expected a phone number in E.164 format (e.g. +33608707197).\",\n })\n}\n\nexport const zE164Phone = (options?: E164PhoneOptions) => makeZE164Phone(z, options)\n","import { z } from \"zod\"\n\nimport { type E164PhoneOptions, makeZE164Phone } from \"./e164Phone\"\n\n\ntype RpcbaseZodExtension = typeof z & {\n e164Phone?: (options?: E164PhoneOptions) => z.ZodString\n}\n\ntype ExtendableZodPrototype = {\n unique?: (this: unknown, arg?: boolean) => unknown\n sparse?: (this: unknown, arg?: boolean) => unknown\n}\n\ndeclare module \"zod\" {\n interface ZodString {\n unique(arg?: boolean): this\n sparse(arg?: boolean): this\n }\n\n interface ZodNumber {\n unique(arg?: boolean): this\n sparse(arg?: boolean): this\n }\n\n interface ZodDate {\n unique(arg?: boolean): this\n sparse(arg?: boolean): this\n }\n\n}\n\nlet zodPrototypesExtended = false\n\nexport function extendZod(zod: typeof z) {\n const zodWithExtension = zod as RpcbaseZodExtension\n if (Object.isExtensible(zodWithExtension) && typeof zodWithExtension.e164Phone !== \"function\") {\n zodWithExtension.e164Phone = (options?: E164PhoneOptions) => makeZE164Phone(zod, options)\n }\n\n if (zodPrototypesExtended) return\n zodPrototypesExtended = true\n\n const supported = [zod.ZodString, zod.ZodNumber, zod.ZodDate] as const\n for (const type of supported) {\n const proto = type?.prototype as ExtendableZodPrototype | undefined\n if (!proto) continue\n\n proto.unique = function unique(this: unknown, _flag = true) {\n return this\n }\n\n proto.sparse = function sparse(this: unknown, _flag = true) {\n return this\n }\n }\n}\n","import { z as baseZ, type ZodError, type ZodString, type ZodType } from \"zod\"\n\nimport { extendZod } from \"./extension\"\n\n\nexport * from \"./e164Phone\"\nexport * from \"./localizedString\"\n\nexport type RpcbaseZod = typeof baseZ & {\n e164Phone: (options?: import(\"./e164Phone\").E164PhoneOptions) => ZodString\n}\n\nexport const z = Object.create(baseZ) as RpcbaseZod\nextendZod(z)\n\nexport namespace z {\n export type infer<T> = import(\"zod\").infer<T>\n export type input<T> = import(\"zod\").input<T>\n export type output<T> = import(\"zod\").output<T>\n export type TypeOf<T> = import(\"zod\").TypeOf<T>\n export type Infer<T> = import(\"zod\").Infer<T>\n}\n\nexport { extendZod, ZodError, ZodType }\n"],"names":["LANGUAGE_CODE_REGEX","LOCALIZED_STRING_PROXY_CACHE","WeakMap","normalizeLocale","locale","trimmed","trim","getCanonicalLocales","Intl","buildLocaleFallbackChain","fallbacks","base","canonical","extra","output","seen","Set","push","value","has","add","addChain","parts","split","filter","Boolean","length","join","pop","fallback","resolveLocalizedString","options","undefined","chain","record","key","Object","prototype","hasOwnProperty","call","withLocalizedStringFallback","Array","isArray","Map","cached","get","proxy","withFallbackRecord","set","getExact","current","hasExact","Proxy","target","prop","Reflect","next","zLocalizedString","schema","z","string","regex","message","zI18nString","E164_PHONE_REGEX","E164_PHONE_OR_EMPTY_REGEX","makeZE164Phone","zod","allowEmpty","zE164Phone","zodPrototypesExtended","extendZod","zodWithExtension","isExtensible","e164Phone","supported","ZodString","ZodNumber","ZodDate","type","proto","unique","_flag","sparse","create","baseZ"],"mappings":";AAiBO,MAAMA,sBAAsB;AAEnC,MAAMC,mDAAmCC,QAAAA;AAEzC,SAASC,gBAAgBC,QAAwB;AAC/C,QAAMC,UAAUD,OAAOE,KAAAA;AACvB,MAAI,CAACD,QAAS,QAAO;AACrB,QAAME,sBAAuBC,KAE1BD;AACH,MAAI,OAAOA,wBAAwB,WAAY,QAAOF;AACtD,MAAI;AACF,WAAOE,oBAAoBF,OAAO,EAAE,CAAC,KAAKA;AAAAA,EAC5C,QAAQ;AACN,WAAOA;AAAAA,EACT;AACF;AAEO,SAASI,yBAAyBL,QAAgBM,WAAyC;AAChG,QAAMC,OAAOP,OAAOE,KAAA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