UNPKG

905 BJavaScriptView Raw
1var collect = require('collect-stream')
2var osm2Obj = require('osm2json')
3
4var errors = require('../errors')
5var isValidContentType = require('../lib/util').isValidContentType
6
7module.exports = function (req, res, api, params, next) {
8 if (!isValidContentType(req)) {
9 return next(new errors.UnsupportedContentType())
10 }
11
12 var r = req.pipe(osm2Obj({types: [params.type], coerceIds: false}))
13 collect(r, function (err, ops) {
14 if (err) return next(new errors.XmlParseError(err))
15 if (!ops.length) return next(new errors.XmlMissingElement(params.type))
16
17 // If multiple elements are provided only the first is created.
18 // The rest is discarded (this behaviour differs from changeset creation).
19 api.createElement(ops[0], function (err, id, node) {
20 if (err) return next(err)
21 res.setHeader('content-type', 'text/plain; charset=utf-8')
22 res.end(id + '\n')
23 })
24 })
25}