-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
entity-renderer: add support for multiple datasetBaseUrl
- Loading branch information
1 parent
0dacbee
commit 0056079
Showing
5 changed files
with
265 additions
and
83 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"@zazuko/trifid-entity-renderer": minor | ||
--- | ||
|
||
Add support for multiple `datasetBaseUrl` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
// @ts-check | ||
|
||
/** | ||
* Check the dataset base URL. | ||
* Some hints are provided if the dataset base URL is not correctly formatted. | ||
* If a value is empty, an error is thrown. | ||
* | ||
* @param {{warn: Function }} logger - The logger instance | ||
* @param {string} datasetBaseUrl - The dataset base URL | ||
* @returns {true} The dataset base URL as an array | ||
*/ | ||
export const checkSingleDatasetBaseUrl = (logger, datasetBaseUrl) => { | ||
if (typeof datasetBaseUrl !== 'string') { | ||
throw new Error('The datasetBaseUrl must be a string') | ||
} | ||
|
||
if (!datasetBaseUrl) { | ||
throw new Error("Value for 'datasetBaseUrl' is missing") | ||
} | ||
|
||
if (!datasetBaseUrl.endsWith('/')) { | ||
logger.warn(`The value for 'datasetBaseUrl' should usually end with a '/' ; it is not the case for '${datasetBaseUrl}'`) | ||
} | ||
|
||
return true | ||
} | ||
|
||
/** | ||
* Check the dataset base URL, and make sure it returns an array. | ||
* Some hints are provided if the dataset base URL is not correctly formatted. | ||
* If the dataset base URL is an array, each value is checked. | ||
* If a value is empty, then an error is thrown. | ||
* | ||
* @param {{warn: Function }} logger - The logger instance | ||
* @param {string | string[]} datasetBaseUrl - The dataset base URL | ||
* @returns {string[]} The dataset base URL as an array | ||
*/ | ||
export const checkDatasetBaseUrl = (logger, datasetBaseUrl) => { | ||
if (!datasetBaseUrl) { | ||
throw new Error('No datasetBaseUrl provided') | ||
} | ||
|
||
if (Array.isArray(datasetBaseUrl)) { | ||
datasetBaseUrl.forEach((value) => checkSingleDatasetBaseUrl(logger, value)) | ||
return datasetBaseUrl | ||
} else { | ||
checkSingleDatasetBaseUrl(logger, datasetBaseUrl) | ||
return [datasetBaseUrl] | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
export const defaultConfiguration = { | ||
resourceNoSlash: true, | ||
resourceExistsQuery: 'ASK { <{{iri}}> ?p ?o }', | ||
resourceGraphQuery: 'DESCRIBE <{{iri}}>', | ||
containerExistsQuery: 'ASK { ?s a ?o. FILTER REGEX(STR(?s), "^{{iri}}") }', | ||
containerGraphQuery: | ||
'CONSTRUCT { ?s a ?o. } WHERE { ?s a ?o. FILTER REGEX(STR(?s), "^{{iri}}") }', | ||
redirectQuery: ` | ||
PREFIX http2011: <http://www.w3.org/2011/http#> | ||
PREFIX http2006: <http://www.w3.org/2006/http#> | ||
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> | ||
SELECT ?req ?res ?location ?responseCode ?validFrom | ||
WHERE { | ||
GRAPH ?g { | ||
# Handle 2011 version | ||
{ | ||
?req2011 rdf:type http2011:GetRequest. | ||
?req2011 http2011:requestURI <{{iri}}>. | ||
?req2011 http2011:response ?res2011. | ||
?res2011 rdf:type http2011:Response. | ||
?res2011 http2011:location ?location2011. | ||
?res2011 http2011:responseCode ?responseCode2011. | ||
OPTIONAL { | ||
?res2011 <http://schema.org/validFrom> ?validFrom2011. | ||
} | ||
} | ||
UNION | ||
# Handle 2006 version | ||
{ | ||
?req2006 rdf:type http2006:GetRequest. | ||
?req2006 http2006:requestURI <{{iri}}>. | ||
?req2006 http2006:response ?res2006. | ||
?res2006 rdf:type http2006:Response. | ||
?res2006 http2006:location ?location2006. | ||
?res2006 http2006:responseCode ?responseCode2006. | ||
OPTIONAL { | ||
?res2006 <http://schema.org/validFrom> ?validFrom2006. | ||
} | ||
} | ||
# Combine results, using priority for 2011 version over 2006 version | ||
BIND(COALESCE(?req2011, ?req2006) AS ?req) | ||
BIND(COALESCE(?res2011, ?res2006) AS ?res) | ||
BIND(COALESCE(?location2011, ?location2006) AS ?location) | ||
BIND(COALESCE(?validFrom2011, ?validFrom2006) AS ?validFrom) | ||
# Just get the response code as a string instead of the full IRI | ||
BIND(STRAFTER(STR(COALESCE(?responseCode2011, ?responseCode2006)), "#") AS ?responseCode) | ||
} | ||
} | ||
LIMIT 1 | ||
`, | ||
followRedirects: false, | ||
enableSchemaUrlRedirect: false, // Experimental | ||
allowEndpointSwitch: false, // Experimental | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
// @ts-check | ||
|
||
import { strictEqual, deepStrictEqual, throws } from 'node:assert' | ||
import { afterEach, beforeEach, describe, it } from 'node:test' | ||
|
||
import { checkSingleDatasetBaseUrl, checkDatasetBaseUrl } from '../lib/base.js' | ||
|
||
describe('lib/base', () => { | ||
describe('checkSingleDatasetBaseUrl', () => { | ||
let logger | ||
let loggerValues | ||
|
||
beforeEach(() => { | ||
loggerValues = [] | ||
logger = { | ||
warn: (/** @type {string} */ msg) => { loggerValues.push(msg) }, | ||
} | ||
}) | ||
|
||
afterEach(() => { | ||
logger = undefined | ||
loggerValues = undefined | ||
}) | ||
|
||
it('should not throw on valid value', () => { | ||
strictEqual(checkSingleDatasetBaseUrl(logger, 'http://example.com/'), true) | ||
}) | ||
|
||
it('should warn on missing trailing slash', () => { | ||
strictEqual(checkSingleDatasetBaseUrl(logger, 'http://example.com'), true) | ||
strictEqual(loggerValues.length, 1) | ||
}) | ||
|
||
it('should throw on empty value', () => { | ||
throws(() => checkSingleDatasetBaseUrl(logger, '')) | ||
}) | ||
|
||
it('should throw on non-string value', () => { | ||
// @ts-expect-error | ||
throws(() => checkSingleDatasetBaseUrl(logger, 42)) | ||
// @ts-expect-error | ||
throws(() => checkSingleDatasetBaseUrl(logger, ['http://example.com/'])) | ||
}) | ||
}) | ||
|
||
describe('checkDatasetBaseUrl', () => { | ||
let logger | ||
let loggerValues | ||
|
||
beforeEach(() => { | ||
loggerValues = [] | ||
logger = { | ||
warn: (/** @type {string} */ msg) => { loggerValues.push(msg) }, | ||
} | ||
}) | ||
|
||
afterEach(() => { | ||
logger = undefined | ||
loggerValues = undefined | ||
}) | ||
|
||
it('should not throw on valid value (string)', () => { | ||
deepStrictEqual(checkDatasetBaseUrl(logger, 'http://example.com/'), ['http://example.com/']) | ||
}) | ||
|
||
it('should not throw on valid value (array)', () => { | ||
deepStrictEqual(checkDatasetBaseUrl(logger, ['http://example.com/']), ['http://example.com/']) | ||
}) | ||
|
||
it('should warn on missing trailing slash', () => { | ||
deepStrictEqual(checkDatasetBaseUrl(logger, 'http://example.com'), ['http://example.com']) | ||
strictEqual(loggerValues.length, 1) | ||
}) | ||
|
||
it('should throw on empty value', () => { | ||
throws(() => checkDatasetBaseUrl(logger, '')) | ||
}) | ||
|
||
it('should throw on array with an empty value', () => { | ||
throws(() => checkDatasetBaseUrl(logger, [''])) | ||
}) | ||
|
||
it('should throw on array that contains an empty value somewhere', () => { | ||
throws(() => checkDatasetBaseUrl(logger, ['', 'http://example.com'])) | ||
throws(() => checkDatasetBaseUrl(logger, ['http://example.com', ''])) | ||
}) | ||
|
||
it('should throw on array that contains a value that is not a string', () => { | ||
// @ts-expect-error | ||
throws(() => checkDatasetBaseUrl(logger, [42, 'http://example.com'])) | ||
// @ts-expect-error | ||
throws(() => checkDatasetBaseUrl(logger, ['http://example.com', 42])) | ||
}) | ||
}) | ||
}) |