generated from senecajs/seneca-gateway
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathgateway-express.ts
238 lines (185 loc) · 4.71 KB
/
gateway-express.ts
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
/* Copyright © 2021-2022 Richard Rodger, MIT License. */
import { Open } from 'gubu'
import type {
GatewayResult
} from '@seneca/gateway'
type GatewayExpressOptions = {
auth?: {
token: {
// Cookie name
name: string
}
// Default cookie fields
cookie?: any
},
error?: {
// Use the default express error handler for errors
next: boolean
},
modify?: {
req?: (req: any) => any
res?: (req: any, msg: any, out: any) => any
}
}
type GatewayExpressDirective = {
done?: boolean
// Call Express response.next (passes error if defined)
next?: boolean
// Set/remove login cookie
auth?: {
// Cookie token value
token: string
// Override option cookie fields
cookie?: any
// Remove auth cookie
remove?: boolean
}
// HTTP redirect
redirect?: {
location: string
}
// HTTP status code
status?: number
header?: Record<string, any>
}
function gateway_express(this: any, options: GatewayExpressOptions) {
const seneca: any = this
const tag = seneca.plugin.tag
const gtag = (null == tag || '-' === tag) ? '' : '$' + tag
const gateway = seneca.export('gateway' + gtag + '/handler')
const parseJSON = seneca.export('gateway' + gtag + '/parseJSON')
seneca.act('sys:gateway,add:hook,hook:delegate', {
gateway: 'express',
tag: seneca.plugin.tag,
action: (_json: any, ctx: any) => {
ctx.req.seneca$ = this
}
})
async function handler(req: any, res: any, next: any) {
if (options.modify?.req) {
req = await options.modify.req.call(req.seneca$, req)
}
const body = req.body
const json = 'string' === typeof body ? parseJSON(body) : body
// console.log('BODY', json)
let headers = null == req.headers ? {} : Object
.entries(req.headers)
.reduce(
(a: any, entry: any) => (a[entry[0].toLowerCase()] = entry[1], a),
({} as any)
)
// TODO: doc as a standard feature
// TODO: implement in other gateways
json.gateway = {
params: req.params || {},
query: req.query || {},
body: req.body,
headers,
}
if (json.error$) {
return res.status(400).send(json)
}
let result: GatewayResult = await gateway(json, { req, res })
if (options.modify?.res) {
result = await options.modify?.res.call(req.seneca$, req, json, result)
}
let gateway$: GatewayExpressDirective | undefined = result.gateway$
if (gateway$) {
if (gateway$.done) {
return res.send(result.out)
}
if (gateway$.auth && options.auth) {
if (gateway$.auth.token) {
res.cookie(
options.auth.token.name,
gateway$.auth.token,
{
...options.auth.cookie,
...(gateway$.auth.cookie || {})
}
)
}
else if (gateway$.auth.remove) {
res.clearCookie(options.auth.token.name)
}
}
// TODO: should also match `headers`
if (gateway$.header) {
res.set(gateway$.header)
}
if (gateway$.next) {
// Uses the default express error handler
return next(result.error ? result.out : undefined)
}
// Should be last as final action
else if (gateway$.redirect?.location) {
return res.redirect(gateway$.redirect?.location)
}
if (result.error) {
if (options.error?.next) {
return next(result.error ? result.out : undefined)
}
else {
res.status(gateway$.status || 500)
return res.send(result.out)
}
}
else {
if (gateway$.status) {
res.status(gateway$.status)
}
return res.send(result.out)
}
}
else {
return res.send(result.out)
}
}
// Named webhook handler
async function hook(req: any, res: any, next: any) {
const body = req.body || {}
const name = req.params.name
const code = req.params.code
// Standard message for hooks based on URL path format:
// /prefix/:name/:code
const hookmsg = {
handle: 'hook',
name,
code,
body: 'string' === typeof body ? parseJSON(body) : body
}
req.body = hookmsg
return handler(req, res, next)
}
return {
name: 'gateway-express',
exports: {
handler,
hook,
}
}
}
// Default options.
gateway_express.defaults = {
auth: {
token: {
name: 'seneca-auth'
},
cookie: Open({
maxAge: 365 * 24 * 60 * 60 * 1000,
httpOnly: true,
sameSite: true,
})
},
error: {
next: false
},
modify: {
req: undefined,
res: undefined,
}
}
export default gateway_express
if ('undefined' !== typeof (module)) {
module.exports = gateway_express
}