@inlang/paraglide-js 2.3.1 → 2.3.2

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.
@@ -15,10 +15,10 @@
15
15
  * @example
16
16
  * setLocale('en', { reload: false });
17
17
  *
18
- * @type {(newLocale: Locale, options?: { reload?: boolean }) => Promise<void> | void}
18
+ * @type {(newLocale: Locale, options?: { reload?: boolean }) => Promise<any> | void}
19
19
  */
20
20
  export let setLocale: (newLocale: Locale, options?: {
21
21
  reload?: boolean;
22
- }) => Promise<void> | void;
22
+ }) => Promise<any> | void;
23
23
  export function overwriteSetLocale(fn: (newLocale: Locale) => void): void;
24
24
  //# sourceMappingURL=set-locale.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"set-locale.d.ts","sourceRoot":"","sources":["../../../src/compiler/runtime/set-locale.js"],"names":[],"mappings":"AAgCA;;;;;;;;;;;;;;;;;;GAkBG;AACH,sBAFU,CAAC,SAAS,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE;IAAE,MAAM,CAAC,EAAE,OAAO,CAAA;CAAE,KAAK,OAAO,CAAC,IAAI,CAAC,GAAG,IAAI,CAmGnF;AAgBK,uCAFI,CAAC,SAAS,EAAE,MAAM,KAAK,IAAI,QAIrC"}
1
+ {"version":3,"file":"set-locale.d.ts","sourceRoot":"","sources":["../../../src/compiler/runtime/set-locale.js"],"names":[],"mappings":"AAgCA;;;;;;;;;;;;;;;;;;GAkBG;AACH,sBAFU,CAAC,SAAS,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE;IAAE,MAAM,CAAC,EAAE,OAAO,CAAA;CAAE,KAAK,OAAO,CAAC,GAAG,CAAC,GAAG,IAAI,CAuGlF;AAgBK,uCAFI,CAAC,SAAS,EAAE,MAAM,KAAK,IAAI,QAIrC"}
@@ -35,7 +35,7 @@ const navigateOrReload = (newLocation) => {
35
35
  * @example
36
36
  * setLocale('en', { reload: false });
37
37
  *
38
- * @type {(newLocale: Locale, options?: { reload?: boolean }) => Promise<void> | void}
38
+ * @type {(newLocale: Locale, options?: { reload?: boolean }) => Promise<any> | void}
39
39
  */
40
40
  export let setLocale = (newLocale, options) => {
41
41
  const optionsWithDefaults = {
@@ -102,11 +102,13 @@ export let setLocale = (newLocale, options) => {
102
102
  else if (isCustomStrategy(strat) && customClientStrategies.has(strat)) {
103
103
  const handler = customClientStrategies.get(strat);
104
104
  if (handler) {
105
- const result = handler.setLocale(newLocale);
105
+ let result = handler.setLocale(newLocale);
106
106
  // Handle async setLocale - fire and forget
107
107
  if (result instanceof Promise) {
108
- result.catch((error) => {
109
- error.message = `Custom strategy "${strat}" setLocale failed: ${error.message}`;
108
+ result = result.catch((error) => {
109
+ throw new Error(`Custom strategy "${strat}" setLocale failed.`, {
110
+ cause: error,
111
+ });
110
112
  });
111
113
  customSetLocalePromises.push(result);
112
114
  }
@@ -127,6 +129,9 @@ export let setLocale = (newLocale, options) => {
127
129
  navigateOrReload(newLocation);
128
130
  }
129
131
  }
132
+ else if (customSetLocalePromises.length) {
133
+ return Promise.all(customSetLocalePromises);
134
+ }
130
135
  return;
131
136
  };
132
137
  /**
@@ -317,6 +317,31 @@ test("calls setLocale on multiple custom strategies", async () => {
317
317
  expect(customLocale1).toBe("de");
318
318
  expect(customLocale2).toBe("de");
319
319
  });
320
+ test("setLocale should return a promise if any custom setLocale function is async", async () => {
321
+ let customLocale1 = "en";
322
+ const runtime = await createParaglide({
323
+ blob: await newProject({
324
+ settings: {
325
+ baseLocale: "en",
326
+ locales: ["en", "fr", "de"],
327
+ },
328
+ }),
329
+ strategy: ["custom-async", "baseLocale"],
330
+ isServer: "false",
331
+ });
332
+ runtime.defineCustomClientStrategy("custom-async", {
333
+ getLocale: () => customLocale1,
334
+ setLocale: async (locale) => {
335
+ customLocale1 = locale;
336
+ },
337
+ });
338
+ const setLocalePromise = runtime.setLocale("de");
339
+ const setLocalePromiseWithoutReload = runtime.setLocale("de", {
340
+ reload: false,
341
+ });
342
+ expect(setLocalePromise).toBeInstanceOf(Promise);
343
+ expect(setLocalePromiseWithoutReload).toBeInstanceOf(Promise);
344
+ });
320
345
  test("awaits async setLocale functions to resolve in custom strategy", async () => {
321
346
  let customLocale1 = "en";
322
347
  globalThis.window = {
@@ -408,7 +433,9 @@ test("reload should not run if async setLocale function rejects in custom strate
408
433
  throw new Error("fetch error");
409
434
  },
410
435
  });
411
- await expect(() => runtime.setLocale("de")).rejects.toThrowError(`Custom strategy "custom-async" setLocale failed: fetch error`);
436
+ const error = expect(() => runtime.setLocale("de")).rejects;
437
+ await error.toThrowError(`Custom strategy "custom-async" setLocale failed.`);
438
+ await error.toMatchObject({ cause: { message: "fetch error" } });
412
439
  // Verify that reload was never called
413
440
  expect(window.location.reload).toHaveBeenCalledTimes(0);
414
441
  expect(customLocale1).toBe("en");
@@ -1,5 +1,5 @@
1
1
  export const ENV_VARIABLES = {
2
2
  PARJS_APP_ID: "library.inlang.paraglideJs",
3
3
  PARJS_POSTHOG_TOKEN: undefined,
4
- PARJS_PACKAGE_VERSION: "2.3.0",
4
+ PARJS_PACKAGE_VERSION: "2.3.2",
5
5
  };
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@inlang/paraglide-js",
3
3
  "type": "module",
4
- "version": "2.3.1",
4
+ "version": "2.3.2",
5
5
  "license": "MIT",
6
6
  "publishConfig": {
7
7
  "access": "public",