Home

Awesome

@rdfjs/fetch-lite

build status npm version

Wrapper for fetch to simplify sending and retrieving RDF data.

This is a light version of the @rdfjs/fetch package, without the @rdfjs/formats-common dependency. It is useful when you want to make a build for the browser with a reduced set of parsers and serializers.

The formats options is required for this package. See also the @rdfjs/fetch documentation.

Since version 3.0, this packages is ESM only. Check version 2.x if you are looking for a CommonJS package.

Usage

The package exports a fetch function which wraps the request and response object for on-the-fly RDF quad processing. The function accepts the same parameters as fetch and some additional options. It also provides extra methods.

Options

The options object accepts the following additional parameters:

The following options influence the logic of RDF quad processing:

Response

The following methods are attached to the standard fetch response object:

The Content-Type header of the response can be changed or set before calling quadStream() or dataset(). That allows enforcing a specific parser or can be used to fix a lacking header.

Example

This example fetches data from a resource on Wikidata. The stream API is used to process all quads. For all rdfs:label quads of the defined entity, the object language and value will be written to the console.

import formats from '@rdfjs/formats-common'
import fetch from '@rdfjs/fetch-lite'

const entity = 'http://www.wikidata.org/entity/Q2'
const label = 'http://www.w3.org/2000/01/rdf-schema#label'

const res = await fetch('https://www.wikidata.org/wiki/Special:EntityData/Q2.ttl', { formats })
const quadStream = await res.quadStream()

quadStream.on('error', err => console.error(err))

quadStream.on('data', quad => {
  if (quad.subject.value === entity && quad.predicate.value === label) {
    console.log(`${quad.object.language}: ${quad.object.value}`)
  }
})