-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhttp.ts
80 lines (72 loc) · 2.53 KB
/
http.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
80
import type { UrlWithParsedQuery } from 'node:url'
import { Context, HandlerConfiguration, Json } from './context.js'
import { registerHttpHandler } from './host/registry.js'
export * from './context.js'
export type ResponseHeaders = {
[key: string]: string
}
export type FullResult = {
headers?: ResponseHeaders
status?: number
body?: unknown
}
export type Result = void | string | FullResult
export type HttpRequest = {
readonly rawUrl: string
readonly url: Readonly<UrlWithParsedQuery> & { pathStepAt: (index: number) => string }
readonly headers: Readonly<ResponseHeaders>
readonly body?: Json | string
}
export type HttpHandlerConfiguration = HandlerConfiguration & {
/**
* A string identifying which domains can access the endpoint cross-origin.
* @default undefined
*/
readonly cors?: string
}
export type Handler = (context: Context, request: HttpRequest) => Promise<Result> | Result
export function get(path: string, fn: Handler): void
export function get(path: string, config: HttpHandlerConfiguration, fn: Handler): void
export function get(
path: string,
configOrHandler: HttpHandlerConfiguration | Handler,
fn?: Handler,
): void {
registerHttpHandler('GET', path, configOrHandler, fn)
}
export function post(path: string, fn: Handler): void
export function post(path: string, config: HttpHandlerConfiguration, fn: Handler): void
export function post(
path: string,
configOrHandler: HttpHandlerConfiguration | Handler,
fn?: Handler,
): void {
registerHttpHandler('POST', path, configOrHandler, fn)
}
export function put(path: string, fn: Handler): void
export function put(path: string, config: HttpHandlerConfiguration, fn: Handler): void
export function put(
path: string,
configOrHandler: HttpHandlerConfiguration | Handler,
fn?: Handler,
): void {
registerHttpHandler('PUT', path, configOrHandler, fn)
}
export function patch(path: string, fn: Handler): void
export function patch(path: string, config: HttpHandlerConfiguration, fn: Handler): void
export function patch(
path: string,
configOrHandler: HttpHandlerConfiguration | Handler,
fn?: Handler,
): void {
registerHttpHandler('PATCH', path, configOrHandler, fn)
}
export function del(path: string, fn: Handler): void
export function del(path: string, config: HttpHandlerConfiguration, fn: Handler): void
export function del(
path: string,
configOrHandler: HttpHandlerConfiguration | Handler,
fn?: Handler,
): void {
registerHttpHandler('DELETE', path, configOrHandler, fn)
}