-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathredirectResponse.js
105 lines (88 loc) · 2.23 KB
/
redirectResponse.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
const RedirectResponseContract = require('@ostro/contracts/http/redirectResponse')
const { Macroable } = require('@ostro/support/macro')
const kUrl = Symbol('url')
const kErrors = Symbol('errors')
const kInputs = Symbol('inputs')
const kFlash = Symbol('flash')
const kResponse = Symbol('response')
class RedirectResponse extends RedirectResponseContract {
constructor(response) {
super()
Object.defineProperties(this, {
[kUrl]: {
value: undefined,
writable: true
},
[kResponse]: {
value: response
},
[kInputs]: {
value: [],
writable: true,
},
[kErrors]: {
value: [],
writable: true
},
[kFlash]: {
value: [],
writable: true
}
})
}
to(url) {
this[kUrl] = url
if (this[kResponse]) {
process.nextTick(() => {
if (this[kResponse].req.session)
this[kResponse].with(...this.getFlash()).withErrors(...this.getErrors()).withInput(...this.getInputs());
this[kResponse].redirect(this[kUrl]);
})
}
return this
}
away(url) {
this[kResponse].redirect(url);
}
route() {
this[kUrl] = app('router').route(...arguments)
return this.to(this[kUrl])
}
withErrors() {
this[kErrors] = arguments
return this
}
withInput() {
this[kInputs] = arguments.length ? arguments : [true]
return this
}
with() {
this[kFlash] = arguments
return this
}
getUrl() {
return this[kUrl]
}
getErrors() {
return this[kErrors]
}
getInputs() {
return this[kInputs]
}
wantedInput() {
return this[kInputs].length
}
getFlash() {
return this[kFlash]
}
back() {
this[kUrl] = 'back'
if (this[kResponse]) {
return this.to(this[kUrl])
}
}
static __get(target, method) {
return this.make(new target, method)
}
}
module.exports = Macroable(RedirectResponse)