# @rdfjs/fetch

> Wrapper for fetch to simplify sending and receiving RDF data

Latest version **3.1.1** (published 2023-05-27) · MIT license · 0 weekly downloads

## Install

```sh
npm install @rdfjs/fetch
pnpm add @rdfjs/fetch
yarn add @rdfjs/fetch
bun add @rdfjs/fetch
```

## Health

**Score 43/100 (D)** — status: abandoned.

Positive: has types package; esm support; no vulnerabilities; high maintenance score; high quality score.

Warnings: low downloads.

Negative: abandoned.

## Facts

| | |
|---|---|
| Version | 3.1.1 |
| Published | 2023-05-27 |
| First published | 2019-10-30 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | separate (@types/rdfjs__fetch) |
| Module format | ESM + CommonJS |
| Dependencies | 3 |
| Unpacked size | 8.9 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 2 |
| Author | Thomas Bergwinkl |
| Maintainers | bergos |
| Keywords | rdf, rdfjs, fetch |

## Links

- npm: https://www.npmjs.com/package/@rdfjs/fetch
- Repository: https://github.com/rdfjs-base/fetch
- Issues: https://github.com/rdfjs-base/fetch/issues
- npm.io page: https://npm.io/package/@rdfjs/fetch

## Dependencies (3)

- [@rdfjs/dataset](https://npm.io/package/@rdfjs/dataset.md) ^2.0.1
- [@rdfjs/fetch-lite](https://npm.io/package/@rdfjs/fetch-lite.md) ^3.2.1
- [@rdfjs/formats-common](https://npm.io/package/@rdfjs/formats-common.md) ^3.1.0

## Alternatives

- [gamedig](https://npm.io/package/gamedig.md) — 29.3K weekly downloads
- [join-monster](https://npm.io/package/join-monster.md) — 12.8K weekly downloads
- [masked](https://npm.io/package/masked.md) — 5.5K weekly downloads
- [@comunica/actor-query-process-explain-logical](https://npm.io/package/@comunica/actor-query-process-explain-logical.md) — 4.7K weekly downloads
- [@veracity/vui](https://npm.io/package/@veracity/vui.md) — 4.6K weekly downloads

## Recent versions

- 3.1.1 (latest) — 2023-05-27
- 3.1.0 — 2022-05-07
- 3.0.0 — 2021-10-14
- 2.1.0 — 2020-04-23
- 2.0.0 — 2019-10-30

## README

# @rdfjs/fetch
[![build status](https://img.shields.io/github/actions/workflow/status/rdfjs-base/fetch/test.yaml?branch=master)](https://github.com/rdfjs-base/fetch/actions/workflows/test.yaml)
[![npm version](https://img.shields.io/npm/v/@rdfjs/fetch.svg)](https://www.npmjs.com/package/@rdfjs/fetch)

Wrapper for fetch to simplify sending and retrieving RDF data.

Since version 3.0, this packages is [ESM](https://nodejs.org/api/esm.html) 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](https://fetch.spec.whatwg.org/) and some additional options.
It also provides extra methods.

### Options

The `options` object accepts the following additional parameters:

- `formats`: A [formats-common](https://github.com/rdfjs-base/formats-common)-compatible object which contains a set of parsers and serializers.
  By default [formats-common](https://github.com/rdfjs-base/formats-common) is used.
- `factory`: The factory which will be used to create a Dataset when `dataset()` is called.
  By default [@rdfjs/dataset](https://github.com/rdfjs-base/dataset) is used.
- `fetch`: An alternative fetch implementation.
  By default [nodeify-fetch](https://github.com/bergos/nodeify-fetch) is used.

The following `options` influence the logic of RDF quad processing: 

- `headers.accept`: The accept header field will be automatically set base on the list of available parsers from the `formats` object.
  If it's already set it will not be overwritten.
  This can be useful when only a subset of the available parsers should be used. 
- `headers.content-type`: When the request has a body, this header field will be automatically set to use matching media type for the corresponding serializer.
  By setting this field manually a specific serializer can be enforced.
- `body`: If the request should send quads, the quads must be given either as a stream or as an iterable like a [DatasetCore](https://rdf.js.org/dataset-spec/#datasetcore-interface) object.
  Iterables will be converted to streams before they are handed over to the serializer.

### Response

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

- async `quadStream()`: This method returns the quads of the response as stream.
  The parser is selected based on the content type header field.
- async `dataset()`: This method uses the `quadStream()` method to parse the content and will pipe it into a dataset, which is also the return value.

### Example

This example fetches the RDF Schema vocab and loops over all quad using the dataset API.
For all `rdfs:label` quads the object value is written to the console.

```javascript
import fetch from '@rdfjs/fetch'

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

fetch('http://www.w3.org/2000/01/rdf-schema')
  .then(res => res.dataset())
  .then(dataset => {
    for (const quad of dataset) {
      if (quad.predicate.value === label) {
        console.log(`${quad.subject.value}: ${quad.object.value}`)
      }
    }
  })
  .catch(err => console.error(err))
```

---
_Source: https://npm.io/package/@rdfjs/fetch · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
