Comparing version 0.0.1 to 0.0.2
{ | ||
"name": "blazegraph", | ||
"version": "0.0.1", | ||
"version": "0.0.2", | ||
"description": "Blazegraph JavaScript API", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -1,2 +0,1 @@ | ||
/* eslint-disable no-shadow */ | ||
const request = require('request'); | ||
@@ -19,13 +18,19 @@ const createRdfParser = require('n3').Parser; | ||
function rejectInput(input) { | ||
return Promise.reject(new Error(`Invalid input: ${JSON.stringify(input)}`)); | ||
} | ||
// Validation functions | ||
function validateInput(input) { | ||
if (!(input && typeof input === 'object')) throw new Error('Malformed input'); | ||
return input; | ||
function isNotObject(input) { | ||
return !(input && typeof input === 'object'); | ||
} | ||
function isNonEmptyString(value) { | ||
function isValidString(value) { | ||
return value && typeof value === 'string'; | ||
} | ||
function isNotFullStatement({ subject, predicate, object, graph }) { | ||
return !(isValidString(subject) && isValidString(predicate) && isValidString(object) && isValidString(graph)); | ||
} | ||
// URI encodes a SPARQL query | ||
@@ -40,6 +45,6 @@ function encodeQuery(query) { | ||
if (isNonEmptyString(subject)) url += `&s=${encodeURIComponent(subject)}`; | ||
if (isNonEmptyString(predicate)) url += `&p=${encodeURIComponent(predicate)}`; | ||
if (isNonEmptyString(object)) url += `&o=${encodeURIComponent(object)}`; | ||
if (isNonEmptyString(graph)) url += `&c=${encodeURIComponent(graph)}`; | ||
if (isValidString(subject)) url += `&s=${encodeURIComponent(subject)}`; | ||
if (isValidString(predicate)) url += `&p=${encodeURIComponent(predicate)}`; | ||
if (isValidString(object)) url += `&o=${encodeURIComponent(object)}`; | ||
if (isValidString(graph)) url += `&c=${encodeURIComponent(graph)}`; | ||
@@ -52,7 +57,2 @@ return url.slice(1); | ||
function serializeQuad({ subject, predicate, object, graph }) { | ||
if (!(isNonEmptyString(subject) && isNonEmptyString(predicate) && isNonEmptyString(object) && isNonEmptyString(graph))) { | ||
throw new Error(`Malformed input: ${JSON.stringify({ subject, predicate, object, graph }, null, 2)}`); | ||
} | ||
return `${subject} ${predicate} ${object} ${graph} .`; | ||
@@ -76,3 +76,3 @@ } | ||
function readQuads(blazegraphUrl, input) { | ||
validateInput(input); | ||
if (isNotObject(input)) return rejectInput(input); | ||
@@ -107,2 +107,6 @@ let fullUrl = `${blazegraphUrl}?GETSTMTS&includeInferred=false&${encodeQuad(input)}`; | ||
function createQuads(blazegraphUrl, input) { | ||
const inputs = Array.isArray(input) ? input : [input]; | ||
if (inputs.some(x => isNotObject(x) || isNotFullStatement(x))) return rejectInput(input); | ||
return makeRequest({ | ||
@@ -114,5 +118,3 @@ url: blazegraphUrl, | ||
}, | ||
body: (Array.isArray(input) ? input : [input]) | ||
.map(quad => serializeQuad(validateInput(quad))) | ||
.join('\n'), | ||
body: inputs.map(serializeQuad).join('\n'), | ||
}); | ||
@@ -132,3 +134,3 @@ } | ||
function updateQuad(blazegraphUrl, input) { | ||
validateInput(input); | ||
if (isNotObject(input) || isNotFullStatement(input) || !isValidString(input.newObject)) return rejectInput(input); | ||
@@ -158,3 +160,3 @@ const oldQuad = serializeQuad(input); | ||
function updateSparql(blazegraphUrl, query) { | ||
if (!isNonEmptyString(query)) throw new Error('Query must be a non-empty string'); | ||
if (!isValidString(query)) return Promise.reject(new Error('Query must be a non-empty string')); | ||
@@ -177,3 +179,3 @@ return makeRequest({ | ||
function deleteQuads(blazegraphUrl, input) { | ||
validateInput(input); | ||
if (isNotObject(input)) return rejectInput(input); | ||
@@ -193,3 +195,3 @@ const params = encodeQuad(input); | ||
function deleteSparql(blazegraphUrl, query) { | ||
if (!isNonEmptyString(query)) throw new Error('Query must be a non-empty string'); | ||
if (!isValidString(query)) return Promise.reject(new Error('Query must be a non-empty string')); | ||
@@ -196,0 +198,0 @@ return makeRequest({ |
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
8852
182
0