UNPKG

4.38 kBJavaScriptView Raw
1var async = require('async');
2
3function getMethodID(method) {
4 return method && method.data &&
5 method.data['x-amazon-apigateway-integration'] &&
6 method.data['x-amazon-apigateway-integration'].uri;
7}
8
9module.exports = {
10 deployRestAPIMethods: function deployRestAPIMethods(done) {
11 var _ = this;
12
13 async.eachSeries(_.APIDeploy.methods, function(method, done) {
14 _.deployRestAPIMethod(method, done);
15 }, done);
16 },
17
18 deployRestAPIMethod: function deployRestAPIMethod(method, done) {
19 var _ = this;
20
21 if (getMethodID(method)) {
22 _.updateRestAPIMethod(method, done);
23 } else {
24 _.createRestAPIMethod(method, done);
25 }
26 },
27
28 createRestAPIMethod: function createRestAPIMethod(method, done) {
29 var _ = this,
30 integration = method.data['x-amazon-apigateway-integration'],
31 body = {
32 authorizationType: 'NONE',
33 apiKeyRequired: integration.apiKeyRequired,
34 requestParameters: {},
35 requestTemplates: {},
36 requestModels: {}
37 },
38 key, val;
39
40 for (key in integration.requestTemplates) {
41 val = integration.requestTemplates[key];
42 if (typeof val === 'object') body.requestTemplates[key] = JSON.stringify(val, null, 4);
43 }
44
45 for (key in integration.requestParameters) {
46 body.requestParameters[key] = integration.requestParameters[key];
47 }
48
49 for (key in integration.requestModels) {
50 body.requestModels[key] = integration.requestModels[key];
51 }
52
53 _.APIDeploy.logger.log('Creating Method - ' + method._path + ' (' + method._method + ')');
54
55 _.AWSRequest({
56 path: '/restapis/' + _.APIDeploy.swagger.data['x-amazon-apigateway-restapi'].id +
57 '/resources/' + method._resource.data['x-amazon-apigateway-resource'].id +
58 '/methods/' + method._method,
59 method: 'PUT',
60 body: body
61 }, function(err, newMethod) {
62 if (err) return done(err);
63
64 _.APIDeploy.logger.log('Created Method - ' + method._path + ' (' + method._method + ')');
65
66 _.createRestAPIMethodResponse(method, function() {
67 _.deployRestAPIAccessPolicy(method, function() {
68 done();
69 });
70 });
71 });
72 },
73
74 updateRestAPIMethod: function updateRestAPIMethod(method, done) {
75 var _ = this;
76
77 _.APIDeploy.logger.log('Updating Method - ' + method._path + ' (' + method._method + ')');
78 _.APIDeploy.logger.log('Updated Method - ' + method._path + ' (' + method._method + ')');
79
80 done();
81 },
82
83 createRestAPIMethodResponse: function createRestAPIMethodResponse(method, done) {
84 var _ = this,
85 integration = method.data['x-amazon-apigateway-integration'],
86 responseParameters = {},
87 responseTemplates = {},
88 val, key;
89
90 for (key in integration.responses['200'].responseParameters) {
91 responseParameters[key] = true;
92 }
93
94 for (key in integration.responses['200'].responseTemplates) {
95 val = integration.responses['200'].responseTemplates[key];
96 responseTemplates[key] = val && typeof val === 'object' ? JSON.stringify(val, null, 4) : null;
97 }
98
99 _.APIDeploy.logger.log('Creating Method Response - ' + method._path + ' (' + method._method + ')');
100
101 _.AWSRequest({
102 path: '/restapis/' + _.APIDeploy.swagger.data['x-amazon-apigateway-restapi'].id +
103 '/resources/' + method._resource.data['x-amazon-apigateway-resource'].id +
104 '/methods/' + method._method +
105 '/responses/200',
106 method: 'PUT',
107 body: {
108 responseParameters: responseParameters,
109 responseTemplates: responseTemplates,
110 responseModels: integration.responses['200'].responseModels || {}
111 }
112 }, function(err, int) {
113 if (err) return done(err);
114
115 _.APIDeploy.logger.log('Created Method Response - ' + method._path + ' (' + method._method + ')');
116
117 done();
118 });
119 }
120};