Skip to content
This repository was archived by the owner on Jul 6, 2025. It is now read-only.

Commit 4bb7e31

Browse files
committed
feat: add new method on Request class
1 parent a94ead4 commit 4bb7e31

File tree

2 files changed

+9
-0
lines changed

2 files changed

+9
-0
lines changed

api.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,13 @@ export class Request extends ServerRequest implements APIRequest {
8484
await this.send(JSON.stringify(data, replacer, space), 'application/json; charset=utf-8')
8585
}
8686

87+
async jsonBody(): Promise<any> {
88+
const buff: Uint8Array = await Deno.readAll(this.body);
89+
const encoded = new TextDecoder("utf-8").decode(buff);
90+
const json = JSON.parse(encoded);
91+
return json;
92+
}
93+
8794
async send(data: string | Uint8Array | ArrayBuffer, contentType?: string): Promise<void> {
8895
if (this.#resp.done) {
8996
log.warn('ServerRequest: repeat respond calls')

types.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,8 @@ export interface APIRequest extends ServerRequest {
9393
send(data: string | Uint8Array | ArrayBuffer, contentType?: string): Promise<void>
9494
/** `json` replies to the request with a json content */
9595
json(data: any): Promise<void>
96+
/** `jsonBody` will return the request body in a JSON format */
97+
jsonBody(): Promise<any>
9698
}
9799

98100
/**

0 commit comments

Comments
 (0)