@@ -2,7 +2,7 @@ import { compress as brotli } from 'https://deno.land/x/
[email protected] /mod.ts'
2
2
import { gzipEncode } from 'https://deno.land/x/[email protected] /mod.ts'
3
3
import log from './log.ts'
4
4
import { ServerRequest } from './std.ts'
5
- import type { APIRequest } from './types.ts'
5
+ import type { APIRequest , FormDataBody } from './types.ts'
6
6
7
7
export class Request extends ServerRequest implements APIRequest {
8
8
#pathname: string
@@ -84,15 +84,30 @@ export class Request extends ServerRequest implements APIRequest {
84
84
await this . send ( JSON . stringify ( data , replacer , space ) , 'application/json; charset=utf-8' )
85
85
}
86
86
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
96
111
}
97
112
}
98
113
0 commit comments