-
Notifications
You must be signed in to change notification settings - Fork 1
/
basics.ts
37 lines (34 loc) · 1.2 KB
/
basics.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
import type { Awaitable } from "./utils/common-types.ts";
import type { Context } from "./index.ts";
import type { Method } from "./cors.ts"
export interface BasicsContext {
request: Request,
headers: Headers,
url: URL,
method: Method,
pathname: string,
// dirname: string,
// filename: string,
searchParams: URLSearchParams,
userAgent: string,
params: { [key: string]: string | undefined }
ip?: string
}
const _extract = (s: URL) => {
const result = s.pathname.match(/^(.*)\/(.*)$/);
return result
? { dirname: result[1], filename: result[2] }
: { dirname: '', filename: '' }
}
export const basics = () => async <X extends Context>(ax: Awaitable<X>): Promise<X & BasicsContext> => {
const x = await ax;
const { request, match } = x;
const { headers } = request;
const method = <Method>request.method;
const url = new URL(request.url)
const { pathname, searchParams } = url;
const userAgent = headers.get('user-agent') ?? '';
const ip = headers.get('x-forwarded-for') ?? <string>x.connInfo?.remoteAddr?.hostname ?? '';
const params = match?.pathname.groups ?? {};
return Object.assign(x, { headers, method, url, pathname, searchParams, userAgent, ip, params });
}