@inlang/paraglide-js 2.0.11 → 2.0.13

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.
Files changed (37) hide show
  1. package/dist/cli/steps/initialize-inlang-project.d.ts.map +1 -1
  2. package/dist/cli/utils.d.ts.map +1 -1
  3. package/dist/compiler/compile-bundle.d.ts.map +1 -1
  4. package/dist/compiler/compile-message.d.ts.map +1 -1
  5. package/dist/compiler/compile-pattern.d.ts.map +1 -1
  6. package/dist/compiler/compile-project.d.ts.map +1 -1
  7. package/dist/compiler/create-paraglide.d.ts +20 -14
  8. package/dist/compiler/create-paraglide.d.ts.map +1 -1
  9. package/dist/compiler/create-paraglide.js +18 -14
  10. package/dist/compiler/create-paraglide.test.js +7 -11
  11. package/dist/compiler/index.d.ts +1 -1
  12. package/dist/compiler/index.d.ts.map +1 -1
  13. package/dist/compiler/index.js +1 -1
  14. package/dist/compiler/runtime/assert-is-locale.test.js +3 -3
  15. package/dist/compiler/runtime/extract-locale-from-cookie.test.js +7 -11
  16. package/dist/compiler/runtime/extract-locale-from-request.test.js +57 -77
  17. package/dist/compiler/runtime/extract-locale-from-url.d.ts.map +1 -1
  18. package/dist/compiler/runtime/extract-locale-from-url.js +35 -13
  19. package/dist/compiler/runtime/extract-locale-from-url.test.js +51 -61
  20. package/dist/compiler/runtime/generate-static-localized-urls.test.js +48 -58
  21. package/dist/compiler/runtime/get-locale.test.js +52 -68
  22. package/dist/compiler/runtime/get-url-origin.test.js +2 -4
  23. package/dist/compiler/runtime/localize-href.d.ts.map +1 -1
  24. package/dist/compiler/runtime/localize-href.js +7 -4
  25. package/dist/compiler/runtime/localize-href.test.js +48 -58
  26. package/dist/compiler/runtime/localize-url.test.js +258 -294
  27. package/dist/compiler/runtime/set-locale.test.js +56 -74
  28. package/dist/compiler/runtime/track-message-call.test.js +1 -1
  29. package/dist/compiler/server/middleware.d.ts +4 -2
  30. package/dist/compiler/server/middleware.d.ts.map +1 -1
  31. package/dist/compiler/server/middleware.js +5 -3
  32. package/dist/compiler/server/middleware.test.js +163 -109
  33. package/dist/services/codegen/quotes.d.ts.map +1 -1
  34. package/dist/services/env-variables/index.js +1 -1
  35. package/dist/services/telemetry/capture.d.ts.map +1 -1
  36. package/dist/utilities/detect-json-formatting.d.ts.map +1 -1
  37. package/package.json +7 -7
@@ -10,16 +10,14 @@ test("sets the cookie to a different locale", async () => {
10
10
  globalThis.window.location = { hostname: "example.com" };
11
11
  globalThis.window.location.reload = vi.fn();
12
12
  const runtime = await createParaglide({
13
- project: await newProject({
13
+ blob: await newProject({
14
14
  settings: {
15
15
  baseLocale: "en",
16
16
  locales: ["en", "de"],
17
17
  },
18
18
  }),
19
- compilerOptions: {
20
- strategy: ["cookie"],
21
- cookieName: "PARAGLIDE_LOCALE",
22
- },
19
+ strategy: ["cookie"],
20
+ cookieName: "PARAGLIDE_LOCALE",
23
21
  });
24
22
  globalThis.document.cookie = "PARAGLIDE_LOCALE=en";
25
23
  runtime.setLocale("de");
@@ -37,17 +35,15 @@ test("sets the cookie with explicit domain to a different locale navigating subd
37
35
  globalThis.window.location = { hostname: "web.example.com" };
38
36
  globalThis.window.location.reload = vi.fn();
39
37
  const runtime = await createParaglide({
40
- project: await newProject({
38
+ blob: await newProject({
41
39
  settings: {
42
40
  baseLocale: "en",
43
41
  locales: ["en", "de"],
44
42
  },
45
43
  }),
46
- compilerOptions: {
47
- strategy: ["cookie"],
48
- cookieName: "PARAGLIDE_LOCALE",
49
- cookieDomain: "example.com",
50
- },
44
+ strategy: ["cookie"],
45
+ cookieName: "PARAGLIDE_LOCALE",
46
+ cookieDomain: "example.com",
51
47
  });
52
48
  globalThis.document.cookie = "PARAGLIDE_LOCALE=en";
53
49
  runtime.setLocale("de");
@@ -65,17 +61,15 @@ test("sets the cookie with explicit domain to a different locale navigating doma
65
61
  globalThis.window.location = { hostname: "example.com" };
66
62
  globalThis.window.location.reload = vi.fn();
67
63
  const runtime = await createParaglide({
68
- project: await newProject({
64
+ blob: await newProject({
69
65
  settings: {
70
66
  baseLocale: "en",
71
67
  locales: ["en", "de"],
72
68
  },
73
69
  }),
74
- compilerOptions: {
75
- strategy: ["cookie"],
76
- cookieName: "PARAGLIDE_LOCALE",
77
- cookieDomain: "example.com",
78
- },
70
+ strategy: ["cookie"],
71
+ cookieName: "PARAGLIDE_LOCALE",
72
+ cookieDomain: "example.com",
79
73
  });
80
74
  globalThis.document.cookie = "PARAGLIDE_LOCALE=en";
81
75
  runtime.setLocale("de");
@@ -92,21 +86,19 @@ test("url pattern strategy sets the window location", async () => {
92
86
  globalThis.window.location.hostname = "example.com";
93
87
  globalThis.window.location.reload = vi.fn();
94
88
  const runtime = await createParaglide({
95
- project: await newProject({
89
+ blob: await newProject({
96
90
  settings: { baseLocale: "en", locales: ["en", "de"] },
97
91
  }),
98
- compilerOptions: {
99
- strategy: ["url"],
100
- urlPatterns: [
101
- {
102
- pattern: "https://example.:tld/:path*",
103
- localized: [
104
- ["en", "https://example.com/:path*"],
105
- ["de", "https://example.de/:path*"],
106
- ],
107
- },
108
- ],
109
- },
92
+ strategy: ["url"],
93
+ urlPatterns: [
94
+ {
95
+ pattern: "https://example.:tld/:path*",
96
+ localized: [
97
+ ["en", "https://example.com/:path*"],
98
+ ["de", "https://example.de/:path*"],
99
+ ],
100
+ },
101
+ ],
110
102
  });
111
103
  globalThis.window.location.href = "https://example.com/page";
112
104
  runtime.setLocale("de");
@@ -117,16 +109,14 @@ test("url pattern strategy sets the window location", async () => {
117
109
  // `!document.cookie` was used which returned false for an empty string
118
110
  test("sets the cookie when it's an empty string", async () => {
119
111
  const runtime = await createParaglide({
120
- project: await newProject({
112
+ blob: await newProject({
121
113
  settings: {
122
114
  baseLocale: "en",
123
115
  locales: ["en", "fr"],
124
116
  },
125
117
  }),
126
- compilerOptions: {
127
- strategy: ["cookie"],
128
- cookieName: "PARAGLIDE_LOCALE",
129
- },
118
+ strategy: ["cookie"],
119
+ cookieName: "PARAGLIDE_LOCALE",
130
120
  });
131
121
  /** @ts-expect-error - client side api */
132
122
  globalThis.document = { cookie: "" };
@@ -135,26 +125,24 @@ test("sets the cookie when it's an empty string", async () => {
135
125
  });
136
126
  test("when strategy precedes URL, it should set the locale and re-direct to the URL", async () => {
137
127
  const runtime = await createParaglide({
138
- project: await newProject({
128
+ blob: await newProject({
139
129
  settings: {
140
130
  baseLocale: "en",
141
131
  locales: ["en", "fr"],
142
132
  },
143
133
  }),
144
- compilerOptions: {
145
- strategy: ["cookie", "url", "baseLocale"],
146
- cookieName: "PARAGLIDE_LOCALE",
147
- isServer: "false",
148
- urlPatterns: [
149
- {
150
- pattern: "https://example.com/en/:path(.*)?",
151
- localized: [
152
- ["en", "https://example.com/en/:path(.*)?"],
153
- ["fr", "https://example.com/fr/:path(.*)?"],
154
- ],
155
- },
156
- ],
157
- },
134
+ strategy: ["cookie", "url", "baseLocale"],
135
+ cookieName: "PARAGLIDE_LOCALE",
136
+ isServer: "false",
137
+ urlPatterns: [
138
+ {
139
+ pattern: "https://example.com/en/:path(.*)?",
140
+ localized: [
141
+ ["en", "https://example.com/en/:path(.*)?"],
142
+ ["fr", "https://example.com/fr/:path(.*)?"],
143
+ ],
144
+ },
145
+ ],
158
146
  });
159
147
  /** @ts-expect-error - client side api */
160
148
  globalThis.document = { cookie: "PARAGLIDE_LOCALE=fr" };
@@ -177,16 +165,14 @@ test("should not reload when setting locale to current locale", async () => {
177
165
  globalThis.window.location = { hostname: "example.com" };
178
166
  globalThis.window.location.reload = vi.fn();
179
167
  const runtime = await createParaglide({
180
- project: await newProject({
168
+ blob: await newProject({
181
169
  settings: {
182
170
  baseLocale: "en",
183
171
  locales: ["en", "de"],
184
172
  },
185
173
  }),
186
- compilerOptions: {
187
- strategy: ["cookie"],
188
- cookieName: "PARAGLIDE_LOCALE",
189
- },
174
+ strategy: ["cookie"],
175
+ cookieName: "PARAGLIDE_LOCALE",
190
176
  });
191
177
  globalThis.document.cookie = "PARAGLIDE_LOCALE=en; path=/";
192
178
  // Setting to the current locale (en)
@@ -209,15 +195,13 @@ test("sets the locale to localStorage", async () => {
209
195
  // @ts-expect-error - global variable definition
210
196
  globalThis.window = {};
211
197
  const runtime = await createParaglide({
212
- project: await newProject({
198
+ blob: await newProject({
213
199
  settings: {
214
200
  baseLocale: "en",
215
201
  locales: ["en", "de"],
216
202
  },
217
203
  }),
218
- compilerOptions: {
219
- strategy: ["localStorage"],
220
- },
204
+ strategy: ["localStorage"],
221
205
  });
222
206
  runtime.setLocale("de");
223
207
  expect(globalThis.localStorage.setItem).toHaveBeenCalledWith("PARAGLIDE_LOCALE", "de");
@@ -241,26 +225,24 @@ test("should set locale in all configured storage mechanisms regardless of which
241
225
  globalThis.window.location.reload = vi.fn();
242
226
  // Create runtime with multiple strategies
243
227
  const runtime = await createParaglide({
244
- project: await newProject({
228
+ blob: await newProject({
245
229
  settings: {
246
230
  baseLocale: "en",
247
231
  locales: ["en", "de", "fr"],
248
232
  },
249
233
  }),
250
- compilerOptions: {
251
- strategy: ["url", "localStorage", "cookie", "baseLocale"],
252
- cookieName: "PARAGLIDE_LOCALE",
253
- urlPatterns: [
254
- {
255
- pattern: "https://example.com/:locale/:path*",
256
- localized: [
257
- ["en", "https://example.com/en/:path*"],
258
- ["de", "https://example.com/de/:path*"],
259
- ["fr", "https://example.com/fr/:path*"],
260
- ],
261
- },
262
- ],
263
- },
234
+ strategy: ["url", "localStorage", "cookie", "baseLocale"],
235
+ cookieName: "PARAGLIDE_LOCALE",
236
+ urlPatterns: [
237
+ {
238
+ pattern: "https://example.com/:locale/:path*",
239
+ localized: [
240
+ ["en", "https://example.com/en/:path*"],
241
+ ["de", "https://example.com/de/:path*"],
242
+ ["fr", "https://example.com/fr/:path*"],
243
+ ],
244
+ },
245
+ ],
264
246
  });
265
247
  // Call setLocale
266
248
  runtime.setLocale("fr");
@@ -4,7 +4,7 @@ import { newProject } from "@inlang/sdk";
4
4
  import { test, expect } from "vitest";
5
5
  test("tracks message calls", async () => {
6
6
  const runtime = await createParaglide({
7
- project: await newProject({
7
+ blob: await newProject({
8
8
  settings: {
9
9
  baseLocale: "en",
10
10
  locales: ["en", "de"],
@@ -17,7 +17,7 @@
17
17
  *
18
18
  * @param {Request} request - The incoming request object
19
19
  * @param {(args: { request: Request, locale: import("./runtime.js").Locale }) => T | Promise<T>} resolve - Function to handle the request
20
- *
20
+ * @param {{ onRedirect:(response: Response) => void }} [callbacks] - Callbacks to handle events from middleware
21
21
  * @returns {Promise<Response>}
22
22
  *
23
23
  * @example
@@ -62,5 +62,7 @@
62
62
  export function paraglideMiddleware<T>(request: Request, resolve: (args: {
63
63
  request: Request;
64
64
  locale: import("./runtime.js").Locale;
65
- }) => T | Promise<T>): Promise<Response>;
65
+ }) => T | Promise<T>, callbacks?: {
66
+ onRedirect: (response: Response) => void;
67
+ }): Promise<Response>;
66
68
  //# sourceMappingURL=middleware.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"middleware.d.ts","sourceRoot":"","sources":["../../../src/compiler/server/middleware.js"],"names":[],"mappings":"AAEA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA4DG;AACH,oCA9Ca,CAAC,WAEH,OAAO,WACP,CAAC,IAAI,EAAE;IAAE,OAAO,EAAE,OAAO,CAAC;IAAC,MAAM,EAAE,OAAO,cAAc,EAAE,MAAM,CAAA;CAAE,KAAK,CAAC,GAAG,OAAO,CAAC,CAAC,CAAC,GAEnF,OAAO,CAAC,QAAQ,CAAC,CA0H7B"}
1
+ {"version":3,"file":"middleware.d.ts","sourceRoot":"","sources":["../../../src/compiler/server/middleware.js"],"names":[],"mappings":"AAEA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA4DG;AACH,oCA9Ca,CAAC,WAEH,OAAO,WACP,CAAC,IAAI,EAAE;IAAE,OAAO,EAAE,OAAO,CAAC;IAAC,MAAM,EAAE,OAAO,cAAc,EAAE,MAAM,CAAA;CAAE,KAAK,CAAC,GAAG,OAAO,CAAC,CAAC,CAAC,cACrF;IAAE,UAAU,EAAC,CAAC,QAAQ,EAAE,QAAQ,KAAK,IAAI,CAAA;CAAE,GACzC,OAAO,CAAC,QAAQ,CAAC,CA4H7B"}
@@ -18,7 +18,7 @@ import * as runtime from "./runtime.js";
18
18
  *
19
19
  * @param {Request} request - The incoming request object
20
20
  * @param {(args: { request: Request, locale: import("./runtime.js").Locale }) => T | Promise<T>} resolve - Function to handle the request
21
- *
21
+ * @param {{ onRedirect:(response: Response) => void }} [callbacks] - Callbacks to handle events from middleware
22
22
  * @returns {Promise<Response>}
23
23
  *
24
24
  * @example
@@ -60,7 +60,7 @@ import * as runtime from "./runtime.js";
60
60
  * };
61
61
  * ```
62
62
  */
63
- export async function paraglideMiddleware(request, resolve) {
63
+ export async function paraglideMiddleware(request, resolve, callbacks) {
64
64
  if (!runtime.disableAsyncLocalStorage && !runtime.serverAsyncLocalStorage) {
65
65
  const { AsyncLocalStorage } = await import("async_hooks");
66
66
  runtime.overwriteServerAsyncLocalStorage(new AsyncLocalStorage());
@@ -76,7 +76,9 @@ export async function paraglideMiddleware(request, resolve) {
76
76
  runtime.strategy.includes("url")) {
77
77
  const localizedUrl = runtime.localizeUrl(request.url, { locale });
78
78
  if (normalizeURL(localizedUrl.href) !== normalizeURL(request.url)) {
79
- return Response.redirect(localizedUrl, 307);
79
+ const response = Response.redirect(localizedUrl, 307);
80
+ callbacks?.onRedirect(response);
81
+ return response;
80
82
  }
81
83
  }
82
84
  // If the strategy includes "url", we need to de-localize the URL