Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add JSON-LD handler to fetcher #439

Merged
merged 6 commits into from
Oct 5, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 35 additions & 2 deletions src/fetcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ import {
Quad_Predicate,
Quad_Subject
} from './tf-types'
import jsonldParser from './jsonldparser'

const Parsable = {
'text/n3': true,
Expand Down Expand Up @@ -479,6 +480,39 @@ class HTMLHandler extends Handler {
}
HTMLHandler.pattern = new RegExp('text/html')

class JsonLdHandler extends Handler {
static toString () {
return 'JsonLdHandler'
}
static register (fetcher: Fetcher) {
fetcher.mediatypes['application/ld+json'] = {
'q': 0.9
}
}
parse (
fetcher: Fetcher,
responseText: string,
options: {
req: Quad_Subject
original: Quad_Subject
resource: Quad_Subject
} & Options,
response: ExtendedResponse
): ExtendedResponse | Promise<FetchError> {
const kb = fetcher.store
return new Promise((resolve, reject) => {
try {
jsonldParser (responseText, kb, options.original.value, resolve)
} catch (err) {
const msg = 'Error trying to parse ' + options.resource +
' as JSON-LD:\n' + err // not err.stack -- irrelevant
resolve(fetcher.failFetch(options, msg, 'parse_error', response))
}
})
}
}
JsonLdHandler.pattern = /application\/ld\+json/

class TextHandler extends Handler {
static toString () {
return 'TextHandler'
Expand Down Expand Up @@ -578,7 +612,7 @@ class N3Handler extends Handler {
N3Handler.pattern = new RegExp('(application|text)/(x-)?(rdf\\+)?(n3|turtle)')

const defaultHandlers = {
RDFXMLHandler, XHTMLHandler, XMLHandler, HTMLHandler, TextHandler, N3Handler
RDFXMLHandler, XHTMLHandler, XMLHandler, HTMLHandler, TextHandler, N3Handler, JsonLdHandler
}

function isXHTML (responseText) {
Expand Down Expand Up @@ -1895,7 +1929,6 @@ export default class Fetcher implements CallbackifyInterface {
const responseNode = this.saveResponseMetadata(response, options)

const contentType = this.normalizedContentType(options, headers) || ''

let contentLocation = headers.get('content-location')

// this.fireCallbacks('recv', xhr.args)
Expand Down
43 changes: 43 additions & 0 deletions tests/unit/fetcher-jsonld-test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import * as rdf from '../../src';

const nock = require('nock')
import chai from 'chai'

const {expect} = chai

describe('Given a JSON-LD resource', () => {

const uri = "http://localhost/jsonld#it"

beforeEach(() => {
const docContents = `
{
"@id": "${uri}",
"https://predicate.example": "value"
}
`
nock('http://localhost').get('/jsonld').reply(200, docContents, {
'Content-Type': 'application/ld+json',
})
});

describe('when it is fetched to the store', () => {

let fetcher, store;
beforeEach(() => {
store = rdf.graph();
fetcher = rdf.fetcher(store);
});

it('then the triples from the document can be found in the store', async () => {
await fetcher.load(uri);
const match = store.anyStatementMatching(rdf.sym(uri), rdf.sym('https://predicate.example'), rdf.literal('value'));
expect(match.subject.value).to.equal(uri);
expect(match.predicate.value).to.equal('https://predicate.example');
expect(match.object.value).to.equal('value');

});
});


});
2 changes: 1 addition & 1 deletion tests/unit/fetcher-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -372,7 +372,7 @@ describe('Fetcher', () => {
it('should set the Accept header', () => {
options = fetcher.initFetchOptions(uri, options)

let expectedHeader = 'image/*;q=0.9, */*;q=0.1, application/rdf+xml;q=0.9, application/xhtml+xml, text/xml;q=0.5, application/xml;q=0.5, text/html;q=0.9, text/plain;q=0.5, text/n3;q=1.0, text/turtle;q=1'
let expectedHeader = 'image/*;q=0.9, */*;q=0.1, application/rdf+xml;q=0.9, application/xhtml+xml, text/xml;q=0.5, application/xml;q=0.5, text/html;q=0.9, text/plain;q=0.5, text/n3;q=1.0, text/turtle;q=1, application/ld+json;q=0.9'

expect(options.headers['accept']).to.equal(expectedHeader)
})
Expand Down