Skip to content

Commit

Permalink
Rewrite udata-proxy using got
Browse files Browse the repository at this point in the history
  • Loading branch information
tusbar committed Oct 12, 2018
1 parent 5f659db commit 29c9a9c
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 44 deletions.
2 changes: 1 addition & 1 deletion plugins/publish-to-udata/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ module.exports = function () {
req.session.redirectTo = undefined
})

app.use('/proxy-api', require('./udataProxy')())
app.use('/proxy-api', require('./udata-proxy'))

app.use('/api', require('./routes/producers')())
app.use('/api', require('./routes/organizations')())
Expand Down
37 changes: 37 additions & 0 deletions plugins/publish-to-udata/udata-proxy.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
const {Router} = require('express')
const got = require('got')

const ALLOWED_METHODS = ['GET', 'HEAD', 'POST', 'PUT', 'DELETE']
const baseUrl = process.env.DATAGOUV_URL + '/api'

const router = new Router({
strict: true
})

router.all('*', (req, res, next) => {
if (!ALLOWED_METHODS.includes(req.method)) {
return res.status(405).send()
}

const options = {
baseUrl,
body: req.body,
method: req.method,
throwHttpErrors: false
}

if (req.user) {
options.headers = {
authorization: `Bearer ${req.user.accessToken}`
}
}

got
.stream(req.path, options)
.on('error', error => {
next(error)
})
.pipe(res)
})

module.exports = router
43 changes: 0 additions & 43 deletions plugins/publish-to-udata/udataProxy.js

This file was deleted.

0 comments on commit 29c9a9c

Please sign in to comment.