@@ -17,9 +17,23 @@ import { IncomingMessage } from 'http'
1717import { RedirectContract , ResponseContract } from '@ioc:Adonis/Core/Response'
1818import { RouterContract , MakeUrlOptions } from '@ioc:Adonis/Core/Route'
1919
20+ /**
21+ * Exposes the API to construct redirect routes
22+ */
2023export class Redirect implements RedirectContract {
24+ /**
25+ * A boolean to forward the existing query string
26+ */
2127 private forwardQueryString = false
28+
29+ /**
30+ * The status code for the redirect
31+ */
2232 private statusCode = 302
33+
34+ /**
35+ * A custom query string to forward
36+ */
2337 private queryString : { [ key : string ] : any } = { }
2438
2539 constructor (
@@ -61,32 +75,34 @@ export class Redirect implements RedirectContract {
6175 * Redirect to the previous path.
6276 */
6377 public back ( ) {
64- const url = ( this . request . headers [ 'referer' ] ||
65- this . request . headers [ 'referrer' ] ||
66- '/' ) as string
78+ let url = this . request . headers [ 'referer' ] || this . request . headers [ 'referrer' ] || '/'
79+ url = Array . isArray ( url ) ? url [ 0 ] : url
6780
68- return this . toPath ( url )
81+ /**
82+ * Remove query string from the referrer
83+ */
84+ return this . toPath ( url . split ( '?' ) [ 0 ] )
6985 }
7086
7187 /**
7288 * Redirect the request using a route identifier.
7389 */
7490 public toRoute ( routeIdentifier : string , urlOptions ?: MakeUrlOptions , domain ?: string ) {
75- const route = this . router . lookup ( routeIdentifier , domain )
91+ // const route = this.router.lookup(routeIdentifier, domain)
7692
77- if ( ! route ) {
93+ const url = this . router . makeUrl ( routeIdentifier , urlOptions , domain )
94+ if ( ! url ) {
7895 throw new Error ( `Unable to lookup route for "${ routeIdentifier } " identifier` )
7996 }
8097
81- const url = this . router . makeUrl ( routeIdentifier , urlOptions , domain ) as string
8298 return this . toPath ( url )
8399 }
84100
85101 /**
86102 * Redirect the request using a path.
87103 */
88104 public toPath ( url : string ) {
89- let query
105+ let query : any
90106
91107 // Extract the current QueryString if we want to forward it.
92108 if ( this . forwardQueryString ) {
0 commit comments