forked from utily/cloudly-http
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.ts
More file actions
27 lines (24 loc) · 1.13 KB
/
index.ts
File metadata and controls
27 lines (24 loc) · 1.13 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
import { Method } from "./Request/Method"
import * as Parser from "./Parser"
import * as Stringifier from "./Serializer"
import { Request } from "./Request"
import { Response } from "./Response"
import { Query } from "./Query"
import { fetch } from "./fetch"
Parser.add(async request => await request.text(), "text/plain", "text/html")
Parser.add(async request => await request.json(), "application/json")
Parser.add(async request => {
const result: { [property: string]: FormDataEntryValue } = {}
if (isRequest(request))
(await request.formData()).forEach((value, key) => (result[key] = value))
return result
}, "multipart/form-data")
Parser.add(async request => Query.parse(request.text()), "application/x-www-urlencoded")
Stringifier.add(async body => (typeof body == "string" ? body : body.toString()), "text/plain", "text/html")
Stringifier.add(async body => JSON.stringify(body), "application/json")
function isRequest(
value: any | globalThis.Request | globalThis.Response
): value is globalThis.Request | globalThis.Response {
return typeof value.formData == "function"
}
export { Method, Parser, Stringifier, Request, Response, Query, fetch }