Repository navigation
Expand file tree
/
Copy pathbodyParser.ts
More file actions
69 lines (63 loc) · 1.69 KB
/
Copy pathbodyParser.ts
File metadata and controls
69 lines (63 loc) · 1.69 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
import { ServerRequest} from './deps.ts';
export type Body = {
[key: string]: Body | number
} | string
/**
* Supported content types on the request headers
*/
const CONTENT_TYPES: {
[key: string]: string | undefined
} = {
'application/json': 'json',
'application/x-www-form-urlencoded': 'urlencoded',
'text/plain': 'text',
'raw': undefined
}
/**
* Regex for the query string parser, matches all characters after and including '?'
*/
const regex = new RegExp(/\?.+$/)
/**
* Parses the body of a request into a JSON format
* Currently only JSON and urlencoded optiosn are supported
* @param request Deno request object
* @return Promise<undefined | Body>
*/
export async function bodyParser(req: ServerRequest): Promise<undefined | Body> {
let body: Body | undefined
const contentType = CONTENT_TYPES[req.headers.get("content-type")?.split(';')[0] || 'raw']
const decoder = new TextDecoder()
const rawBody = decoder.decode(await Deno.readAll(req.body))
if (!contentType) {
return rawBody
}
switch (contentType) {
case 'json': {
body = JSON.parse(rawBody)
break;
}
case 'urlencoded': {
const searchParams = new URLSearchParams(rawBody.replace(/\+/g, " "))
body = {}
for (var pair of searchParams.entries()) {
body[pair[0]] = pair[1]
}
break;
}
default:
body = rawBody
break;
}
return body
}
/**
* Parses the query string of a request into an URLSearchParams object
* @param request Deno request object
* @return URLSearchParams | undefined
*/
export function queryParser(req: ServerRequest) {
const match = regex.exec(req.url)
if (match?.length) {
return new URLSearchParams(match[0])
}
}