diff --git a/types/sparql-http-client/index.d.ts b/types/sparql-http-client/index.d.ts new file mode 100644 index 0000000000..418cc32a01 --- /dev/null +++ b/types/sparql-http-client/index.d.ts @@ -0,0 +1,40 @@ +// Type definitions for sparql-http-client 1.1 +// Project: https://github.com/zazuko/sparql-http-client +// Definitions by: Tomasz Pluskiewicz +// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped +// TypeScript Version: 2.3 + +import { Term } from 'rdf-js'; +import fetch, { Response, RequestInit } from 'node-fetch'; +import { URL } from 'url'; + +export interface SparqlHttpOptions { + endpointUrl?: string; + updateUrl?: string; +} + +export interface SparqlClientOptions extends SparqlHttpOptions { + fetch: typeof fetch; + URL: typeof URL; +} + +export interface QueryRequestInit extends SparqlHttpOptions, RequestInit {} + +export interface SelectBindings { + results: { bindings: ReadonlyArray> }; +} + +export interface AskResult { + boolean: boolean; +} + +export interface SelectResponse { + json(): Promise; +} + +export class SparqlHttp { + constructor(options?: SparqlHttpOptions); + updateQuery(query: string, options?: QueryRequestInit): Promise; + selectQuery(query: string, options?: QueryRequestInit): Promise; + constructQuery(query: string, options?: QueryRequestInit): Promise; +} diff --git a/types/sparql-http-client/sparql-http-client-tests.ts b/types/sparql-http-client/sparql-http-client-tests.ts new file mode 100644 index 0000000000..9a271bd0d8 --- /dev/null +++ b/types/sparql-http-client/sparql-http-client-tests.ts @@ -0,0 +1,41 @@ +import { SparqlHttp } from 'sparql-http-client'; + +const client = new SparqlHttp(); + +async function selectQuery() { + const response = await client.selectQuery('...', { + endpointUrl: 'http://example.com/endpoint', + }); + + const json = await response.json(); + + json.results.bindings.forEach(binding => { + console.log(`${binding.name.value} (${binding.name.termType})`); + }); +} + +async function askQuery() { + const response = await client.selectQuery('...', { + endpointUrl: 'http://example.com/endpoint', + }); + + const json = await response.json(); + + console.log(`The answer was ${json.boolean ? 'YES' : 'NO'}`); +} + +async function constructQuery() { + const response = await client.constructQuery('...'); + + const json = await response.json(); + + console.log(`The answer was ${json.boolean ? 'YES' : 'NO'}`); +} + +async function updateQuery() { + const response = await client.constructQuery('...', { + updateUrl: 'http://example.com/endpoint', + }); + + console.log(`The update ${response.ok ? 'succeeded' : 'faield'}`); +} diff --git a/types/sparql-http-client/tsconfig.json b/types/sparql-http-client/tsconfig.json new file mode 100644 index 0000000000..8139d915be --- /dev/null +++ b/types/sparql-http-client/tsconfig.json @@ -0,0 +1,23 @@ +{ + "compilerOptions": { + "module": "commonjs", + "lib": [ + "es6" + ], + "noImplicitAny": true, + "noImplicitThis": true, + "strictFunctionTypes": true, + "strictNullChecks": true, + "baseUrl": "../", + "typeRoots": [ + "../" + ], + "types": [], + "noEmit": true, + "forceConsistentCasingInFileNames": true + }, + "files": [ + "index.d.ts", + "sparql-http-client-tests.ts" + ] +} diff --git a/types/sparql-http-client/tslint.json b/types/sparql-http-client/tslint.json new file mode 100644 index 0000000000..3db14f85ea --- /dev/null +++ b/types/sparql-http-client/tslint.json @@ -0,0 +1 @@ +{ "extends": "dtslint/dt.json" }