cloudflare-next-intl 0.8.39 → 0.8.40
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.
|
@@ -5,20 +5,20 @@ import type { GenerateRoutingConfig, RequestOrHeaders } from '../../types/types'
|
|
|
5
5
|
* Checks in order:
|
|
6
6
|
* 1. Explicit `input` (Request, NextRequest, or Headers) if provided
|
|
7
7
|
* 2. Next.js request headers via `headers()` (`x-cf-country`, `cf-ipcountry`)
|
|
8
|
-
* 3. `
|
|
8
|
+
* 3. `generate.getCloudflareContext` or `cf.country` if passed
|
|
9
9
|
* 4. `undefined` if outside request scope or unavailable
|
|
10
10
|
*/
|
|
11
|
-
export declare function getCountry(input?: RequestOrHeaders): Promise<string | undefined>;
|
|
11
|
+
export declare function getCountry(input?: RequestOrHeaders, generate?: GenerateRoutingConfig): Promise<string | undefined>;
|
|
12
12
|
/**
|
|
13
13
|
* Resolves the client's IANA timezone string (e.g. "America/New_York", "Europe/Kyiv", "UTC").
|
|
14
14
|
*
|
|
15
15
|
* Checks in order:
|
|
16
16
|
* 1. Explicit `input` (Request, NextRequest, or Headers) if provided
|
|
17
17
|
* 2. Next.js request headers via `headers()` (`x-cf-timezone`, `cf-timezone`)
|
|
18
|
-
* 3. `
|
|
18
|
+
* 3. `generate.getCloudflareContext` or `cf.timezone` if passed
|
|
19
19
|
* 4. `fallback` (or `undefined`) if outside request scope or unavailable
|
|
20
20
|
*/
|
|
21
|
-
export declare function getTimezone(input?: RequestOrHeaders, fallback?: string): Promise<string | undefined>;
|
|
21
|
+
export declare function getTimezone(input?: RequestOrHeaders, fallback?: string, generate?: GenerateRoutingConfig): Promise<string | undefined>;
|
|
22
22
|
/**
|
|
23
23
|
* Resolves the Cloudflare environment bindings object from `generate.env` or `generate.getCloudflareContext`.
|
|
24
24
|
*/
|
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
import config from '../../config/intl_config';
|
|
2
1
|
function extractHeader(h, name) {
|
|
3
2
|
if (typeof h.get === 'function') {
|
|
4
3
|
const val = h.get(name);
|
|
@@ -14,10 +13,10 @@ function extractHeader(h, name) {
|
|
|
14
13
|
* Checks in order:
|
|
15
14
|
* 1. Explicit `input` (Request, NextRequest, or Headers) if provided
|
|
16
15
|
* 2. Next.js request headers via `headers()` (`x-cf-country`, `cf-ipcountry`)
|
|
17
|
-
* 3. `
|
|
16
|
+
* 3. `generate.getCloudflareContext` or `cf.country` if passed
|
|
18
17
|
* 4. `undefined` if outside request scope or unavailable
|
|
19
18
|
*/
|
|
20
|
-
export async function getCountry(input) {
|
|
19
|
+
export async function getCountry(input, generate) {
|
|
21
20
|
if (input) {
|
|
22
21
|
if ('headers' in input && input.headers) {
|
|
23
22
|
const country = extractHeader(input.headers, 'x-cf-country') ?? extractHeader(input.headers, 'cf-ipcountry');
|
|
@@ -44,9 +43,9 @@ export async function getCountry(input) {
|
|
|
44
43
|
catch {
|
|
45
44
|
// Outside request scope / build time
|
|
46
45
|
}
|
|
47
|
-
if (
|
|
46
|
+
if (generate?.getCloudflareContext) {
|
|
48
47
|
try {
|
|
49
|
-
const ctx = await
|
|
48
|
+
const ctx = await generate.getCloudflareContext({ async: true });
|
|
50
49
|
if (ctx?.cf?.country && typeof ctx.cf.country === 'string' && ctx.cf.country.length > 0) {
|
|
51
50
|
return ctx.cf.country;
|
|
52
51
|
}
|
|
@@ -63,10 +62,10 @@ export async function getCountry(input) {
|
|
|
63
62
|
* Checks in order:
|
|
64
63
|
* 1. Explicit `input` (Request, NextRequest, or Headers) if provided
|
|
65
64
|
* 2. Next.js request headers via `headers()` (`x-cf-timezone`, `cf-timezone`)
|
|
66
|
-
* 3. `
|
|
65
|
+
* 3. `generate.getCloudflareContext` or `cf.timezone` if passed
|
|
67
66
|
* 4. `fallback` (or `undefined`) if outside request scope or unavailable
|
|
68
67
|
*/
|
|
69
|
-
export async function getTimezone(input, fallback) {
|
|
68
|
+
export async function getTimezone(input, fallback, generate) {
|
|
70
69
|
if (input) {
|
|
71
70
|
if ('headers' in input && input.headers) {
|
|
72
71
|
const tz = extractHeader(input.headers, 'x-cf-timezone') ?? extractHeader(input.headers, 'cf-timezone');
|
|
@@ -93,9 +92,9 @@ export async function getTimezone(input, fallback) {
|
|
|
93
92
|
catch {
|
|
94
93
|
// Outside request scope
|
|
95
94
|
}
|
|
96
|
-
if (
|
|
95
|
+
if (generate?.getCloudflareContext) {
|
|
97
96
|
try {
|
|
98
|
-
const ctx = await
|
|
97
|
+
const ctx = await generate.getCloudflareContext({ async: true });
|
|
99
98
|
if (ctx?.cf?.timezone && typeof ctx.cf.timezone === 'string' && ctx.cf.timezone.length > 0) {
|
|
100
99
|
return ctx.cf.timezone;
|
|
101
100
|
}
|
|
@@ -110,15 +109,15 @@ export async function getTimezone(input, fallback) {
|
|
|
110
109
|
* Resolves the Cloudflare environment bindings object from `generate.env` or `generate.getCloudflareContext`.
|
|
111
110
|
*/
|
|
112
111
|
export async function resolveEnv(generate) {
|
|
113
|
-
|
|
114
|
-
if (!gen)
|
|
112
|
+
if (!generate)
|
|
115
113
|
return undefined;
|
|
116
|
-
if (
|
|
117
|
-
|
|
114
|
+
if (generate.env) {
|
|
115
|
+
const resolved = typeof generate.env === 'function' ? await generate.env() : generate.env;
|
|
116
|
+
return resolved;
|
|
118
117
|
}
|
|
119
|
-
if (
|
|
118
|
+
if (generate.getCloudflareContext) {
|
|
120
119
|
try {
|
|
121
|
-
const ctx = await
|
|
120
|
+
const ctx = await generate.getCloudflareContext({ async: true });
|
|
122
121
|
return ctx?.env;
|
|
123
122
|
}
|
|
124
123
|
catch {
|
|
@@ -118,7 +118,7 @@ export interface GenerateRoutingConfig {
|
|
|
118
118
|
* Cloudflare environment bindings (or getter returning bindings).
|
|
119
119
|
* Supported in Vinext, Cloudflare Workers, and OpenNext.
|
|
120
120
|
*/
|
|
121
|
-
env?: Record<string, unknown> | (() => Record<string, unknown> | Promise<Record<string, unknown>>);
|
|
121
|
+
env?: object | Record<string, unknown> | (() => object | Record<string, unknown> | Promise<object | Record<string, unknown>>);
|
|
122
122
|
/**
|
|
123
123
|
* Request execution context (providing `waitUntil`), or a getter returning it.
|
|
124
124
|
* In Vinext, `getRequestExecutionContext()` from `vinext/shims/request-context` can be passed.
|