@rdfjs/fetch-lite
Advanced tools
Comparing version 3.2.1 to 3.2.2
import { isReadableStream } from 'is-stream' | ||
import { Headers } from 'nodeify-fetch' | ||
import { Readable } from 'readable-stream' | ||
import headersToLowerCase from './headersToLowerCase.js' | ||
function patchRequest (options, formats) { | ||
options.headers = headersToLowerCase(options.headers || {}) | ||
options.headers = new Headers(options.headers) | ||
// if no accept header is defined, list all of the parsers map | ||
options.headers.accept = options.headers.accept || [...formats.parsers.keys()].join(', ') | ||
if (!options.headers.has('accept')) { | ||
options.headers.set('accept', [...formats.parsers.keys()].join(', ')) | ||
} | ||
@@ -22,9 +24,15 @@ // nothing to do if there is no content to send | ||
// content-type defined but no serializer available for the media type available | ||
if (options.headers['content-type'] && !formats.serializers.has(options.headers['content-type'])) { | ||
throw new Error(`no serializer found for media type: ${options.headers['content-type']}`) | ||
let contentType = options.headers.get('content-type') | ||
if (contentType && !formats.serializers.has(contentType)) { | ||
throw new Error(`no serializer found for media type: ${options.headers.get('content-type')}`) | ||
} | ||
// if no content-type was defined, use the first in the serializer map | ||
options.headers['content-type'] = options.headers['content-type'] || formats.serializers.keys().next().value | ||
if (!contentType) { | ||
contentType = formats.serializers.keys().next().value | ||
options.headers.set('content-type', contentType) | ||
} | ||
// if body is an iterable, replace it with a stream | ||
@@ -36,3 +44,3 @@ if (!isReadableStream(options.body) && options.body[Symbol.iterator]) { | ||
// replace body quad stream with the serializer stream | ||
options.body = formats.serializers.import(options.headers['content-type'], options.body) | ||
options.body = formats.serializers.import(contentType, options.body) | ||
@@ -39,0 +47,0 @@ return options |
{ | ||
"name": "@rdfjs/fetch-lite", | ||
"version": "3.2.1", | ||
"version": "3.2.2", | ||
"description": "Wrapper for fetch to simplify sending and receiving RDF data", | ||
@@ -13,3 +13,3 @@ "type": "module", | ||
"type": "git", | ||
"url": "git://github.com/rdfjs-base/fetch-lite.git" | ||
"url": "https://github.com/rdfjs-base/fetch-lite.git" | ||
}, | ||
@@ -32,3 +32,3 @@ "keywords": [ | ||
"nodeify-fetch": "^3.1.0", | ||
"readable-stream": "^4.2.0" | ||
"readable-stream": "^4.4.2" | ||
}, | ||
@@ -38,14 +38,14 @@ "devDependencies": { | ||
"@rdfjs/dataset": "^2.0.1", | ||
"@rdfjs/environment": "^0.1.1", | ||
"@rdfjs/environment": "^0.1.2", | ||
"@rdfjs/formats-common": "^3.1.0", | ||
"@rdfjs/sink-map": "^2.0.0", | ||
"@rdfjs/to-ntriples": "^2.0.0", | ||
"assert": "^2.0.0", | ||
"c8": "^7.12.0", | ||
"assert": "^2.1.0", | ||
"c8": "^8.0.1", | ||
"express-as-promise": "^1.2.0", | ||
"mocha": "^10.2.0", | ||
"stream-chunks": "^1.0.0", | ||
"stricter-standard": "^0.2.0", | ||
"stricter-standard": "^0.3.0", | ||
"test-runner-browser": "^0.1.0" | ||
} | ||
} |
# @rdfjs/fetch-lite | ||
[![build status](https://img.shields.io/github/workflow/status/rdfjs-base/fetch-lite/Test)](https://github.com/rdfjs-base/fetch-lite/actions/workflows/test.yaml) | ||
[![build status](https://img.shields.io/github/actions/workflow/status/rdfjs-base/fetch-lite/test.yaml?branch=master)](https://github.com/rdfjs-base/fetch-lite/actions/workflows/test.yaml) | ||
[![npm version](https://img.shields.io/npm/v/@rdfjs/fetch-lite.svg)](https://www.npmjs.com/package/@rdfjs/fetch-lite) | ||
@@ -4,0 +5,0 @@ |
@@ -6,3 +6,3 @@ import { rejects, strictEqual } from 'assert' | ||
import { Readable } from 'readable-stream' | ||
import rdfFetch from '../index.js' | ||
import rdfFetch, { Headers } from '../index.js' | ||
import example from './support/example.js' | ||
@@ -31,3 +31,3 @@ import simpleServer from './support/simpleServer.js' | ||
}, | ||
formats: formats | ||
formats | ||
}) | ||
@@ -38,6 +38,21 @@ | ||
it('should use the given accept header', async () => { | ||
it('should use the given accept header given as Headers object', async () => { | ||
const context = await simpleServer(async ({ baseUrl }) => { | ||
await rdfFetch(baseUrl, { | ||
formats, | ||
headers: new Headers({ | ||
accept: 'text/html' | ||
}) | ||
}) | ||
}, { | ||
'/': {} | ||
}) | ||
strictEqual(context.resources['/'].req.headers.accept, 'text/html') | ||
}) | ||
it('should use the given accept header given as plain object', async () => { | ||
const context = await simpleServer(async ({ baseUrl }) => { | ||
await rdfFetch(baseUrl, { | ||
formats, | ||
headers: { | ||
@@ -83,3 +98,3 @@ accept: 'text/html' | ||
it('should serialize a stream body using the serializer given in the content-type header', async () => { | ||
it('should serialize a stream body using the serializer given in the content-type header as Headers object', async () => { | ||
const context = await simpleServer(async ({ baseUrl }) => { | ||
@@ -89,2 +104,21 @@ await rdfFetch(baseUrl, { | ||
method: 'POST', | ||
headers: new Headers({ | ||
'content-type': 'application/n-triples' | ||
}), | ||
body: Readable.from(example.dataset) | ||
}) | ||
}, { | ||
'/': { | ||
method: 'POST' | ||
} | ||
}) | ||
strictEqual(context.resources['/'].req.content, example.quadNt) | ||
}) | ||
it('should serialize a stream body using the serializer given in the content-type header as plain object', async () => { | ||
const context = await simpleServer(async ({ baseUrl }) => { | ||
await rdfFetch(baseUrl, { | ||
formats, | ||
method: 'POST', | ||
headers: { | ||
@@ -91,0 +125,0 @@ 'content-type': 'application/n-triples' |
@@ -5,4 +5,3 @@ import 'mocha' | ||
import '../Headers.test.js' | ||
import '../headersToLowerCase.test.js' | ||
import '../jsonldContextLinkUrl.test.js' | ||
import '../test.js' |
Sorry, the diff of this file is not supported yet
40933
1002
79
23
Updatedreadable-stream@^4.4.2