deno.land / x / duck_web_framework@0.1.1 / response.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
import { Response, ServerRequest } from "https://deno.land/std@0.70.0/http/server.ts";import { lookup } from "https://deno.land/x/media_types/mod.ts";import { CookieJar } from "./cookie_jar.ts";
export class DuckResponse { private request: ServerRequest; private response: Response; private sent: boolean = false; cookies?: CookieJar;
constructor(request: ServerRequest) { this.request = request; this.response = { status: 200, headers: new Headers(), }; }
send(data?: string | object) { if (this.sent) return console.warn("Response has been already sent, cancelling"); this.sent = true; if (data) { if (typeof data === "object") { data = JSON.stringify(data); this.response.headers?.set("Content-Type", "application/json"); } else { this.response.headers?.set("Content-Type", "text/plain"); } this.response.body = data; } this.request.respond(this.response); }
status(status: number) { this.response.status = status; return this; }
async file(filePath: string) { try { const contentType: any = await lookup(filePath); this.response.headers?.set("Content-Type", contentType); this.response.body = await Deno.open(filePath); this.request.respond(this.response); } catch (e) { if (e instanceof Deno.errors.NotFound) return console.error(`File not found: ${filePath}`); console.error(e); } }
get headers(): Headers { return this.response.headers!; }
get gStatus(): Number { return this.response.status!; }}
duck_web_framework

Version Info

Tagged at
3 years ago