Skip to content

Commit

Permalink
add missing route shorthands (fastify#4409)
Browse files Browse the repository at this point in the history
  • Loading branch information
Uzlopak committed Nov 9, 2022
1 parent a4fffa7 commit cd45d4e
Show file tree
Hide file tree
Showing 5 changed files with 122 additions and 4 deletions.
18 changes: 18 additions & 0 deletions docs/Reference/Routes.md
Expand Up @@ -176,6 +176,24 @@ The above route declaration is more *Hapi*-like, but if you prefer an

`fastify.patch(path, [options], handler)`

`fastify.propfind(path, [options], handler)`

`fastify.proppatch(path, [options], handler)`

`fastify.mkcol(path, [options], handler)`

`fastify.copy(path, [options], handler)`

`fastify.move(path, [options], handler)`

`fastify.lock(path, [options], handler)`

`fastify.unlock(path, [options], handler)`

`fastify.trace(path, [options], handler)`

`fastify.search(path, [options], handler)`

Example:
```js
const opts = {
Expand Down
27 changes: 27 additions & 0 deletions fastify.js
Expand Up @@ -263,6 +263,33 @@ function fastify (options) {
options: function _options (url, options, handler) {
return router.prepareRoute.call(this, { method: 'OPTIONS', url, options, handler })
},
propfind: function _propfind (url, options, handler) {
return router.prepareRoute.call(this, { method: 'PROPFIND', url, options, handler })
},
proppatch: function _proppatch (url, options, handler) {
return router.prepareRoute.call(this, { method: 'PROPPATCH', url, options, handler })
},
mkcol: function _mkcol (url, options, handler) {
return router.prepareRoute.call(this, { method: 'MKCOL', url, options, handler })
},
copy: function _copy (url, options, handler) {
return router.prepareRoute.call(this, { method: 'COPY', url, options, handler })
},
move: function _move (url, options, handler) {
return router.prepareRoute.call(this, { method: 'MOVE', url, options, handler })
},
lock: function _lock (url, options, handler) {
return router.prepareRoute.call(this, { method: 'LOCK', url, options, handler })
},
unlock: function _unlock (url, options, handler) {
return router.prepareRoute.call(this, { method: 'UNLOCK', url, options, handler })
},
trace: function _trace (url, options, handler) {
return router.prepareRoute.call(this, { method: 'TRACE', url, options, handler })
},
search: function _search (url, options, handler) {
return router.prepareRoute.call(this, { method: 'SEARCH', url, options, handler })
},
all: function _all (url, options, handler) {
return router.prepareRoute.call(this, { method: supportedMethods, url, options, handler })
},
Expand Down
60 changes: 60 additions & 0 deletions test/route-shorthand.test.js
@@ -0,0 +1,60 @@
'use strict'

const t = require('tap')
const test = t.test
const sget = require('simple-get').concat
const Fastify = require('../fastify')
const supportedMethods = require('../lib/httpMethods').supportedMethods

test('route-shorthand', t => {
t.plan(supportedMethods.length + 1)
const test = t.test

for (const method of supportedMethods) {
test(`route-shorthand - ${method.toLowerCase()}`, t => {
t.plan(3)
const fastify = new Fastify()
fastify[method.toLowerCase()]('/', function (req, reply) {
t.equal(req.method, method)
reply.send()
})
fastify.listen({ port: 0 }, function (err) {
if (err) t.error(err)
t.teardown(() => { fastify.close() })
sget({
method,
url: 'http://localhost:' + fastify.server.address().port
}, (err, response, body) => {
t.error(err)
t.equal(response.statusCode, 200)
})
})
})
}

test('route-shorthand - all', t => {
t.plan(3 * supportedMethods.length)
const fastify = new Fastify()
let currentMethod = ''
fastify.all('/', function (req, reply) {
t.equal(req.method, currentMethod)
reply.send()
})
fastify.listen({ port: 0 }, async function (err) {
if (err) t.error(err)
t.teardown(() => { fastify.close() })
for (const method of supportedMethods) {
currentMethod = method
await new Promise(resolve => sget({
method,
url: 'http://localhost:' + fastify.server.address().port
}, (err, response, body) => {
t.error(err)
t.equal(response.statusCode, 200)
resolve()
})
)
}
})
})
})
8 changes: 6 additions & 2 deletions test/types/route.test-d.ts
Expand Up @@ -35,9 +35,13 @@ const routeHandlerWithReturnValue: RouteHandlerMethod = function (request, reply
return reply.send()
}

type LowerCaseHTTPMethods = 'get' | 'post' | 'put' | 'patch' | 'head' | 'delete' | 'options'
type LowerCaseHTTPMethods = 'delete' | 'get' | 'head' | 'patch' | 'post' | 'put' |
'options' | 'propfind' | 'proppatch' | 'mkcol' | 'copy' | 'move' | 'lock' |
'unlock' | 'trace' | 'search'

;['GET', 'POST', 'PUT', 'PATCH', 'HEAD', 'DELETE', 'OPTIONS'].forEach(method => {
;['DELETE', 'GET', 'HEAD', 'PATCH', 'POST', 'PUT', 'OPTIONS', 'PROPFIND',
'PROPPATCH', 'MKCOL', 'COPY', 'MOVE', 'LOCK', 'UNLOCK', 'TRACE', 'SEARCH'
].forEach(method => {
// route method
expectType<FastifyInstance>(fastify().route({
method: method as HTTPMethods,
Expand Down
13 changes: 11 additions & 2 deletions types/instance.d.ts
Expand Up @@ -177,13 +177,22 @@ export interface FastifyInstance<
SchemaCompiler extends FastifySchema = FastifySchema,
>(opts: RouteOptions<RawServer, RawRequest, RawReply, RouteGeneric, ContextConfig, SchemaCompiler, TypeProvider>): FastifyInstance<RawServer, RawRequest, RawReply, Logger, TypeProvider>;

delete: RouteShorthandMethod<RawServer, RawRequest, RawReply, TypeProvider>;
get: RouteShorthandMethod<RawServer, RawRequest, RawReply, TypeProvider>;
head: RouteShorthandMethod<RawServer, RawRequest, RawReply, TypeProvider>;
patch: RouteShorthandMethod<RawServer, RawRequest, RawReply, TypeProvider>;
post: RouteShorthandMethod<RawServer, RawRequest, RawReply, TypeProvider>;
put: RouteShorthandMethod<RawServer, RawRequest, RawReply, TypeProvider>;
delete: RouteShorthandMethod<RawServer, RawRequest, RawReply, TypeProvider>;
options: RouteShorthandMethod<RawServer, RawRequest, RawReply, TypeProvider>;
patch: RouteShorthandMethod<RawServer, RawRequest, RawReply, TypeProvider>;
propfind: RouteShorthandMethod<RawServer, RawRequest, RawReply, TypeProvider>;
proppatch: RouteShorthandMethod<RawServer, RawRequest, RawReply, TypeProvider>;
mkcol: RouteShorthandMethod<RawServer, RawRequest, RawReply, TypeProvider>;
copy: RouteShorthandMethod<RawServer, RawRequest, RawReply, TypeProvider>;
move: RouteShorthandMethod<RawServer, RawRequest, RawReply, TypeProvider>;
lock: RouteShorthandMethod<RawServer, RawRequest, RawReply, TypeProvider>;
unlock: RouteShorthandMethod<RawServer, RawRequest, RawReply, TypeProvider>;
trace: RouteShorthandMethod<RawServer, RawRequest, RawReply, TypeProvider>;
search: RouteShorthandMethod<RawServer, RawRequest, RawReply, TypeProvider>;
all: RouteShorthandMethod<RawServer, RawRequest, RawReply, TypeProvider>;

hasRoute<
Expand Down

0 comments on commit cd45d4e

Please sign in to comment.