@0xchain/i18n 1.1.0-beta.71 → 1.1.0-beta.72

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/index.d.ts CHANGED
@@ -2,4 +2,5 @@ export * from './navigation';
2
2
  export * from './routing';
3
3
  export * from './request';
4
4
  export * from './middleware';
5
+ export * from './localeCookie';
5
6
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,cAAc,CAAC;AAC7B,cAAc,WAAW,CAAC;AAC1B,cAAc,WAAW,CAAC;AAC1B,cAAc,cAAc,CAAC"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,cAAc,CAAC;AAC7B,cAAc,WAAW,CAAC;AAC1B,cAAc,WAAW,CAAC;AAC1B,cAAc,cAAc,CAAC;AAC7B,cAAc,gBAAgB,CAAC"}
package/dist/index.js CHANGED
@@ -1,12 +1,19 @@
1
1
  import { Link, getPathname, redirect, usePathname, useRouter } from "./navigation.js";
2
2
  import { routing } from "./routing.js";
3
3
  import "./request.js";
4
- import { middleware } from "./middleware.js";
4
+ import { createI18nMiddleware, middleware } from "./middleware.js";
5
+ import { LOCALE_COOKIE_MAX_AGE, LOCALE_COOKIE_NAME, localeCookieOptions, requestHostname, resolveLocaleCookieDomain } from "./localeCookie.js";
5
6
  export {
7
+ LOCALE_COOKIE_MAX_AGE,
8
+ LOCALE_COOKIE_NAME,
6
9
  Link,
10
+ createI18nMiddleware,
7
11
  getPathname,
12
+ localeCookieOptions,
8
13
  middleware,
9
14
  redirect,
15
+ requestHostname,
16
+ resolveLocaleCookieDomain,
10
17
  routing,
11
18
  usePathname,
12
19
  useRouter
@@ -0,0 +1,18 @@
1
+ export declare const LOCALE_COOKIE_NAME = "NEXT_LOCALE";
2
+ export declare const LOCALE_COOKIE_MAX_AGE: number;
3
+ export declare function resolveLocaleCookieDomain(hostname: string): string | undefined;
4
+ export declare function localeCookieOptions(hostname: string): {
5
+ domain?: string | undefined;
6
+ path: string;
7
+ sameSite: "lax";
8
+ maxAge: number;
9
+ };
10
+ export declare function requestHostname(request: {
11
+ headers: {
12
+ get(name: string): string | null;
13
+ };
14
+ nextUrl: {
15
+ hostname: string;
16
+ };
17
+ }): string;
18
+ //# sourceMappingURL=localeCookie.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"localeCookie.d.ts","sourceRoot":"","sources":["../src/localeCookie.ts"],"names":[],"mappings":"AAAA,eAAO,MAAM,kBAAkB,gBAAgB,CAAC;AAChD,eAAO,MAAM,qBAAqB,QAAqB,CAAC;AAExD,wBAAgB,yBAAyB,CAAC,QAAQ,EAAE,MAAM,GAAG,MAAM,GAAG,SAAS,CAK9E;AAED,wBAAgB,mBAAmB,CAAC,QAAQ,EAAE,MAAM;;;;;EAQnD;AAED,wBAAgB,eAAe,CAAC,OAAO,EAAE;IACvC,OAAO,EAAE;QAAE,GAAG,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,GAAG,IAAI,CAAA;KAAE,CAAC;IAC9C,OAAO,EAAE;QAAE,QAAQ,EAAE,MAAM,CAAA;KAAE,CAAC;CAC/B,UAMA"}
@@ -0,0 +1,27 @@
1
+ const LOCALE_COOKIE_NAME = "NEXT_LOCALE";
2
+ const LOCALE_COOKIE_MAX_AGE = 60 * 60 * 24 * 365;
3
+ function resolveLocaleCookieDomain(hostname) {
4
+ const host = hostname.split(":")[0];
5
+ if (/(^|\.)ichatgo\.ai$/i.test(host)) return ".ichatgo.ai";
6
+ if (/(^|\.)chaindigg\.com$/i.test(host)) return ".chaindigg.com";
7
+ return void 0;
8
+ }
9
+ function localeCookieOptions(hostname) {
10
+ const domain = resolveLocaleCookieDomain(hostname);
11
+ return {
12
+ path: "/",
13
+ sameSite: "lax",
14
+ maxAge: LOCALE_COOKIE_MAX_AGE,
15
+ ...domain ? { domain } : {}
16
+ };
17
+ }
18
+ function requestHostname(request) {
19
+ return (request.headers.get("x-forwarded-host") || request.headers.get("host") || request.nextUrl.hostname).split(":")[0];
20
+ }
21
+ export {
22
+ LOCALE_COOKIE_MAX_AGE,
23
+ LOCALE_COOKIE_NAME,
24
+ localeCookieOptions,
25
+ requestHostname,
26
+ resolveLocaleCookieDomain
27
+ };
@@ -1,2 +1,4 @@
1
- export declare function middleware(request: unknown): unknown;
1
+ import { NextRequest } from 'next/server';
2
+ export declare function createI18nMiddleware(request: NextRequest): import('next/server').NextResponse<unknown>;
3
+ export declare function middleware(request: NextRequest): import('next/server').NextResponse<unknown>;
2
4
  //# sourceMappingURL=middleware.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"middleware.d.ts","sourceRoot":"","sources":["../src/middleware.ts"],"names":[],"mappings":"AAKA,wBAAgB,UAAU,CAAC,OAAO,EAAE,OAAO,GAAG,OAAO,CAEpD"}
1
+ {"version":3,"file":"middleware.d.ts","sourceRoot":"","sources":["../src/middleware.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,aAAa,CAAC;AAS/C,wBAAgB,oBAAoB,CAAC,OAAO,EAAE,WAAW,+CAmBxD;AAED,wBAAgB,UAAU,CAAC,OAAO,EAAE,WAAW,+CAE9C"}
@@ -1,9 +1,27 @@
1
1
  import createMiddleware from "next-intl/middleware";
2
+ import { requestHostname, LOCALE_COOKIE_NAME, localeCookieOptions, resolveLocaleCookieDomain } from "./localeCookie.js";
2
3
  import { routing } from "./routing.js";
3
- const i18n = createMiddleware(routing);
4
+ function createI18nMiddleware(request) {
5
+ const hostname = requestHostname(request);
6
+ const handle = createMiddleware({
7
+ ...routing,
8
+ localeCookie: {
9
+ name: LOCALE_COOKIE_NAME,
10
+ ...localeCookieOptions(hostname)
11
+ }
12
+ });
13
+ const response = handle(request);
14
+ const locale = request.nextUrl.pathname.split("/")[1];
15
+ if (resolveLocaleCookieDomain(hostname) && routing.locales.includes(locale)) {
16
+ response.cookies.set(LOCALE_COOKIE_NAME, "", { path: "/", maxAge: 0 });
17
+ response.cookies.set(LOCALE_COOKIE_NAME, locale, localeCookieOptions(hostname));
18
+ }
19
+ return response;
20
+ }
4
21
  function middleware(request) {
5
- return i18n(request);
22
+ return createI18nMiddleware(request);
6
23
  }
7
24
  export {
25
+ createI18nMiddleware,
8
26
  middleware
9
27
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@0xchain/i18n",
3
- "version": "1.1.0-beta.71",
3
+ "version": "1.1.0-beta.72",
4
4
  "type": "module",
5
5
  "main": "./dist/index.js",
6
6
  "module": "./dist/index.js",
@@ -32,6 +32,11 @@
32
32
  "import": "./dist/middleware.js",
33
33
  "default": "./dist/middleware.js"
34
34
  },
35
+ "./localeCookie": {
36
+ "types": "./dist/localeCookie.d.ts",
37
+ "import": "./dist/localeCookie.js",
38
+ "default": "./dist/localeCookie.js"
39
+ },
35
40
  "./redirectWithLanguage": {
36
41
  "types": "./dist/redirectWithLanguage.d.ts",
37
42
  "import": "./dist/redirectWithLanguage.js",