@klnap/next-proxy-chain 1.1.0 → 1.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.
package/dist/index.d.cts CHANGED
@@ -5,6 +5,97 @@ import { NextRequest, NextFetchEvent, NextResponse } from 'next/server';
5
5
  */
6
6
  declare const NEXT: unique symbol;
7
7
 
8
+ type DebugLevel = false | true | 'compact' | 'detailed' | 'verbose';
9
+ type SkipReason = 'include-mismatch' | 'exclude-match' | 'when-false';
10
+ type HandlerDiagnosticDetails = {
11
+ include?: readonly string[];
12
+ includeResult?: 'matched' | 'mismatch';
13
+ exclude?: readonly string[];
14
+ excludeResult?: 'not-matched' | 'matched';
15
+ when?: 'matched' | 'false' | 'not-evaluated';
16
+ timeoutMs?: number;
17
+ timeoutSource?: 'handler' | 'chain';
18
+ };
19
+ type NamedHandler = {
20
+ name: string;
21
+ details?: HandlerDiagnosticDetails;
22
+ };
23
+ type HandlerTrace = (NamedHandler & {
24
+ outcome: 'continue';
25
+ source: 'undefined' | 'NextResponse.next()' | 'next()';
26
+ durationMs: number;
27
+ }) | (NamedHandler & {
28
+ outcome: 'rewrite';
29
+ durationMs: number;
30
+ destination: string;
31
+ final: boolean;
32
+ }) | (NamedHandler & {
33
+ outcome: 'redirect';
34
+ durationMs: number;
35
+ status: number;
36
+ destination?: string;
37
+ }) | (NamedHandler & {
38
+ outcome: 'response';
39
+ durationMs: number;
40
+ status: number;
41
+ }) | (NamedHandler & {
42
+ outcome: 'skipped';
43
+ reason: SkipReason;
44
+ }) | (NamedHandler & {
45
+ outcome: 'not-reached';
46
+ }) | (NamedHandler & {
47
+ outcome: 'recovered';
48
+ durationMs: number;
49
+ recovery: 'continue' | 'rewrite' | 'redirect' | 'response';
50
+ policy: 'handler' | 'chain';
51
+ }) | (NamedHandler & {
52
+ outcome: 'failed' | 'timeout';
53
+ durationMs: number;
54
+ policy: 'none' | 'handler' | 'chain';
55
+ });
56
+ type ChainOutcome = {
57
+ type: 'next';
58
+ } | {
59
+ type: 'rewrite';
60
+ destination: string;
61
+ } | {
62
+ type: 'redirect';
63
+ status: number;
64
+ destination?: string;
65
+ } | {
66
+ type: 'response';
67
+ status: number;
68
+ } | {
69
+ type: 'error';
70
+ status: number;
71
+ classification: 'handler' | 'engine';
72
+ } | {
73
+ type: 'timeout';
74
+ status: number;
75
+ timeoutMs: number;
76
+ };
77
+ type ChainTrace = {
78
+ method: string;
79
+ pathname?: string;
80
+ durationMs: number;
81
+ outcome: ChainOutcome;
82
+ handlers: readonly HandlerTrace[];
83
+ };
84
+ type TraceCallback = (trace: ChainTrace) => void | Promise<void>;
85
+ type HandlerWhen<TContext = ProxyContext> = (request: NextRequest, event: NextFetchEvent, context: TContext) => boolean | Promise<boolean>;
86
+ type HandlerDebugOptions<TContext = ProxyContext> = {
87
+ name?: string;
88
+ debug?: DebugLevel;
89
+ timeout?: number;
90
+ onError?: OnErrorHandler<TContext>;
91
+ rules?: {
92
+ include?: RouteScopeList;
93
+ exclude?: RouteScopeList;
94
+ when?: HandlerWhen<TContext>;
95
+ };
96
+ handler: (request: NextRequest, event: NextFetchEvent, context: TContext) => Response | NextResult | undefined | void | Promise<Response | NextResult | undefined | void>;
97
+ };
98
+
8
99
  /**
9
100
  * Configuration options for chain continuation.
10
101
  */
@@ -43,11 +134,19 @@ type CompiledScope = {
43
134
  host?: RegExp;
44
135
  path?: RegExp;
45
136
  };
46
- interface BaseProxyRule<TFn> {
137
+ interface BaseProxyRule<TFn, TContext = ProxyContext> {
47
138
  run: TFn;
48
139
  include?: CompiledScope[];
49
140
  exclude?: CompiledScope[];
50
141
  name?: string;
142
+ debug?: DebugLevel;
143
+ timeout?: number;
144
+ when?: HandlerWhen<TContext>;
145
+ onError?: OnErrorHandler<TContext>;
146
+ diagnosticRules?: {
147
+ include?: RouteScopeList;
148
+ exclude?: RouteScopeList;
149
+ };
51
150
  }
52
151
  type ProxyPathFilter = {
53
152
  include?: RouteScopeList;
@@ -73,7 +172,7 @@ context: TContext) => NextResponse | Response | NextResult | undefined | void |
73
172
  * A single proxy execution function.
74
173
  */
75
174
  type ProxyFn<TContext = ProxyContext> = (req: NextRequest, ctx: TContext, event: NextFetchEvent) => NextResponse | Response | NextResult | undefined | void | Promise<NextResponse | Response | NextResult | undefined | void>;
76
- type ProxyRule<TContext = ProxyContext> = BaseProxyRule<ProxyFn<TContext>>;
175
+ type ProxyRule<TContext = ProxyContext> = BaseProxyRule<ProxyFn<TContext>, TContext>;
77
176
  type DefinedProxy<TContext = ProxyContext> = ProxyFn<TContext> & ((filter: ProxyPathFilter) => ProxyRule<TContext>) & ContextRuleEntry<TContext>;
78
177
  type Entry<TContext = ProxyContext> = ProxyFn<TContext> | ProxyRule<TContext>;
79
178
  type MergeStrategy = 'last-write-wins' | 'first-write-wins' | ((key: string, existing: string, incoming: string) => string | null);
@@ -111,8 +210,10 @@ interface ChainConfig<TContext = ProxyContext> {
111
210
  */
112
211
  headerMergeStrategy: MergeStrategy;
113
212
  onError?: OnErrorHandler<TContext>;
114
- debug: boolean;
213
+ debug: DebugLevel;
115
214
  logger: ChainLogger;
215
+ onTrace?: TraceCallback;
216
+ tracePath?: boolean;
116
217
  /** Maximum wall-clock execution time per rule in milliseconds. 0 disables timeout. */
117
218
  ruleTimeoutMs: number;
118
219
  requestHeaderPolicy: RequestHeaderPolicy;
@@ -189,6 +290,11 @@ declare function chain<TContext>(entries: readonly ChainEntry<TContext>[], optio
189
290
  createContext: (req: NextRequest, event: NextFetchEvent) => TContext;
190
291
  }): ReturnType<typeof proxyChain>;
191
292
 
293
+ /**
294
+ * Defines a native-signature handler with optional chain rules and local settings.
295
+ */
296
+ declare function defineHandler<TContext = ProxyContext>(options: HandlerDebugOptions<TContext>): ProxyRule<TContext>;
297
+
192
298
  /**
193
299
  * Creates an isolated per-request context instance.
194
300
  * Backed by Map to eliminate prototype pollution risks.
@@ -213,4 +319,4 @@ declare class RuleTimeoutError extends Error {
213
319
  constructor(ruleName: string, timeoutMs: number);
214
320
  }
215
321
 
216
- export { type ChainEntry, type ChainLogger, type ChainScope, type DefinedProxy, type Entry, type HostMatcher, type MergeStrategy, type NativeProxy, type NextOptions, type NextResult, type OnErrorHandler, type OnErrorResult, type PathMatcher, type ProxyChainOptions, type ProxyContext, type ProxyFn, type ProxyPathFilter, type ProxyRule, type RequestHeaderPolicy, RuleTimeoutError, chain, createProxyContext, defineProxy, host, isNextResult, next, path, proxyChain };
322
+ export { type ChainEntry, type ChainLogger, type ChainOutcome, type ChainScope, type ChainTrace, type DebugLevel, type DefinedProxy, type Entry, type HandlerDebugOptions, type HandlerDiagnosticDetails, type HandlerTrace, type HandlerWhen, type HostMatcher, type MergeStrategy, type NativeProxy, type NextOptions, type NextResult, type OnErrorHandler, type OnErrorResult, type PathMatcher, type ProxyChainOptions, type ProxyContext, type ProxyFn, type ProxyPathFilter, type ProxyRule, type RequestHeaderPolicy, RuleTimeoutError, type SkipReason, type TraceCallback, chain, createProxyContext, defineHandler, defineProxy, host, isNextResult, next, path, proxyChain };
package/dist/index.d.ts CHANGED
@@ -5,6 +5,97 @@ import { NextRequest, NextFetchEvent, NextResponse } from 'next/server';
5
5
  */
6
6
  declare const NEXT: unique symbol;
7
7
 
8
+ type DebugLevel = false | true | 'compact' | 'detailed' | 'verbose';
9
+ type SkipReason = 'include-mismatch' | 'exclude-match' | 'when-false';
10
+ type HandlerDiagnosticDetails = {
11
+ include?: readonly string[];
12
+ includeResult?: 'matched' | 'mismatch';
13
+ exclude?: readonly string[];
14
+ excludeResult?: 'not-matched' | 'matched';
15
+ when?: 'matched' | 'false' | 'not-evaluated';
16
+ timeoutMs?: number;
17
+ timeoutSource?: 'handler' | 'chain';
18
+ };
19
+ type NamedHandler = {
20
+ name: string;
21
+ details?: HandlerDiagnosticDetails;
22
+ };
23
+ type HandlerTrace = (NamedHandler & {
24
+ outcome: 'continue';
25
+ source: 'undefined' | 'NextResponse.next()' | 'next()';
26
+ durationMs: number;
27
+ }) | (NamedHandler & {
28
+ outcome: 'rewrite';
29
+ durationMs: number;
30
+ destination: string;
31
+ final: boolean;
32
+ }) | (NamedHandler & {
33
+ outcome: 'redirect';
34
+ durationMs: number;
35
+ status: number;
36
+ destination?: string;
37
+ }) | (NamedHandler & {
38
+ outcome: 'response';
39
+ durationMs: number;
40
+ status: number;
41
+ }) | (NamedHandler & {
42
+ outcome: 'skipped';
43
+ reason: SkipReason;
44
+ }) | (NamedHandler & {
45
+ outcome: 'not-reached';
46
+ }) | (NamedHandler & {
47
+ outcome: 'recovered';
48
+ durationMs: number;
49
+ recovery: 'continue' | 'rewrite' | 'redirect' | 'response';
50
+ policy: 'handler' | 'chain';
51
+ }) | (NamedHandler & {
52
+ outcome: 'failed' | 'timeout';
53
+ durationMs: number;
54
+ policy: 'none' | 'handler' | 'chain';
55
+ });
56
+ type ChainOutcome = {
57
+ type: 'next';
58
+ } | {
59
+ type: 'rewrite';
60
+ destination: string;
61
+ } | {
62
+ type: 'redirect';
63
+ status: number;
64
+ destination?: string;
65
+ } | {
66
+ type: 'response';
67
+ status: number;
68
+ } | {
69
+ type: 'error';
70
+ status: number;
71
+ classification: 'handler' | 'engine';
72
+ } | {
73
+ type: 'timeout';
74
+ status: number;
75
+ timeoutMs: number;
76
+ };
77
+ type ChainTrace = {
78
+ method: string;
79
+ pathname?: string;
80
+ durationMs: number;
81
+ outcome: ChainOutcome;
82
+ handlers: readonly HandlerTrace[];
83
+ };
84
+ type TraceCallback = (trace: ChainTrace) => void | Promise<void>;
85
+ type HandlerWhen<TContext = ProxyContext> = (request: NextRequest, event: NextFetchEvent, context: TContext) => boolean | Promise<boolean>;
86
+ type HandlerDebugOptions<TContext = ProxyContext> = {
87
+ name?: string;
88
+ debug?: DebugLevel;
89
+ timeout?: number;
90
+ onError?: OnErrorHandler<TContext>;
91
+ rules?: {
92
+ include?: RouteScopeList;
93
+ exclude?: RouteScopeList;
94
+ when?: HandlerWhen<TContext>;
95
+ };
96
+ handler: (request: NextRequest, event: NextFetchEvent, context: TContext) => Response | NextResult | undefined | void | Promise<Response | NextResult | undefined | void>;
97
+ };
98
+
8
99
  /**
9
100
  * Configuration options for chain continuation.
10
101
  */
@@ -43,11 +134,19 @@ type CompiledScope = {
43
134
  host?: RegExp;
44
135
  path?: RegExp;
45
136
  };
46
- interface BaseProxyRule<TFn> {
137
+ interface BaseProxyRule<TFn, TContext = ProxyContext> {
47
138
  run: TFn;
48
139
  include?: CompiledScope[];
49
140
  exclude?: CompiledScope[];
50
141
  name?: string;
142
+ debug?: DebugLevel;
143
+ timeout?: number;
144
+ when?: HandlerWhen<TContext>;
145
+ onError?: OnErrorHandler<TContext>;
146
+ diagnosticRules?: {
147
+ include?: RouteScopeList;
148
+ exclude?: RouteScopeList;
149
+ };
51
150
  }
52
151
  type ProxyPathFilter = {
53
152
  include?: RouteScopeList;
@@ -73,7 +172,7 @@ context: TContext) => NextResponse | Response | NextResult | undefined | void |
73
172
  * A single proxy execution function.
74
173
  */
75
174
  type ProxyFn<TContext = ProxyContext> = (req: NextRequest, ctx: TContext, event: NextFetchEvent) => NextResponse | Response | NextResult | undefined | void | Promise<NextResponse | Response | NextResult | undefined | void>;
76
- type ProxyRule<TContext = ProxyContext> = BaseProxyRule<ProxyFn<TContext>>;
175
+ type ProxyRule<TContext = ProxyContext> = BaseProxyRule<ProxyFn<TContext>, TContext>;
77
176
  type DefinedProxy<TContext = ProxyContext> = ProxyFn<TContext> & ((filter: ProxyPathFilter) => ProxyRule<TContext>) & ContextRuleEntry<TContext>;
78
177
  type Entry<TContext = ProxyContext> = ProxyFn<TContext> | ProxyRule<TContext>;
79
178
  type MergeStrategy = 'last-write-wins' | 'first-write-wins' | ((key: string, existing: string, incoming: string) => string | null);
@@ -111,8 +210,10 @@ interface ChainConfig<TContext = ProxyContext> {
111
210
  */
112
211
  headerMergeStrategy: MergeStrategy;
113
212
  onError?: OnErrorHandler<TContext>;
114
- debug: boolean;
213
+ debug: DebugLevel;
115
214
  logger: ChainLogger;
215
+ onTrace?: TraceCallback;
216
+ tracePath?: boolean;
116
217
  /** Maximum wall-clock execution time per rule in milliseconds. 0 disables timeout. */
117
218
  ruleTimeoutMs: number;
118
219
  requestHeaderPolicy: RequestHeaderPolicy;
@@ -189,6 +290,11 @@ declare function chain<TContext>(entries: readonly ChainEntry<TContext>[], optio
189
290
  createContext: (req: NextRequest, event: NextFetchEvent) => TContext;
190
291
  }): ReturnType<typeof proxyChain>;
191
292
 
293
+ /**
294
+ * Defines a native-signature handler with optional chain rules and local settings.
295
+ */
296
+ declare function defineHandler<TContext = ProxyContext>(options: HandlerDebugOptions<TContext>): ProxyRule<TContext>;
297
+
192
298
  /**
193
299
  * Creates an isolated per-request context instance.
194
300
  * Backed by Map to eliminate prototype pollution risks.
@@ -213,4 +319,4 @@ declare class RuleTimeoutError extends Error {
213
319
  constructor(ruleName: string, timeoutMs: number);
214
320
  }
215
321
 
216
- export { type ChainEntry, type ChainLogger, type ChainScope, type DefinedProxy, type Entry, type HostMatcher, type MergeStrategy, type NativeProxy, type NextOptions, type NextResult, type OnErrorHandler, type OnErrorResult, type PathMatcher, type ProxyChainOptions, type ProxyContext, type ProxyFn, type ProxyPathFilter, type ProxyRule, type RequestHeaderPolicy, RuleTimeoutError, chain, createProxyContext, defineProxy, host, isNextResult, next, path, proxyChain };
322
+ export { type ChainEntry, type ChainLogger, type ChainOutcome, type ChainScope, type ChainTrace, type DebugLevel, type DefinedProxy, type Entry, type HandlerDebugOptions, type HandlerDiagnosticDetails, type HandlerTrace, type HandlerWhen, type HostMatcher, type MergeStrategy, type NativeProxy, type NextOptions, type NextResult, type OnErrorHandler, type OnErrorResult, type PathMatcher, type ProxyChainOptions, type ProxyContext, type ProxyFn, type ProxyPathFilter, type ProxyRule, type RequestHeaderPolicy, RuleTimeoutError, type SkipReason, type TraceCallback, chain, createProxyContext, defineHandler, defineProxy, host, isNextResult, next, path, proxyChain };