8
8
* You should have received a copy of the MIT License along with this program.
9
9
*/
10
10
import { ClassType , CompilerContext , getClassName , isArray , isClass , urlJoin } from '@deepkit/core' ;
11
- import {
12
- entity ,
13
- ReflectionClass ,
14
- ReflectionFunction ,
15
- ReflectionKind ,
16
- ReflectionParameter ,
17
- SerializationOptions ,
18
- serializer ,
19
- Serializer ,
20
- Type ,
21
- ValidationError ,
22
- } from '@deepkit/type' ;
11
+ import { entity , ReflectionClass , ReflectionFunction , ReflectionKind , ReflectionParameter , SerializationOptions , serializer , Serializer , Type , ValidationError } from '@deepkit/type' ;
23
12
import { getActions , HttpAction , httpClass , HttpController , HttpDecorator } from './decorator.js' ;
24
13
import { HttpRequest , HttpRequestPositionedParameters , HttpRequestQuery , HttpRequestResolvedParameters } from './model.js' ;
25
14
import { InjectorContext , InjectorModule } from '@deepkit/injector' ;
@@ -30,7 +19,7 @@ import { HttpMiddlewareConfig, HttpMiddlewareFn } from './middleware.js';
30
19
31
20
//@ts -ignore
32
21
import qs from 'qs' ;
33
- import { HtmlResponse , JSONResponse , Response } from './http.js' ;
22
+ import { HtmlResponse , JSONResponse , Redirect , Response } from './http.js' ;
34
23
import { getRequestParserCodeForParameters , ParameterForRequestParser , parseRoutePathToRegex } from './request-parser.js' ;
35
24
import { HttpConfig } from './module.config.js' ;
36
25
@@ -352,7 +341,7 @@ export interface HttpRouterFunctionOptions {
352
341
function convertOptions ( methods : string [ ] , pathOrOptions : string | HttpRouterFunctionOptions , defaultOptions : Partial < HttpRouterFunctionOptions > ) : HttpRouterFunctionOptions {
353
342
const options = 'string' === typeof pathOrOptions ? { path : pathOrOptions } : pathOrOptions ;
354
343
if ( options . methods ) return options ;
355
- return { ...options , methods } ;
344
+ return { ...defaultOptions , ... options , methods } ;
356
345
}
357
346
358
347
/**
@@ -444,6 +433,13 @@ export abstract class HttpRouterRegistryFunctionRegistrar {
444
433
this . register ( convertOptions ( [ 'HEAD' ] , pathOrOptions , this . defaultOptions ) , callback ) ;
445
434
}
446
435
436
+ public redirectCallback ( pathOrOptions : string | HttpRouterFunctionOptions , callback : ( request : HttpRequest ) => string , redirectCode : number = 302 ) {
437
+ const options = convertOptions ( [ 'GET' ] , pathOrOptions , this . defaultOptions ) ;
438
+ this . register ( options , ( request : HttpRequest ) => {
439
+ return Redirect . toUrl ( callback ( request ) , redirectCode ) ;
440
+ } ) ;
441
+ }
442
+
447
443
private register ( options : HttpRouterFunctionOptions , callback : ( ...args : any [ ] ) => any , module ?: InjectorModule < any > ) {
448
444
const fn = ReflectionFunction . from ( callback ) ;
449
445
@@ -536,7 +532,7 @@ export class HttpRouterRegistry extends HttpRouterRegistryFunctionRegistrar {
536
532
type : 'controller' ,
537
533
controller,
538
534
module,
539
- methodName : action . methodName
535
+ methodName : action . methodName ,
540
536
} ;
541
537
const routeConfig = createRouteConfigFromHttpAction ( routeAction , action , module , controllerData ) ;
542
538
@@ -578,7 +574,7 @@ export class HttpRouter {
578
574
controllers : ( ClassType | { module : InjectorModule < any > , controller : ClassType } ) [ ] ,
579
575
middlewareRegistry : MiddlewareRegistry = new MiddlewareRegistry ( ) ,
580
576
module : InjectorModule < any > = new InjectorModule ( ) ,
581
- config : HttpConfig = new HttpConfig ( )
577
+ config : HttpConfig = new HttpConfig ( ) ,
582
578
) : HttpRouter {
583
579
return new this ( new HttpControllers ( controllers . map ( v => {
584
580
return isClass ( v ) ? { controller : v , module } : v ;
@@ -636,7 +632,7 @@ export class HttpRouter {
636
632
resolverForToken : routeConfig . resolverForToken ,
637
633
resolverForParameterName : routeConfig . resolverForParameterName ,
638
634
pathParameterNames : parsedRoute . pathParameterNames ,
639
- routeConfig
635
+ routeConfig,
640
636
} ) ;
641
637
642
638
let methodCheck = '' ;
0 commit comments