mirror of
https://github.com/gosticks/DefinitelyTyped.git
synced 2025-10-16 12:05:41 +00:00
added typings for sparql-http-client (#38915)
* added typings for sparql-http-client * use readonly array for select result
This commit is contained in:
parent
cd093195bb
commit
7ea9422586
40
types/sparql-http-client/index.d.ts
vendored
Normal file
40
types/sparql-http-client/index.d.ts
vendored
Normal 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>;
|
||||
}
|
||||
41
types/sparql-http-client/sparql-http-client-tests.ts
Normal file
41
types/sparql-http-client/sparql-http-client-tests.ts
Normal 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'}`);
|
||||
}
|
||||
23
types/sparql-http-client/tsconfig.json
Normal file
23
types/sparql-http-client/tsconfig.json
Normal 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"
|
||||
]
|
||||
}
|
||||
1
types/sparql-http-client/tslint.json
Normal file
1
types/sparql-http-client/tslint.json
Normal file
@ -0,0 +1 @@
|
||||
{ "extends": "dtslint/dt.json" }
|
||||
Loading…
Reference in New Issue
Block a user