Skip to content

Commit a457028

Browse files
authored
Merge pull request #17 from anzharip/feat/accept-additional-options
feat: accept additional options
2 parents 7f52b81 + 2bc8a21 commit a457028

File tree

4 files changed

+66
-3
lines changed

4 files changed

+66
-3
lines changed

README.md

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,3 +84,34 @@ This is the example reponse received on the client:
8484
]
8585
}
8686
```
87+
88+
You can also pass [busboy constructor config](https://github.com/mscdex/busboy#busboy-methods) as an optional parameter:
89+
90+
```typescript
91+
import { AzureFunction, Context, HttpRequest } from "@azure/functions";
92+
import parseMultipartFormData from "@anzp/azure-function-multipart";
93+
94+
const httpTrigger: AzureFunction = async function (
95+
context: Context,
96+
req: HttpRequest
97+
): Promise<void> {
98+
// Set the max number of non-file fields to 1 (Default: Infinity).
99+
const config = {
100+
limits: { fields: 1 },
101+
};
102+
const { fields, files } = await parseMultipartFormData(req, config);
103+
context.log("HTTP trigger function processed a request.");
104+
const name = req.query.name || (req.body && req.body.name);
105+
const responseMessage = {
106+
fields,
107+
files,
108+
};
109+
110+
context.res = {
111+
// status: 200, /* Defaults to 200 */
112+
body: responseMessage,
113+
};
114+
};
115+
116+
export default httpTrigger;
117+
```

src/index.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,23 @@ import { HttpRequest } from "@azure/functions";
33
import { ParsedField } from "./types/parsed-field.type";
44
import { ParsedFile } from "./types/parsed-file.type";
55
import { ParsedMultipartFormData } from "./types/parsed-multipart-form-data.type";
6+
import { Config } from "./types/config.type";
67

78
export default async function parseMultipartFormData(
8-
request: HttpRequest
9+
request: HttpRequest,
10+
options?: Config
911
): Promise<ParsedMultipartFormData> {
1012
return new Promise((resolve, reject) => {
1113
try {
1214
const fields: Promise<ParsedField>[] = [];
1315
const files: Promise<ParsedFile>[] = [];
14-
const busboy = new Busboy({ headers: request.headers });
16+
17+
let busboy;
18+
if (options) {
19+
busboy = new Busboy({ headers: request.headers, ...options });
20+
} else {
21+
busboy = new Busboy({ headers: request.headers });
22+
}
1523

1624
busboy.on(
1725
"file",

src/types/config.type.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
export type Config = {
2+
highWaterMark?: number;
3+
fileHwm?: number;
4+
defCharset?: string;
5+
preservePath?: boolean;
6+
limits?: {
7+
fieldNameSize?: number;
8+
fieldSize?: number;
9+
fields?: number;
10+
fileSize?: number;
11+
files?: number;
12+
parts?: number;
13+
headerPairs?: number;
14+
};
15+
};

test/index.spec.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ describe("index.js", () => {
1010
const file1 = fs.readFileSync("test/fixture/dummy-data.json");
1111
const body = new FormData();
1212
body.append("field1", "value1");
13+
body.append("field2", "value2");
14+
body.append("field3", "value3");
1315
body.append("file1", file1);
1416

1517
request = {
@@ -26,7 +28,7 @@ describe("index.js", () => {
2628

2729
it("should populate fields property", async () => {
2830
const { fields } = await parseMultipartFormData(request);
29-
expect(fields.length).toBe(1);
31+
expect(fields.length).toBe(3);
3032
});
3133

3234
it("should populate files property", async () => {
@@ -47,4 +49,11 @@ describe("index.js", () => {
4749
await parseMultipartFormData(request);
4850
}).rejects.toThrow();
4951
});
52+
53+
it("should accept options parameter, parse only one field", async () => {
54+
const { fields } = await parseMultipartFormData(request, {
55+
limits: { fields: 1 },
56+
});
57+
expect(fields.length).toBe(1);
58+
});
5059
});

0 commit comments

Comments
 (0)