Skip to content

Commit

Permalink
Merge pull request #17 from anzharip/feat/accept-additional-options
Browse files Browse the repository at this point in the history
feat: accept additional options
  • Loading branch information
anzharip authored Apr 23, 2021
2 parents 7f52b81 + 2bc8a21 commit a457028
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 3 deletions.
31 changes: 31 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,3 +84,34 @@ This is the example reponse received on the client:
]
}
```

You can also pass [busboy constructor config](https://github.com/mscdex/busboy#busboy-methods) as an optional parameter:

```typescript
import { AzureFunction, Context, HttpRequest } from "@azure/functions";
import parseMultipartFormData from "@anzp/azure-function-multipart";

const httpTrigger: AzureFunction = async function (
context: Context,
req: HttpRequest
): Promise<void> {
// Set the max number of non-file fields to 1 (Default: Infinity).
const config = {
limits: { fields: 1 },
};
const { fields, files } = await parseMultipartFormData(req, config);
context.log("HTTP trigger function processed a request.");
const name = req.query.name || (req.body && req.body.name);
const responseMessage = {
fields,
files,
};

context.res = {
// status: 200, /* Defaults to 200 */
body: responseMessage,
};
};

export default httpTrigger;
```
12 changes: 10 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,23 @@ import { HttpRequest } from "@azure/functions";
import { ParsedField } from "./types/parsed-field.type";
import { ParsedFile } from "./types/parsed-file.type";
import { ParsedMultipartFormData } from "./types/parsed-multipart-form-data.type";
import { Config } from "./types/config.type";

export default async function parseMultipartFormData(
request: HttpRequest
request: HttpRequest,
options?: Config
): Promise<ParsedMultipartFormData> {
return new Promise((resolve, reject) => {
try {
const fields: Promise<ParsedField>[] = [];
const files: Promise<ParsedFile>[] = [];
const busboy = new Busboy({ headers: request.headers });

let busboy;
if (options) {
busboy = new Busboy({ headers: request.headers, ...options });
} else {
busboy = new Busboy({ headers: request.headers });
}

busboy.on(
"file",
Expand Down
15 changes: 15 additions & 0 deletions src/types/config.type.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
export type Config = {
highWaterMark?: number;
fileHwm?: number;
defCharset?: string;
preservePath?: boolean;
limits?: {
fieldNameSize?: number;
fieldSize?: number;
fields?: number;
fileSize?: number;
files?: number;
parts?: number;
headerPairs?: number;
};
};
11 changes: 10 additions & 1 deletion test/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ describe("index.js", () => {
const file1 = fs.readFileSync("test/fixture/dummy-data.json");
const body = new FormData();
body.append("field1", "value1");
body.append("field2", "value2");
body.append("field3", "value3");
body.append("file1", file1);

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

it("should populate fields property", async () => {
const { fields } = await parseMultipartFormData(request);
expect(fields.length).toBe(1);
expect(fields.length).toBe(3);
});

it("should populate files property", async () => {
Expand All @@ -47,4 +49,11 @@ describe("index.js", () => {
await parseMultipartFormData(request);
}).rejects.toThrow();
});

it("should accept options parameter, parse only one field", async () => {
const { fields } = await parseMultipartFormData(request, {
limits: { fields: 1 },
});
expect(fields.length).toBe(1);
});
});

0 comments on commit a457028

Please sign in to comment.