@rdfjs/express-handler
Advanced tools
Comparing version 1.0.2 to 1.1.0
37
index.js
@@ -6,2 +6,4 @@ const defaultFormats = require('@rdfjs/formats-common') | ||
const { promisify } = require('util') | ||
const { PassThrough } = require('readable-stream') | ||
const absoluteUrl = require('absolute-url') | ||
@@ -50,3 +52,14 @@ async function readDataset ({ factory, options, req }) { | ||
function init ({ factory = rdf, formats = defaultFormats, defaultMediaType } = {}) { | ||
function init ({ factory = rdf, formats = defaultFormats, defaultMediaType, baseIriFromRequest } = {}) { | ||
let getBaseIri | ||
if (baseIriFromRequest === true) { | ||
getBaseIri = (req) => { | ||
absoluteUrl.attach(req) | ||
return req.absoluteUrl() | ||
} | ||
} else if (typeof baseIriFromRequest === 'function') { | ||
getBaseIri = baseIriFromRequest | ||
} | ||
// middleware | ||
@@ -76,7 +89,25 @@ return (req, res, next) => { | ||
req.dataset = async options => { | ||
req.dataset = async userOptions => { | ||
const options = { ...userOptions } | ||
if (getBaseIri) { | ||
options.baseIRI = await getBaseIri(req) | ||
} | ||
return readDataset({ factory, options, req }) | ||
} | ||
req.quadStream = options => { | ||
req.quadStream = userOptions => { | ||
const options = { ...userOptions } | ||
if (getBaseIri) { | ||
const passThrough = new PassThrough() | ||
Promise.resolve().then(async () => { | ||
options.baseIRI = getBaseIri(req) | ||
readQuadStream({ formats, mediaType, options, req }).pipe(passThrough) | ||
}) | ||
return passThrough | ||
} | ||
return readQuadStream({ formats, mediaType, options, req }) | ||
@@ -83,0 +114,0 @@ } |
{ | ||
"name": "@rdfjs/express-handler", | ||
"version": "1.0.2", | ||
"version": "1.1.0", | ||
"description": "Handle incoming and outgoing RDF data in Express", | ||
@@ -29,5 +29,7 @@ "main": "index.js", | ||
"@rdfjs/formats-common": "^2.0.0", | ||
"absolute-url": "^1.2.2", | ||
"http-errors": "^1.7.2", | ||
"isstream": "^0.1.2", | ||
"rdf-dataset-ext": "^1.0.0" | ||
"rdf-dataset-ext": "^1.0.0", | ||
"readable-stream": "^3.6.0" | ||
}, | ||
@@ -34,0 +36,0 @@ "devDependencies": { |
@@ -31,2 +31,3 @@ # @rdfjs/express-handler | ||
- `defaultMediaType`: If an unknown Content-Type is given, this media type will be used. Default: `undefined` | ||
- `baseIRI`: If `true`, will call [absolute-url](https://npm.im/absolute-url) to get the requested IRI and pass as base IRI to the parser. It can also be a function `async (req) => string` which can be used to compute the base IRI for the parser. | ||
@@ -86,2 +87,2 @@ ### Request | ||
}) | ||
``` | ||
``` |
@@ -134,4 +134,59 @@ /* global describe, it */ | ||
assert.strictEqual(givenOptions, options) | ||
assert.deepStrictEqual(givenOptions, options) | ||
}) | ||
it('should pass base IRI to parser when set on handler', async () => { | ||
let givenOptions = null | ||
const options = { | ||
baseIRI: 'http://example.com/resource/name' | ||
} | ||
const app = express() | ||
const formats = formatsMock({ | ||
parse: (stream, options) => { | ||
givenOptions = options | ||
} | ||
}) | ||
app.use(rdfHandler({ formats, baseIriFromRequest: true })) | ||
app.use(async (req, res, next) => { | ||
await req.dataset() | ||
next() | ||
}) | ||
await request(app).post('/resource/name') | ||
.set('content-type', 'text/plain') | ||
.set('host', 'example.com') | ||
.send('') | ||
assert.deepStrictEqual(givenOptions, options) | ||
}) | ||
it('should pass computed base IRI to parser when set on handler', async () => { | ||
let givenOptions = null | ||
const options = { | ||
baseIRI: 'http://example.com/resource-base/' | ||
} | ||
const app = express() | ||
const formats = formatsMock({ | ||
parse: (stream, options) => { | ||
givenOptions = options | ||
} | ||
}) | ||
app.use(rdfHandler({ formats, baseIriFromRequest: () => 'http://example.com/resource-base/' })) | ||
app.use(async (req, res, next) => { | ||
await req.dataset() | ||
next() | ||
}) | ||
await request(app).post('/resource/name') | ||
.set('content-type', 'text/plain') | ||
.send('') | ||
assert.deepStrictEqual(givenOptions, options) | ||
}) | ||
}) | ||
@@ -236,5 +291,60 @@ | ||
assert.strictEqual(givenOptions, options) | ||
assert.deepStrictEqual(givenOptions, options) | ||
}) | ||
it('should pass base IRI to parser when set on handler', async () => { | ||
let givenOptions = null | ||
const options = { | ||
baseIRI: 'http://example.com/resource/name' | ||
} | ||
const app = express() | ||
const formats = formatsMock({ | ||
parse: (stream, options) => { | ||
givenOptions = options | ||
} | ||
}) | ||
app.use(rdfHandler({ formats, baseIriFromRequest: true })) | ||
app.use(async (req, res, next) => { | ||
await req.quadStream() | ||
next() | ||
}) | ||
await request(app).post('/resource/name') | ||
.set('content-type', 'text/plain') | ||
.set('host', 'example.com') | ||
.send('') | ||
assert.deepStrictEqual(givenOptions, options) | ||
}) | ||
it('should pass computed base IRI to parser when set on handler', async () => { | ||
let givenOptions = null | ||
const options = { | ||
baseIRI: 'http://example.com/resource-base/' | ||
} | ||
const app = express() | ||
const formats = formatsMock({ | ||
parse: (stream, options) => { | ||
givenOptions = options | ||
} | ||
}) | ||
app.use(rdfHandler({ formats, baseIriFromRequest: () => 'http://example.com/resource-base/' })) | ||
app.use(async (req, res, next) => { | ||
await req.quadStream() | ||
next() | ||
}) | ||
await request(app).post('/resource/name') | ||
.set('content-type', 'text/plain') | ||
.send('') | ||
assert.deepStrictEqual(givenOptions, options) | ||
}) | ||
}) | ||
}) |
29332
745
87
7
+ Addedabsolute-url@^1.2.2
+ Addedreadable-stream@^3.6.0
+ Addedabsolute-url@1.2.2(transitive)