Repository navigation
Expand file tree
/
Copy pathresponse.ts
More file actions
104 lines (92 loc) · 2.87 KB
/
Copy pathresponse.ts
File metadata and controls
104 lines (92 loc) · 2.87 KB
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
import { Response as DenoResponse, ServerRequest } from './deps.ts'
/** Encodes the url preventing double enconding */
export function encodeUrl(url: string) {
return String(url)
.replace(UNMATCHED_SURROGATE_PAIR_REGEXP, UNMATCHED_SURROGATE_PAIR_REPLACE)
.replace(ENCODE_CHARS_REGEXP, encodeURI)
}
const ENCODE_CHARS_REGEXP = /(?:[^\x21\x25\x26-\x3B\x3D\x3F-\x5B\x5D\x5F\x61-\x7A\x7E]|%(?:[^0-9A-Fa-f]|[0-9A-Fa-f][^0-9A-Fa-f]|$))+/g
const UNMATCHED_SURROGATE_PAIR_REGEXP = /(^|[^\uD800-\uDBFF])[\uDC00-\uDFFF]|[\uD800-\uDBFF]([^\uDC00-\uDFFF]|$)/g;
const UNMATCHED_SURROGATE_PAIR_REPLACE = "$1\uFFFD$2";
export const REDIRECT_BACK = Symbol("redirect backwards")
export interface IResponse extends DenoResponse {
error?: any
body: any
headers: Headers
finished: boolean
sent: boolean
sendResponse: () => Promise<boolean>
redirect: (
url: string | URL | typeof REDIRECT_BACK,
alt?: string | URL
) => void
[key: string]: any
}
/**
* This class represents the response object from Mith framework
*/
export class Response implements IResponse{
error?: any
body: any = {}
headers: Headers = new Headers()
finished: boolean = false
sent: boolean = false
status: number = 200
private request: ServerRequest
constructor(req: ServerRequest) {
this.request = req
}
/**
* Redirects the user to the chosen URL
* @param url string or url or symbol redirect backwards
* @param alt string or url to be used in case of unexisting "Referrer" when redirecting the user backwards
* @return void
*/
redirect(url: string | URL | typeof REDIRECT_BACK, alt: string | URL = "/") {
if (url === REDIRECT_BACK) {
url = this.request.headers.get("Referrer") ?? String(alt);
} else if (typeof url === "object") {
url = String(url);
}
this.headers.set("Location", encodeUrl(url));
this.status = 302;
this.headers.set('Content-Type', 'text/plain; charset=utf-8')
this.body = this.body.length !== undefined && this.body.length !== 0 ? this.body : `Redirecting to ${url}.`
}
/**
* Sends the response back to the caller
*
* @param request Deno Server Request Object
* @param response Mith Server Response Object
* @return void
*/
public async sendResponse() {
let body = this.body
if (!this.sent) {
this.sent = true
if (typeof this.body === 'object') {
if (typeof body.read !== 'function') {
if (!this.headers.get('content-type')) {
this.headers.set('content-type', 'application/json')
}
body = JSON.stringify(this.body)
}
}
return this.request.respond({
status: this.status,
headers: this.headers,
body,
}).then(
() => {
return true
}
).catch(
(e) => {
console.log(e)
return false
}
)
}
return false
}
}