11import { applyHandler } from '../internals/handler' ;
22import { loadPackage } from '../internals/loadPackage' ;
33
4- export enum HttpVerb {
4+ export enum HttpMethod {
55 GET = 'GET' ,
66 POST = 'POST' ,
77 PUT = 'PUT' ,
88 DELETE = 'DELETE' ,
9- PATCH = 'PATCH'
9+ PATCH = 'PATCH' ,
10+ OPTIONS = 'OPTIONS' ,
11+ HEAD = 'HEAD' ,
12+ CONNECT = 'CONNECT' ,
13+ TRACE = 'TRACE'
1014}
1115
1216export interface HandlerMethod {
13- verb : HttpVerb ;
17+ method : HttpMethod ;
18+ options ?: HandlerOptions ;
1419 path : string ;
1520 propertyKey : string | symbol ;
1621}
1722
18- export const HTTP_METHOD_TOKEN = Symbol ( 'ams:next:httpMethod' ) ;
23+ interface HandlerOptions {
24+ extraMethods ?: HttpMethod [ ] ;
25+ }
26+
27+ export const HTTP_METHOD_TOKEN = Symbol ( 'instant:next:httpMethod' ) ;
1928
20- function applyHttpMethod ( verb : HttpVerb , path : string ) {
29+ function applyHttpMethod ( { method , path, options } : { method : HttpMethod ; path : string ; options ?: HandlerOptions } ) {
2130 if ( process . env . NODE_ENV === 'development' && path !== '/' ) {
2231 loadPackage ( 'path-to-regexp' , {
23- context : '@' + verb . charAt ( 0 ) . toUpperCase ( ) + verb . slice ( 1 ) . toLowerCase ( ) ,
32+ context : '@' + method . charAt ( 0 ) . toUpperCase ( ) + method . slice ( 1 ) . toLowerCase ( ) ,
2433 docsUrl : 'https://next-api-decorators.vercel.app/docs/routing/route-matching'
2534 } ) ;
2635 }
2736
2837 return function ( target : object , propertyKey : string | symbol , descriptor : TypedPropertyDescriptor < any > ) {
2938 const methods : Array < HandlerMethod > = Reflect . getMetadata ( HTTP_METHOD_TOKEN , target . constructor ) ?? [ ] ;
3039
31- methods . push ( { path, verb , propertyKey } ) ;
40+ methods . push ( { path, method , options , propertyKey } ) ;
3241
3342 Reflect . defineMetadata ( HTTP_METHOD_TOKEN , methods , target . constructor ) ;
3443
3544 return applyHandler ( target , propertyKey , descriptor ) ;
3645 } ;
3746}
3847
48+ function getPath ( pathOrOptions ?: string | HandlerOptions ) {
49+ return typeof pathOrOptions === 'string' ? pathOrOptions : '/' ;
50+ }
51+
52+ function getOptions ( pathOrOptions ?: string | HandlerOptions , options ?: HandlerOptions ) {
53+ return typeof pathOrOptions === 'object' ? pathOrOptions : options ;
54+ }
55+
3956/** Makes the method a GET request handler. */
4057export function Get ( ) : MethodDecorator ;
58+ export function Get ( options : HandlerOptions ) : MethodDecorator ;
4159/**
4260 * Makes the method for the defined path a GET request handler.
4361 *
@@ -49,12 +67,18 @@ export function Get(): MethodDecorator;
4967 * More information: [route matching](https://next-api-decorators.vercel.app/docs/routing/route-matching)
5068 */
5169export function Get ( path : string ) : MethodDecorator ;
52- export function Get ( path : string = '/' ) : MethodDecorator {
53- return applyHttpMethod ( HttpVerb . GET , path ) ;
70+ export function Get ( path : string , options : HandlerOptions ) : MethodDecorator ;
71+ export function Get ( pathOrOptions ?: string | HandlerOptions , options ?: HandlerOptions ) : MethodDecorator {
72+ return applyHttpMethod ( {
73+ method : HttpMethod . GET ,
74+ path : getPath ( pathOrOptions ) ,
75+ options : getOptions ( pathOrOptions , options )
76+ } ) ;
5477}
5578
5679/** Makes the method a POST request handler. */
5780export function Post ( ) : MethodDecorator ;
81+ export function Post ( options : HandlerOptions ) : MethodDecorator ;
5882/**
5983 * Makes the method for the defined path a POST request handler.
6084 *
@@ -66,12 +90,18 @@ export function Post(): MethodDecorator;
6690 * More information: [route matching](https://next-api-decorators.vercel.app/docs/routing/route-matching)
6791 */
6892export function Post ( path : string ) : MethodDecorator ;
69- export function Post ( path : string = '/' ) : MethodDecorator {
70- return applyHttpMethod ( HttpVerb . POST , path ) ;
93+ export function Post ( path : string , options : HandlerOptions ) : MethodDecorator ;
94+ export function Post ( pathOrOptions ?: string | HandlerOptions , options ?: HandlerOptions ) : MethodDecorator {
95+ return applyHttpMethod ( {
96+ method : HttpMethod . POST ,
97+ path : getPath ( pathOrOptions ) ,
98+ options : getOptions ( pathOrOptions , options )
99+ } ) ;
71100}
72101
73102/** Makes the method a PUT request handler. */
74103export function Put ( ) : MethodDecorator ;
104+ export function Put ( options : HandlerOptions ) : MethodDecorator ;
75105/**
76106 * Makes the method for the defined path a PUT request handler.
77107 *
@@ -83,12 +113,18 @@ export function Put(): MethodDecorator;
83113 * More information: [route matching](https://next-api-decorators.vercel.app/docs/routing/route-matching)
84114 */
85115export function Put ( path : string ) : MethodDecorator ;
86- export function Put ( path : string = '/' ) : MethodDecorator {
87- return applyHttpMethod ( HttpVerb . PUT , path ) ;
116+ export function Put ( path : string , options : HandlerOptions ) : MethodDecorator ;
117+ export function Put ( pathOrOptions ?: string | HandlerOptions , options ?: HandlerOptions ) : MethodDecorator {
118+ return applyHttpMethod ( {
119+ method : HttpMethod . PUT ,
120+ path : getPath ( pathOrOptions ) ,
121+ options : getOptions ( pathOrOptions , options )
122+ } ) ;
88123}
89124
90125/** Makes the method a DELETE request handler. */
91126export function Delete ( ) : MethodDecorator ;
127+ export function Delete ( options : HandlerOptions ) : MethodDecorator ;
92128/**
93129 * Makes the method for the defined path a DELETE request handler.
94130 *
@@ -100,12 +136,18 @@ export function Delete(): MethodDecorator;
100136 * More information: [route matching](https://next-api-decorators.vercel.app/docs/routing/route-matching)
101137 */
102138export function Delete ( path : string ) : MethodDecorator ;
103- export function Delete ( path : string = '/' ) : MethodDecorator {
104- return applyHttpMethod ( HttpVerb . DELETE , path ) ;
139+ export function Delete ( path : string , options : HandlerOptions ) : MethodDecorator ;
140+ export function Delete ( pathOrOptions ?: string | HandlerOptions , options ?: HandlerOptions ) : MethodDecorator {
141+ return applyHttpMethod ( {
142+ method : HttpMethod . DELETE ,
143+ path : getPath ( pathOrOptions ) ,
144+ options : getOptions ( pathOrOptions , options )
145+ } ) ;
105146}
106147
107148/** Makes the method a PATCH request handler. */
108149export function Patch ( ) : MethodDecorator ;
150+ export function Patch ( options : HandlerOptions ) : MethodDecorator ;
109151/**
110152 * Makes the method for the defined path a PATCH request handler.
111153 *
@@ -117,6 +159,11 @@ export function Patch(): MethodDecorator;
117159 * More information: [route matching](https://next-api-decorators.vercel.app/docs/routing/route-matching)
118160 */
119161export function Patch ( path : string ) : MethodDecorator ;
120- export function Patch ( path : string = '/' ) : MethodDecorator {
121- return applyHttpMethod ( HttpVerb . PATCH , path ) ;
162+ export function Patch ( path : string , options : HandlerOptions ) : MethodDecorator ;
163+ export function Patch ( pathOrOptions ?: string | HandlerOptions , options ?: HandlerOptions ) : MethodDecorator {
164+ return applyHttpMethod ( {
165+ method : HttpMethod . PATCH ,
166+ path : getPath ( pathOrOptions ) ,
167+ options : getOptions ( pathOrOptions , options )
168+ } ) ;
122169}
0 commit comments