-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
88 lines (74 loc) · 1.99 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
76
77
78
79
80
81
82
83
84
85
86
87
88
const fetch = require('node-fetch');
const xmltojs = require('xml2js').parseString;
// Test example response of Medium RSS feed
// const junkData = require('response.js');
const sendRes = (status, body) => {
const response = {
isBase64Encoded: true | false,
statusCode: status,
headers: {
'Content-Type': 'application/json',
'Access-Control-Allow-Origin': '*'
},
body: JSON.stringify(body)
};
return response;
};
const isObject = obj => typeof obj === 'object' && !Array.isArray(obj);
const walk = (array, fn) => {
return array.map(item => {
item = fn(item);
if (isObject(item)) {
Object.keys(item).map(i => {
item[i] = fn(item[i]);
item[i] = Array.isArray(item[i]) ? walk(item[i], fn) : item[i];
});
}
return Array.isArray(item) ? walk(item, fn) : item;
});
};
const formatter = item => {
if (Array.isArray(item) && item.length === 1 && !isObject(item[0])) {
item = item[0];
} else if (Array.isArray(item) && isObject(item[0])) {
cleanUpJsonItem(item[0]);
} else if (Array.isArray(item)) {
item.forEach(element => {
if (typeof element === 'string') {
element = element;
}
});
} else {
item = item;
}
return item;
};
const cleanUpJsonItem = jsonItem => {
Object.keys(jsonItem).forEach(key => {
if (Array.isArray(jsonItem[key]) && jsonItem[key].length === 1) {
jsonItem[key] = jsonItem[key].shift();
} else {
jsonItem[key] = jsonItem[key];
}
return jsonItem;
});
};
exports.handler = async event => {
let rssFeed = event.queryStringParameters.rssFeed;
let msgKey = 'error';
let response = {};
response[msgKey] = 'No items found.';
await fetch(rssFeed)
.then(response => {
return response.text();
})
.then(text => {
xmltojs(text, (err, result) => {
if (err) {
response[msgKey] = err;
}
response = walk(result.rss.channel, formatter);
});
});
return sendRes(200, response);
};