-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.js
43 lines (37 loc) · 1.15 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
const mock = require('./mock');
class ServerlessPlugin {
constructor(serverless) {
this.serverless = serverless;
this.enabled = !!(serverless.service.custom || {
'serverless-offline-http-mock-enabled': false,
})['serverless-offline-http-mock-enabled'];
this.items = (serverless.service.custom || {})[
'serverless-offline-http-mock'
];
this.hooks = {
'before:offline:start': this.startHandler.bind(this),
'before:offline:start:init': this.startHandler.bind(this),
'before:invoke:local:invoke': this.startHandler.bind(this),
'before:appsync-offline:start:startHandler': this.startHandler.bind(this),
};
}
startHandler() {
if (
!this.enabled ||
this.items === undefined ||
!this.items ||
!this.items.length
) {
return;
}
this.items.forEach((item) => {
mock.validate(item);
const { servicePath } = this.serverless.config;
item.mocks.forEach((filename) => {
this.serverless.cli.log(`Loading HTTP mocks in ${filename}`);
mock.start({ item, servicePath, filename });
});
});
}
}
module.exports = ServerlessPlugin;