@rpcbase/db 0.75.0 → 0.77.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/dist/model.js CHANGED
@@ -1,2 +0,0 @@
1
-
2
- //# sourceMappingURL=model.js.map
@@ -0,0 +1,123 @@
1
+ import { z } from "zod";
2
+ //#region src/zod/localizedString.ts
3
+ var LANGUAGE_CODE_REGEX = /^[a-z]{2,3}(?:-[A-Za-z0-9]{2,8})*$/;
4
+ var LOCALIZED_STRING_PROXY_CACHE = /* @__PURE__ */ new WeakMap();
5
+ function normalizeLocale(locale) {
6
+ const trimmed = locale.trim();
7
+ if (!trimmed) return "";
8
+ const getCanonicalLocales = Intl.getCanonicalLocales;
9
+ if (typeof getCanonicalLocales !== "function") return trimmed;
10
+ try {
11
+ return getCanonicalLocales(trimmed)[0] ?? trimmed;
12
+ } catch {
13
+ return trimmed;
14
+ }
15
+ }
16
+ function buildLocaleFallbackChain(locale, fallbacks) {
17
+ const base = locale.trim();
18
+ const canonical = normalizeLocale(base);
19
+ const extra = typeof fallbacks === "string" ? [fallbacks] : fallbacks ?? [];
20
+ const output = [];
21
+ const seen = /* @__PURE__ */ new Set();
22
+ const push = (value) => {
23
+ if (!value) return;
24
+ if (seen.has(value)) return;
25
+ seen.add(value);
26
+ output.push(value);
27
+ };
28
+ const addChain = (value) => {
29
+ if (!value) return;
30
+ const parts = value.split("-").filter(Boolean);
31
+ while (parts.length > 0) {
32
+ push(parts.join("-"));
33
+ parts.pop();
34
+ }
35
+ };
36
+ addChain(base);
37
+ addChain(canonical);
38
+ for (const fallback of extra) addChain(normalizeLocale(fallback));
39
+ return output;
40
+ }
41
+ function resolveLocalizedString(value, locale, options) {
42
+ if (!value) return void 0;
43
+ const chain = buildLocaleFallbackChain(locale, options?.fallbacks);
44
+ if (chain.length === 0) return void 0;
45
+ const record = value;
46
+ for (const key of chain) {
47
+ if (!Object.prototype.hasOwnProperty.call(record, key)) continue;
48
+ return record[key];
49
+ }
50
+ }
51
+ function withLocalizedStringFallback(value) {
52
+ if (!value || typeof value !== "object" || Array.isArray(value)) return value;
53
+ if (value instanceof Map) return value;
54
+ const cached = LOCALIZED_STRING_PROXY_CACHE.get(value);
55
+ if (cached) return cached;
56
+ const proxy = withFallbackRecord(value);
57
+ LOCALIZED_STRING_PROXY_CACHE.set(value, proxy);
58
+ return proxy;
59
+ }
60
+ function withFallbackRecord(record) {
61
+ const getExact = (key) => {
62
+ const current = record[key];
63
+ return typeof current === "string" ? current : void 0;
64
+ };
65
+ const hasExact = (key) => Object.prototype.hasOwnProperty.call(record, key);
66
+ return new Proxy(record, {
67
+ get(target, prop) {
68
+ if (prop === "getExact") return getExact;
69
+ if (prop === "hasExact") return hasExact;
70
+ if (prop === "get") return (key) => resolveLocalizedString(target, key);
71
+ if (typeof prop === "string" && !(prop in target)) return resolveLocalizedString(target, prop);
72
+ return Reflect.get(target, prop);
73
+ },
74
+ set(target, prop, next) {
75
+ return Reflect.set(target, prop, next);
76
+ }
77
+ });
78
+ }
79
+ var zLocalizedString = () => {
80
+ return z.record(z.string().regex(LANGUAGE_CODE_REGEX, { message: "Expected a language code (BCP 47, e.g. en or fr-FR)." }), z.string());
81
+ };
82
+ var zI18nString = zLocalizedString;
83
+ //#endregion
84
+ //#region src/zod/e164Phone.ts
85
+ var E164_PHONE_REGEX = /^\+[1-9]\d{1,14}$/;
86
+ var E164_PHONE_OR_EMPTY_REGEX = /^(?:\+[1-9]\d{1,14})?$/;
87
+ var makeZE164Phone = (zod, options) => {
88
+ const allowEmpty = options?.allowEmpty ?? false;
89
+ return zod.string().trim().regex(allowEmpty ? E164_PHONE_OR_EMPTY_REGEX : E164_PHONE_REGEX, { message: allowEmpty ? "Expected an empty string or a phone number in E.164 format (e.g. +33608707197)." : "Expected a phone number in E.164 format (e.g. +33608707197)." });
90
+ };
91
+ var zE164Phone = (options) => makeZE164Phone(z, options);
92
+ //#endregion
93
+ //#region src/zod/extension.ts
94
+ var zodPrototypesExtended = false;
95
+ function extendZod(zod) {
96
+ const zodWithExtension = zod;
97
+ if (Object.isExtensible(zodWithExtension) && typeof zodWithExtension.e164Phone !== "function") zodWithExtension.e164Phone = (options) => makeZE164Phone(zod, options);
98
+ if (zodPrototypesExtended) return;
99
+ zodPrototypesExtended = true;
100
+ const supported = [
101
+ zod.ZodString,
102
+ zod.ZodNumber,
103
+ zod.ZodDate
104
+ ];
105
+ for (const type of supported) {
106
+ const proto = type?.prototype;
107
+ if (!proto) continue;
108
+ proto.unique = function unique(_flag = true) {
109
+ return this;
110
+ };
111
+ proto.sparse = function sparse(_flag = true) {
112
+ return this;
113
+ };
114
+ }
115
+ }
116
+ //#endregion
117
+ //#region src/zod/index.ts
118
+ var z$1 = Object.create(z);
119
+ extendZod(z$1);
120
+ //#endregion
121
+ export { makeZE164Phone as a, buildLocaleFallbackChain as c, zI18nString as d, zLocalizedString as f, E164_PHONE_REGEX as i, resolveLocalizedString as l, extendZod as n, zE164Phone as o, E164_PHONE_OR_EMPTY_REGEX as r, LANGUAGE_CODE_REGEX as s, z$1 as t, withLocalizedStringFallback as u };
122
+
123
+ //# sourceMappingURL=zod-Co_dG17m.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"zod-Co_dG17m.js","names":["z","LanguageCode","LocalizedString","Record","I18nStringRecord","I18nString","LocalizedStringWithFallback","get","key","getExact","hasExact","LANGUAGE_CODE_REGEX","LOCALIZED_STRING_PROXY_CACHE","WeakMap","normalizeLocale","locale","trimmed","trim","getCanonicalLocales","Intl","locales","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","Object","prototype","hasOwnProperty","call","withLocalizedStringFallback","T","Array","isArray","Map","cached","proxy","withFallbackRecord","set","current","Proxy","target","prop","Reflect","next","zLocalizedString","schema","string","regex","message","ZodRecord","ZodString","zI18nString","z","E164_PHONE_REGEX","E164_PHONE_OR_EMPTY_REGEX","E164PhoneOptions","allowEmpty","makeZE164Phone","zod","options","string","trim","regex","message","zE164Phone","z","E164PhoneOptions","makeZE164Phone","RpcbaseZodExtension","e164Phone","options","ZodString","ExtendableZodPrototype","unique","this","arg","sparse","ZodNumber","ZodDate","zodPrototypesExtended","extendZod","zod","zodWithExtension","Object","isExtensible","supported","const","type","proto","prototype","_flag","z","baseZ","ZodError","ZodString","ZodType","extendZod","RpcbaseZod","e164Phone","options","E164PhoneOptions","Object","create","infer","T","input","output","TypeOf","Infer"],"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"],"mappings":";;AAiBA,IAAaW,sBAAsB;AAEnC,IAAMC,+CAA+B,IAAIC,SAA0B;AAEnE,SAASC,gBAAgBC,QAAwB;CAC/C,MAAMC,UAAUD,OAAOE,MAAM;AAC7B,KAAI,CAACD,QAAS,QAAO;CACrB,MAAME,sBAAuBC,KAE1BD;AACH,KAAI,OAAOA,wBAAwB,WAAY,QAAOF;AACtD,KAAI;AACF,SAAOE,oBAAoBF,QAAQ,CAAC,MAAMA;SACpC;AACN,SAAOA;;;AAIX,SAAgBK,yBAAyBN,QAAgBO,WAAyC;CAChG,MAAMC,OAAOR,OAAOE,MAAM;CAC1B,MAAMO,YAAYV,gBAAgBS,KAAK;CACvC,MAAME,QAAQ,OAAOH,cAAc,WAAW,CAACA,UAAU,GAAIA,aAAa,EAAG;CAE7E,MAAMI,SAAmB,EAAE;CAC3B,MAAMC,uBAAO,IAAIC,KAAa;CAE9B,MAAMC,QAAQC,UAAkB;AAC9B,MAAI,CAACA,MAAO;AACZ,MAAIH,KAAKI,IAAID,MAAM,CAAE;AACrBH,OAAKK,IAAIF,MAAM;AACfJ,SAAOG,KAAKC,MAAM;;CAGpB,MAAMG,YAAYH,UAAkB;AAClC,MAAI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f,SAAS,SAASA,OAAsBiB,QAAQ,MAAM;AAC1D,UAAO;;AAGTF,QAAMZ,SAAS,SAASA,OAAsBc,QAAQ,MAAM;AAC1D,UAAO;;;;;;ACzCb,IAAaC,MAAIU,OAAOC,OAAOV,EAAM;AACrCI,UAAUL,IAAE"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@rpcbase/db",
3
- "version": "0.75.0",
3
+ "version": "0.77.0",
4
4
  "type": "module",
5
5
  "files": [
6
6
  "dist"
@@ -74,12 +74,12 @@
74
74
  "zod": "^4"
75
75
  },
76
76
  "dependencies": {
77
- "@casl/ability": "6.7.5",
78
- "@casl/mongoose": "8.0.4"
77
+ "@casl/ability": "6.8.0",
78
+ "@casl/mongoose": "8.0.5"
79
79
  },
80
80
  "devDependencies": {
81
- "@types/node": "25.0.3",
82
- "@vitest/coverage-v8": "4.0.16",
83
- "vitest": "4.0.16"
81
+ "@types/node": "25.5.0",
82
+ "@vitest/coverage-v8": "4.1.1",
83
+ "vitest": "4.1.1"
84
84
  }
85
85
  }
@@ -1,132 +0,0 @@
1
- import { AbilityBuilder, createMongoAbility, subject } from "@casl/ability";
2
- import { accessibleBy } from "@casl/mongoose";
3
- const POLICIES_GLOBAL_KEY = /* @__PURE__ */ Symbol.for("@rpcbase/db/acl/policiesBySubject");
4
- const getGlobalPoliciesMap = () => {
5
- const store = globalThis;
6
- const existing = store[POLICIES_GLOBAL_KEY];
7
- if (existing instanceof Map) {
8
- return existing;
9
- }
10
- const created = /* @__PURE__ */ new Map();
11
- store[POLICIES_GLOBAL_KEY] = created;
12
- return created;
13
- };
14
- const policiesBySubject = getGlobalPoliciesMap();
15
- const isPolicy = (value) => {
16
- if (!value || typeof value !== "object") return false;
17
- const maybe = value;
18
- return typeof maybe.subject === "string" && typeof maybe.define === "function";
19
- };
20
- const registerPolicy = (policy) => {
21
- const set = policiesBySubject.get(policy.subject) ?? /* @__PURE__ */ new Set();
22
- set.add(policy.define);
23
- policiesBySubject.set(policy.subject, set);
24
- };
25
- const registerPoliciesFromModules = (modules) => {
26
- for (const [exportName, value] of Object.entries(modules)) {
27
- if (!isPolicy(value)) continue;
28
- if (!exportName.endsWith("Policy")) {
29
- throw new Error(`Invalid policy export name "${exportName}". Policies must be exported as "<ModelName>Policy".`);
30
- }
31
- const expectedSubject = exportName.slice(0, -"Policy".length);
32
- if (value.subject !== expectedSubject) {
33
- throw new Error(`Invalid policy "${exportName}": expected subject "${expectedSubject}", got "${value.subject}".`);
34
- }
35
- registerPolicy(value);
36
- }
37
- };
38
- const getRegisteredPolicies = () => {
39
- const out = [];
40
- for (const set of policiesBySubject.values()) {
41
- for (const fn of set) out.push(fn);
42
- }
43
- return out;
44
- };
45
- const hasRegisteredPolicy = (subject2) => {
46
- return policiesBySubject.has(subject2);
47
- };
48
- const buildAbility = (ctx) => {
49
- const builder = new AbilityBuilder(createMongoAbility);
50
- const {
51
- can: can2
52
- } = builder;
53
- if (ctx.roles.includes("owner") || ctx.roles.includes("admin")) {
54
- can2("manage", "all");
55
- }
56
- for (const define of getRegisteredPolicies()) {
57
- define(builder, ctx);
58
- }
59
- return builder.build();
60
- };
61
- const normalizeRole = (raw) => {
62
- if (typeof raw !== "string") return null;
63
- const normalized = raw.trim();
64
- return normalized ? normalized : null;
65
- };
66
- const normalizeRoles = (raw) => {
67
- if (Array.isArray(raw)) {
68
- return raw.map(normalizeRole).filter((r) => Boolean(r));
69
- }
70
- const role = normalizeRole(raw);
71
- return role ? [role] : [];
72
- };
73
- const normalizeRolesByTenantId = (raw) => {
74
- if (!raw || typeof raw !== "object") return null;
75
- if (raw instanceof Map) {
76
- return Object.fromEntries(Array.from(raw.entries()).map(([key, value]) => [String(key), normalizeRoles(value)]));
77
- }
78
- const obj = raw;
79
- const out = {};
80
- for (const [key, value] of Object.entries(obj)) {
81
- const tenantId = String(key).trim();
82
- if (!tenantId) continue;
83
- out[tenantId] = normalizeRoles(value);
84
- }
85
- return out;
86
- };
87
- const getTenantRolesFromSessionUser = (user, tenantId) => {
88
- if (!user || typeof user !== "object") return [];
89
- const rolesByTenantId = normalizeRolesByTenantId(user.tenantRoles);
90
- if (!rolesByTenantId) return [];
91
- return rolesByTenantId[tenantId]?.filter(Boolean) ?? [];
92
- };
93
- const buildAbilityFromSession = ({
94
- tenantId,
95
- session,
96
- claims
97
- }) => {
98
- const user = session?.user;
99
- const userId = typeof user?.id === "string" ? user.id.trim() : "";
100
- const rolesByTenantId = normalizeRolesByTenantId(user?.tenantRoles);
101
- const hasExplicitTenantEntry = Boolean(rolesByTenantId && Object.prototype.hasOwnProperty.call(rolesByTenantId, tenantId));
102
- const signedInTenantsRaw = user?.signedInTenants;
103
- const signedInTenants = Array.isArray(signedInTenantsRaw) ? signedInTenantsRaw.map(String) : [];
104
- const roles = hasExplicitTenantEntry ? rolesByTenantId[tenantId] ?? [] : userId && signedInTenants.includes(tenantId) ? ["owner"] : getTenantRolesFromSessionUser(user, tenantId);
105
- return buildAbility({
106
- tenantId,
107
- userId: userId || null,
108
- roles,
109
- claims
110
- });
111
- };
112
- const getAccessibleByQuery = (ability, action, subject2) => {
113
- return accessibleBy(ability, action).ofType(subject2);
114
- };
115
- const can = (ability, action, subjectType, object) => {
116
- if (object && typeof object === "object") {
117
- return ability.can(action, subject(subjectType, object));
118
- }
119
- return ability.can(action, subjectType);
120
- };
121
- export {
122
- registerPoliciesFromModules as a,
123
- buildAbility as b,
124
- getTenantRolesFromSessionUser as c,
125
- buildAbilityFromSession as d,
126
- getAccessibleByQuery as e,
127
- can as f,
128
- getRegisteredPolicies as g,
129
- hasRegisteredPolicy as h,
130
- registerPolicy as r
131
- };
132
- //# sourceMappingURL=can--Y-1LK22.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"can--Y-1LK22.js","sources":["../src/acl/registry.ts","../src/acl/buildAbility.ts","../src/acl/session.ts","../src/acl/accessible.ts","../src/acl/can.ts"],"sourcesContent":["import type { AbilityBuilder } from \"@casl/ability\"\n\nimport type { AclContext, AclSubjectType, AppAbility } from \"./types\"\n\n\nexport type AclPolicy = {\n subject: Exclude<AclSubjectType, \"all\">\n define: (builder: AbilityBuilder<AppAbility>, ctx: AclContext) => void\n}\n\nconst POLICIES_GLOBAL_KEY = Symbol.for(\"@rpcbase/db/acl/policiesBySubject\")\n\nconst getGlobalPoliciesMap = (): Map<string, Set<AclPolicy[\"define\"]>> => {\n const store = globalThis as unknown as Record<symbol, unknown>\n const existing = store[POLICIES_GLOBAL_KEY]\n if (existing instanceof Map) {\n return existing as Map<string, Set<AclPolicy[\"define\"]>>\n }\n const created = new Map<string, Set<AclPolicy[\"define\"]>>()\n store[POLICIES_GLOBAL_KEY] = created\n return created\n}\n\nconst policiesBySubject = getGlobalPoliciesMap()\n\nconst isPolicy = (value: unknown): value is AclPolicy => {\n if (!value || typeof value !== \"object\") return false\n const maybe = value as Partial<AclPolicy>\n return typeof maybe.subject === \"string\" && typeof maybe.define === \"function\"\n}\n\nexport const registerPolicy = (policy: AclPolicy): void => {\n const set = policiesBySubject.get(policy.subject) ?? new Set<AclPolicy[\"define\"]>()\n set.add(policy.define)\n policiesBySubject.set(policy.subject, set)\n}\n\nexport const registerPoliciesFromModules = (modules: Record<string, unknown>): void => {\n for (const [exportName, value] of Object.entries(modules)) {\n if (!isPolicy(value)) continue\n\n if (!exportName.endsWith(\"Policy\")) {\n throw new Error(\n `Invalid policy export name \"${exportName}\". Policies must be exported as \"<ModelName>Policy\".`,\n )\n }\n\n const expectedSubject = exportName.slice(0, -(\"Policy\".length))\n if (value.subject !== expectedSubject) {\n throw new Error(\n `Invalid policy \"${exportName}\": expected subject \"${expectedSubject}\", got \"${value.subject}\".`,\n )\n }\n\n registerPolicy(value)\n }\n}\n\nexport const getRegisteredPolicies = (): Array<AclPolicy[\"define\"]> => {\n const out: Array<AclPolicy[\"define\"]> = []\n for (const set of policiesBySubject.values()) {\n for (const fn of set) out.push(fn)\n }\n return out\n}\n\nexport const hasRegisteredPolicy = (subject: string): boolean => {\n return policiesBySubject.has(subject)\n}\n","import { AbilityBuilder, createMongoAbility } from \"@casl/ability\"\n\nimport { getRegisteredPolicies } from \"./registry\"\nimport type { AclContext, AppAbility } from \"./types\"\n\n\nexport const buildAbility = (ctx: AclContext): AppAbility => {\n const builder = new AbilityBuilder<AppAbility>(createMongoAbility)\n\n const { can } = builder\n\n if (ctx.roles.includes(\"owner\") || ctx.roles.includes(\"admin\")) {\n can(\"manage\", \"all\")\n }\n\n for (const define of getRegisteredPolicies()) {\n define(builder, ctx)\n }\n\n return builder.build()\n}\n\n","import { buildAbility } from \"./buildAbility\"\nimport type { AppAbility, TenantRolesByTenantId } from \"./types\"\n\n\ntype SessionUserLike = {\n id?: unknown\n signedInTenants?: unknown\n tenantRoles?: unknown\n}\n\ntype SessionLike = {\n user?: SessionUserLike\n}\n\nconst normalizeRole = (raw: unknown): string | null => {\n if (typeof raw !== \"string\") return null\n const normalized = raw.trim()\n return normalized ? normalized : null\n}\n\nconst normalizeRoles = (raw: unknown): string[] => {\n if (Array.isArray(raw)) {\n return raw.map(normalizeRole).filter((r): r is string => Boolean(r))\n }\n const role = normalizeRole(raw)\n return role ? [role] : []\n}\n\nconst normalizeRolesByTenantId = (raw: unknown): TenantRolesByTenantId | null => {\n if (!raw || typeof raw !== \"object\") return null\n if (raw instanceof Map) {\n return Object.fromEntries(Array.from(raw.entries()).map(([key, value]) => [String(key), normalizeRoles(value)]))\n }\n const obj = raw as Record<string, unknown>\n const out: TenantRolesByTenantId = {}\n for (const [key, value] of Object.entries(obj)) {\n const tenantId = String(key).trim()\n if (!tenantId) continue\n out[tenantId] = normalizeRoles(value)\n }\n return out\n}\n\nexport const getTenantRolesFromSessionUser = (user: unknown, tenantId: string): string[] => {\n if (!user || typeof user !== \"object\") return []\n const rolesByTenantId = normalizeRolesByTenantId((user as SessionUserLike).tenantRoles)\n if (!rolesByTenantId) return []\n return rolesByTenantId[tenantId]?.filter(Boolean) ?? []\n}\n\nexport const buildAbilityFromSession = ({\n tenantId,\n session,\n claims,\n}: {\n tenantId: string\n session: SessionLike | null | undefined\n claims?: Record<string, unknown>\n}): AppAbility => {\n const user = session?.user\n const userId = typeof user?.id === \"string\" ? user.id.trim() : \"\"\n const rolesByTenantId = normalizeRolesByTenantId(user?.tenantRoles)\n const hasExplicitTenantEntry = Boolean(\n rolesByTenantId && Object.prototype.hasOwnProperty.call(rolesByTenantId, tenantId),\n )\n\n const signedInTenantsRaw = user?.signedInTenants\n const signedInTenants = Array.isArray(signedInTenantsRaw) ? signedInTenantsRaw.map(String) : []\n\n const roles = hasExplicitTenantEntry\n ? rolesByTenantId![tenantId] ?? []\n : userId && signedInTenants.includes(tenantId)\n ? [\"owner\"]\n : getTenantRolesFromSessionUser(user, tenantId)\n\n return buildAbility({\n tenantId,\n userId: userId || null,\n roles,\n claims,\n })\n}\n","import { accessibleBy } from \"@casl/mongoose\"\n\nimport type { AclAction, AclSubjectType, AppAbility } from \"./types\"\n\n\nexport const getAccessibleByQuery = (\n ability: AppAbility,\n action: AclAction,\n subject: Exclude<AclSubjectType, \"all\">,\n): Record<string, unknown> => {\n return accessibleBy(ability, action).ofType(subject)\n}\n","import { subject } from \"@casl/ability\"\n\nimport type { AclAction, AclSubject, AclSubjectMap, AclSubjectType, AppAbility } from \"./types\"\n\n\nexport const can = <TSubject extends Exclude<AclSubjectType, \"all\">>(\n ability: AppAbility,\n action: AclAction,\n subjectType: TSubject,\n object?: Partial<AclSubjectMap[TSubject]> | null,\n): boolean => {\n if (object && typeof object === \"object\") {\n return ability.can(action, subject(subjectType, object) as unknown as AclSubject)\n }\n return ability.can(action, subjectType)\n}\n"],"names":["POLICIES_GLOBAL_KEY","Symbol","for","getGlobalPoliciesMap","store","globalThis","existing","Map","created","policiesBySubject","isPolicy","value","maybe","subject","define","registerPolicy","policy","set","get","Set","add","registerPoliciesFromModules","modules","exportName","Object","entries","endsWith","Error","expectedSubject","slice","length","getRegisteredPolicies","out","values","fn","push","hasRegisteredPolicy","has","buildAbility","ctx","builder","AbilityBuilder","createMongoAbility","can","roles","includes","build","normalizeRole","raw","normalized","trim","normalizeRoles","Array","isArray","map","filter","r","Boolean","role","normalizeRolesByTenantId","fromEntries","from","key","String","obj","tenantId","getTenantRolesFromSessionUser","user","rolesByTenantId","tenantRoles","buildAbilityFromSession","session","claims","userId","id","hasExplicitTenantEntry","prototype","hasOwnProperty","call","signedInTenantsRaw","signedInTenants","getAccessibleByQuery","ability","action","accessibleBy","ofType","subjectType","object"],"mappings":";;AAUA,MAAMA,sBAAsBC,uBAAOC,IAAI,mCAAmC;AAE1E,MAAMC,uBAAuBA,MAA6C;AACxE,QAAMC,QAAQC;AACd,QAAMC,WAAWF,MAAMJ,mBAAmB;AAC1C,MAAIM,oBAAoBC,KAAK;AAC3B,WAAOD;AAAAA,EACT;AACA,QAAME,8BAAcD,IAAAA;AACpBH,QAAMJ,mBAAmB,IAAIQ;AAC7B,SAAOA;AACT;AAEA,MAAMC,oBAAoBN,qBAAAA;AAE1B,MAAMO,WAAWA,CAACC,UAAuC;AACvD,MAAI,CAACA,SAAS,OAAOA,UAAU,SAAU,QAAO;AAChD,QAAMC,QAAQD;AACd,SAAO,OAAOC,MAAMC,YAAY,YAAY,OAAOD,MAAME,WAAW;AACtE;AAEO,MAAMC,iBAAiBA,CAACC,WAA4B;AACzD,QAAMC,MAAMR,kBAAkBS,IAAIF,OAAOH,OAAO,yBAASM,IAAAA;AACzDF,MAAIG,IAAIJ,OAAOF,MAAM;AACrBL,oBAAkBQ,IAAID,OAAOH,SAASI,GAAG;AAC3C;AAEO,MAAMI,8BAA8BA,CAACC,YAA2C;AACrF,aAAW,CAACC,YAAYZ,KAAK,KAAKa,OAAOC,QAAQH,OAAO,GAAG;AACzD,QAAI,CAACZ,SAASC,KAAK,EAAG;AAEtB,QAAI,CAACY,WAAWG,SAAS,QAAQ,GAAG;AAClC,YAAM,IAAIC,MACR,+BAA+BJ,UAAU,sDAC3C;AAAA,IACF;AAEA,UAAMK,kBAAkBL,WAAWM,MAAM,GA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,IAAI,CAAA;AAE7F,QAAMnB,QAAQ+B,yBACVP,gBAAiBH,QAAQ,KAAK,CAAA,IAC9BQ,UAAUO,gBAAgBnC,SAASoB,QAAQ,IACzC,CAAC,OAAO,IACRC,8BAA8BC,MAAMF,QAAQ;AAElD,SAAO3B,aAAa;AAAA,IAClB2B;AAAAA,IACAQ,QAAQA,UAAU;AAAA,IAClB7B;AAAAA,IACA4B;AAAAA,EAAAA,CACD;AACH;AC5EO,MAAMS,uBAAuBA,CAClCC,SACAC,QACAtE,aAC4B;AAC5B,SAAOuE,aAAaF,SAASC,MAAM,EAAEE,OAAOxE,QAAO;AACrD;ACNO,MAAM8B,MAAM,CACjBuC,SACAC,QACAG,aACAC,WACY;AACZ,MAAIA,UAAU,OAAOA,WAAW,UAAU;AACxC,WAAOL,QAAQvC,IAAIwC,QAAQtE,QAAQyE,aAAaC,MAAM,CAA0B;AAAA,EAClF;AACA,SAAOL,QAAQvC,IAAIwC,QAAQG,WAAW;AACxC;"}
@@ -1,133 +0,0 @@
1
- import { z as z$1 } from "zod";
2
- const LANGUAGE_CODE_REGEX = /^[a-z]{2,3}(?:-[A-Za-z0-9]{2,8})*$/;
3
- const LOCALIZED_STRING_PROXY_CACHE = /* @__PURE__ */ new WeakMap();
4
- function normalizeLocale(locale) {
5
- const trimmed = locale.trim();
6
- if (!trimmed) return "";
7
- const getCanonicalLocales = Intl.getCanonicalLocales;
8
- if (typeof getCanonicalLocales !== "function") return trimmed;
9
- try {
10
- return getCanonicalLocales(trimmed)[0] ?? trimmed;
11
- } catch {
12
- return trimmed;
13
- }
14
- }
15
- function buildLocaleFallbackChain(locale, fallbacks) {
16
- const base = locale.trim();
17
- const canonical = normalizeLocale(base);
18
- const extra = typeof fallbacks === "string" ? [fallbacks] : fallbacks ?? [];
19
- const output = [];
20
- const seen = /* @__PURE__ */ new Set();
21
- const push = (value) => {
22
- if (!value) return;
23
- if (seen.has(value)) return;
24
- seen.add(value);
25
- output.push(value);
26
- };
27
- const addChain = (value) => {
28
- if (!value) return;
29
- const parts = value.split("-").filter(Boolean);
30
- while (parts.length > 0) {
31
- push(parts.join("-"));
32
- parts.pop();
33
- }
34
- };
35
- addChain(base);
36
- addChain(canonical);
37
- for (const fallback of extra) addChain(normalizeLocale(fallback));
38
- return output;
39
- }
40
- function resolveLocalizedString(value, locale, options) {
41
- if (!value) return void 0;
42
- const chain = buildLocaleFallbackChain(locale, options?.fallbacks);
43
- if (chain.length === 0) return void 0;
44
- const record = value;
45
- for (const key of chain) {
46
- if (!Object.prototype.hasOwnProperty.call(record, key)) continue;
47
- return record[key];
48
- }
49
- return void 0;
50
- }
51
- function withLocalizedStringFallback(value) {
52
- if (!value || typeof value !== "object" || Array.isArray(value)) return value;
53
- if (value instanceof Map) return value;
54
- const cached = LOCALIZED_STRING_PROXY_CACHE.get(value);
55
- if (cached) return cached;
56
- const proxy = withFallbackRecord(value);
57
- LOCALIZED_STRING_PROXY_CACHE.set(value, proxy);
58
- return proxy;
59
- }
60
- function withFallbackRecord(record) {
61
- const getExact = (key) => {
62
- const current = record[key];
63
- return typeof current === "string" ? current : void 0;
64
- };
65
- const hasExact = (key) => Object.prototype.hasOwnProperty.call(record, key);
66
- return new Proxy(record, {
67
- get(target, prop) {
68
- if (prop === "getExact") return getExact;
69
- if (prop === "hasExact") return hasExact;
70
- if (prop === "get") return (key) => resolveLocalizedString(target, key);
71
- if (typeof prop === "string" && !(prop in target)) {
72
- return resolveLocalizedString(target, prop);
73
- }
74
- return Reflect.get(target, prop);
75
- },
76
- set(target, prop, next) {
77
- return Reflect.set(target, prop, next);
78
- }
79
- });
80
- }
81
- const zLocalizedString = () => {
82
- const schema = z$1.record(z$1.string().regex(LANGUAGE_CODE_REGEX, {
83
- message: "Expected a language code (BCP 47, e.g. en or fr-FR)."
84
- }), z$1.string());
85
- return schema;
86
- };
87
- const zI18nString = zLocalizedString;
88
- const E164_PHONE_REGEX = /^\+[1-9]\d{1,14}$/;
89
- const E164_PHONE_OR_EMPTY_REGEX = /^(?:\+[1-9]\d{1,14})?$/;
90
- const makeZE164Phone = (zod, options) => {
91
- const allowEmpty = options?.allowEmpty ?? false;
92
- return zod.string().trim().regex(allowEmpty ? E164_PHONE_OR_EMPTY_REGEX : E164_PHONE_REGEX, {
93
- message: allowEmpty ? "Expected an empty string or a phone number in E.164 format (e.g. +33608707197)." : "Expected a phone number in E.164 format (e.g. +33608707197)."
94
- });
95
- };
96
- const zE164Phone = (options) => makeZE164Phone(z$1, options);
97
- let zodPrototypesExtended = false;
98
- function extendZod(zod) {
99
- const zodWithExtension = zod;
100
- if (Object.isExtensible(zodWithExtension) && typeof zodWithExtension.e164Phone !== "function") {
101
- zodWithExtension.e164Phone = (options) => makeZE164Phone(zod, options);
102
- }
103
- if (zodPrototypesExtended) return;
104
- zodPrototypesExtended = true;
105
- const supported = [zod.ZodString, zod.ZodNumber, zod.ZodDate];
106
- for (const type of supported) {
107
- const proto = type?.prototype;
108
- if (!proto) continue;
109
- proto.unique = function unique(_flag = true) {
110
- return this;
111
- };
112
- proto.sparse = function sparse(_flag = true) {
113
- return this;
114
- };
115
- }
116
- }
117
- const z = Object.create(z$1);
118
- extendZod(z);
119
- export {
120
- E164_PHONE_REGEX as E,
121
- LANGUAGE_CODE_REGEX as L,
122
- E164_PHONE_OR_EMPTY_REGEX as a,
123
- zE164Phone as b,
124
- buildLocaleFallbackChain as c,
125
- zLocalizedString as d,
126
- extendZod as e,
127
- zI18nString as f,
128
- makeZE164Phone as m,
129
- resolveLocalizedString as r,
130
- withLocalizedStringFallback as w,
131
- z
132
- };
133
- //# sourceMappingURL=index-g1_0RZ-U.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"index-g1_0RZ-U.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,KAAAA;AACpB,QAAMM,YAAYT,gBAAgBQ,IAAI;AACtC,QAAME,QAAQ,OAAOH,cAAc,WAAW,CAACA,SAAS,IAAKA,aAAa,CAAA;AAE1E,QAAMI,SAAmB,CAAA;AACzB,QAAMC,2BAAWC,IAAAA;AAEjB,QAAMC,OAAOA,CAACC,UAAkB;AAC9B,QAAI,CAACA,MAAO;AACZ,QAAIH,KAAKI,IAAID,KAAK,EAAG;AACrBH,SAAKK,IAAIF,KAAK;AACdJ,WAAOG,KAAKC,KAAK;AAA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AAO;AAAA,EAC1F;AAEA,MAAIuC,sBAAuB;AAC3BA,0BAAwB;AAExB,QAAMK,YAAY,CAACR,IAAIS,WAAWT,IAAIU,WAAWV,IAAIW,OAAO;AAC5D,aAAWC,QAAQJ,WAAW;AAC5B,UAAMK,QAAQD,MAAM1C;AACpB,QAAI,CAAC2C,MAAO;AAEZA,UAAMC,SAAS,SAASA,OAAsBC,QAAQ,MAAM;AAC1D,aAAO;AAAA,IACT;AAEAF,UAAMG,SAAS,SAASA,OAAsBD,QAAQ,MAAM;AAC1D,aAAO;AAAA,IACT;AAAA,EACF;AACF;AC5CO,MAAMvB,IAAIvB,OAAOgD,OAAOC,GAAK;AACpCd,UAAUZ,CAAC;"}
@@ -1 +0,0 @@
1
- {"version":3,"file":"index.browser.js","sources":[],"sourcesContent":[],"names":[],"mappings":";"}
package/dist/model.js.map DELETED
@@ -1 +0,0 @@
1
- {"version":3,"file":"model.js","sources":[],"sourcesContent":[],"names":[],"mappings":""}