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

Commit bb5774e

Browse files
committed
feat: added formdata from OAK's multipart file
1 parent 41dd2c1 commit bb5774e

File tree

2 files changed

+22
-2
lines changed

2 files changed

+22
-2
lines changed

api.ts

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
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'
23
import { gzipEncode } from 'https://deno.land/x/[email protected]/mod.ts'
34
import log from './log.ts'
45
import { ServerRequest } from './std.ts'
@@ -84,7 +85,9 @@ export class Request extends ServerRequest implements APIRequest {
8485
await this.send(JSON.stringify(data, replacer, space), 'application/json; charset=utf-8')
8586
}
8687

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> {
8891
if (type === "text") {
8992
try {
9093
const buff: Uint8Array = await Deno.readAll(this.body);
@@ -107,7 +110,23 @@ export class Request extends ServerRequest implements APIRequest {
107110
}
108111

109112
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+
111130
}
112131
}
113132

std.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,5 @@ export type { Response } from 'https://deno.land/[email protected]/http/server.ts'
88
export { fromStreamReader } from 'https://deno.land/[email protected]/io/mod.ts'
99
export * as path from 'https://deno.land/[email protected]/path/mod.ts'
1010
export * as ws from 'https://deno.land/[email protected]/ws/mod.ts'
11+
export * as multipart from 'https://deno.land/[email protected]/mime/multipart.ts'
1112

0 commit comments

Comments
 (0)