Skip to main content
Module

x/dinar/lib/core/Context.ts

deno web framewrok maybe :<
Latest
File
import { ServerRequest, qs, ReactDOMServer } from "../deps.ts";import { RouteMethod } from "../constant/RouteMethods.ts";import { NotImplementException } from "../constant/Exception.ts";
export class Context { static fromContext(c: Context, params: any) { c.req.params = params; return c; } req!: Request; res!: Response; start_time: string; end_time?: string; state: any = {}; constructor(rawRequest: ServerRequest, url: string, query: any, params: any, body: any) { this.start_time = Date.now() + ""; this.req = new Request(rawRequest, url, query, params, body); this.res = new Response(rawRequest, this); }
get query() { return this.req.query; }
get params() { return this.req.params; }
get url() { return this.req.url; }
get ip() { throw new NotImplementException(); }
get body() { return this.req.body; }
get cookie() { return this.req.cookie; }
get session() { return this.req.session; }
get method() { return this.req.method; } write(value: any) { this.res.body = value; }
send() { this.res.send(); }
sendFile() {}
render() {}}
class Request { private _body?: any; private _query: any; private _params?: any; private _headers!: Headers; private _url!: string; private _method!: RouteMethod; private _cookie?: any; private _session?: any; private _files?: any[]; private _canSetParams = true; constructor(private rawRequest: ServerRequest, url: string, query: any, params: any, body: any) { const method = this.rawRequest.method.toUpperCase() as RouteMethod; this._headers = this.rawRequest.headers; this._url = url; this._method = method; this._body = body; this._query = query; this._params = params; }
get url() { return this._url; }
get headers() { return this._headers; }
get method() { return this._method; }
get body() { return this._body; } get query() { return this._query; }
get params() { return this._params; }
set params(value: any) { if (value !== undefined && this._canSetParams) { // this._params = value; this._canSetParams = false; } }
get files() { return this._files; }
get cookie() { return this._cookie; }
get session() { return this._session; }}
class Response { private _body?: any; private _isClose: boolean = false; status: number = 200; headers: Headers = new Headers(); isStatic: boolean = false; constructor(private req: ServerRequest, private c: Context) {}
send() { if (this._isClose) { return; } this.c.end_time = Date.now() + ""; this._isClose = true; // 根据各种信息 this.req.respond({ body: this._body, headers: this.headers, status: this.status }); }
set body(value: any) { /// transform this._body = this.resultFormater(value); } get body() { return this._body; }
private resultFormater(result: any): any { // 可以加入拦截器 if (!result) { result = ""; } else if (result.$$typeof) { console.log("this is react element"); result = (ReactDOMServer as any).renderToString(result); } else if (result instanceof Object) { result = JSON.stringify(result); } else if (typeof result === "number") { result = String(result); } return result; }}