-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
52 lines (45 loc) · 1.52 KB
/
index.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
import { Router } from 'express'
import errors from './static/errors'
import { checkConfig } from './helpers/config'
import { apiStatus } from '../../../lib/util'
import { prismicApi } from './connectors/prismic';
import { syncPrismic } from './helpers/sync'
import Path from 'path'
module.exports = ({ config }) => {
/* ------- Initialization ------- */
checkConfig(config)
const api = Router()
/* ------- Routes ------- */
// Receive webhook callbacks
api.post('/webhook', (req, res) => {
if (!req.body) return apiStatus(res, errors.invalidWebhookCallback, 400)
if (!req.body.type || !['api-update', 'test-update', 'test-trigger'].includes(req.body.type)) apiStatus(res, errors.invalidWebhookCallback, 400)
if (!req.body.secret || req.body.secret !== config.extensions.prismic.webhookSecret) return apiStatus(res, errors.invalidWebhookSecret, 401)
syncPrismic().then(result => {
return apiStatus(res, result, 200)
}).catch(err => {
console.error(errors.syncError, err)
return apiStatus(res, errors.syncError, 500)
})
})
api.get('/images/:name', function (req, res, next) {
var options = {
root: Path.join('/tmp/prismic-images'),
dotfiles: 'deny',
cacheControl: false,
headers: {
'x-timestamp': Date.now(),
'x-sent': true
}
}
var fileName = req.params.name
res.sendFile(fileName, options, function (err) {
if (err) {
next(err)
} else {
console.log('Sent:', fileName)
}
})
})
return api
}