-
-
Notifications
You must be signed in to change notification settings - Fork 446
/
types.ts
79 lines (67 loc) · 2.47 KB
/
types.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
import type { Context } from './context.ts'
export interface ContextVariableMap {}
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export type Bindings = Record<string, any> // For Cloudflare Workers
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export type Variables = Record<string, any> // For c.set/c.get functions
export type Environment = {
Bindings: Bindings
Variables: Variables
}
export type Handler<
P extends string = string,
E extends Partial<Environment> = Environment,
// eslint-disable-next-line @typescript-eslint/no-explicit-any
S = any
> = (c: Context<P, E, S>, next: Next) => Response | Promise<Response | undefined | void>
export type MiddlewareHandler<
P extends string = string,
E extends Partial<Environment> = Environment,
// eslint-disable-next-line @typescript-eslint/no-explicit-any
S = any
> = (c: Context<P, E, S>, next: Next) => Promise<Response | undefined | void>
export type NotFoundHandler<E extends Partial<Environment> = Environment> = (
c: Context<string, E>
) => Response | Promise<Response>
export type ErrorHandler<E extends Partial<Environment> = Environment> = (
err: Error,
c: Context<string, E>
) => Response
export type Next = () => Promise<void>
// eslint-disable-next-line @typescript-eslint/no-unused-vars
type ParamKeyName<NameWithPattern> = NameWithPattern extends `${infer Name}{${infer _Pattern}`
? Name
: NameWithPattern
type ParamKey<Component> = Component extends `:${infer NameWithPattern}`
? ParamKeyName<NameWithPattern>
: never
export type ParamKeys<Path> = Path extends `${infer Component}/${infer Rest}`
? ParamKey<Component> | ParamKeys<Rest>
: ParamKey<Path>
// This is not used for internally
// Will be used by users as `Handler`
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export interface CustomHandler<P = string, E = Partial<Environment>, S = any> {
(
c: Context<
P extends string ? P : string,
P extends Partial<Environment> ? P : E extends Partial<Environment> ? E : never,
P extends string
? E extends Partial<Environment>
? S
: P extends Partial<Environment>
? E
: never
: P extends Partial<Environment>
? E extends Partial<Environment>
? S
: E
: P
>,
next: Next
): Response | Promise<Response | undefined | void>
}
export interface ExecutionContext {
waitUntil(promise: Promise<void>): void
passThroughOnException(): void
}