hono-method-override
v0.2.1
Published
[![npm version][npm-version-src]][npm-version-href] [![npm downloads][npm-downloads-src]][npm-downloads-href] [![bundle][bundle-src]][bundle-href] [![License][license-src]][license-href]
Downloads
124
Readme
hono-method-override
A Hono plugin to let you use HTTP verbs such as PUT or DELETE in places where the client doesn't support it.
This repo is highly inspired by express method-override.
Install
$ npm install hono-method-override
Usage
import { Hono } from 'hono';
import { methodOverride } from 'hono-method-override';
const app = new Hono();
export default {
fetch: methodOverride(app.fetch),
};
That's it! Simply wrap the app.fetch
with methodOverride
API
methodOverride(honoFetch: HonoRequest, _getter?: string | string[])
What is a getter , a getter is where to look up the overridden request method, the default getter is X-HTTP-Method-Override
, which means that the methodOverride
plugin will look for and parse the overridden method in the X-HTTP-Method-Override
request header
Tow types of getter is supported:
HeaderGetter
: getter start withx-/X-
, it will parse from request header, eg:methodOverride(app.fetch, 'X-Method-Override')
should parsecurl -X POST -H 'X-Method-Override: PATCH' http://localhost
QueryGetter
: any string not start withx-/X-
, it will parse from request query, eg:methodOverride(app.fetch, '_method')
should parsecurl -X POST http://localhost?_method=PATCH
! Note that ONLY POST
request can be override, this design is for making the API concise and reduce errors.
Examples
header getter
import { Hono } from 'hono';
import { methodOverride } from './src/method-override';
const app = new Hono();
app.patch('/', (c) => c.text('hello world'));
export default {
fetch: methodOverride(app.fetch, 'X-Method-Override'),
};
// curl -X POST http://127.0.0.1:3000 -H 'X-Method-Override: patch' # hello world
query getter
import { Hono } from 'hono';
import { methodOverride } from './src/method-override';
const app = new Hono();
app.patch('/', (c) => c.text('hello world'));
export default {
fetch: methodOverride(app.fetch, '_method'),
};
// curl -X POST http://127.0.0.1:3000\?_method\=patch # hello world
multiple getters
If multiple getters are specified, the order passed to methodOverride
getter array matters, the last one take precedence.
import { Hono } from 'hono';
import { methodOverride } from './src/method-override';
const app = new Hono();
app
.patch('/', (c) => c.text('hello patch'))
.put('/', (c) => c.text('hello put'))
.delete('/', (c) => c.text('hello delete'));
export default {
fetch: methodOverride(app.fetch, ['_method', 'x-method', '_method2']),
};
// curl -X POST http://127.0.0.1:3000\?_method2\=delete\&_method\=patch -H 'x-method: put' # hello delete
more examples
You can find more usages and examples in test, this project is 100% tested.