@remix-run/method-override-middleware 0.0.0

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/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025 Shopify Inc.
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
22
+
package/README.md ADDED
@@ -0,0 +1,67 @@
1
+ # method-override-middleware
2
+
3
+ Middleware for overriding HTTP request methods from form data for use with [`@remix-run/fetch-router`](https://github.com/remix-run/remix/tree/main/packages/fetch-router).
4
+
5
+ Allows HTML forms (which only support GET and POST) to submit with other HTTP methods like PUT, PATCH, or DELETE by including a special form field.
6
+
7
+ ## Installation
8
+
9
+ ```sh
10
+ npm install @remix-run/method-override-middleware
11
+ ```
12
+
13
+ ## Usage
14
+
15
+ This middleware runs after [the `formData` middleware](https://github.com/remix-run/remix/tree/main/packages/form-data-middleware) and updates the request context's `context.method` with the value of the method override field. This is useful for simulating RESTful API request methods like PUT and DELETE using HTML forms.
16
+
17
+ ```ts
18
+ import { createRouter } from '@remix-run/fetch-router'
19
+ import { formData } from '@remix-run/form-data-middleware'
20
+ import { methodOverride } from '@remix-run/method-override-middleware'
21
+
22
+ let router = createRouter({
23
+ // methodOverride must come AFTER formData middleware
24
+ middleware: [formData(), methodOverride()],
25
+ })
26
+
27
+ router.delete('/users/:id', async (context) => {
28
+ let userId = context.params.id
29
+ // Delete user logic...
30
+ return new Response('User deleted')
31
+ })
32
+ ```
33
+
34
+ In your HTML form:
35
+
36
+ ```html
37
+ <form method="POST" action="/users/123">
38
+ <input type="hidden" name="_method" value="DELETE" />
39
+ <button type="submit">Delete User</button>
40
+ </form>
41
+ ```
42
+
43
+ ### Custom Field Name
44
+
45
+ You can customize the name of the method override field by passing a `fieldName` option to the `methodOverride()` middleware.
46
+
47
+ ```ts
48
+ let router = createRouter({
49
+ middleware: [formData(), methodOverride({ fieldName: '__method__' })],
50
+ })
51
+ ```
52
+
53
+ ```html
54
+ <form method="POST" action="/users/123">
55
+ <input type="hidden" name="__method__" value="PUT" />
56
+ <button type="submit">Update User</button>
57
+ </form>
58
+ ```
59
+
60
+ ## Related Packages
61
+
62
+ - [`fetch-router`](https://github.com/remix-run/remix/tree/main/packages/fetch-router) - Router for the web Fetch API
63
+ - [`form-data-middleware`](https://github.com/remix-run/remix/tree/main/packages/form-data-middleware) - Required for parsing form data
64
+
65
+ ## License
66
+
67
+ See [LICENSE](https://github.com/remix-run/remix/blob/main/LICENSE)
@@ -0,0 +1,2 @@
1
+ export { type MethodOverrideOptions, methodOverride } from './lib/method-override.ts';
2
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,qBAAqB,EAAE,cAAc,EAAE,MAAM,0BAA0B,CAAA"}
package/dist/index.js ADDED
@@ -0,0 +1 @@
1
+ export { methodOverride } from "./lib/method-override.js";
@@ -0,0 +1,19 @@
1
+ import type { Middleware } from '@remix-run/fetch-router';
2
+ export interface MethodOverrideOptions {
3
+ /**
4
+ * The name of the form field to check for request method override.
5
+ * Default is `_method`.
6
+ */
7
+ fieldName?: string;
8
+ }
9
+ /**
10
+ * Middleware that overrides `context.method` with the value of the method override field.
11
+ *
12
+ * Note: This middleware must be placed after the `formData` middleware in the middleware chain, or
13
+ * some other middleware that provides `context.formData`.
14
+ *
15
+ * @param options (optional) Options for the method override middleware
16
+ * @returns A middleware that overrides `context.method` with the value of the method override field
17
+ */
18
+ export declare function methodOverride(options?: MethodOverrideOptions): Middleware;
19
+ //# sourceMappingURL=method-override.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"method-override.d.ts","sourceRoot":"","sources":["../../src/lib/method-override.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,UAAU,EAAkB,MAAM,yBAAyB,CAAA;AAEzE,MAAM,WAAW,qBAAqB;IACpC;;;OAGG;IACH,SAAS,CAAC,EAAE,MAAM,CAAA;CACnB;AAMD;;;;;;;;GAQG;AACH,wBAAgB,cAAc,CAAC,OAAO,CAAC,EAAE,qBAAqB,GAAG,UAAU,CAoB1E"}
@@ -0,0 +1,27 @@
1
+ const RequestMethods = ['GET', 'POST', 'PUT', 'PATCH', 'DELETE', 'HEAD', 'OPTIONS'];
2
+ /**
3
+ * Middleware that overrides `context.method` with the value of the method override field.
4
+ *
5
+ * Note: This middleware must be placed after the `formData` middleware in the middleware chain, or
6
+ * some other middleware that provides `context.formData`.
7
+ *
8
+ * @param options (optional) Options for the method override middleware
9
+ * @returns A middleware that overrides `context.method` with the value of the method override field
10
+ */
11
+ export function methodOverride(options) {
12
+ let fieldName = options?.fieldName ?? '_method';
13
+ return async (context) => {
14
+ let formData = context.formData;
15
+ if (formData == null) {
16
+ return;
17
+ }
18
+ let method = formData.get(fieldName);
19
+ if (typeof method !== 'string') {
20
+ return;
21
+ }
22
+ let requestMethod = method.toUpperCase();
23
+ if (RequestMethods.includes(requestMethod)) {
24
+ context.method = requestMethod;
25
+ }
26
+ };
27
+ }
package/package.json ADDED
@@ -0,0 +1,50 @@
1
+ {
2
+ "name": "@remix-run/method-override-middleware",
3
+ "version": "0.0.0",
4
+ "description": "Middleware for overriding HTTP request methods from form data",
5
+ "author": "Michael Jackson <mjijackson@gmail.com>",
6
+ "license": "MIT",
7
+ "repository": {
8
+ "type": "git",
9
+ "url": "git+https://github.com/remix-run/remix.git",
10
+ "directory": "packages/method-override-middleware"
11
+ },
12
+ "homepage": "https://github.com/remix-run/remix/tree/main/packages/method-override-middleware#readme",
13
+ "files": [
14
+ "LICENSE",
15
+ "README.md",
16
+ "dist",
17
+ "src",
18
+ "!src/**/*.test.ts"
19
+ ],
20
+ "type": "module",
21
+ "exports": {
22
+ ".": {
23
+ "types": "./dist/index.d.ts",
24
+ "default": "./dist/index.js"
25
+ },
26
+ "./package.json": "./package.json"
27
+ },
28
+ "devDependencies": {
29
+ "@types/node": "^24.6.0",
30
+ "typescript": "^5.9.3",
31
+ "@remix-run/fetch-router": "0.9.0",
32
+ "@remix-run/form-data-middleware": "0.0.0"
33
+ },
34
+ "peerDependencies": {
35
+ "@remix-run/fetch-router": "^0.9.0"
36
+ },
37
+ "keywords": [
38
+ "fetch",
39
+ "router",
40
+ "middleware",
41
+ "method-override",
42
+ "http-method"
43
+ ],
44
+ "scripts": {
45
+ "build": "tsc -p tsconfig.build.json",
46
+ "clean": "git clean -fdX",
47
+ "test": "node --disable-warning=ExperimentalWarning --test './src/**/*.test.ts'",
48
+ "typecheck": "tsc --noEmit"
49
+ }
50
+ }
package/src/index.ts ADDED
@@ -0,0 +1,2 @@
1
+ export { type MethodOverrideOptions, methodOverride } from './lib/method-override.ts'
2
+
@@ -0,0 +1,44 @@
1
+ import type { Middleware, RequestContext } from '@remix-run/fetch-router'
2
+
3
+ export interface MethodOverrideOptions {
4
+ /**
5
+ * The name of the form field to check for request method override.
6
+ * Default is `_method`.
7
+ */
8
+ fieldName?: string
9
+ }
10
+
11
+ const RequestMethods = ['GET', 'POST', 'PUT', 'PATCH', 'DELETE', 'HEAD', 'OPTIONS'] as const
12
+
13
+ type RequestMethod = (typeof RequestMethods)[number]
14
+
15
+ /**
16
+ * Middleware that overrides `context.method` with the value of the method override field.
17
+ *
18
+ * Note: This middleware must be placed after the `formData` middleware in the middleware chain, or
19
+ * some other middleware that provides `context.formData`.
20
+ *
21
+ * @param options (optional) Options for the method override middleware
22
+ * @returns A middleware that overrides `context.method` with the value of the method override field
23
+ */
24
+ export function methodOverride(options?: MethodOverrideOptions): Middleware {
25
+ let fieldName = options?.fieldName ?? '_method'
26
+
27
+ return async (context: RequestContext) => {
28
+ let formData = context.formData
29
+ if (formData == null) {
30
+ return
31
+ }
32
+
33
+ let method = formData.get(fieldName)
34
+ if (typeof method !== 'string') {
35
+ return
36
+ }
37
+
38
+ let requestMethod = method.toUpperCase() as RequestMethod
39
+
40
+ if (RequestMethods.includes(requestMethod)) {
41
+ context.method = requestMethod
42
+ }
43
+ }
44
+ }