-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcors.js
More file actions
33 lines (28 loc) · 819 Bytes
/
cors.js
File metadata and controls
33 lines (28 loc) · 819 Bytes
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
import AbstractLambdaPlugin from './abstract-lambda-plugin'
import PluginHook from '../enums/hooks'
import LambdaType from '../enums/lambda-type'
export default class Cors extends AbstractLambdaPlugin {
constructor () {
super('cors', LambdaType.API_GATEWAY)
this.addHook(PluginHook.PRE_EXECUTE, this.corsPlugin.bind(this))
}
/**
* Add cors headers to event
*/
corsPlugin (useCors) {
return (req, res, error, context, done) => {
if (useCors) {
const corsHeaders = {
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Credentials': true,
'Access-Control-Allow-Methods': 'POST, GET, PUT, PATCH, DELETE, OPTIONS'
}
res.headers = {
...corsHeaders,
...res.headers
}
}
done()
}
}
}