@rpcbase/db 0.123.0 → 0.125.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/acl/index.js +109 -13
- package/dist/acl/index.js.map +1 -1
- package/dist/index.browser.js +2 -16
- package/dist/index.js +2122 -2461
- package/dist/index.js.map +1 -1
- package/dist/model.js +0 -2
- package/dist/mongo.js +34 -22
- package/dist/mongo.js.map +1 -1
- package/dist/zod-DSnhyNso.js +132 -0
- package/dist/zod-DSnhyNso.js.map +1 -0
- package/package.json +1 -1
- package/dist/can-B4pD_pnR.js +0 -132
- package/dist/can-B4pD_pnR.js.map +0 -1
- package/dist/index-DrIoUXc2.js +0 -142
- package/dist/index-DrIoUXc2.js.map +0 -1
- package/dist/index.browser.js.map +0 -1
- package/dist/model.js.map +0 -1
package/dist/model.js
CHANGED
package/dist/mongo.js
CHANGED
|
@@ -1,24 +1,36 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
1
|
+
//#region src/getMongoUrl.ts
|
|
2
|
+
var parseMongoDirectConnectionOverride = (env = process.env) => {
|
|
3
|
+
const rawValue = env.RB_MONGO_DIRECT_CONNECTION ?? env.MONGO_DIRECT_CONNECTION;
|
|
4
|
+
if (typeof rawValue !== "string") return null;
|
|
5
|
+
const value = rawValue.trim().toLowerCase();
|
|
6
|
+
if ([
|
|
7
|
+
"1",
|
|
8
|
+
"true",
|
|
9
|
+
"yes",
|
|
10
|
+
"on"
|
|
11
|
+
].includes(value)) return true;
|
|
12
|
+
if ([
|
|
13
|
+
"0",
|
|
14
|
+
"false",
|
|
15
|
+
"no",
|
|
16
|
+
"off"
|
|
17
|
+
].includes(value)) return false;
|
|
18
|
+
return null;
|
|
8
19
|
};
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
20
|
+
var getMongoDirectConnection = (env = process.env) => parseMongoDirectConnectionOverride(env) ?? [
|
|
21
|
+
"",
|
|
22
|
+
"localhost",
|
|
23
|
+
"127.0.0.1",
|
|
24
|
+
"::1"
|
|
25
|
+
].includes((env.DB_HOST ?? "").trim().toLowerCase());
|
|
26
|
+
var getMongoUrl = (env = process.env, options = {}) => {
|
|
27
|
+
const port = env.DB_PORT?.trim();
|
|
28
|
+
if (!port) throw new Error("Missing DB_PORT (required to build MongoDB connection URL)");
|
|
29
|
+
const baseUrl = `mongodb://${env.DB_HOST?.trim() || "localhost"}:${port}`;
|
|
30
|
+
const dbName = options.dbName?.trim();
|
|
31
|
+
return dbName ? `${baseUrl}/${dbName}` : baseUrl;
|
|
19
32
|
};
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
//# sourceMappingURL=mongo.js.map
|
|
33
|
+
//#endregion
|
|
34
|
+
export { getMongoDirectConnection, getMongoUrl };
|
|
35
|
+
|
|
36
|
+
//# sourceMappingURL=mongo.js.map
|
package/dist/mongo.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"mongo.js","sources":["../src/getMongoUrl.ts"],"sourcesContent":["type EnvRecord = { [key: string]: string | undefined }\n\ntype MongoUrlOptions = {\n dbName?: string\n}\n\nconst parseMongoDirectConnectionOverride = (env: EnvRecord = process.env): boolean | null => {\n const rawValue = env.RB_MONGO_DIRECT_CONNECTION ?? env.MONGO_DIRECT_CONNECTION\n if (typeof rawValue !== \"string\") return null\n\n const value = rawValue.trim().toLowerCase()\n if ([\"1\", \"true\", \"yes\", \"on\"].includes(value)) return true\n if ([\"0\", \"false\", \"no\", \"off\"].includes(value)) return false\n return null\n}\n\nexport const getMongoDirectConnection = (env: EnvRecord = process.env): boolean =>\n parseMongoDirectConnectionOverride(env) ??\n [\"\", \"localhost\", \"127.0.0.1\", \"::1\"].includes((env.DB_HOST ?? \"\").trim().toLowerCase())\n\nexport const getMongoUrl = (env: EnvRecord = process.env, options: MongoUrlOptions = {}): string => {\n const port = env.DB_PORT?.trim()\n if (!port) {\n throw new Error(\"Missing DB_PORT (required to build MongoDB connection URL)\")\n }\n\n const host = env.DB_HOST?.trim() || \"localhost\"\n const baseUrl = `mongodb://${host}:${port}`\n const dbName = options.dbName?.trim()\n\n return dbName ? `${baseUrl}/${dbName}` : baseUrl\n}\n"],"
|
|
1
|
+
{"version":3,"file":"mongo.js","names":["EnvRecord","key","MongoUrlOptions","dbName","parseMongoDirectConnectionOverride","env","process","rawValue","RB_MONGO_DIRECT_CONNECTION","MONGO_DIRECT_CONNECTION","value","trim","toLowerCase","includes","getMongoDirectConnection","DB_HOST","getMongoUrl","options","port","DB_PORT","Error","host","baseUrl"],"sources":["../src/getMongoUrl.ts"],"sourcesContent":["type EnvRecord = { [key: string]: string | undefined }\n\ntype MongoUrlOptions = {\n dbName?: string\n}\n\nconst parseMongoDirectConnectionOverride = (env: EnvRecord = process.env): boolean | null => {\n const rawValue = env.RB_MONGO_DIRECT_CONNECTION ?? env.MONGO_DIRECT_CONNECTION\n if (typeof rawValue !== \"string\") return null\n\n const value = rawValue.trim().toLowerCase()\n if ([\"1\", \"true\", \"yes\", \"on\"].includes(value)) return true\n if ([\"0\", \"false\", \"no\", \"off\"].includes(value)) return false\n return null\n}\n\nexport const getMongoDirectConnection = (env: EnvRecord = process.env): boolean =>\n parseMongoDirectConnectionOverride(env) ??\n [\"\", \"localhost\", \"127.0.0.1\", \"::1\"].includes((env.DB_HOST ?? \"\").trim().toLowerCase())\n\nexport const getMongoUrl = (env: EnvRecord = process.env, options: MongoUrlOptions = {}): string => {\n const port = env.DB_PORT?.trim()\n if (!port) {\n throw new Error(\"Missing DB_PORT (required to build MongoDB connection URL)\")\n }\n\n const host = env.DB_HOST?.trim() || \"localhost\"\n const baseUrl = `mongodb://${host}:${port}`\n const dbName = options.dbName?.trim()\n\n return dbName ? `${baseUrl}/${dbName}` : baseUrl\n}\n"],"mappings":";AAMA,IAAMI,sCAAsCC,MAAiBC,QAAQD,QAAwB;CAC3F,MAAME,WAAWF,IAAIG,8BAA8BH,IAAII;CACvD,IAAI,OAAOF,aAAa,UAAU,OAAO;CAEzC,MAAMG,QAAQH,SAASI,KAAK,CAAC,CAACC,YAAY;CAC1C,IAAI;EAAC;EAAK;EAAQ;EAAO;CAAI,CAAC,CAACC,SAASH,KAAK,GAAG,OAAO;CACvD,IAAI;EAAC;EAAK;EAAS;EAAM;CAAK,CAAC,CAACG,SAASH,KAAK,GAAG,OAAO;CACxD,OAAO;AACT;AAEA,IAAaI,4BAA4BT,MAAiBC,QAAQD,QAChED,mCAAmCC,GAAG,KACtC;CAAC;CAAI;CAAa;CAAa;AAAK,CAAC,CAACQ,UAAUR,IAAIU,WAAW,GAAA,CAAIJ,KAAK,CAAC,CAACC,YAAY,CAAC;AAEzF,IAAaI,eAAeX,MAAiBC,QAAQD,KAAKY,UAA2B,CAAC,MAAc;CAClG,MAAMC,OAAOb,IAAIc,SAASR,KAAK;CAC/B,IAAI,CAACO,MACH,MAAM,IAAIE,MAAM,4DAA4D;CAI9E,MAAME,UAAU,aADHjB,IAAIU,SAASJ,KAAK,KAAK,YACH,GAAIO;CACrC,MAAMf,SAASc,QAAQd,QAAQQ,KAAK;CAEpC,OAAOR,SAAS,GAAGmB,QAAO,GAAInB,WAAWmB;AAC3C"}
|
|
@@ -0,0 +1,132 @@
|
|
|
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
|
+
return resolveLocalizedStringEntry(value, locale, options)?.value;
|
|
43
|
+
}
|
|
44
|
+
function resolveLocalizedStringEntry(value, locale, options) {
|
|
45
|
+
if (!value) return void 0;
|
|
46
|
+
const chain = buildLocaleFallbackChain(locale, options?.fallbacks);
|
|
47
|
+
if (chain.length === 0) return void 0;
|
|
48
|
+
const record = value;
|
|
49
|
+
for (const key of chain) {
|
|
50
|
+
if (!Object.prototype.hasOwnProperty.call(record, key)) continue;
|
|
51
|
+
return record[key];
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
function withLocalizedStringFallback(value) {
|
|
55
|
+
if (!value || typeof value !== "object" || Array.isArray(value)) return value;
|
|
56
|
+
if (value instanceof Map) return value;
|
|
57
|
+
const cached = LOCALIZED_STRING_PROXY_CACHE.get(value);
|
|
58
|
+
if (cached) return cached;
|
|
59
|
+
const proxy = withFallbackRecord(value);
|
|
60
|
+
LOCALIZED_STRING_PROXY_CACHE.set(value, proxy);
|
|
61
|
+
return proxy;
|
|
62
|
+
}
|
|
63
|
+
function withFallbackRecord(record) {
|
|
64
|
+
const getExact = (key) => {
|
|
65
|
+
if (!hasExact(key)) return void 0;
|
|
66
|
+
return record[key];
|
|
67
|
+
};
|
|
68
|
+
const hasExact = (key) => Object.prototype.hasOwnProperty.call(record, key);
|
|
69
|
+
return new Proxy(record, {
|
|
70
|
+
get(target, prop) {
|
|
71
|
+
if (prop === "getExact") return getExact;
|
|
72
|
+
if (prop === "hasExact") return hasExact;
|
|
73
|
+
if (prop === "get") return (key) => resolveLocalizedStringEntry(target, key);
|
|
74
|
+
if (typeof prop === "string" && !(prop in target)) return resolveLocalizedStringEntry(target, prop);
|
|
75
|
+
return Reflect.get(target, prop);
|
|
76
|
+
},
|
|
77
|
+
set(target, prop, next) {
|
|
78
|
+
return Reflect.set(target, prop, next);
|
|
79
|
+
}
|
|
80
|
+
});
|
|
81
|
+
}
|
|
82
|
+
var zLocalizedString = () => {
|
|
83
|
+
const languageCodeSchema = z.string().regex(LANGUAGE_CODE_REGEX, { message: "Expected a language code (BCP 47, e.g. en or fr-FR)." });
|
|
84
|
+
const entrySchema = z.object({
|
|
85
|
+
value: z.string(),
|
|
86
|
+
updatedAt: z.date(),
|
|
87
|
+
autoTranslated: z.boolean().optional()
|
|
88
|
+
}).passthrough();
|
|
89
|
+
return z.record(languageCodeSchema, entrySchema);
|
|
90
|
+
};
|
|
91
|
+
var zI18nString = zLocalizedString;
|
|
92
|
+
//#endregion
|
|
93
|
+
//#region src/zod/e164Phone.ts
|
|
94
|
+
var E164_PHONE_REGEX = /^\+[1-9]\d{1,14}$/;
|
|
95
|
+
var E164_PHONE_OR_EMPTY_REGEX = /^(?:\+[1-9]\d{1,14})?$/;
|
|
96
|
+
var makeZE164Phone = (zod, options) => {
|
|
97
|
+
const allowEmpty = options?.allowEmpty ?? false;
|
|
98
|
+
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)." });
|
|
99
|
+
};
|
|
100
|
+
var zE164Phone = (options) => makeZE164Phone(z, options);
|
|
101
|
+
//#endregion
|
|
102
|
+
//#region src/zod/extension.ts
|
|
103
|
+
var zodPrototypesExtended = false;
|
|
104
|
+
function extendZod(zod) {
|
|
105
|
+
const zodWithExtension = zod;
|
|
106
|
+
if (Object.isExtensible(zodWithExtension) && typeof zodWithExtension.e164Phone !== "function") zodWithExtension.e164Phone = (options) => makeZE164Phone(zod, options);
|
|
107
|
+
if (zodPrototypesExtended) return;
|
|
108
|
+
zodPrototypesExtended = true;
|
|
109
|
+
const supported = [
|
|
110
|
+
zod.ZodString,
|
|
111
|
+
zod.ZodNumber,
|
|
112
|
+
zod.ZodDate
|
|
113
|
+
];
|
|
114
|
+
for (const type of supported) {
|
|
115
|
+
const proto = type?.prototype;
|
|
116
|
+
if (!proto) continue;
|
|
117
|
+
proto.unique = function unique(_flag = true) {
|
|
118
|
+
return this;
|
|
119
|
+
};
|
|
120
|
+
proto.sparse = function sparse(_flag = true) {
|
|
121
|
+
return this;
|
|
122
|
+
};
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
//#endregion
|
|
126
|
+
//#region src/zod/index.ts
|
|
127
|
+
var z$1 = Object.create(z);
|
|
128
|
+
extendZod(z$1);
|
|
129
|
+
//#endregion
|
|
130
|
+
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 };
|
|
131
|
+
|
|
132
|
+
//# sourceMappingURL=zod-DSnhyNso.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"zod-DSnhyNso.js","names":["z","LanguageCode","LocalizedStringEntry","Record","value","updatedAt","Date","autoTranslated","TExtra","LocalizedString","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","has","add","addChain","parts","split","filter","Boolean","length","join","pop","fallback","resolveLocalizedString","options","resolveLocalizedStringEntry","undefined","chain","record","Object","prototype","hasOwnProperty","call","withLocalizedStringFallback","T","Array","isArray","Map","cached","proxy","withFallbackRecord","set","Proxy","target","prop","Reflect","next","zLocalizedString","languageCodeSchema","string","regex","message","entrySchema","object","date","boolean","optional","passthrough","schema","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 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"],"mappings":";;AA0BA,IAAaiB,sBAAsB;AAEnC,IAAMC,+CAA+B,IAAIC,QAAyB;AAElE,SAASC,gBAAgBC,QAAwB;CAC/C,MAAMC,UAAUD,OAAOE,KAAK;CAC5B,IAAI,CAACD,SAAS,OAAO;CACrB,MAAME,sBAAuBC,KAE1BD;CACH,IAAI,OAAOA,wBAAwB,YAAY,OAAOF;CACtD,IAAI;EACF,OAAOE,oBAAoBF,OAAO,CAAC,CAAC,MAAMA;CAC5C,QAAQ;EACN,OAAOA;CACT;AACF;AAEA,SAAgBK,yBAAyBN,QAAgBO,WAAyC;CAChG,MAAMC,OAAOR,OAAOE,KAAK;CACzB,MAAMO,YAAYV,gBAAgBS,IAAI;CACtC,MAAME,QAAQ,OAAOH,cAAc,WAAW,CAACA,SAAS,IAAKA,aAAa,CAAA;CAE1E,MAAMI,SAAmB,CAAA;CACzB,MAAMC,uBAAO,IAAIC,IAAY;CAE7B,MAAMC,QAAQ/B,UAAkB;EAC9B,IAAI,CAACA,OAAO;EACZ,IAAI6B,KAAKG,IAAIhC,KAAK,GAAG;EACrB6B,KAAKI,IAAIjC,KAAK;EACd4B,OAAOG,KAAK/B,KAAK;CACnB;CAEA,MAAMkC,YAAYlC,UAAkB;EAClC,IAAI,CAACA,OAAO;EACZ,MAAMmC,QAAQnC,MAAMoC,MA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OC,aAAaF,gBAAgB,KAAK,OAAOA,iBAAiBb,cAAc,YACjFa,iBAAiBb,aAAaC,YAA+BH,eAAec,KAAKX,OAAO;CAG1F,IAAIS,uBAAuB;CAC3BA,wBAAwB;CAExB,MAAMM,YAAY;EAACJ,IAAIV;EAAWU,IAAIJ;EAAWI,IAAIH;CAAO;CAC5D,KAAK,MAAMS,QAAQF,WAAW;EAC5B,MAAMG,QAAQD,MAAME;EACpB,IAAI,CAACD,OAAO;EAEZA,MAAMf,SAAS,SAASA,OAAsBiB,QAAQ,MAAM;GAC1D,OAAO;EACT;EAEAF,MAAMZ,SAAS,SAASA,OAAsBc,QAAQ,MAAM;GAC1D,OAAO;EACT;CACF;AACF;;;AC5CA,IAAaC,MAAIU,OAAOC,OAAOV,CAAK;AACpCI,UAAUL,GAAC"}
|
package/package.json
CHANGED
package/dist/can-B4pD_pnR.js
DELETED
|
@@ -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
|
-
buildAbilityFromSession as a,
|
|
123
|
-
buildAbility as b,
|
|
124
|
-
can as c,
|
|
125
|
-
getRegisteredPolicies as d,
|
|
126
|
-
getTenantRolesFromSessionUser as e,
|
|
127
|
-
registerPolicy as f,
|
|
128
|
-
getAccessibleByQuery as g,
|
|
129
|
-
hasRegisteredPolicy as h,
|
|
130
|
-
registerPoliciesFromModules as r
|
|
131
|
-
};
|
|
132
|
-
//# sourceMappingURL=can-B4pD_pnR.js.map
|
package/dist/can-B4pD_pnR.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"can-B4pD_pnR.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,GAAG,CAAE,SAASC,MAAO;AAC9D,QAAInB,MAAME,YAAYe,iBAAiB;AACrC,YAAM,IAAID,MACR,mBAAmBJ,UAAU,wBAAwBK,eAAe,WAAWjB,MAAME,OAAO,IAC9F;AAAA,IACF;AAEAE,mBAAeJ,KAAK;AAAA,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AAa;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;"}
|
package/dist/index-DrIoUXc2.js
DELETED
|
@@ -1,142 +0,0 @@
|
|
|
1
|
-
import { z as z$1 } from "zod";
|
|
2
|
-
const E164_PHONE_REGEX = /^\+[1-9]\d{1,14}$/;
|
|
3
|
-
const E164_PHONE_OR_EMPTY_REGEX = /^(?:\+[1-9]\d{1,14})?$/;
|
|
4
|
-
const makeZE164Phone = (zod, options) => {
|
|
5
|
-
const allowEmpty = options?.allowEmpty ?? false;
|
|
6
|
-
return zod.string().trim().regex(allowEmpty ? E164_PHONE_OR_EMPTY_REGEX : E164_PHONE_REGEX, {
|
|
7
|
-
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)."
|
|
8
|
-
});
|
|
9
|
-
};
|
|
10
|
-
const zE164Phone = (options) => makeZE164Phone(z$1, options);
|
|
11
|
-
let zodPrototypesExtended = false;
|
|
12
|
-
function extendZod(zod) {
|
|
13
|
-
const zodWithExtension = zod;
|
|
14
|
-
if (Object.isExtensible(zodWithExtension) && typeof zodWithExtension.e164Phone !== "function") {
|
|
15
|
-
zodWithExtension.e164Phone = (options) => makeZE164Phone(zod, options);
|
|
16
|
-
}
|
|
17
|
-
if (zodPrototypesExtended) return;
|
|
18
|
-
zodPrototypesExtended = true;
|
|
19
|
-
const supported = [zod.ZodString, zod.ZodNumber, zod.ZodDate];
|
|
20
|
-
for (const type of supported) {
|
|
21
|
-
const proto = type?.prototype;
|
|
22
|
-
if (!proto) continue;
|
|
23
|
-
proto.unique = function unique(_flag = true) {
|
|
24
|
-
return this;
|
|
25
|
-
};
|
|
26
|
-
proto.sparse = function sparse(_flag = true) {
|
|
27
|
-
return this;
|
|
28
|
-
};
|
|
29
|
-
}
|
|
30
|
-
}
|
|
31
|
-
const LANGUAGE_CODE_REGEX = /^[a-z]{2,3}(?:-[A-Za-z0-9]{2,8})*$/;
|
|
32
|
-
const LOCALIZED_STRING_PROXY_CACHE = /* @__PURE__ */ new WeakMap();
|
|
33
|
-
function normalizeLocale(locale) {
|
|
34
|
-
const trimmed = locale.trim();
|
|
35
|
-
if (!trimmed) return "";
|
|
36
|
-
const getCanonicalLocales = Intl.getCanonicalLocales;
|
|
37
|
-
if (typeof getCanonicalLocales !== "function") return trimmed;
|
|
38
|
-
try {
|
|
39
|
-
return getCanonicalLocales(trimmed)[0] ?? trimmed;
|
|
40
|
-
} catch {
|
|
41
|
-
return trimmed;
|
|
42
|
-
}
|
|
43
|
-
}
|
|
44
|
-
function buildLocaleFallbackChain(locale, fallbacks) {
|
|
45
|
-
const base = locale.trim();
|
|
46
|
-
const canonical = normalizeLocale(base);
|
|
47
|
-
const extra = typeof fallbacks === "string" ? [fallbacks] : fallbacks ?? [];
|
|
48
|
-
const output = [];
|
|
49
|
-
const seen = /* @__PURE__ */ new Set();
|
|
50
|
-
const push = (value) => {
|
|
51
|
-
if (!value) return;
|
|
52
|
-
if (seen.has(value)) return;
|
|
53
|
-
seen.add(value);
|
|
54
|
-
output.push(value);
|
|
55
|
-
};
|
|
56
|
-
const addChain = (value) => {
|
|
57
|
-
if (!value) return;
|
|
58
|
-
const parts = value.split("-").filter(Boolean);
|
|
59
|
-
while (parts.length > 0) {
|
|
60
|
-
push(parts.join("-"));
|
|
61
|
-
parts.pop();
|
|
62
|
-
}
|
|
63
|
-
};
|
|
64
|
-
addChain(base);
|
|
65
|
-
addChain(canonical);
|
|
66
|
-
for (const fallback of extra) addChain(normalizeLocale(fallback));
|
|
67
|
-
return output;
|
|
68
|
-
}
|
|
69
|
-
function resolveLocalizedString(value, locale, options) {
|
|
70
|
-
return resolveLocalizedStringEntry(value, locale, options)?.value;
|
|
71
|
-
}
|
|
72
|
-
function resolveLocalizedStringEntry(value, locale, options) {
|
|
73
|
-
if (!value) return void 0;
|
|
74
|
-
const chain = buildLocaleFallbackChain(locale, options?.fallbacks);
|
|
75
|
-
if (chain.length === 0) return void 0;
|
|
76
|
-
const record = value;
|
|
77
|
-
for (const key of chain) {
|
|
78
|
-
if (!Object.prototype.hasOwnProperty.call(record, key)) continue;
|
|
79
|
-
return record[key];
|
|
80
|
-
}
|
|
81
|
-
return void 0;
|
|
82
|
-
}
|
|
83
|
-
function withLocalizedStringFallback(value) {
|
|
84
|
-
if (!value || typeof value !== "object" || Array.isArray(value)) return value;
|
|
85
|
-
if (value instanceof Map) return value;
|
|
86
|
-
const cached = LOCALIZED_STRING_PROXY_CACHE.get(value);
|
|
87
|
-
if (cached) return cached;
|
|
88
|
-
const proxy = withFallbackRecord(value);
|
|
89
|
-
LOCALIZED_STRING_PROXY_CACHE.set(value, proxy);
|
|
90
|
-
return proxy;
|
|
91
|
-
}
|
|
92
|
-
function withFallbackRecord(record) {
|
|
93
|
-
const getExact = (key) => {
|
|
94
|
-
if (!hasExact(key)) return void 0;
|
|
95
|
-
return record[key];
|
|
96
|
-
};
|
|
97
|
-
const hasExact = (key) => Object.prototype.hasOwnProperty.call(record, key);
|
|
98
|
-
return new Proxy(record, {
|
|
99
|
-
get(target, prop) {
|
|
100
|
-
if (prop === "getExact") return getExact;
|
|
101
|
-
if (prop === "hasExact") return hasExact;
|
|
102
|
-
if (prop === "get") return (key) => resolveLocalizedStringEntry(target, key);
|
|
103
|
-
if (typeof prop === "string" && !(prop in target)) {
|
|
104
|
-
return resolveLocalizedStringEntry(target, prop);
|
|
105
|
-
}
|
|
106
|
-
return Reflect.get(target, prop);
|
|
107
|
-
},
|
|
108
|
-
set(target, prop, next) {
|
|
109
|
-
return Reflect.set(target, prop, next);
|
|
110
|
-
}
|
|
111
|
-
});
|
|
112
|
-
}
|
|
113
|
-
const zLocalizedString = () => {
|
|
114
|
-
const languageCodeSchema = z$1.string().regex(LANGUAGE_CODE_REGEX, {
|
|
115
|
-
message: "Expected a language code (BCP 47, e.g. en or fr-FR)."
|
|
116
|
-
});
|
|
117
|
-
const entrySchema = z$1.object({
|
|
118
|
-
value: z$1.string(),
|
|
119
|
-
updatedAt: z$1.date(),
|
|
120
|
-
autoTranslated: z$1.boolean().optional()
|
|
121
|
-
}).passthrough();
|
|
122
|
-
const schema = z$1.record(languageCodeSchema, entrySchema);
|
|
123
|
-
return schema;
|
|
124
|
-
};
|
|
125
|
-
const zI18nString = zLocalizedString;
|
|
126
|
-
const z = Object.create(z$1);
|
|
127
|
-
extendZod(z);
|
|
128
|
-
export {
|
|
129
|
-
E164_PHONE_OR_EMPTY_REGEX as E,
|
|
130
|
-
LANGUAGE_CODE_REGEX as L,
|
|
131
|
-
E164_PHONE_REGEX as a,
|
|
132
|
-
buildLocaleFallbackChain as b,
|
|
133
|
-
zE164Phone as c,
|
|
134
|
-
zI18nString as d,
|
|
135
|
-
extendZod as e,
|
|
136
|
-
zLocalizedString as f,
|
|
137
|
-
makeZE164Phone as m,
|
|
138
|
-
resolveLocalizedString as r,
|
|
139
|
-
withLocalizedStringFallback as w,
|
|
140
|
-
z
|
|
141
|
-
};
|
|
142
|
-
//# sourceMappingURL=index-DrIoUXc2.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"index-DrIoUXc2.js","sources":["../src/zod/e164Phone.ts","../src/zod/extension.ts","../src/zod/localizedString.ts","../src/zod/index.ts"],"sourcesContent":["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 } 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 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":["E164_PHONE_REGEX","E164_PHONE_OR_EMPTY_REGEX","makeZE164Phone","zod","options","allowEmpty","string","trim","regex","message","zE164Phone","z","zodPrototypesExtended","extendZod","zodWithExtension","Object","isExtensible","e164Phone","supported","ZodString","ZodNumber","ZodDate","type","proto","prototype","unique","_flag","sparse","LANGUAGE_CODE_REGEX","LOCALIZED_STRING_PROXY_CACHE","WeakMap","normalizeLocale","locale","trimmed","getCanonicalLocales","Intl","buildLocaleFallbackChain","fallbacks","base","canonical","extra","output","seen","Set","push","value","has","add","addChain","parts","split","filter","Boolean","length","join","pop","fallback","resolveLocalizedString","resolveLocalizedStringEntry","undefined","chain","record","key","hasOwnProperty","call","withLocalizedStringFallback","Array","isArray","Map","cached","get","proxy","withFallbackRecord","set","getExact","hasExact","Proxy","target","prop","Reflect","next","zLocalizedString","languageCodeSchema","entrySchema","object","updatedAt","date","autoTranslated","boolean","optional","passthrough","schema","zI18nString","create","baseZ"],"mappings":";AAGO,MAAMA,mBAAmB;AACzB,MAAMC,4BAA4B;AAMlC,MAAMC,iBAAiBA,CAACC,KAAeC,YAA+B;AAC3E,QAAMC,aAAaD,SAASC,cAAc;AAC1C,SAAOF,IAAIG,SAASC,KAAAA,EAAOC,MAAMH,aAAaJ,4BAA4BD,kBAAk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AAOC,GAAK;AACpCjF,UAAUF,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":""}
|