added typings for sparql-http-client (#38915)

* added typings for sparql-http-client

* use readonly array for select result
This commit is contained in:
Tomasz Pluskiewicz 2019-10-09 01:07:05 +02:00 committed by Armando Aguirre
parent cd093195bb
commit 7ea9422586
4 changed files with 105 additions and 0 deletions

40
types/sparql-http-client/index.d.ts vendored Normal file
View File

@ -0,0 +1,40 @@
// Type definitions for sparql-http-client 1.1
// Project: https://github.com/zazuko/sparql-http-client
// Definitions by: Tomasz Pluskiewicz <https://github.com/tpluscode>
// 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<Record<string, Term>> };
}
export interface AskResult {
boolean: boolean;
}
export interface SelectResponse {
json(): Promise<SelectBindings & AskResult>;
}
export class SparqlHttp<TResponse extends Response = Response> {
constructor(options?: SparqlHttpOptions);
updateQuery(query: string, options?: QueryRequestInit): Promise<Response>;
selectQuery(query: string, options?: QueryRequestInit): Promise<SelectResponse & TResponse>;
constructQuery(query: string, options?: QueryRequestInit): Promise<TResponse>;
}

View File

@ -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'}`);
}

View File

@ -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"
]
}

View File

@ -0,0 +1 @@
{ "extends": "dtslint/dt.json" }