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

Commit 41dd2c1

Browse files
committed
feat: rename function to decodeBody, implemented string and json decoding
1 parent ae843c7 commit 41dd2c1

File tree

2 files changed

+47
-12
lines changed

2 files changed

+47
-12
lines changed

api.ts

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { compress as brotli } from 'https://deno.land/x/[email protected]/mod.ts'
22
import { gzipEncode } from 'https://deno.land/x/[email protected]/mod.ts'
33
import log from './log.ts'
44
import { ServerRequest } from './std.ts'
5-
import type { APIRequest } from './types.ts'
5+
import type { APIRequest, FormDataBody } from './types.ts'
66

77
export class Request extends ServerRequest implements APIRequest {
88
#pathname: string
@@ -84,15 +84,30 @@ 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-
try {
89-
const buff: Uint8Array = await Deno.readAll(this.body);
90-
const encoded = new TextDecoder("utf-8").decode(buff);
91-
const json = JSON.parse(encoded);
92-
return json;
93-
} catch (err) {
94-
console.error("Failed to parse the request body.", err);
95-
return null;
87+
async decodeBody(type: "text" | "json" | "form-data"): Promise<string | any | FormDataBody> {
88+
if (type === "text") {
89+
try {
90+
const buff: Uint8Array = await Deno.readAll(this.body);
91+
const encoded = new TextDecoder("utf-8").decode(buff);
92+
return encoded;
93+
} catch (err) {
94+
console.error("Failed to parse the request body.", err);
95+
}
96+
}
97+
98+
if (type === "json") {
99+
try {
100+
const buff: Uint8Array = await Deno.readAll(this.body);
101+
const encoded = new TextDecoder("utf-8").decode(buff);
102+
const json = JSON.parse(encoded);
103+
return json;
104+
} catch (err) {
105+
console.error("Failed to parse the request body.", err);
106+
}
107+
}
108+
109+
if (type === "form-data") {
110+
// TODO
96111
}
97112
}
98113

types.ts

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,8 +93,10 @@ 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>
96+
/** `decodeBody` will return a string, a form-data or any json object */
97+
decodeBody(type: "text"): Promise<string>
98+
decodeBody(type: "json"): Promise<any>
99+
decodeBody(type: "form-data"): Promise<FormDataBody>
98100
}
99101

100102
/**
@@ -107,3 +109,21 @@ export interface RouterURL {
107109
readonly params: Record<string, string>
108110
readonly query: URLSearchParams
109111
}
112+
113+
/**
114+
* The form data body
115+
*/
116+
export interface FormDataBody {
117+
get(key: string): string
118+
getFile(key: string): FormFile
119+
}
120+
121+
/**
122+
* The form file data
123+
*/
124+
export interface FormFile {
125+
filename: string
126+
contentType: string
127+
content: Uint8Array
128+
size: number
129+
}

0 commit comments

Comments
 (0)