Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/pietercolpaert/ldfetch
An HTTP client for RDF resources
https://github.com/pietercolpaert/ldfetch
http-client hydra linked-data nodejs rdf rdf-resources triples
Last synced: 3 months ago
JSON representation
An HTTP client for RDF resources
- Host: GitHub
- URL: https://github.com/pietercolpaert/ldfetch
- Owner: pietercolpaert
- Created: 2017-06-09T13:56:21.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2024-01-10T06:46:49.000Z (about 1 year ago)
- Last Synced: 2024-04-13T23:54:54.486Z (10 months ago)
- Topics: http-client, hydra, linked-data, nodejs, rdf, rdf-resources, triples
- Language: JavaScript
- Homepage: https://www.npmjs.com/package/ldfetch
- Size: 4.63 MB
- Stars: 15
- Watchers: 3
- Forks: 5
- Open Issues: 11
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Linked Data Fetch: a HTTP client for RDF resources
[![npm version](https://badge.fury.io/js/ldfetch.svg)](https://badge.fury.io/js/ldfetch) [![CDN JSDelivr](https://data.jsdelivr.com/v1/package/npm/ldfetch/badge)](https://cdn.jsdelivr.net/npm/ldfetch/dist/main.js)
Fetch Linked Data documents within your browser, from the command line, or from your NodeJS script.
```bash
npm install -g ldfetch
```
In order to use it as a library, you can leave out the `-g`.## Features
Supports these features over standard `fetch`:
* Sets an accept header for negotiating an RDF serialization
* Uses [rdf-parse](https://github.com/rubensworks/rdf-parse.js) by Ruben Taelman to parse a wide variety of RDF serializations
* Returns the Triples/Quads containing the data in the [RDFJS triple representation](http://rdf.js.org/)
* Returns the URL of the document after redirects
* Emits events for: `request`, `response`, `redirect`, `cache-hit`, `cache-miss` and `parsed`Features for the NodeJS framework in specific:
* Automatically follows redirects
* Able to be configured with HTTP caching
* Able to limit the amount of concurrent requests and schedule theseFeatures for the Command Line:
* Writes data on any URL in TriG on stdout
* Extra features to automatically follow links (see `ldfetch --help` after `npm install -g ldfetch`)## How to use it
### Command line
![Quite easy](https://raw.githubusercontent.com/pietercolpaert/ldfetch/master/tty.gif "Straightforward to use this on a CLI")
You can also use [JSON-LD framing](https://json-ld.org/spec/latest/json-ld-framing/) from the CLI: `ldfetch https://pietercolpaert.be/ --frame {}` to return a JSON-LD object complying to your frame.
For full, well tested and modular SPARQL or GraphQL Web Querying, we refer to the [Comunica project](http://comunica.linkeddatafragments.org).
### Browser
And using Browserify you can compile it for browser purposes:
```bash
npm run build
``````html
let fetcher = new window.ldfetch();
let main = async function () {
let objects = await fetcher.get('http://ruben.verborgh.org').then(response => {
//LDFetch also exposes a frame function that can be used on the triples
//See https://json-ld.org/spec/latest/json-ld-framing/
return fetcher.frame(response.triples, {'@graph':{}});
});
console.log(objects);
}
try {
main();
} catch (e) {
console.error(e);
}```
### NodeJS
A small example fetching the next page of a paged collection and returning the url
```javascript
let ldfetch = require('../lib/ldfetch.js');
try {
let url = 'https://graph.irail.be/sncb/connections/';
let fetch = new ldfetch({}); //options: allow to add more headers if needed
let response = await fetch.get(url);
for (let i = 0; i < response.triples.length; i ++) {
let triple = response.triples[i];
if (triple.subject.value === response.url && triple.predicate.value === 'http://www.w3.org/ns/hydra/core#next') {
console.error('The next page is: ', triple.object.value);
}
}
fetch.frame(response.triples, {'http://www.w3.org/ns/hydra/core#next': {}}).then(object => {
console.error('Or you can also use the JSON-LD frame functionality to get what you want in a JS object', object);
});
} catch (e) {
console.error(e);
}
```
If HTTP requests with specific headers are needed, the `options` object may be used by defining an object inside of it, named `headers` containing HTTP header names and values.The response object will look like this:
```json
{
"responseCode": 200,
"triples": [{},{},{}],
"url": "https://{url after redirects}"
}
```## License and copyright
This library was developed by [Pieter Colpaert](https://pietercolpaert.be) and contributors. The source code is available under an MIT license.