-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
75 lines (59 loc) · 1.81 KB
/
index.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
'use strict';
/**
* Serverless Base Path Plugin
*/
module.exports = function (S) {
const path = require('path'),
fs = require('fs'),
BbPromise = require('bluebird');
/**
* ServerlessPluginBoierplate
*/
class ServerlessBasePathPlugin extends S.classes.Plugin {
/**
* Constructor
*/
constructor() {
super();
this.name = 'ServerlessBasePath'
}
static getName() {
return 'com.daffinity.' + ServerlessBasePathPlugin;
}
registerActions() {
S.addAction(this._prefixEndpoints.bind(this), {
handler: 'registerHooks',
context: 'custom',
contextAction: 'run',
});
}
/**
* Register Hooks
*/
registerHooks() {
S.addHook(this._prefixEndpoints.bind(this), {
action: 'endpointBuildApiGateway',
event: 'pre'
});
return BbPromise.resolve();
}
/**
* Endpoint Build API Gateway Pre Hook
* This adds the basePath from the Component to each Function Endpoint path.
*/
_prefixEndpoints(evt) {
let endpoint = S.getProject().getEndpoint(evt.options.name);
let basePath = S.getProject().custom.basePath;
let _e = endpoint.toObject();
let endpointName = `${basePath + _e.path}`;
if (!basePath) return BbPromise.resolve(evt);
//set base path to endpoint
_e.path = endpointName;
endpoint.fromObject(_e);
evt.options.name = `${endpointName}~${_e.method}`;
return BbPromise.resolve(evt);
}
}
// Export Plugin Class
return ServerlessBasePathPlugin;
};