116 lines
2.8 KiB
TypeScript
116 lines
2.8 KiB
TypeScript
|
import { ServerRequest } from '../external/http.ts'
|
||
|
import { HTTPProtocol, HTTPRequest, RemoteHost } from './type/HTTPRequest.ts'
|
||
|
import { Response } from './Response.ts'
|
||
|
import { HTTPResponse } from './type/HTTPResponse.ts'
|
||
|
import Utility from '../service/utility/Utility.ts'
|
||
|
import { Injectable } from '../../../di/src/decorator/Injection.ts'
|
||
|
|
||
|
@Injectable()
|
||
|
export class Request implements HTTPRequest {
|
||
|
public readonly response: HTTPResponse
|
||
|
private readonly _deno_req: ServerRequest
|
||
|
private _body: any
|
||
|
private _query: { [key: string]: any } = {}
|
||
|
|
||
|
public readonly url: string
|
||
|
public readonly method: string
|
||
|
public readonly protocol: HTTPProtocol
|
||
|
public readonly connection: Deno.Conn
|
||
|
public readonly secure: boolean = false
|
||
|
|
||
|
public get headers() {
|
||
|
return this._deno_req.headers
|
||
|
}
|
||
|
|
||
|
get to_native(): ServerRequest {
|
||
|
return this._deno_req
|
||
|
}
|
||
|
|
||
|
get cookies() {
|
||
|
return this.response.cookies
|
||
|
}
|
||
|
|
||
|
constructor(
|
||
|
protected utility: Utility,
|
||
|
from: ServerRequest
|
||
|
) {
|
||
|
this._deno_req = from
|
||
|
this.url = this._deno_req.url
|
||
|
this.method = this._deno_req.method.toLowerCase()
|
||
|
this.protocol = {
|
||
|
string: this._deno_req.proto,
|
||
|
major: this._deno_req.protoMajor,
|
||
|
minor: this._deno_req.protoMinor,
|
||
|
}
|
||
|
this.connection = this._deno_req.conn
|
||
|
this.response = new Response(this)
|
||
|
}
|
||
|
|
||
|
public async prepare() {
|
||
|
this._body = await Deno.readAll(this._deno_req.body)
|
||
|
|
||
|
const url_params = new URLSearchParams(this.url.substr(1))
|
||
|
const param_obj = Object.fromEntries(url_params)
|
||
|
const params: any = {}
|
||
|
for ( const key in param_obj ) {
|
||
|
if ( !param_obj.hasOwnProperty(key) ) continue
|
||
|
if ( param_obj[key] === '' ) params[key] = true
|
||
|
else params[key] = this.utility.infer(param_obj[key])
|
||
|
}
|
||
|
|
||
|
this._query = params
|
||
|
}
|
||
|
|
||
|
respond(res: any) {
|
||
|
return this._deno_req.respond(res)
|
||
|
}
|
||
|
|
||
|
// public body: RequestBody = {}
|
||
|
// public original_body: RequestBody = {}
|
||
|
|
||
|
get remote() {
|
||
|
return this.connection.remoteAddr as RemoteHost
|
||
|
}
|
||
|
|
||
|
get body() {
|
||
|
return this._body
|
||
|
}
|
||
|
|
||
|
get query() {
|
||
|
return this._query
|
||
|
}
|
||
|
|
||
|
get hostname() {
|
||
|
return this.headers.get('host')?.split(':')[0]
|
||
|
}
|
||
|
|
||
|
get path() {
|
||
|
return this.url.split('?')[0]
|
||
|
}
|
||
|
|
||
|
get xhr() {
|
||
|
return this.headers.get('x-requested-with')?.toLowerCase() === 'xmlhttprequest'
|
||
|
}
|
||
|
|
||
|
/*
|
||
|
body
|
||
|
fresh/stale - cache
|
||
|
remote ips (proxy)
|
||
|
params
|
||
|
route?
|
||
|
signedCookies
|
||
|
*/
|
||
|
|
||
|
/*
|
||
|
accepts content type
|
||
|
accepts charsets
|
||
|
accepts encodings
|
||
|
accepts languages
|
||
|
get header
|
||
|
is content type
|
||
|
get param with default value
|
||
|
get input with default value
|
||
|
range header parser
|
||
|
*/
|
||
|
}
|