-
Notifications
You must be signed in to change notification settings - Fork 0
/
minified-function.js
48 lines (47 loc) · 1.36 KB
/
minified-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
const fli = require('fli-webtask');
const request = fli.npm.request;
const as = fli.npm.async;
const _ = fli.npm.lodash;
const loader = fli.lib.loader;
const minified = (context, next) => loader({
method: 'put',
url: `${context.secrets.storeFunction}/${context.body.db}/${context.body._id}`,
qs: {token: context.secrets.token},
json: {
state: 'minified'
}
}, next);
/**
* @param context {WebtaskContext}
*/
module.exports = function(context, cb) {
if(context.secrets.token !== context.query.token) {
return cb('No token.');
}
console.log(`- minified`);
if(!!_.get(context, 'body.payload.shortUrl')) {
return minified(context, () => cb('shortUrl already provided.'));
}
if(!_.get(context, 'body._id')) {
return cb('No _id provided.');
}
if(!_.get(context, 'body.payload.url')) {
return cb('No url provided.');
}
return as.waterfall([
(next) => loader({
url: context.secrets.amazonFunction,
qs: {
token: context.secrets.token,
url: context.body.payload.url
}
}, next),
(info, next) => loader({
method: 'patch',
url: `${context.secrets.storeFunction}/${context.body.db}/${context.body._id}`,
qs: {token: context.secrets.token},
json: {shortUrl: _.get(info, 'shortUrl', '')}
}, () => next(null, info)),
(info, next) => minified(context, () => next(null, info))
], cb);
};