@haste-health/koa-multipart-form 0.4.4

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 ADDED
@@ -0,0 +1 @@
1
+ # koa-multipart-form
package/lib/index.d.ts ADDED
@@ -0,0 +1,27 @@
1
+ import type Koa from "koa";
2
+ import originalMulter from "multer";
3
+ interface Field {
4
+ /** The field name. */
5
+ name: string;
6
+ /** Optional maximum number of files per field to accept. */
7
+ maxCount?: number;
8
+ }
9
+ interface Instance {
10
+ /** In case you need to handle a text-only multipart form, you can use any of the multer methods (.single(), .array(), fields()), req.body contains the text fields */
11
+ /** Accept a single file with the name fieldName. The single file will be stored in req.file. */
12
+ single(fieldName?: string): Koa.Middleware;
13
+ /** Accept an array of files, all with the name fieldName. Optionally error out if more than maxCount files are uploaded. The array of files will be stored in req.files. */
14
+ array(fieldName: string, maxCount?: number): Koa.Middleware;
15
+ /** Accept a mix of files, specified by fields. An object with arrays of files will be stored in req.files. */
16
+ fields(fields: Field[]): Koa.Middleware;
17
+ /** Accepts all files that comes over the wire. An array of files will be stored in req.files. */
18
+ any(): Koa.Middleware;
19
+ /** No files accepted. */
20
+ none(): Koa.Middleware;
21
+ }
22
+ export declare function multer(options?: originalMulter.Options): Instance;
23
+ export declare namespace multer {
24
+ var diskStorage: typeof originalMulter.diskStorage;
25
+ var memoryStorage: typeof originalMulter.memoryStorage;
26
+ }
27
+ export {};
package/lib/index.js ADDED
@@ -0,0 +1,69 @@
1
+ "use strict";
2
+ /* eslint @typescript-eslint/no-explicit-any: 0 */
3
+ var __importDefault = (this && this.__importDefault) || function (mod) {
4
+ return (mod && mod.__esModule) ? mod : { "default": mod };
5
+ };
6
+ Object.defineProperty(exports, "__esModule", { value: true });
7
+ exports.multer = multer;
8
+ const multer_1 = __importDefault(require("multer"));
9
+ function multer(options) {
10
+ const m = (0, multer_1.default)(options);
11
+ makePromise(m, "any");
12
+ makePromise(m, "array");
13
+ makePromise(m, "fields");
14
+ makePromise(m, "none");
15
+ makePromise(m, "single");
16
+ return m;
17
+ }
18
+ function makePromise(multer, name) {
19
+ if (!multer[name])
20
+ return;
21
+ const fn = multer[name];
22
+ multer[name] = function () {
23
+ // eslint-disable-next-line prefer-rest-params
24
+ const middleware = Reflect.apply(fn, this, arguments);
25
+ return async (ctx, next) => {
26
+ if (ctx.header["content-type"] !== "multipart/form-data")
27
+ return next();
28
+ await new Promise((resolve, reject) => {
29
+ // Ignore if not multipart/form-data
30
+ middleware(ctx.req, ctx.res, (err) => {
31
+ if (err)
32
+ return reject(err);
33
+ if ("request" in ctx) {
34
+ // @ts-ignore
35
+ if (ctx.req.body) {
36
+ // @ts-ignore
37
+ ctx.request.body = ctx.req.body;
38
+ // @ts-ignore
39
+ delete ctx.req.body;
40
+ }
41
+ // @ts-ignore
42
+ if (ctx.req.file) {
43
+ // @ts-ignore
44
+ ctx.request.file = ctx.req.file;
45
+ // @ts-ignore
46
+ ctx.file = ctx.req.file;
47
+ // @ts-ignore
48
+ delete ctx.req.file;
49
+ }
50
+ // @ts-ignore
51
+ if (ctx.req.files) {
52
+ // @ts-ignore
53
+ ctx.request.files = ctx.req.files;
54
+ // @ts-ignore
55
+ ctx.files = ctx.req.files;
56
+ // @ts-ignore
57
+ delete ctx.req.files;
58
+ }
59
+ }
60
+ resolve(ctx);
61
+ });
62
+ });
63
+ return next();
64
+ };
65
+ };
66
+ }
67
+ multer.diskStorage = multer_1.default.diskStorage;
68
+ multer.memoryStorage = multer_1.default.memoryStorage;
69
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";AAAA,kDAAkD;;;;;AA6BlD,wBAUC;AAjCD,oDAAoC;AAuBpC,SAAgB,MAAM,CAAC,OAAgC;IACrD,MAAM,CAAC,GAAG,IAAA,gBAAc,EAAC,OAAO,CAAC,CAAC;IAElC,WAAW,CAAC,CAAC,EAAE,KAAK,CAA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CAAC,GAAG,CAAC,IAAI,EAAE,CAAC;4BACjB,aAAa;4BACb,GAAG,CAAC,OAAO,CAAC,IAAI,GAAG,GAAG,CAAC,GAAG,CAAC,IAAI,CAAC;4BAChC,aAAa;4BACb,GAAG,CAAC,IAAI,GAAG,GAAG,CAAC,GAAG,CAAC,IAAI,CAAC;4BACxB,aAAa;4BACb,OAAO,GAAG,CAAC,GAAG,CAAC,IAAI,CAAC;wBACtB,CAAC;wBAED,aAAa;wBACb,IAAI,GAAG,CAAC,GAAG,CAAC,KAAK,EAAE,CAAC;4BAClB,aAAa;4BACb,GAAG,CAAC,OAAO,CAAC,KAAK,GAAG,GAAG,CAAC,GAAG,CAAC,KAAK,CAAC;4BAClC,aAAa;4BACb,GAAG,CAAC,KAAK,GAAG,GAAG,CAAC,GAAG,CAAC,KAAK,CAAC;4BAC1B,aAAa;4BACb,OAAO,GAAG,CAAC,GAAG,CAAC,KAAK,CAAC;wBACvB,CAAC;oBACH,CAAC;oBAED,OAAO,CAAC,GAAG,CAAC,CAAC;gBACf,CAAC,CAAC,CAAC;YACL,CAAC,CAAC,CAAC;YAEH,OAAO,IAAI,EAAE,CAAC;QAChB,CAAC,CAAC;IACJ,CAAC,CAAC;AACJ,CAAC;AAED,MAAM,CAAC,WAAW,GAAG,gBAAc,CAAC,WAAW,CAAC;AAChD,MAAM,CAAC,aAAa,GAAG,gBAAc,CAAC,aAAa,CAAC"}
package/package.json ADDED
@@ -0,0 +1,18 @@
1
+ {
2
+ "name": "@haste-health/koa-multipart-form",
3
+ "version": "0.4.4",
4
+ "dependencies": {
5
+ "@types/koa": "^2.15.0",
6
+ "multer": "^2.0.0"
7
+ },
8
+ "devDependencies": {
9
+ "@types/multer": "^1.4.12",
10
+ "typescript": "5.9.2"
11
+ },
12
+ "types": "lib/index.d.ts",
13
+ "main": "lib/index.js",
14
+ "scripts": {
15
+ "build": "tsc",
16
+ "publish": "pnpm build && pnpm npm publish --access public --tolerate-republish"
17
+ }
18
+ }
package/src/index.ts ADDED
@@ -0,0 +1,100 @@
1
+ /* eslint @typescript-eslint/no-explicit-any: 0 */
2
+
3
+ // Code Pulled from https://github.com/koajs/multer
4
+ // Fixes issues with esm. This is a temporary fix until the original package is updated to support esm properly and not conflict tsx.
5
+
6
+ import type Koa from "koa";
7
+ import originalMulter from "multer";
8
+
9
+ interface Field {
10
+ /** The field name. */
11
+ name: string;
12
+ /** Optional maximum number of files per field to accept. */
13
+ maxCount?: number;
14
+ }
15
+
16
+ interface Instance {
17
+ /** In case you need to handle a text-only multipart form, you can use any of the multer methods (.single(), .array(), fields()), req.body contains the text fields */
18
+ /** Accept a single file with the name fieldName. The single file will be stored in req.file. */
19
+ single(fieldName?: string): Koa.Middleware;
20
+ /** Accept an array of files, all with the name fieldName. Optionally error out if more than maxCount files are uploaded. The array of files will be stored in req.files. */
21
+ array(fieldName: string, maxCount?: number): Koa.Middleware;
22
+ /** Accept a mix of files, specified by fields. An object with arrays of files will be stored in req.files. */
23
+ fields(fields: Field[]): Koa.Middleware;
24
+ /** Accepts all files that comes over the wire. An array of files will be stored in req.files. */
25
+ any(): Koa.Middleware;
26
+ /** No files accepted. */
27
+ none(): Koa.Middleware;
28
+ }
29
+
30
+ export function multer(options?: originalMulter.Options): Instance {
31
+ const m = originalMulter(options);
32
+
33
+ makePromise(m, "any");
34
+ makePromise(m, "array");
35
+ makePromise(m, "fields");
36
+ makePromise(m, "none");
37
+ makePromise(m, "single");
38
+
39
+ return m as originalMulter.Multer & Instance;
40
+ }
41
+
42
+ function makePromise(
43
+ multer: originalMulter.Multer & Record<string, any>,
44
+ name: string,
45
+ ) {
46
+ if (!multer[name]) return;
47
+
48
+ const fn = multer[name];
49
+
50
+ multer[name] = function () {
51
+ // eslint-disable-next-line prefer-rest-params
52
+ const middleware = Reflect.apply(fn, this, arguments);
53
+
54
+ return async (ctx: Koa.Context, next: () => any) => {
55
+ if (ctx.header["content-type"] !== "multipart/form-data") return next();
56
+ await new Promise((resolve, reject) => {
57
+ // Ignore if not multipart/form-data
58
+ middleware(ctx.req, ctx.res, (err: any) => {
59
+ if (err) return reject(err);
60
+ if ("request" in ctx) {
61
+ // @ts-ignore
62
+ if (ctx.req.body) {
63
+ // @ts-ignore
64
+ ctx.request.body = ctx.req.body;
65
+ // @ts-ignore
66
+ delete ctx.req.body;
67
+ }
68
+
69
+ // @ts-ignore
70
+ if (ctx.req.file) {
71
+ // @ts-ignore
72
+ ctx.request.file = ctx.req.file;
73
+ // @ts-ignore
74
+ ctx.file = ctx.req.file;
75
+ // @ts-ignore
76
+ delete ctx.req.file;
77
+ }
78
+
79
+ // @ts-ignore
80
+ if (ctx.req.files) {
81
+ // @ts-ignore
82
+ ctx.request.files = ctx.req.files;
83
+ // @ts-ignore
84
+ ctx.files = ctx.req.files;
85
+ // @ts-ignore
86
+ delete ctx.req.files;
87
+ }
88
+ }
89
+
90
+ resolve(ctx);
91
+ });
92
+ });
93
+
94
+ return next();
95
+ };
96
+ };
97
+ }
98
+
99
+ multer.diskStorage = originalMulter.diskStorage;
100
+ multer.memoryStorage = originalMulter.memoryStorage;
package/tsconfig.json ADDED
@@ -0,0 +1,8 @@
1
+ {
2
+ "extends": "../../config/base.tsconfig.json",
3
+ "exclude": ["./**/*.test.ts", "lib/*", "jest.config.js"],
4
+ "compilerOptions": {
5
+ "rootDir": "src",
6
+ "outDir": "lib"
7
+ }
8
+ }