@haste-health/koa-multipart-form 0.127.1 → 0.127.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +55 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1 +1,55 @@
|
|
|
1
|
-
# koa-multipart-form
|
|
1
|
+
# @haste-health/koa-multipart-form
|
|
2
|
+
|
|
3
|
+
Koa middleware for `multipart/form-data` requests, built on [multer](https://github.com/expressjs/multer). It is a port of [@koa/multer](https://github.com/koajs/multer) that loads cleanly as an ES module (the original conflicts with ESM loaders such as tsx). The middleware only engages when the request's `Content-Type` is `multipart/form-data`; every other request passes straight to `next()`.
|
|
4
|
+
|
|
5
|
+
It has no FHIR dependency and works in any Koa 2 application.
|
|
6
|
+
|
|
7
|
+
## Install
|
|
8
|
+
|
|
9
|
+
```bash
|
|
10
|
+
npm install @haste-health/koa-multipart-form koa
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
## Usage
|
|
14
|
+
|
|
15
|
+
```ts
|
|
16
|
+
import Koa from "koa";
|
|
17
|
+
import Router from "@koa/router";
|
|
18
|
+
import { multer } from "@haste-health/koa-multipart-form";
|
|
19
|
+
|
|
20
|
+
const upload = multer({
|
|
21
|
+
storage: multer.memoryStorage(),
|
|
22
|
+
limits: { fileSize: 10 * 1024 * 1024 },
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
const router = new Router();
|
|
26
|
+
|
|
27
|
+
router.post("/upload", upload.single("file"), (ctx) => {
|
|
28
|
+
// Text fields are on ctx.request.body, the file on ctx.file (also ctx.request.file).
|
|
29
|
+
const { originalname, size, buffer } = ctx.file;
|
|
30
|
+
ctx.body = { name: originalname, size, fields: ctx.request.body };
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
const app = new Koa();
|
|
34
|
+
app.use(router.routes());
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
`multer(options)` takes multer's options (`storage`, `limits`, `fileFilter`, ...) and returns an instance whose methods produce Koa middleware:
|
|
38
|
+
|
|
39
|
+
| Method | Accepts | Result |
|
|
40
|
+
| ------------------------------ | ------------------------------------ | ----------------------------------- |
|
|
41
|
+
| `single(fieldName)` | One file under `fieldName` | `ctx.file` |
|
|
42
|
+
| `array(fieldName, maxCount?)` | Several files under `fieldName` | `ctx.files` (array) |
|
|
43
|
+
| `fields([{ name, maxCount }])` | Files under several field names | `ctx.files` (object keyed by field) |
|
|
44
|
+
| `any()` | Any files | `ctx.files` (array) |
|
|
45
|
+
| `none()` | Text fields only; a file is an error | `ctx.request.body` |
|
|
46
|
+
|
|
47
|
+
`multer.diskStorage` and `multer.memoryStorage` are re-exported. The middleware assigns `ctx.file` and `ctx.files` at run time without augmenting Koa's TypeScript types, so declare them on your context type if you use strict typing.
|
|
48
|
+
|
|
49
|
+
## Links
|
|
50
|
+
|
|
51
|
+
- [API reference](https://typescript.haste.health/modules/_haste-health_koa-multipart-form.html)
|
|
52
|
+
- [Source](https://github.com/HasteHealth/HasteHealth/tree/main/frontend/packages/koa-multipart-form)
|
|
53
|
+
- [multer options](https://github.com/expressjs/multer#multeropts)
|
|
54
|
+
|
|
55
|
+
Part of [Haste Health](https://haste.health), licensed under [Apache-2.0](https://github.com/HasteHealth/HasteHealth/blob/main/LICENSE).
|