-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
86 lines (76 loc) · 2.16 KB
/
Copy pathindex.js
File metadata and controls
86 lines (76 loc) · 2.16 KB
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
//@prettier
const raven = require('raven')
raven
.config(process.env.SENTRY_DSN_LINKCOM, {
logger: 'server',
name: process.env.NOW_URL,
})
.install()
const {send} = require('micro')
const {GraphQLClient} = require('graphql-request')
const parse = require('url').parse
const route = require('path-match')()
const matchTest = route('/link/test')
const matchOffer = route('/link/offer/:id')
const client = new GraphQLClient(
'https://api.graph.cool/simple/v1/cjb9vgsrd1hlf0187xaxi7te1',
{headers: {Authorization: `Bearer ${process.env.GRAPHQL_TOKEN}`}},
)
const offerQuery = `query Offer($id: ID!){
Offer(id: $id) {
url
merchant
}
}`
const thriveBase = 'http://www.kqzyfj.com/click-8542692-13259174?url='
const walmartBase =
'http://linksynergy.walmart.com/deeplink?id=8loQjOHw*oo&mid=2149&murl='
const tierraFarmSuffix = '?rfsn=1243153.258842'
const azureSuffix = '?a_aid=BL7vh5V1Rb'
const handleErrors = fn => async (req, res) => {
try {
return await fn(req, res)
} catch (err) {
raven.captureException(err, {req})
return send(
res,
500,
'<h3>On snap! This Gluten Project robot has suffered a heart attack! The founders have been notified and will investigate ASAP.</h3>',
)
}
}
module.exports = handleErrors(async (req, res) => {
res.setHeader('X-Robots-Tag', 'noindex, nofollow')
let Location = ''
const offer = matchOffer(parse(req.url).pathname)
if (offer !== false) {
const {Offer: {url, merchant}} = await client.request(offerQuery, {
id: offer.id,
})
switch (merchant) {
case 'Thrive':
Location = thriveBase + encodeURIComponent(url)
break
case 'Walmart':
Location = walmartBase + encodeURIComponent(url)
break
case 'Tierra Farm':
Location = url + tierraFarmSuffix
break
case 'Azure':
Location = url + azureSuffix
break
default:
Location = url
}
}
if (Location) {
res.writeHead(302, {Location})
return res.end()
}
const test = matchTest(parse(req.url).pathname)
if (test !== false) {
return send(res, 200)
}
return send(res, 404, '<h1>Link not found :(</h1>')
})