-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathnewsletter.js
95 lines (87 loc) · 1.93 KB
/
newsletter.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
89
90
91
92
93
94
95
import axios from 'axios';
async function fetchBookmarks(consumerKey, accessToken) {
const res = await axios.post('https://getpocket.com/v3/get', {
consumer_key: consumerKey,
access_token: accessToken,
tag: 'newsletter',
state: 'all',
detailType: 'complete'
});
const {list} = res.data;
// List is a key-value timestamp->entry map
const entries = Object.values(list);
return entries.map(
({
given_title,
given_url,
resolved_url,
resolved_title,
excerpt,
authors,
rest
}) => ({
...rest,
title: given_title || resolved_title,
url: given_url || resolved_url,
excerpt,
authors: authors
? Object.values(authors)
.map(({name}) => name)
.filter(Boolean)
.join(',')
: ''
})
);
}
function parseUrlEncoded(urlEncodedString) {
const keyValuePairs = urlEncodedString.split('&');
return keyValuePairs.reduce((acc, kvPairString) => {
const [k, v] = kvPairString.split('=');
acc[k] = v;
return acc;
}, {});
}
export async function handler(event) {
if (event.httpMethod !== 'POST') {
return {
statusCode: 404,
body: 'Not Found'
};
}
if (!event.body) {
return {
statusCode: 400,
body: 'Bad Request'
};
}
const {
pocket_consumer_key: pocketConsumerKey,
pocket_access_token: pocketAccessToken
} = event.isBase64Encoded
? parseUrlEncoded(Buffer.from(event.body, 'base64').toString('utf-8'))
: JSON.parse(event.body);
if (!pocketConsumerKey || !pocketAccessToken) {
return {
statusCode: 400,
body: 'Bad Request'
};
}
try {
const bookmarks = await fetchBookmarks(pocketConsumerKey, pocketAccessToken);
return {
statusCode: 200,
body: JSON.stringify(bookmarks)
};
} catch(e) {
if (e.response) {
return {
statusCode: e.response.statusCode,
body: `Error while connecting to Pocket API: ${e.response.statusText}`
}
}
return {
statusCode: 500,
body: e.message
}
}
}