@remix-run/method-override-middleware 0.1.3 → 0.1.5
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
CHANGED
|
@@ -1,13 +1,17 @@
|
|
|
1
1
|
# method-override-middleware
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Method override middleware for Remix. It allows HTML forms to simulate `PUT`, `PATCH`, and `DELETE` requests using a hidden form field.
|
|
4
4
|
|
|
5
|
-
|
|
5
|
+
## Features
|
|
6
|
+
|
|
7
|
+
- **Form Method Overrides** - Translate posted form fields into request methods
|
|
8
|
+
- **HTML Form Friendly** - Supports REST-style routes from standard browser forms
|
|
9
|
+
- **Configurable Field Name** - Choose a custom override field key
|
|
6
10
|
|
|
7
11
|
## Installation
|
|
8
12
|
|
|
9
13
|
```sh
|
|
10
|
-
npm
|
|
14
|
+
npm i remix
|
|
11
15
|
```
|
|
12
16
|
|
|
13
17
|
## Usage
|
|
@@ -15,9 +19,9 @@ npm install @remix-run/method-override-middleware
|
|
|
15
19
|
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
20
|
|
|
17
21
|
```ts
|
|
18
|
-
import { createRouter } from '
|
|
19
|
-
import { formData } from '
|
|
20
|
-
import { methodOverride } from '
|
|
22
|
+
import { createRouter } from 'remix/fetch-router'
|
|
23
|
+
import { formData } from 'remix/form-data-middleware'
|
|
24
|
+
import { methodOverride } from 'remix/method-override-middleware'
|
|
21
25
|
|
|
22
26
|
let router = createRouter({
|
|
23
27
|
// methodOverride must come AFTER formData middleware
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import type { Middleware } from '@remix-run/fetch-router';
|
|
2
2
|
/**
|
|
3
|
-
* Options for the
|
|
3
|
+
* Options for the {@link methodOverride} middleware.
|
|
4
4
|
*/
|
|
5
5
|
export interface MethodOverrideOptions {
|
|
6
6
|
/**
|
|
@@ -13,8 +13,9 @@ export interface MethodOverrideOptions {
|
|
|
13
13
|
/**
|
|
14
14
|
* Middleware that overrides `context.method` with the value of the method override field.
|
|
15
15
|
*
|
|
16
|
-
* Note: This middleware must be placed after the
|
|
17
|
-
*
|
|
16
|
+
* Note: This middleware must be placed after the
|
|
17
|
+
* {@link import('@remix-run/form-data-middleware').formData} middleware in the middleware
|
|
18
|
+
* chain, or some other middleware that provides `context.get(FormData)`.
|
|
18
19
|
*
|
|
19
20
|
* @param options Options for the method override middleware
|
|
20
21
|
* @returns A middleware that overrides `context.method` with the value of the method override field
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"method-override.d.ts","sourceRoot":"","sources":["../../src/lib/method-override.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,UAAU,EAAiC,MAAM,yBAAyB,CAAA;AAExF;;GAEG;AACH,MAAM,WAAW,qBAAqB;IACpC;;;;OAIG;IACH,SAAS,CAAC,EAAE,MAAM,CAAA;CACnB;AAED
|
|
1
|
+
{"version":3,"file":"method-override.d.ts","sourceRoot":"","sources":["../../src/lib/method-override.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,UAAU,EAAiC,MAAM,yBAAyB,CAAA;AAExF;;GAEG;AACH,MAAM,WAAW,qBAAqB;IACpC;;;;OAIG;IACH,SAAS,CAAC,EAAE,MAAM,CAAA;CACnB;AAED;;;;;;;;;GASG;AACH,wBAAgB,cAAc,CAAC,OAAO,CAAC,EAAE,qBAAqB,GAAG,UAAU,CAe1E"}
|
|
@@ -2,8 +2,9 @@ import { RequestMethods } from '@remix-run/fetch-router';
|
|
|
2
2
|
/**
|
|
3
3
|
* Middleware that overrides `context.method` with the value of the method override field.
|
|
4
4
|
*
|
|
5
|
-
* Note: This middleware must be placed after the
|
|
6
|
-
*
|
|
5
|
+
* Note: This middleware must be placed after the
|
|
6
|
+
* {@link import('@remix-run/form-data-middleware').formData} middleware in the middleware
|
|
7
|
+
* chain, or some other middleware that provides `context.get(FormData)`.
|
|
7
8
|
*
|
|
8
9
|
* @param options Options for the method override middleware
|
|
9
10
|
* @returns A middleware that overrides `context.method` with the value of the method override field
|
|
@@ -11,7 +12,7 @@ import { RequestMethods } from '@remix-run/fetch-router';
|
|
|
11
12
|
export function methodOverride(options) {
|
|
12
13
|
let fieldName = options?.fieldName ?? '_method';
|
|
13
14
|
return (context) => {
|
|
14
|
-
let method = context.
|
|
15
|
+
let method = context.has(FormData) ? context.get(FormData).get(fieldName) : null;
|
|
15
16
|
if (typeof method !== 'string') {
|
|
16
17
|
return;
|
|
17
18
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@remix-run/method-override-middleware",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.5",
|
|
4
4
|
"description": "Middleware for overriding HTTP request methods from form data",
|
|
5
5
|
"author": "Michael Jackson <mjijackson@gmail.com>",
|
|
6
6
|
"license": "MIT",
|
|
@@ -28,11 +28,11 @@
|
|
|
28
28
|
"devDependencies": {
|
|
29
29
|
"@types/node": "^24.6.0",
|
|
30
30
|
"@typescript/native-preview": "7.0.0-dev.20251125.1",
|
|
31
|
-
"@remix-run/fetch-router": "0.
|
|
32
|
-
"@remix-run/form-data-middleware": "0.
|
|
31
|
+
"@remix-run/fetch-router": "0.18.0",
|
|
32
|
+
"@remix-run/form-data-middleware": "0.2.0"
|
|
33
33
|
},
|
|
34
34
|
"dependencies": {
|
|
35
|
-
"@remix-run/fetch-router": "^0.
|
|
35
|
+
"@remix-run/fetch-router": "^0.18.0"
|
|
36
36
|
},
|
|
37
37
|
"keywords": [
|
|
38
38
|
"fetch",
|
|
@@ -44,7 +44,7 @@
|
|
|
44
44
|
"scripts": {
|
|
45
45
|
"build": "tsgo -p tsconfig.build.json",
|
|
46
46
|
"clean": "git clean -fdX",
|
|
47
|
-
"test": "node --
|
|
47
|
+
"test": "node --test",
|
|
48
48
|
"typecheck": "tsgo --noEmit"
|
|
49
49
|
}
|
|
50
50
|
}
|
|
@@ -2,7 +2,7 @@ import { RequestMethods } from '@remix-run/fetch-router'
|
|
|
2
2
|
import type { Middleware, RequestContext, RequestMethod } from '@remix-run/fetch-router'
|
|
3
3
|
|
|
4
4
|
/**
|
|
5
|
-
* Options for the
|
|
5
|
+
* Options for the {@link methodOverride} middleware.
|
|
6
6
|
*/
|
|
7
7
|
export interface MethodOverrideOptions {
|
|
8
8
|
/**
|
|
@@ -16,8 +16,9 @@ export interface MethodOverrideOptions {
|
|
|
16
16
|
/**
|
|
17
17
|
* Middleware that overrides `context.method` with the value of the method override field.
|
|
18
18
|
*
|
|
19
|
-
* Note: This middleware must be placed after the
|
|
20
|
-
*
|
|
19
|
+
* Note: This middleware must be placed after the
|
|
20
|
+
* {@link import('@remix-run/form-data-middleware').formData} middleware in the middleware
|
|
21
|
+
* chain, or some other middleware that provides `context.get(FormData)`.
|
|
21
22
|
*
|
|
22
23
|
* @param options Options for the method override middleware
|
|
23
24
|
* @returns A middleware that overrides `context.method` with the value of the method override field
|
|
@@ -26,7 +27,7 @@ export function methodOverride(options?: MethodOverrideOptions): Middleware {
|
|
|
26
27
|
let fieldName = options?.fieldName ?? '_method'
|
|
27
28
|
|
|
28
29
|
return (context: RequestContext) => {
|
|
29
|
-
let method = context.
|
|
30
|
+
let method = context.has(FormData) ? context.get(FormData).get(fieldName) : null
|
|
30
31
|
if (typeof method !== 'string') {
|
|
31
32
|
return
|
|
32
33
|
}
|