Skip to content

Commit 1fc5621

Browse files
committed
standard
1 parent d201455 commit 1fc5621

File tree

2 files changed

+20
-22
lines changed

2 files changed

+20
-22
lines changed

functions/tweet-to-trello/tweet-to-trello.js

+9-12
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
const fetch = require('node-fetch')
2-
31
const {
42
createParams,
53
fetchHtml,
@@ -12,10 +10,9 @@ const {
1210
} = require('./utils')
1311

1412
exports.handler = async (event, context) => {
15-
1613
console.log('=== request accepted ===')
1714

18-
if (event.httpMethod !== 'POST'){
15+
if (event.httpMethod !== 'POST') {
1916
console.log('ERR: method is not post')
2017
return {
2118
statusCode: 400,
@@ -24,11 +21,11 @@ exports.handler = async (event, context) => {
2421
}
2522

2623
const bodyFormat = event.headers['x-bodyformat'] || 'TEXT'
27-
const converter = bodyFormat === 'JSON' ? createDataFromJSON : createDataFromBodyText;
24+
const converter = bodyFormat === 'JSON' ? createDataFromJSON : createDataFromBodyText
2825
const { tweetText, urlSource, appSecret } = converter(event.body)
2926

3027
// check params
31-
if(!urlSource || !appSecret) {
28+
if (!urlSource || !appSecret) {
3229
console.log('ERR: params not enough')
3330
return {
3431
statusCode: 400,
@@ -37,21 +34,20 @@ exports.handler = async (event, context) => {
3734
}
3835

3936
// need valid appSecret
40-
if(!isValidSecret(appSecret)) {
37+
if (!isValidSecret(appSecret)) {
4138
console.log('ERR: invalid appSecret')
4239
return {
4340
statusCode: 400,
4441
body: 'invalid appSecret'
4542
}
4643
}
4744

48-
let formattedPageText;
45+
let formattedPageText
4946

5047
try {
5148
// fetch target page's html as text
5249
const html = await fetchHtml(urlSource)
5350
formattedPageText = createFormattedTextFromHtml(html)
54-
5551
} catch (err) {
5652
console.log('ERR: fetching page failed')
5753
console.log(err)
@@ -77,19 +73,21 @@ exports.handler = async (event, context) => {
7773
console.log('ERR: trello api says response.ok is false')
7874
// NOT res.status >= 200 && res.status < 300
7975
const data = await response.json()
76+
console.log(data)
8077
return {
8178
statusCode: response.status,
8279
body: response.statusText
8380
}
8481
}
8582

86-
const data = await response.json()
83+
// const data = await response.json()
8784

8885
console.log('DONE: succeeded')
8986
// succeeded!
9087
return {
9188
statusCode: 200,
92-
body: '' //JSON.stringify(data)
89+
body: ''
90+
// body: JSON.stringify(data)
9391
}
9492
} catch (err) {
9593
// something wrong
@@ -100,5 +98,4 @@ exports.handler = async (event, context) => {
10098
body: JSON.stringify({ msg: err.message }) // Could be a custom message or object i.e. JSON.stringify(err)
10199
}
102100
}
103-
104101
}

functions/tweet-to-trello/utils.js

+11-10
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
11
require('dotenv').config()
2-
const { URLSearchParams } = require('url');
2+
const { URLSearchParams } = require('url')
33
const fetch = require('node-fetch')
44
const parseHtml = require('node-html-parser').parse
55
const frontMatter = require('front-matter')
66

77
const {
88
TRELLO_API_KEY: key,
99
TRELLO_API_TOKEN: token,
10-
TRELLO_LIST_ID_NEW_CARD_TO_BE_APPENDED: list_id_tweet,
11-
TRELLO_LIST_ID_IOS: list_id_ios,
10+
TRELLO_LIST_ID_NEW_CARD_TO_BE_APPENDED: listIdTweet,
11+
TRELLO_listIdIos: listIdIos,
1212
TWEET_TO_TRELLO_SECRET: correctAppSecret
1313
} = process.env
1414

1515
module.exports.isValidSecret = appSecret => {
16-
if(appSecret !== correctAppSecret) return false
16+
if (appSecret !== correctAppSecret) return false
1717
return true
1818
}
1919

@@ -44,9 +44,9 @@ ${a[2]}`
4444

4545
module.exports.createParams = ({ desc, urlSource, fromTweet, fromIos }) => {
4646
const params = new URLSearchParams()
47-
const list_id = fromTweet ? list_id_tweet : list_id_ios
47+
const listId = fromTweet ? listIdTweet : listIdIos
4848
params.append('pos', 'top')
49-
params.append('idList', list_id)
49+
params.append('idList', listId)
5050
params.append('key', key)
5151
params.append('token', token)
5252
params.append('desc', desc)
@@ -68,7 +68,7 @@ module.exports.createFormattedTextFromHtml = html => {
6868
script: false,
6969
style: false,
7070
pre: true,
71-
comment: false,
71+
comment: false
7272
}
7373
// parsed should be a formatted DOM element that node-html-parser generates
7474
const parsed = parseHtml(html, options)
@@ -79,8 +79,8 @@ module.exports.createFormattedTextFromHtml = html => {
7979
}
8080

8181
module.exports.combineText = (tweetText, pageText) => {
82-
let text;
83-
if(tweetText) {
82+
let text
83+
if (tweetText) {
8484
text = `${tweetText}\n\n---\n\n${pageText}`
8585
} else {
8686
text = pageText
@@ -89,9 +89,10 @@ module.exports.combineText = (tweetText, pageText) => {
8989
}
9090

9191
module.exports.createTrelloCard = async params => {
92-
return await fetch('https://api.trello.com/1/cards', {
92+
const response = await fetch('https://api.trello.com/1/cards', {
9393
method: 'post',
9494
headers: { Accept: 'application/json' },
9595
body: params
9696
})
97+
return response
9798
}

0 commit comments

Comments
 (0)