1
1
import { compress as brotli } from 'https://deno.land/x/[email protected] /mod.ts'
2
+ import { FormDataReader } from 'https://deno.land/x/[email protected] /multipart.ts'
2
3
import { gzipEncode } from 'https://deno.land/x/[email protected] /mod.ts'
3
4
import log from './log.ts'
4
5
import { ServerRequest } from './std.ts'
@@ -84,7 +85,9 @@ export class Request extends ServerRequest implements APIRequest {
84
85
await this . send ( JSON . stringify ( data , replacer , space ) , 'application/json; charset=utf-8' )
85
86
}
86
87
87
- async decodeBody ( type : "text" | "json" | "form-data" ) : Promise < string | any | FormDataBody > {
88
+ async decodeBody ( type : "text" ) : Promise < string >
89
+ async decodeBody ( type : "json" ) : Promise < any >
90
+ async decodeBody ( type : "form-data" ) : Promise < FormDataBody > {
88
91
if ( type === "text" ) {
89
92
try {
90
93
const buff : Uint8Array = await Deno . readAll ( this . body ) ;
@@ -107,7 +110,23 @@ export class Request extends ServerRequest implements APIRequest {
107
110
}
108
111
109
112
if ( type === "form-data" ) {
110
- // TODO
113
+ try {
114
+ const boundary = this . headers . get ( "content-type" ) ;
115
+
116
+ if ( ! boundary ) throw new Error ( "Failed to get the content-type" )
117
+
118
+ const reader = new FormDataReader ( boundary , this . body ) ;
119
+ const { fields, files } = await reader . read ( { maxSize : 1024 * 1024 * 10 } ) ;
120
+
121
+ return {
122
+ get : ( key : string ) => fields [ key ] ,
123
+ getFile : ( key : string ) => files ?. find ( i => i . name === key )
124
+ }
125
+
126
+ } catch ( err ) {
127
+ console . error ( "Failed to parse the request form-data" , err )
128
+ }
129
+
111
130
}
112
131
}
113
132
0 commit comments