-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
47 lines (38 loc) · 1.2 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
'use strict';
const fs = require('fs');
const pathExists = require('path-exists');
const pify = require('pify');
const APIGateway = require('./lib/apigateway');
const transformer = require('./lib/transformer');
const utils = require('./lib/utils');
const fsP = pify(fs);
module.exports = function (filePath, options) {
options = options || {};
if (typeof filePath !== 'string' || !pathExists.sync(filePath)) {
return Promise.reject(utils.createError('Input file does not exist'));
}
// Initialize the gateway
const gateway = new APIGateway(options);
return fsP.readFile(filePath, 'utf8')
.then(contents => {
// Parse the contents
contents = JSON.parse(contents.toString());
// Transform the definition
const definition = transformer.transform(contents, options);
const name = definition.info.title;
if (!name) {
// Trow an error if the name could not be determined
throw utils.createError('No AWS API gateway name provided');
}
return gateway.findRestApi(name)
.then(api => {
return api.import(definition);
});
})
.catch(err => {
if (err.name === 'SyntaxError') {
throw utils.createError('Input file provided is not valid JSON');
}
throw err;
});
};