Skip to content

Commit

Permalink
♻️ Refactor more require syntax to import syntax
Browse files Browse the repository at this point in the history
  • Loading branch information
jh0ker authored and mraerino committed May 18, 2018
1 parent 6ae92ec commit 9f92e9b
Show file tree
Hide file tree
Showing 35 changed files with 143 additions and 166 deletions.
5 changes: 3 additions & 2 deletions .eslintrc
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"plugins": ["node", "jest"],
"plugins": ["node", "jest", "import"],
"extends": ["eslint:recommended", "plugin:node/recommended", "plugin:jest/recommended"],
"env": {
"node": true,
Expand Down Expand Up @@ -133,6 +133,7 @@
}
],
"node/no-unsupported-features": 0,
"node/shebang": 0
"node/shebang": 0,
"import/no-commonjs": 2
}
}
8 changes: 4 additions & 4 deletions __mocks__/request-promise-native.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@
// limitations under the License.


const extend = require('extend');
const hash = require('object-hash');
const fs = require('fs');
import extend from 'extend';
import hash from 'object-hash';
import fs from 'fs';

const calls = [];
beforeEach(() => {
Expand Down Expand Up @@ -117,4 +117,4 @@ request.del = verbFunc('delete');
request['delete'] = verbFunc('delete');
request.calls = calls;

module.exports = request;
export default request;
3 changes: 2 additions & 1 deletion config/.eslintrc
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
{
"rules": {
"node/no-unsupported-features": 2
"node/no-unsupported-features": 2,
"import/no-commonjs": 0
}
}
24 changes: 12 additions & 12 deletions entrypoint/fb.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
const facebook = require('../lib/facebook');
const { getAttachmentId } = require('../lib/facebookAttachments');
const dialogflow = require('dialogflow');
const handler = require('../handler');
const getTiming = require('../lib/timing');
const { assemblePush, getLatestPush, markSent } = require('../lib/pushData');
const Raven = require('raven');
const RavenLambdaWrapper = require('serverless-sentry-lib');
import facebook from '../lib/facebook';
import { getAttachmentId } from '../lib/facebookAttachments';
import dialogflow from 'dialogflow';
import handler from '../handler';
import getTiming from '../lib/timing';
import { assemblePush, getLatestPush, markSent } from '../lib/pushData';
import Raven from 'raven';
import RavenLambdaWrapper from 'serverless-sentry-lib';


module.exports.verify = RavenLambdaWrapper.handler(Raven, (event, context, callback) => {
export const verify = RavenLambdaWrapper.handler(Raven, (event, context, callback) => {
const params = event.queryStringParameters || {};

const token = params['hub.verify_token'];
Expand All @@ -32,7 +32,7 @@ module.exports.verify = RavenLambdaWrapper.handler(Raven, (event, context, callb
});
});

module.exports.message = RavenLambdaWrapper.handler(Raven, async (event, context, callback) => {
export const message = RavenLambdaWrapper.handler(Raven, async (event, context, callback) => {
const payload = JSON.parse(event.body);

callback(null, {
Expand Down Expand Up @@ -114,7 +114,7 @@ module.exports.message = RavenLambdaWrapper.handler(Raven, async (event, context
}
});

module.exports.push = RavenLambdaWrapper.handler(Raven, async (event, context, callback) => {
export const push = RavenLambdaWrapper.handler(Raven, async (event, context, callback) => {
let timing;
try {
timing = getTiming(event);
Expand Down Expand Up @@ -148,7 +148,7 @@ module.exports.push = RavenLambdaWrapper.handler(Raven, async (event, context, c
}
});

module.exports.attachment = RavenLambdaWrapper.handler(Raven, async (event, context, callback) => {
export const attachment = RavenLambdaWrapper.handler(Raven, async (event, context, callback) => {
const payload = JSON.parse(event.body);
const url = payload.url;

Expand Down
21 changes: 10 additions & 11 deletions entrypoint/fbPushLegacy.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
const getTiming = require('../lib/timing');
const { assemblePush, getLatestPush, markSent } = require('../lib/pushData');
const facebook = require('../lib/facebook');
const ddb = require('../lib/dynamodb');
const Raven = require('raven');
const RavenLambdaWrapper = require('serverless-sentry-lib');
import getTiming from '../lib/timing';
import { assemblePush, getLatestPush, markSent } from '../lib/pushData';
import facebook from '../lib/facebook';
import ddb from '../lib/dynamodb';
import Raven from 'raven';
import RavenLambdaWrapper from 'serverless-sentry-lib';


module.exports.fetch = RavenLambdaWrapper.handler(Raven, function(event, context, callback) {
export const fetch = RavenLambdaWrapper.handler(Raven, function(event, context, callback) {
console.log(JSON.stringify(event, null, 2));

// check if timing is right
Expand All @@ -33,7 +33,7 @@ module.exports.fetch = RavenLambdaWrapper.handler(Raven, function(event, context
});
});

module.exports.send = RavenLambdaWrapper.handler(Raven, function(event, context, callback) {
export const send = RavenLambdaWrapper.handler(Raven, function(event, context, callback) {
console.log('attempting to push chunk for push', event.push.id);

const { intro, button } = assemblePush(event.push);
Expand Down Expand Up @@ -76,7 +76,7 @@ module.exports.send = RavenLambdaWrapper.handler(Raven, function(event, context,
});
});

function getUsers(timing, start = null, limit = 100) {
export function getUsers(timing, start = null, limit = 100) {
const params = {
Limit: limit,
TableName: process.env.DYNAMODB_SUBSCRIPTIONS,
Expand All @@ -97,9 +97,8 @@ function getUsers(timing, start = null, limit = 100) {
});
});
}
module.exports.getUsers = getUsers;

module.exports.finish = RavenLambdaWrapper.handler(Raven, function(event, context, callback) {
export const finish = RavenLambdaWrapper.handler(Raven, function(event, context, callback) {
console.log('Sending of push finished:', event);
markSent(event.id)
.then(() => callback(null, {}))
Expand Down
11 changes: 5 additions & 6 deletions entrypoint/metrics.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const ddb = require('../lib/dynamodb');
const { Gauge, register } = require('prom-client');
const Raven = require('raven');
const RavenLambdaWrapper = require('serverless-sentry-lib');
import ddb from '../lib/dynamodb';
import { Gauge, register } from 'prom-client';
import Raven from 'raven';
import RavenLambdaWrapper from 'serverless-sentry-lib';

const subs = new Gauge({
name: 'subscriptions',
Expand Down Expand Up @@ -37,8 +37,7 @@ function collectSubscriberMetrics(timing) {
});
}

module.exports.prometheus = RavenLambdaWrapper.handler(Raven, async (event, context, callback) => {

export const prometheus = RavenLambdaWrapper.handler(Raven, async (event, context, callback) => {
try {
const metrics = [
collectSubscriberMetrics(),
Expand Down
2 changes: 1 addition & 1 deletion handler/__tests__/actionCurrentNews.test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const { Expect } = require('../../lib/testing');
const facebook = require('../../lib/facebook');

const currentNews = require('../actionCurrentNews');
const currentNews = require('../actionCurrentNews').default;

describe('actionCurrentNews', () => {
it('sends a specific message with a button', async () => {
Expand Down
2 changes: 1 addition & 1 deletion handler/__tests__/actionCurrentTime.test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const { Expect } = require('../../lib/testing');
const facebook = require('../../lib/facebook');

const currentTime = require('../actionCurrentTime');
const currentTime = require('../actionCurrentTime').default;

describe('actionCurrentTime', () => {
it('sends a message that contains a time', async () => {
Expand Down
2 changes: 1 addition & 1 deletion handler/__tests__/payloadFaq.test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const { Expect } = require('../../lib/testing');
const facebook = require('../../lib/facebook');

const { payloadFaq } = require('../payloadFaq');
const payloadFaq = require('../payloadFaq').default;

describe('payloadFaq', () => {
it('sends a specific faq', () => {
Expand Down
4 changes: 1 addition & 3 deletions handler/actionCurrentNews.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import request from 'request-promise-native';
import urls from '../lib/urls';


const currentNews = async (chat) => {
export default async (chat) => {
const data = await request({
uri: urls.pushes,
json: true,
Expand All @@ -28,5 +28,3 @@ const currentNews = async (chat) => {
});
return chat.sendButtons(introHeadlines, [ button ]);
};

module.exports = currentNews;
6 changes: 2 additions & 4 deletions handler/actionCurrentTime.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
const moment = require('moment-timezone');
import moment from 'moment-timezone';

const currentTime = (chat) => {
export default (chat) => {
const time = moment.tz('Europe/Berlin').format('HH:mm:ss');
return chat.sendText(`Die exakte Uhrzeit lautet: ${time}`);
};

module.exports = currentTime;
9 changes: 2 additions & 7 deletions handler/actionNewsAbout.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import fragmentSender from '../lib/fragmentSender';
import { buttonPostback, listElement } from '../lib/facebook';


const newsAbout = async (chat, payload) => {
export const newsAbout = async (chat, payload) => {
let id;

try {
Expand Down Expand Up @@ -52,7 +52,7 @@ const newsAbout = async (chat, payload) => {
return chat.sendList(elements.slice(0, 4));
};

const searchId = async (payload) => {
export const searchId = async (payload) => {
const searchParameter = [
'genres',
'topics',
Expand Down Expand Up @@ -84,8 +84,3 @@ const searchId = async (payload) => {

throw Error();
};

module.exports = {
searchId,
newsAbout,
};
2 changes: 1 addition & 1 deletion handler/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@

module.exports = {
export default {
actions: {
'current_time': require('./actionCurrentTime'),
'current_news': require('./actionCurrentNews'),
Expand Down
4 changes: 1 addition & 3 deletions handler/payloadFaq.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import request from 'request-promise-native';
import urls from '../lib/urls';
import fragmentSender from '../lib/fragmentSender';

export async function payloadFaq(chat, payload) {
export default async function(chat, payload) {
const url = `${urls.faqBySlug(payload.slug)}`;

const faq = await request({ uri: url, json: true });
Expand All @@ -14,5 +14,3 @@ export async function payloadFaq(chat, payload) {
payload.type = payload.action;
return fragmentSender(chat, faq[0].next_fragments, payload, faq[0].text, faq[0].media);
}

export default payloadFaq;
4 changes: 1 addition & 3 deletions handler/payloadFragmentNext.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import request from 'request-promise-native';
import urls from '../lib/urls';
import fragmentSender from '../lib/fragmentSender';

const fragmentNext = async (chat, payload) => {
export default async (chat, payload) => {
let url = null;
if (payload.type === 'push' || payload.type === 'report') {
url = `${urls.reportFragment(payload.fragment)}?withNext=yes`;
Expand All @@ -20,5 +20,3 @@ const fragmentNext = async (chat, payload) => {
chat, fragment.next_fragments, payload, fragment.text, fragment.media);
}
};

module.exports = fragmentNext;
4 changes: 1 addition & 3 deletions handler/payloadPushOutro.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import request from 'request-promise-native';
import urls from '../lib/urls';

const pushOutro = async (chat, payload) => {
export default async (chat, payload) => {
const push = await request({ uri: `${urls.push(payload.push)}`, json: true });

if (push.media) {
Expand All @@ -11,5 +11,3 @@ const pushOutro = async (chat, payload) => {

return chat.sendText(push.outro);
};

module.exports = pushOutro;
4 changes: 1 addition & 3 deletions handler/payloadReportStart.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,11 @@ import request from 'request-promise-native';
import urls from '../lib/urls';
import fragmentSender from '../lib/fragmentSender';

const reportStart = async (chat, payload) => {
export default async (chat, payload) => {
const report = await request({
uri: `${urls.report(payload.report)}?withFragments=1`,
json: true,
});

return fragmentSender(chat, report.next_fragments, payload, report.text, report.media);
};

module.exports = reportStart;
6 changes: 3 additions & 3 deletions handler/payloadShare.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { buttonShare, buttonUrl, genericElement } from '../lib/facebook';

module.exports = function(chat) {
export default async function(chat) {
const text = 'Teile den Informanten mit deinen Freunden!';
const title = 'Jetzt den 1LIVE Informanten testen 😎';
const subtitle = 'Erhalte 1LIVE News im Facebook Messenger';
Expand All @@ -15,5 +15,5 @@ module.exports = function(chat) {
null,
[ buttonUrl(callToAction, informantUrl) ]),
];
return chat.sendButtons(text, [ buttonShare(sharedContent) ]);
};
return await chat.sendButtons(text, [ buttonShare(sharedContent) ]);
}
18 changes: 9 additions & 9 deletions handler/payloadSubscribe.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const { buttonPostback, listElement } = require('../lib/facebook');
const subscriptions = require('../lib/subscriptions');
import { buttonPostback, listElement } from '../lib/facebook';
import libSubscriptions from '../lib/subscriptions';

const getHasLabel = async function(chat) {
const labels = await chat.getLabels();
Expand All @@ -10,10 +10,10 @@ const getHasLabel = async function(chat) {

const disableSubscription = async function(psid, timing) {
try {
const sub = await subscriptions.update(psid, timing, false);
const sub = await libSubscriptions.update(psid, timing, false);
console.log(`Disabled subscription ${timing} in dynamoDB for ${psid}`);
if (!sub.morning && !sub.evening) {
await subscriptions.remove(psid);
await libSubscriptions.remove(psid);
console.log(`Deleted User in dynamoDB with psid ${psid}`);
}
} catch (error) {
Expand All @@ -27,20 +27,20 @@ const enableSubscription = async function(psid, timing) {
evening: timing === 'evening',
};
try {
await subscriptions.create(psid, item);
await libSubscriptions.create(psid, item);
console.log(`Created in dynamoDB ${psid} with ${timing}`);
} catch (error) {
console.log('Creating user in dynamoDB failed: ', error);
try {
await subscriptions.update(psid, timing, true);
await libSubscriptions.update(psid, timing, true);
console.log(`Enabled subscription ${timing} in dynamoDB for ${psid}`);
} catch (error) {
console.log('Updating user in dynamoDB failed: ', error);
}
}
};

module.exports.subscriptions = async function(chat) {
export const subscriptions = async function(chat) {
const promTxt = chat.sendText('Meine Infos kannst du ein oder zweimal am Tag haben: ' +
'Morgens, abends oder beides. Und ich melde mich, wenn etwas wirklich Wichtiges passiert.');

Expand Down Expand Up @@ -91,7 +91,7 @@ module.exports.subscriptions = async function(chat) {
return chat.sendList(elements);
};

module.exports.subscribe = function(chat, payload) {
export const subscribe = function(chat, payload) {
const promises = [ chat.addLabel('push-breaking') ];
if (payload.subscription === 'morning' || payload.subscription === 'all') {
promises.push(
Expand All @@ -108,7 +108,7 @@ module.exports.subscribe = function(chat, payload) {
`Wenn du die letzte Ausgabe sehen willst, schreib einfach "Leg los"`)));
};

module.exports.unsubscribe = async function(chat, payload) {
export const unsubscribe = async function(chat, payload) {
const hasLabel = await getHasLabel(chat);
const promises = [];
if (payload.subscription === 'morning' || payload.subscription === 'all') {
Expand Down
1 change: 1 addition & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* eslint-disable import/no-commonjs */
module.exports = {
collectCoverage: true,
verbose: true,
Expand Down
Loading

0 comments on commit 9f92e9b

Please sign in to comment.