-
Notifications
You must be signed in to change notification settings - Fork 167
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #35 from shinspiegel/json-body
feat: add new method on Request class
- Loading branch information
Showing
2 changed files
with
70 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,9 @@ | ||
import { compress as brotli } from 'https://deno.land/x/[email protected]/mod.ts' | ||
import { FormDataReader } from 'https://deno.land/x/[email protected]/multipart.ts' | ||
import { gzipEncode } from 'https://deno.land/x/[email protected]/mod.ts' | ||
import log from './log.ts' | ||
import { ServerRequest } from './std.ts' | ||
import type { APIRequest } from './types.ts' | ||
import type { APIRequest, FormDataBody } from './types.ts' | ||
|
||
export class Request extends ServerRequest implements APIRequest { | ||
#pathname: string | ||
|
@@ -84,6 +85,51 @@ export class Request extends ServerRequest implements APIRequest { | |
await this.send(JSON.stringify(data, replacer, space), 'application/json; charset=utf-8') | ||
} | ||
|
||
async decodeBody(type: "text"): Promise<string> | ||
async decodeBody(type: "json"): Promise<any> | ||
async decodeBody(type: "form-data"): Promise<FormDataBody> | ||
async decodeBody(type: string): Promise<any> { | ||
if (type === "text") { | ||
try { | ||
const buff: Uint8Array = await Deno.readAll(this.body); | ||
const encoded = new TextDecoder("utf-8").decode(buff); | ||
return encoded; | ||
} catch (err) { | ||
console.error("Failed to parse the request body.", err); | ||
} | ||
} | ||
|
||
if (type === "json") { | ||
try { | ||
const buff: Uint8Array = await Deno.readAll(this.body); | ||
const encoded = new TextDecoder("utf-8").decode(buff); | ||
const json = JSON.parse(encoded); | ||
return json; | ||
} catch (err) { | ||
console.error("Failed to parse the request body.", err); | ||
} | ||
} | ||
|
||
if (type === "form-data") { | ||
try { | ||
const boundary = this.headers.get("content-type"); | ||
|
||
if (!boundary) throw new Error("Failed to get the content-type") | ||
|
||
const reader = new FormDataReader(boundary, this.body); | ||
const { fields, files } = await reader.read({ maxSize: 1024 * 1024 * 10 }); | ||
|
||
return { | ||
get: (key: string) => fields[key], | ||
getFile: (key: string) => files?.find(i => i.name === key) | ||
} | ||
|
||
} catch (err) { | ||
console.error("Failed to parse the request form-data", err) | ||
} | ||
} | ||
} | ||
|
||
async send(data: string | Uint8Array | ArrayBuffer, contentType?: string): Promise<void> { | ||
if (this.#resp.done) { | ||
log.warn('ServerRequest: repeat respond calls') | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters