import type { Body, BodyBytes, BodyForm, BodyFormData, BodyJson, BodyOptions, BodyReader, BodyStream, BodyText,} from "./body.ts";import { RequestBody } from "./body.ts";import { accepts, acceptsEncodings, acceptsLanguages } from "./deps.ts";import type { HTTPMethods, ServerRequest } from "./types.d.ts";
export interface OakRequestOptions { jsonBodyReviver?: (key: string, value: unknown) => unknown; proxy?: boolean; secure?: boolean;}
export class Request { #body: RequestBody; #proxy: boolean; #secure: boolean; #serverRequest: ServerRequest; #url?: URL;
#getRemoteAddr(): string { return this.#serverRequest.remoteAddr ?? ""; }
get hasBody(): boolean { return this.#body.has(); }
get headers(): Headers { return this.#serverRequest.headers; }
get ip(): string { return (this.#proxy ? this.ips[0] : this.#getRemoteAddr()) ?? ""; }
get ips(): string[] { return this.#proxy ? (this.#serverRequest.headers.get("x-forwarded-for") ?? this.#getRemoteAddr()).split(/\s*,\s*/) : []; }
get method(): HTTPMethods { return this.#serverRequest.method as HTTPMethods; }
get secure(): boolean { return this.#secure; }
get originalRequest(): ServerRequest { return this.#serverRequest; }
get url(): URL { if (!this.#url) { const serverRequest = this.#serverRequest; if (!this.#proxy) { try { if (serverRequest.rawUrl) { this.#url = new URL(serverRequest.rawUrl); return this.#url; } } catch { } } let proto: string; let host: string; if (this.#proxy) { proto = serverRequest .headers.get("x-forwarded-proto")?.split(/\s*,\s*/, 1)[0] ?? "http"; host = serverRequest.headers.get("x-forwarded-host") ?? serverRequest.headers.get("host") ?? ""; } else { proto = this.#secure ? "https" : "http"; host = serverRequest.headers.get("host") ?? ""; } try { this.#url = new URL(`${proto}://${host}${serverRequest.url}`); } catch { throw new TypeError( `The server request URL of "${proto}://${host}${serverRequest.url}" is invalid.`, ); } } return this.#url; }
constructor( serverRequest: ServerRequest, { proxy = false, secure = false, jsonBodyReviver }: OakRequestOptions = {}, ) { this.#proxy = proxy; this.#secure = secure; this.#serverRequest = serverRequest; this.#body = new RequestBody( serverRequest.getBody(), serverRequest.headers, jsonBodyReviver, ); }
accepts(): string[] | undefined; accepts(...types: string[]): string | undefined; accepts(...types: string[]): string | string[] | undefined { if (!this.#serverRequest.headers.has("Accept")) { return types.length ? types[0] : ["*/*"]; } if (types.length) { return accepts(this.#serverRequest, ...types); } return accepts(this.#serverRequest); }
acceptsEncodings(): string[] | undefined; acceptsEncodings(...encodings: string[]): string | undefined; acceptsEncodings(...encodings: string[]): string[] | string | undefined { if (!this.#serverRequest.headers.has("Accept-Encoding")) { return encodings.length ? encodings[0] : ["*"]; } if (encodings.length) { return acceptsEncodings(this.#serverRequest, ...encodings); } return acceptsEncodings(this.#serverRequest); }
acceptsLanguages(): string[] | undefined; acceptsLanguages(...langs: string[]): string | undefined; acceptsLanguages(...langs: string[]): string[] | string | undefined { if (!this.#serverRequest.headers.get("Accept-Language")) { return langs.length ? langs[0] : ["*"]; } if (langs.length) { return acceptsLanguages(this.#serverRequest, ...langs); } return acceptsLanguages(this.#serverRequest); }
body(options: BodyOptions<"bytes">): BodyBytes; body(options: BodyOptions<"form">): BodyForm; body(options: BodyOptions<"form-data">): BodyFormData; body(options: BodyOptions<"json">): BodyJson; body(options: BodyOptions<"reader">): BodyReader; body(options: BodyOptions<"stream">): BodyStream; body(options: BodyOptions<"text">): BodyText; body(options?: BodyOptions): Body; body(options: BodyOptions = {}): Body | BodyReader | BodyStream { return this.#body.get(options); }
[Symbol.for("Deno.customInspect")](inspect: (value: unknown) => string) { const { hasBody, headers, ip, ips, method, secure, url } = this; return `${this.constructor.name} ${ inspect({ hasBody, headers, ip, ips, method, secure, url: url.toString(), }) }`; }
[Symbol.for("nodejs.util.inspect.custom")]( depth: number, options: any, inspect: (value: unknown, options?: unknown) => string, ) { if (depth < 0) { return options.stylize(`[${this.constructor.name}]`, "special"); }
const newOptions = Object.assign({}, options, { depth: options.depth === null ? null : options.depth - 1, }); const { hasBody, headers, ip, ips, method, secure, url } = this; return `${options.stylize(this.constructor.name, "special")} ${ inspect( { hasBody, headers, ip, ips, method, secure, url }, newOptions, ) }`; }}