forked from contentful/extensions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlambda-function.js
51 lines (49 loc) · 1.41 KB
/
lambda-function.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
const https = require("https");
const request = ({ method, hostname, path, headers }) =>
new Promise((resolve, reject) => {
https
.request({ method, hostname, path, headers }, response => {
let data = "";
response.on("data", chunk => (data += chunk));
response.on("end", () => resolve(JSON.parse(data)));
})
.on("error", err => reject(err))
.end();
});
exports.handler = function(event, context, callback) {
request({
method: "GET",
hostname: `${process.env.MKTO_MUNCHKIN_ID}.mktorest.com`,
path: `/identity/oauth/token?grant_type=client_credentials&client_id=${
process.env.MKTO_CLIENT_ID
}&client_secret=${process.env.MKTO_CLIENT_SECRET}`
})
.then(res => res.access_token)
.then(bearer =>
request({
method: "GET",
hostname: `${process.env.MKTO_MUNCHKIN_ID}.mktorest.com`,
path: `/rest/asset/v1/forms.json`,
headers: {
Authorization: `Bearer ${bearer}`
}
})
.then(res =>
callback(null, {
statusCode: 200,
headers: {
"Access-Control-Allow-Origin": "*",
"Content-Type": "application/json"
},
body: JSON.stringify(res.result)
})
)
.catch(err => {
console.log(err);
callback(err);
})
)
.catch(err => {
callback(err);
});
};