prototypey.org - atproto lexicon typescript toolkit - mirror https://github.com/tylersayshi/prototypey
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

provide langString helper

Allows easy grouping of translations with their default, for clarity.

authored by

JP Hastings-Spital and committed by
Tyler Lawson
59305872 b23cb325

+46 -21
+5 -1
packages/prototypey/README.md
··· 82 82 ```ts 83 83 const authCore = lx.lexicon("com.example.authCore", { 84 84 main: lx.permissionSet({ 85 - title: "MyApp: Core functionality", 85 + // Offer translations with langString 86 + title: lx.langString("MyApp: Core functionality", { 87 + "es": "MyApp: Funciones centrales", 88 + }), 89 + // A string provides no translations 86 90 detail: "The core functionality for MyApp", 87 91 permissions: [ 88 92 lx.repoPermission({
+37 -14
packages/prototypey/core/lib.ts
··· 357 357 } 358 358 359 359 /** 360 + * A translatable string with optional language variants. 361 + * Used particularly permission-sets. 362 + */ 363 + type LangString = { 364 + default: string; 365 + lang: Record<string, string>; 366 + }; 367 + 368 + /** A plain string or a translatable string with language variants. */ 369 + type Translatable = string | LangString; 370 + 371 + function resolveTranslatable(value: Translatable): { 372 + value: string; 373 + lang?: Record<string, string>; 374 + } { 375 + if (typeof value === "string") return { value }; 376 + return { value: value.default, lang: value.lang }; 377 + } 378 + 379 + /** 360 380 * Options for a repo-resource permission. 361 381 * @see https://atproto.com/specs/permission#repo 362 382 */ ··· 479 499 * @see https://atproto.com/specs/permission 480 500 */ 481 501 type PermissionSetOptions = { 482 - /** Human-readable title */ 483 - title: string; 484 - /** Internationalized title translations */ 485 - "title:lang"?: Record<string, string>; 486 - /** Human-readable detail/description of what this permission set grants */ 487 - detail: string; 488 - /** Internationalized detail translations */ 489 - "detail:lang"?: Record<string, string>; 502 + /** Human-readable title, optionally translatable via lx.langString() */ 503 + title: Translatable; 504 + /** Human-readable detail, optionally translatable via lx.langString() */ 505 + detail: Translatable; 490 506 /** List of permissions in this set */ 491 507 permissions: PermissionEntry[]; 492 508 /** Human-readable description */ ··· 828 844 }; 829 845 }, 830 846 /** 847 + * Creates a translatable string with language variants. 848 + * @see https://atproto.com/specs/permission 849 + */ 850 + langString(defaultValue: string, lang: Record<string, string>): LangString { 851 + return { default: defaultValue, lang }; 852 + }, 853 + /** 831 854 * Creates a permission-set definition. 832 855 * @see https://atproto.com/specs/permission#permission-sets 833 856 */ 834 857 permissionSet(options: PermissionSetOptions) { 858 + const title = resolveTranslatable(options.title); 859 + const detail = resolveTranslatable(options.detail); 835 860 return { 836 861 type: "permission-set" as const, 837 862 key: "literal:self" as const, 838 - title: options.title, 839 - ...(options["title:lang"] ? { "title:lang": options["title:lang"] } : {}), 840 - detail: options.detail, 841 - ...(options["detail:lang"] 842 - ? { "detail:lang": options["detail:lang"] } 843 - : {}), 863 + title: title.value, 864 + ...(title.lang ? { "title:lang": title.lang } : {}), 865 + detail: detail.value, 866 + ...(detail.lang ? { "detail:lang": detail.lang } : {}), 844 867 permissions: options.permissions, 845 868 ...(options.description ? { description: options.description } : {}), 846 869 };
+4 -6
packages/prototypey/core/tests/permission-set.test.ts
··· 176 176 test("permission-set with i18n fields", () => { 177 177 const permSet = lx.lexicon("com.example.i18nPerms", { 178 178 main: lx.permissionSet({ 179 - title: "My App: Core", 180 - "title:lang": { 179 + title: lx.langString("My App: Core", { 181 180 es: "Mi App: Núcleo", 182 181 fr: "Mon App: Noyau", 183 - }, 184 - detail: "Core functionality", 185 - "detail:lang": { 182 + }), 183 + detail: lx.langString("Core functionality", { 186 184 es: "Funcionalidad principal", 187 185 fr: "Fonctionnalité principale", 188 - }, 186 + }), 189 187 permissions: [ 190 188 lx.repoPermission({ 191 189 collection: ["com.example.post"],