@remix-run/form-data-middleware 0.0.0 → 0.1.1

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.
@@ -1,8 +1,13 @@
1
1
  import { type FileUploadHandler, type ParseFormDataOptions } from '@remix-run/form-data-parser';
2
2
  import type { Middleware } from '@remix-run/fetch-router';
3
+ /**
4
+ * Options for the `formData` middleware.
5
+ */
3
6
  export interface FormDataOptions extends ParseFormDataOptions {
4
7
  /**
5
- * Set `true` to suppress parse errors. Default is `false`.
8
+ * Set `true` to suppress parse errors.
9
+ *
10
+ * @default false
6
11
  */
7
12
  suppressErrors?: boolean;
8
13
  /**
@@ -14,8 +19,9 @@ export interface FormDataOptions extends ParseFormDataOptions {
14
19
  }
15
20
  /**
16
21
  * Middleware that parses `FormData` from the request body and populates `context.formData`.
22
+ *
17
23
  * @param options Options for parsing form data
18
- * @returns A middleware function that parses form data
24
+ * @return A middleware function that parses form data
19
25
  */
20
26
  export declare function formData(options?: FormDataOptions): Middleware;
21
27
  //# sourceMappingURL=form-data.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"form-data.d.ts","sourceRoot":"","sources":["../../src/lib/form-data.ts"],"names":[],"mappings":"AAAA,OAAO,EAGL,KAAK,iBAAiB,EACtB,KAAK,oBAAoB,EAC1B,MAAM,6BAA6B,CAAA;AAEpC,OAAO,KAAK,EAAE,UAAU,EAAkB,MAAM,yBAAyB,CAAA;AAEzE,MAAM,WAAW,eAAgB,SAAQ,oBAAoB;IAC3D;;OAEG;IACH,cAAc,CAAC,EAAE,OAAO,CAAA;IACxB;;;;OAIG;IACH,aAAa,CAAC,EAAE,iBAAiB,CAAA;CAClC;AAED;;;;GAIG;AACH,wBAAgB,QAAQ,CAAC,OAAO,CAAC,EAAE,eAAe,GAAG,UAAU,CA8B9D"}
1
+ {"version":3,"file":"form-data.d.ts","sourceRoot":"","sources":["../../src/lib/form-data.ts"],"names":[],"mappings":"AAAA,OAAO,EAGL,KAAK,iBAAiB,EACtB,KAAK,oBAAoB,EAC1B,MAAM,6BAA6B,CAAA;AACpC,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,yBAAyB,CAAA;AAEzD;;GAEG;AACH,MAAM,WAAW,eAAgB,SAAQ,oBAAoB;IAC3D;;;;OAIG;IACH,cAAc,CAAC,EAAE,OAAO,CAAA;IACxB;;;;OAIG;IACH,aAAa,CAAC,EAAE,iBAAiB,CAAA;CAClC;AAED;;;;;GAKG;AACH,wBAAgB,QAAQ,CAAC,OAAO,CAAC,EAAE,eAAe,GAAG,UAAU,CA6B9D"}
@@ -1,22 +1,22 @@
1
1
  import { FormDataParseError, parseFormData, } from '@remix-run/form-data-parser';
2
2
  /**
3
3
  * Middleware that parses `FormData` from the request body and populates `context.formData`.
4
+ *
4
5
  * @param options Options for parsing form data
5
- * @returns A middleware function that parses form data
6
+ * @return A middleware function that parses form data
6
7
  */
7
8
  export function formData(options) {
8
9
  let suppressErrors = options?.suppressErrors ?? false;
9
10
  let uploadHandler = options?.uploadHandler;
10
11
  return async (context) => {
11
- // Get the method from context to respect any method override middleware
12
- let method = context.method;
13
- if (method === 'GET' || method === 'HEAD') {
12
+ if (context.method === 'GET' || context.method === 'HEAD') {
14
13
  return;
15
14
  }
16
15
  let contentType = context.headers.get('Content-Type');
17
16
  if (contentType == null ||
18
17
  (!contentType.startsWith('multipart/') &&
19
18
  !contentType.startsWith('application/x-www-form-urlencoded'))) {
19
+ context.formData = new FormData();
20
20
  return;
21
21
  }
22
22
  try {
@@ -26,6 +26,7 @@ export function formData(options) {
26
26
  if (!suppressErrors || !(error instanceof FormDataParseError)) {
27
27
  throw error;
28
28
  }
29
+ context.formData = new FormData();
29
30
  }
30
31
  };
31
32
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@remix-run/form-data-middleware",
3
- "version": "0.0.0",
3
+ "version": "0.1.1",
4
4
  "description": "Middleware for parsing FormData from request bodies",
5
5
  "author": "Michael Jackson <mjijackson@gmail.com>",
6
6
  "license": "MIT",
@@ -27,12 +27,13 @@
27
27
  },
28
28
  "devDependencies": {
29
29
  "@types/node": "^24.6.0",
30
+ "@typescript/native-preview": "7.0.0-dev.20251125.1",
30
31
  "typescript": "^5.9.3",
31
- "@remix-run/fetch-router": "0.9.0",
32
+ "@remix-run/fetch-router": "0.13.0",
32
33
  "@remix-run/form-data-parser": "0.14.0"
33
34
  },
34
35
  "peerDependencies": {
35
- "@remix-run/fetch-router": "^0.9.0",
36
+ "@remix-run/fetch-router": "^0.13.0",
36
37
  "@remix-run/form-data-parser": "^0.14.0"
37
38
  },
38
39
  "keywords": [
@@ -44,9 +45,9 @@
44
45
  "file-upload"
45
46
  ],
46
47
  "scripts": {
47
- "build": "tsc -p tsconfig.build.json",
48
+ "build": "tsgo -p tsconfig.build.json",
48
49
  "clean": "git clean -fdX",
49
- "test": "node --disable-warning=ExperimentalWarning --test './src/**/*.test.ts'",
50
- "typecheck": "tsc --noEmit"
50
+ "test": "node --disable-warning=ExperimentalWarning --test",
51
+ "typecheck": "tsgo --noEmit"
51
52
  }
52
53
  }
package/src/index.ts CHANGED
@@ -2,4 +2,3 @@ export { FormDataParseError } from '@remix-run/form-data-parser'
2
2
  export type { FileUpload, FileUploadHandler } from '@remix-run/form-data-parser'
3
3
 
4
4
  export { type FormDataOptions, formData } from './lib/form-data.ts'
5
-
@@ -4,12 +4,16 @@ import {
4
4
  type FileUploadHandler,
5
5
  type ParseFormDataOptions,
6
6
  } from '@remix-run/form-data-parser'
7
+ import type { Middleware } from '@remix-run/fetch-router'
7
8
 
8
- import type { Middleware, RequestContext } from '@remix-run/fetch-router'
9
-
9
+ /**
10
+ * Options for the `formData` middleware.
11
+ */
10
12
  export interface FormDataOptions extends ParseFormDataOptions {
11
13
  /**
12
- * Set `true` to suppress parse errors. Default is `false`.
14
+ * Set `true` to suppress parse errors.
15
+ *
16
+ * @default false
13
17
  */
14
18
  suppressErrors?: boolean
15
19
  /**
@@ -22,28 +26,26 @@ export interface FormDataOptions extends ParseFormDataOptions {
22
26
 
23
27
  /**
24
28
  * Middleware that parses `FormData` from the request body and populates `context.formData`.
29
+ *
25
30
  * @param options Options for parsing form data
26
- * @returns A middleware function that parses form data
31
+ * @return A middleware function that parses form data
27
32
  */
28
33
  export function formData(options?: FormDataOptions): Middleware {
29
34
  let suppressErrors = options?.suppressErrors ?? false
30
35
  let uploadHandler = options?.uploadHandler
31
36
 
32
- return async (context: RequestContext) => {
33
- // Get the method from context to respect any method override middleware
34
- let method = context.method
35
-
36
- if (method === 'GET' || method === 'HEAD') {
37
+ return async (context) => {
38
+ if (context.method === 'GET' || context.method === 'HEAD') {
37
39
  return
38
40
  }
39
41
 
40
42
  let contentType = context.headers.get('Content-Type')
41
-
42
43
  if (
43
44
  contentType == null ||
44
45
  (!contentType.startsWith('multipart/') &&
45
46
  !contentType.startsWith('application/x-www-form-urlencoded'))
46
47
  ) {
48
+ context.formData = new FormData()
47
49
  return
48
50
  }
49
51
 
@@ -53,6 +55,8 @@ export function formData(options?: FormDataOptions): Middleware {
53
55
  if (!suppressErrors || !(error instanceof FormDataParseError)) {
54
56
  throw error
55
57
  }
58
+
59
+ context.formData = new FormData()
56
60
  }
57
61
  }
58
62
  }