next-geo-block 0.2.0

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.
@@ -0,0 +1,70 @@
1
+ import { NextRequest, NextResponse } from 'next/server';
2
+ export { NextRequest } from 'next/server';
3
+
4
+ declare function timezoneToCountry(tz: string): string | undefined;
5
+
6
+ type CountryCode = string;
7
+ interface GeoBlockRule {
8
+ /**
9
+ * Path patterns this rule applies to. Supports:
10
+ * - exact paths: '/checkout'
11
+ * - trailing wildcard: '/admin/*' (any depth under /admin)
12
+ * - single-segment '*': '/u/* /settings' (matches /u/<anything>/settings)
13
+ * - regex (anchored): /^\/api\/v\d+\/private/
14
+ */
15
+ paths: Array<string | RegExp>;
16
+ /** ISO 3166-1 alpha-2 codes to block on these paths. Case-insensitive. */
17
+ block: CountryCode[];
18
+ }
19
+ type OnBlock = (ctx: {
20
+ request: NextRequest;
21
+ country: CountryCode;
22
+ pathname: string;
23
+ matchedRule: GeoBlockRule;
24
+ /** How the country was resolved. */
25
+ source: 'header' | 'timezone';
26
+ }) => Response | NextResponse | Promise<Response | NextResponse>;
27
+ /**
28
+ * Use the client's local timezone as a country fallback when no country
29
+ * header is present. The client must have already populated a cookie or
30
+ * header — middleware/proxy cannot read `Intl.DateTimeFormat()` directly.
31
+ * See README for the client snippet.
32
+ *
33
+ * Pass `true` for defaults (cookie `client_tz`, header `x-client-timezone`).
34
+ */
35
+ type TimezoneFallback = boolean | {
36
+ cookie?: string;
37
+ header?: string;
38
+ /**
39
+ * Custom IANA → ISO 3166-1 alpha-2 resolver. Overrides the bundled map.
40
+ */
41
+ resolve?: (tz: string) => CountryCode | undefined;
42
+ };
43
+ interface GeoBlockOptions {
44
+ rules: GeoBlockRule[];
45
+ /**
46
+ * Path to rewrite blocked requests to. Defaults to '/blocked'.
47
+ * Set to `null` to skip the default rewrite and use `onBlock` (or a 451).
48
+ */
49
+ blockedPath?: string | null;
50
+ /**
51
+ * When the country still can't be resolved (no header, no timezone),
52
+ * let the request through (true) or treat it as blocked (false). Default: true.
53
+ */
54
+ failOpen?: boolean;
55
+ /**
56
+ * Custom response when a request is blocked. Overrides `blockedPath`.
57
+ */
58
+ onBlock?: OnBlock;
59
+ /**
60
+ * Additional headers to read country from, tried first.
61
+ * Built-ins: x-vercel-ip-country, cf-ipcountry, x-country-code.
62
+ */
63
+ countryHeaders?: string[];
64
+ /** See {@link TimezoneFallback}. Default: false. */
65
+ fallbackTimezone?: TimezoneFallback;
66
+ }
67
+ declare function getCountry(request: NextRequest, extraHeaders?: string[]): CountryCode | undefined;
68
+ declare function createGeoBlock(options: GeoBlockOptions): (request: NextRequest) => Promise<NextResponse | Response>;
69
+
70
+ export { type CountryCode, type GeoBlockOptions, type GeoBlockRule, type OnBlock, type TimezoneFallback, createGeoBlock, getCountry, timezoneToCountry };
@@ -0,0 +1,70 @@
1
+ import { NextRequest, NextResponse } from 'next/server';
2
+ export { NextRequest } from 'next/server';
3
+
4
+ declare function timezoneToCountry(tz: string): string | undefined;
5
+
6
+ type CountryCode = string;
7
+ interface GeoBlockRule {
8
+ /**
9
+ * Path patterns this rule applies to. Supports:
10
+ * - exact paths: '/checkout'
11
+ * - trailing wildcard: '/admin/*' (any depth under /admin)
12
+ * - single-segment '*': '/u/* /settings' (matches /u/<anything>/settings)
13
+ * - regex (anchored): /^\/api\/v\d+\/private/
14
+ */
15
+ paths: Array<string | RegExp>;
16
+ /** ISO 3166-1 alpha-2 codes to block on these paths. Case-insensitive. */
17
+ block: CountryCode[];
18
+ }
19
+ type OnBlock = (ctx: {
20
+ request: NextRequest;
21
+ country: CountryCode;
22
+ pathname: string;
23
+ matchedRule: GeoBlockRule;
24
+ /** How the country was resolved. */
25
+ source: 'header' | 'timezone';
26
+ }) => Response | NextResponse | Promise<Response | NextResponse>;
27
+ /**
28
+ * Use the client's local timezone as a country fallback when no country
29
+ * header is present. The client must have already populated a cookie or
30
+ * header — middleware/proxy cannot read `Intl.DateTimeFormat()` directly.
31
+ * See README for the client snippet.
32
+ *
33
+ * Pass `true` for defaults (cookie `client_tz`, header `x-client-timezone`).
34
+ */
35
+ type TimezoneFallback = boolean | {
36
+ cookie?: string;
37
+ header?: string;
38
+ /**
39
+ * Custom IANA → ISO 3166-1 alpha-2 resolver. Overrides the bundled map.
40
+ */
41
+ resolve?: (tz: string) => CountryCode | undefined;
42
+ };
43
+ interface GeoBlockOptions {
44
+ rules: GeoBlockRule[];
45
+ /**
46
+ * Path to rewrite blocked requests to. Defaults to '/blocked'.
47
+ * Set to `null` to skip the default rewrite and use `onBlock` (or a 451).
48
+ */
49
+ blockedPath?: string | null;
50
+ /**
51
+ * When the country still can't be resolved (no header, no timezone),
52
+ * let the request through (true) or treat it as blocked (false). Default: true.
53
+ */
54
+ failOpen?: boolean;
55
+ /**
56
+ * Custom response when a request is blocked. Overrides `blockedPath`.
57
+ */
58
+ onBlock?: OnBlock;
59
+ /**
60
+ * Additional headers to read country from, tried first.
61
+ * Built-ins: x-vercel-ip-country, cf-ipcountry, x-country-code.
62
+ */
63
+ countryHeaders?: string[];
64
+ /** See {@link TimezoneFallback}. Default: false. */
65
+ fallbackTimezone?: TimezoneFallback;
66
+ }
67
+ declare function getCountry(request: NextRequest, extraHeaders?: string[]): CountryCode | undefined;
68
+ declare function createGeoBlock(options: GeoBlockOptions): (request: NextRequest) => Promise<NextResponse | Response>;
69
+
70
+ export { type CountryCode, type GeoBlockOptions, type GeoBlockRule, type OnBlock, type TimezoneFallback, createGeoBlock, getCountry, timezoneToCountry };