Skip to content

Commit

Permalink
feat: add a more modern proxy package to resolve module resolution pr…
Browse files Browse the repository at this point in the history
…oblems
  • Loading branch information
ceopaludetto committed May 30, 2024
1 parent 7b34499 commit 12e38b2
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 25 deletions.
10 changes: 10 additions & 0 deletions .changeset/soft-flies-itch.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
---
"@diacritic/runtime": patch
"@diacritic/core": patch
"@diacritic/detector": patch
"@diacritic/parser": patch
"@diacritic/react": patch
"@diacritic/utilities": patch
---

Add a more standard package to deal with the proxy to avoid conflicts in legacy modern resolutions
2 changes: 1 addition & 1 deletion packages/runtime/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
},
"dependencies": {
"@diacritic/utilities": "workspace:*",
"proxy-deep": "^4.0.1",
"@qiwi/deep-proxy": "^3.0.0",
"tiny-invariant": "^1.3.3"
}
}
39 changes: 23 additions & 16 deletions packages/runtime/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,31 @@
import type { Proxy } from "~translations/proxy";

import { capitalizeFirst } from "@diacritic/utilities";
import { DeepProxy } from "proxy-deep";
import { toCamelCase } from "@diacritic/utilities";
import { DeepProxy } from "@qiwi/deep-proxy";

type Registry = typeof import("~translations/registry");

export type Language = Registry["languages"][number];
export type Namespace = Registry["namespaces"][number];

function createProxy(language: Language, modules: Record<Language, Record<Namespace, any>>) {
const proxy: Proxy = new DeepProxy({}, {
get(_, property) {
if (typeof property !== "string") return;
function emptyFn() {}

return this.nest(() => {});
},
apply(_, __, args) {
const [namespace, ...path] = this.path;
function createProxy(language: Language, modules: Record<Language, Record<Namespace, any>>) {
const proxy: Proxy = new DeepProxy({}, ({ trapName, path, args, DEFAULT, PROXY }) => {
if (trapName === "set") throw new TypeError("Cannot set properties on a Diacritic proxy object");

if (trapName === "get") {
if (path.length === 0 && !modules[language]) console.warn(`[Diacritic] language ${language} is not loaded`);

const [namespace] = path;
if (namespace && !modules[language]?.[namespace as Namespace])
console.warn(`[Diacritic] namespace ${namespace} is not loaded in language ${language}`);

return PROXY(emptyFn);
}

if (trapName === "apply") {
const [namespace, ...rest] = path;

if (!namespace) throw new Error("Namespace is not specified");

Expand All @@ -26,18 +35,16 @@ function createProxy(language: Language, modules: Record<Language, Record<Namesp
if (!modules[language]![namespace as Language])
throw new Error(`Namespace ${namespace} is not loaded`);

const name = path.reduce((acc, item, index) => {
if (index === 0) return item;
return acc + capitalizeFirst(item);
}, "");

const name = toCamelCase(rest.join("-"));
const fn: any = modules[language]![namespace as Namespace][name];

if (typeof fn !== "function")
throw new Error(`Function ${name} is not defined in namespace ${namespace}`);

return fn(proxy, ...args);
},
}

return DEFAULT;
});

return proxy;
Expand Down
2 changes: 2 additions & 0 deletions packages/runtime/test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ it("should load new modules correctly", async () => {
it("should call translation functions correctly", async () => {
importTranslationModule.mockResolvedValue({
hello: () => "world",
someReallyDeepFunction: () => "deep",
});

const runtime = new Diacritic(registry, "en");
Expand All @@ -84,4 +85,5 @@ it("should call translation functions correctly", async () => {
runtime.setLanguage("en");

expect((runtime.t as any).common.hello()).toBe("world");
expect((runtime.t as any).common.some.really.deep.function()).toBe("deep");
});
16 changes: 8 additions & 8 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 12e38b2

Please sign in to comment.