mirror of
https://github.com/FlipsideCrypto/DefinitelyTyped.git
synced 2026-02-06 19:07:08 +00:00
update sparql-http-client to v2.2 (#43529)
* wip: sparql-http-client 2.0 * update to sparql-http-client 2.2
This commit is contained in:
parent
8b25cefffb
commit
2bcc698e2d
10
types/sparql-http-client/BaseClient.d.ts
vendored
Normal file
10
types/sparql-http-client/BaseClient.d.ts
vendored
Normal file
@ -0,0 +1,10 @@
|
||||
import { BaseQuad, Quad } from 'rdf-js';
|
||||
import { Client, ClientOptions, Query, Store } from '.';
|
||||
|
||||
declare class BaseClient<TQuery extends Query, Q extends BaseQuad = Quad, TStore extends Store<Q> = never> implements Client<TQuery, Q, TStore> {
|
||||
constructor(options: ClientOptions<TQuery, Q, TStore>);
|
||||
query: TQuery;
|
||||
store: TStore;
|
||||
}
|
||||
|
||||
export = BaseClient;
|
||||
48
types/sparql-http-client/Endpoint.d.ts
vendored
Normal file
48
types/sparql-http-client/Endpoint.d.ts
vendored
Normal file
@ -0,0 +1,48 @@
|
||||
declare namespace Endpoint {
|
||||
interface EndpointOptions {
|
||||
endpointUrl: string;
|
||||
fetch?: typeof fetch;
|
||||
headers?: HeadersInit;
|
||||
password?: string;
|
||||
storeUrl?: string;
|
||||
updateUrl?: string;
|
||||
user?: string;
|
||||
}
|
||||
|
||||
interface RequestOptions {
|
||||
headers?: HeadersInit;
|
||||
update?: boolean;
|
||||
}
|
||||
|
||||
interface Endpoint {
|
||||
endpointUrl: string;
|
||||
fetch: typeof fetch;
|
||||
headers: HeadersInit;
|
||||
password?: string;
|
||||
storeUrl?: string;
|
||||
updateUrl?: string;
|
||||
user?: string;
|
||||
|
||||
get(query: string, options?: RequestOptions): Promise<Response>;
|
||||
postDirect(query: string, options?: RequestOptions): Promise<Response>;
|
||||
postUrlencoded(query: string, options?: RequestOptions): Promise<Response>;
|
||||
mergeHeaders(args?: HeadersInit): Headers;
|
||||
}
|
||||
}
|
||||
|
||||
declare class Endpoint implements Endpoint.Endpoint {
|
||||
constructor(options: Endpoint.EndpointOptions);
|
||||
endpointUrl: string;
|
||||
fetch: typeof fetch;
|
||||
headers: HeadersInit;
|
||||
password?: string;
|
||||
storeUrl?: string;
|
||||
updateUrl?: string;
|
||||
user?: string;
|
||||
get(query: string, options?: Endpoint.RequestOptions): Promise<Response>;
|
||||
postDirect(query: string, options?: Endpoint.RequestOptions): Promise<Response>;
|
||||
postUrlencoded(query: string, options?: Endpoint.RequestOptions): Promise<Response>;
|
||||
mergeHeaders(args?: HeadersInit): Headers;
|
||||
}
|
||||
|
||||
export = Endpoint;
|
||||
15
types/sparql-http-client/ParsingClient.d.ts
vendored
Normal file
15
types/sparql-http-client/ParsingClient.d.ts
vendored
Normal file
@ -0,0 +1,15 @@
|
||||
import { BaseQuad, Quad } from 'rdf-js';
|
||||
import BaseClient = require('./BaseClient');
|
||||
import { EndpointOptions } from './Endpoint';
|
||||
import { ClientOptions, Client } from '.';
|
||||
import { ParsingQuery } from './ParsingQuery';
|
||||
|
||||
declare namespace ParsingClient {
|
||||
type ParsingClientOptions<Q extends BaseQuad = Quad> = EndpointOptions & Pick<ClientOptions<ParsingQuery, Q>, 'factory'>;
|
||||
}
|
||||
|
||||
declare class ParsingClient<Q extends BaseQuad = Quad> extends BaseClient<ParsingQuery<Q>, Q> implements Client<ParsingQuery<Q>, Q> {
|
||||
constructor(options: ParsingClient.ParsingClientOptions<Q>);
|
||||
}
|
||||
|
||||
export = ParsingClient;
|
||||
19
types/sparql-http-client/ParsingQuery.d.ts
vendored
Normal file
19
types/sparql-http-client/ParsingQuery.d.ts
vendored
Normal file
@ -0,0 +1,19 @@
|
||||
import { Quad, BaseQuad } from 'rdf-js';
|
||||
import { QueryOptions, Query, QueryInit } from '.';
|
||||
import { Endpoint } from './Endpoint';
|
||||
import { ResultRow } from './ResultParser';
|
||||
|
||||
declare namespace ParsingQuery {
|
||||
type ParsingQuery<Q extends BaseQuad = Quad> = Query<boolean, Q[], ResultRow[], void>;
|
||||
}
|
||||
|
||||
declare class ParsingQuery<Q extends BaseQuad = Quad> implements ParsingQuery.ParsingQuery<Q> {
|
||||
constructor(options: QueryInit);
|
||||
endpoint: Endpoint;
|
||||
ask(query: string, options?: QueryOptions): Promise<boolean>;
|
||||
construct(query: string, options?: QueryOptions): Promise<Q[]>;
|
||||
select(query: string, options?: QueryOptions): Promise<ResultRow[]>;
|
||||
update(query: string, options?: QueryOptions): Promise<void>;
|
||||
}
|
||||
|
||||
export = ParsingQuery;
|
||||
17
types/sparql-http-client/RawQuery.d.ts
vendored
Normal file
17
types/sparql-http-client/RawQuery.d.ts
vendored
Normal file
@ -0,0 +1,17 @@
|
||||
import { Endpoint } from './Endpoint';
|
||||
import { QueryOptions, QueryInit, Query } from '.';
|
||||
|
||||
declare namespace RawQuery {
|
||||
type RawQuery = Query<Response, Response, Response, Response>;
|
||||
}
|
||||
|
||||
declare class RawQuery {
|
||||
constructor(options: QueryInit);
|
||||
endpoint: Endpoint;
|
||||
ask(query: string, options?: QueryOptions): Promise<Response>;
|
||||
construct(query: string, options?: QueryOptions): Promise<Response>;
|
||||
select(query: string, options?: QueryOptions): Promise<Response>;
|
||||
update(query: string, options?: QueryOptions): Promise<Response>;
|
||||
}
|
||||
|
||||
export = RawQuery;
|
||||
16
types/sparql-http-client/ResultParser.d.ts
vendored
Normal file
16
types/sparql-http-client/ResultParser.d.ts
vendored
Normal file
@ -0,0 +1,16 @@
|
||||
import { Duplex } from 'stream';
|
||||
import { DataFactory, BaseQuad, Quad, Term } from 'rdf-js';
|
||||
|
||||
declare namespace ResultParser {
|
||||
type ResultRow = Record<string, Term>;
|
||||
|
||||
interface ResultParserInit<Q extends BaseQuad = Quad> {
|
||||
factory: DataFactory<Q>;
|
||||
}
|
||||
}
|
||||
|
||||
declare class ResultParser<Q extends BaseQuad = Quad> extends Duplex {
|
||||
constructor(options?: ResultParser.ResultParserInit<Q>)
|
||||
}
|
||||
|
||||
export = ResultParser;
|
||||
67
types/sparql-http-client/StreamClient.d.ts
vendored
Normal file
67
types/sparql-http-client/StreamClient.d.ts
vendored
Normal file
@ -0,0 +1,67 @@
|
||||
import { BaseQuad, Quad, DataFactory, Stream } from 'rdf-js';
|
||||
import BaseClient = require('./BaseClient');
|
||||
import StreamQuery = require('./StreamQuery');
|
||||
import StreamStore = require('./StreamStore');
|
||||
import { Endpoint, EndpointOptions } from './Endpoint';
|
||||
|
||||
interface Constructor<T, Q extends BaseQuad = Quad> {
|
||||
new (options: { endpoint: Endpoint; factory: DataFactory<Q>; }): T;
|
||||
}
|
||||
|
||||
declare namespace StreamClient {
|
||||
interface QueryOptions {
|
||||
headers?: HeadersInit;
|
||||
operation?: 'get' | 'postUrlencoded' | 'postDirect';
|
||||
}
|
||||
|
||||
interface QueryInit {
|
||||
endpoint: Endpoint;
|
||||
}
|
||||
|
||||
interface SelectQuery<TSelect> {
|
||||
select(query: string, options?: QueryOptions): Promise<TSelect>;
|
||||
}
|
||||
|
||||
interface ConstructQuery<TConstruct> {
|
||||
construct(query: string, options?: QueryOptions): Promise<TConstruct>;
|
||||
}
|
||||
|
||||
interface AskQuery<TAsk> {
|
||||
ask(query: string, options?: QueryOptions): Promise<TAsk>;
|
||||
}
|
||||
|
||||
interface UpdateQuery<TUpdate> {
|
||||
update(query: string, options?: QueryOptions): Promise<TUpdate>;
|
||||
}
|
||||
|
||||
interface Query<TAsk = any, TConstruct = any, TSelect = any, TUpdate = any> extends AskQuery<TAsk>, SelectQuery<TSelect>, ConstructQuery<TConstruct>, UpdateQuery<TUpdate> {
|
||||
endpoint: Endpoint;
|
||||
}
|
||||
|
||||
interface Store<Q extends BaseQuad = Quad> {
|
||||
endpoint: Endpoint;
|
||||
get(graph: Quad['graph']): Promise<Stream<Q>>;
|
||||
post(stream: Stream): Promise<void>;
|
||||
put(stream: Stream): Promise<void>;
|
||||
}
|
||||
|
||||
interface ClientOptions<TQuery extends Query, Q extends BaseQuad = Quad, TStore extends Store<Q> = never> {
|
||||
endpoint: Endpoint;
|
||||
factory?: DataFactory<Q>;
|
||||
Query?: Constructor<TQuery, Q>;
|
||||
Store?: Constructor<TStore, Q>;
|
||||
}
|
||||
|
||||
type StreamClientOptions<Q extends BaseQuad = Quad> = EndpointOptions & Pick<ClientOptions<StreamQuery, Q, StreamStore<Q>>, 'factory'>;
|
||||
|
||||
interface Client<TQuery extends Query, Q extends BaseQuad = Quad, TStore extends Store<Q> = never> {
|
||||
query: TQuery;
|
||||
store: TStore;
|
||||
}
|
||||
}
|
||||
|
||||
declare class StreamClient<Q extends BaseQuad = Quad> extends BaseClient<StreamQuery<Q>, Q, StreamStore<Q>> implements StreamClient.Client<StreamQuery<Q>, Q, StreamStore<Q>> {
|
||||
constructor(options: StreamClient.StreamClientOptions<Q>)
|
||||
}
|
||||
|
||||
export = StreamClient;
|
||||
23
types/sparql-http-client/StreamQuery.d.ts
vendored
Normal file
23
types/sparql-http-client/StreamQuery.d.ts
vendored
Normal file
@ -0,0 +1,23 @@
|
||||
import { DataFactory, BaseQuad, Quad, Stream } from 'rdf-js';
|
||||
import { QueryOptions, Query, QueryInit } from '.';
|
||||
import { Endpoint } from './Endpoint';
|
||||
import { Readable } from 'stream';
|
||||
|
||||
declare namespace StreamQuery {
|
||||
interface StreamQueryInit<Q extends BaseQuad = Quad> extends QueryInit {
|
||||
factory?: DataFactory<Q>;
|
||||
}
|
||||
|
||||
type StreamQuery<Q extends BaseQuad = Quad> = Query<boolean, Stream<Q>, Readable, void>;
|
||||
}
|
||||
|
||||
declare class StreamQuery<Q extends BaseQuad = Quad> implements StreamQuery.StreamQuery<Q> {
|
||||
constructor(options: StreamQuery.StreamQueryInit<Q>);
|
||||
endpoint: Endpoint;
|
||||
ask(query: string, options?: QueryOptions): Promise<boolean>;
|
||||
construct(query: string, options?: QueryOptions): Promise<Stream<Q>>;
|
||||
select(query: string, options?: QueryOptions): Promise<Readable>;
|
||||
update(query: string, options?: QueryOptions): Promise<void>;
|
||||
}
|
||||
|
||||
export = StreamQuery;
|
||||
20
types/sparql-http-client/StreamStore.d.ts
vendored
Normal file
20
types/sparql-http-client/StreamStore.d.ts
vendored
Normal file
@ -0,0 +1,20 @@
|
||||
import { Store } from "./";
|
||||
import { BaseQuad, Quad, DataFactory, Stream } from "rdf-js";
|
||||
import Endpoint = require("./Endpoint");
|
||||
|
||||
declare namespace StreamStore {
|
||||
interface StreamStoreInit<Q extends BaseQuad = Quad> {
|
||||
endpoint: Endpoint;
|
||||
factor?: DataFactory<Q>;
|
||||
}
|
||||
}
|
||||
|
||||
declare class StreamStore<Q extends BaseQuad = Quad> implements Store<Q> {
|
||||
constructor(options: StreamStore.StreamStoreInit<Q>)
|
||||
endpoint: Endpoint;
|
||||
get(graph: Quad['graph']): Promise<Stream<Q>>;
|
||||
post(stream: Stream): Promise<void>;
|
||||
put(stream: Stream): Promise<void>;
|
||||
}
|
||||
|
||||
export = StreamStore;
|
||||
50
types/sparql-http-client/index.d.ts
vendored
50
types/sparql-http-client/index.d.ts
vendored
@ -1,53 +1,9 @@
|
||||
// Type definitions for sparql-http-client 1.2
|
||||
// Type definitions for sparql-http-client 2.2
|
||||
// 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.6
|
||||
|
||||
import { Term } from 'rdf-js';
|
||||
import { URL } from 'url';
|
||||
import Client = require('./StreamClient');
|
||||
|
||||
declare namespace SparqlHttp {
|
||||
interface SparqlHttpClient<TResponse extends Response = Response> {
|
||||
updateQuery(query: string, options?: QueryRequestInit): Promise<Response>;
|
||||
selectQuery(query: string, options?: QueryRequestInit): Promise<SelectResponse & TResponse>;
|
||||
constructQuery(query: string, options?: QueryRequestInit): Promise<TResponse>;
|
||||
}
|
||||
|
||||
interface SparqlHttpOptions {
|
||||
endpointUrl?: string;
|
||||
updateUrl?: string;
|
||||
}
|
||||
|
||||
interface SparqlClientOptions extends SparqlHttpOptions {
|
||||
fetch?: typeof fetch;
|
||||
URL?: typeof URL;
|
||||
defaultHeaders?: HeadersInit;
|
||||
}
|
||||
|
||||
interface QueryRequestInit extends SparqlHttpOptions, RequestInit {}
|
||||
|
||||
interface SelectBindings {
|
||||
results: { bindings: ReadonlyArray<Record<string, Term>> };
|
||||
}
|
||||
|
||||
interface AskResult {
|
||||
boolean: boolean;
|
||||
}
|
||||
|
||||
interface SelectResponse {
|
||||
json(): Promise<SelectBindings & AskResult>;
|
||||
}
|
||||
}
|
||||
|
||||
declare class SparqlHttp<TResponse extends Response = Response> implements SparqlHttp.SparqlHttpClient<TResponse> {
|
||||
constructor(options?: SparqlHttp.SparqlClientOptions);
|
||||
|
||||
updateQuery(query: string, options?: SparqlHttp.QueryRequestInit): Promise<Response>;
|
||||
|
||||
selectQuery(query: string, options?: SparqlHttp.QueryRequestInit): Promise<SparqlHttp.SelectResponse & TResponse>;
|
||||
|
||||
constructQuery(query: string, options?: SparqlHttp.QueryRequestInit): Promise<TResponse>;
|
||||
}
|
||||
|
||||
export = SparqlHttp;
|
||||
export = Client;
|
||||
|
||||
@ -1,61 +1,174 @@
|
||||
import * as SparqlHttp from 'sparql-http-client';
|
||||
import StreamClient = require('sparql-http-client');
|
||||
import Endpoint = require('sparql-http-client/Endpoint');
|
||||
import BaseClient = require('sparql-http-client/BaseClient');
|
||||
import ParsingClient = require('sparql-http-client/ParsingClient');
|
||||
import RawQuery = require('sparql-http-client/RawQuery');
|
||||
import { Client } from 'sparql-http-client';
|
||||
import { Readable } from 'stream';
|
||||
import { DataFactory, Stream, Quad, NamedNode, Term } from 'rdf-js';
|
||||
|
||||
const client: SparqlHttp.SparqlHttpClient = new SparqlHttp();
|
||||
interface TestQuad extends Quad {
|
||||
toCanonical(): string;
|
||||
}
|
||||
|
||||
async function selectQuery() {
|
||||
const response = await client.selectQuery('...', {
|
||||
endpointUrl: 'http://example.com/endpoint',
|
||||
const endpointUrl = '';
|
||||
const query = '';
|
||||
const factory: DataFactory<TestQuad> = <any> {};
|
||||
const headers: HeadersInit = <any> {};
|
||||
const password: string = <any> {};
|
||||
const user: string = <any> {};
|
||||
const storeUrl: string = <any> {};
|
||||
const updateUrl: string = <any> {};
|
||||
const graph: NamedNode = <any> {};
|
||||
const stream: Stream = <any> {};
|
||||
|
||||
const endpoint: Endpoint = new Endpoint({
|
||||
endpointUrl,
|
||||
fetch,
|
||||
headers,
|
||||
password,
|
||||
user,
|
||||
storeUrl,
|
||||
updateUrl,
|
||||
});
|
||||
|
||||
async function streamingClient() {
|
||||
// construct
|
||||
const minimalOptions: StreamClient = new StreamClient({
|
||||
endpointUrl,
|
||||
});
|
||||
const fullOptions: StreamClient<TestQuad> = new StreamClient({
|
||||
endpointUrl,
|
||||
factory,
|
||||
fetch,
|
||||
headers,
|
||||
password,
|
||||
user,
|
||||
storeUrl,
|
||||
updateUrl,
|
||||
});
|
||||
|
||||
const json = await response.json();
|
||||
let endpoint: Endpoint = fullOptions.query.endpoint;
|
||||
endpoint = fullOptions.store.endpoint;
|
||||
|
||||
json.results.bindings.forEach(binding => {
|
||||
console.log(`${binding.name.value} (${binding.name.termType})`);
|
||||
// query.ask
|
||||
const askNoOptions: boolean = await fullOptions.query.ask(query);
|
||||
const askFullOptions: boolean = await fullOptions.query.ask(query, {
|
||||
headers,
|
||||
operation: 'postDirect'
|
||||
});
|
||||
|
||||
// query.select
|
||||
const selectNoOptions: Readable = await fullOptions.query.select(query);
|
||||
const selectFullOptions: Readable = await fullOptions.query.select(query, {
|
||||
headers,
|
||||
operation: 'postUrlencoded'
|
||||
});
|
||||
|
||||
// query.construct
|
||||
const constructNoOptions: Stream<TestQuad> = await fullOptions.query.construct(query);
|
||||
const constructFullOptions: Stream<TestQuad> = await fullOptions.query.construct(query, {
|
||||
headers,
|
||||
operation: 'get'
|
||||
});
|
||||
|
||||
// query.update
|
||||
const updateNoOptions: Promise<void> = fullOptions.query.update(query);
|
||||
const updateFullOptions: Promise<void> = fullOptions.query.update(query, {
|
||||
headers,
|
||||
operation: 'get'
|
||||
});
|
||||
|
||||
// store.get
|
||||
const get: Stream<TestQuad> = await fullOptions.store.get(graph);
|
||||
|
||||
// store.put
|
||||
const put: Promise<void> = fullOptions.store.put(stream);
|
||||
|
||||
// store.post
|
||||
const post: Promise<void> = fullOptions.store.post(stream);
|
||||
}
|
||||
|
||||
async function parsingClient() {
|
||||
// construct
|
||||
const minimalOptions: ParsingClient = new ParsingClient({
|
||||
endpointUrl,
|
||||
});
|
||||
const fullOptions: ParsingClient<TestQuad> = new ParsingClient({
|
||||
endpointUrl,
|
||||
factory,
|
||||
fetch,
|
||||
headers,
|
||||
password,
|
||||
user,
|
||||
storeUrl,
|
||||
updateUrl,
|
||||
});
|
||||
|
||||
const endpoint: Endpoint = fullOptions.query.endpoint;
|
||||
const store: never = fullOptions.store;
|
||||
|
||||
// query.ask
|
||||
const askNoOptions: boolean = await fullOptions.query.ask(query);
|
||||
const askFullOptions: boolean = await fullOptions.query.ask(query, {
|
||||
headers,
|
||||
operation: 'postDirect'
|
||||
});
|
||||
|
||||
// query.select
|
||||
const selectNoOptions: Array<Record<string, Term>> = await fullOptions.query.select(query);
|
||||
const selectFullOptions: Array<Record<string, Term>> = await fullOptions.query.select(query, {
|
||||
headers,
|
||||
operation: 'postUrlencoded'
|
||||
});
|
||||
|
||||
// query.construct
|
||||
const constructNoOptions: TestQuad[] = await fullOptions.query.construct(query);
|
||||
const constructFullOptions: TestQuad[] = await fullOptions.query.construct(query, {
|
||||
headers,
|
||||
operation: 'get'
|
||||
});
|
||||
|
||||
// query.update
|
||||
const updateNoOptions: Promise<void> = fullOptions.query.update(query);
|
||||
const updateFullOptions: Promise<void> = fullOptions.query.update(query, {
|
||||
headers,
|
||||
operation: 'get'
|
||||
});
|
||||
}
|
||||
|
||||
async function askQuery() {
|
||||
const response = await client.selectQuery('...', {
|
||||
endpointUrl: 'http://example.com/endpoint',
|
||||
async function testBaseClient_RawQuery() {
|
||||
// construct
|
||||
const client: Client<RawQuery, TestQuad> = new BaseClient<RawQuery, TestQuad>({
|
||||
endpoint,
|
||||
Query: RawQuery,
|
||||
});
|
||||
|
||||
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',
|
||||
// query.ask
|
||||
const askNoOptions: Response = await client.query.ask(query);
|
||||
const askFullOptions: Response = await client.query.ask(query, {
|
||||
headers,
|
||||
operation: 'postDirect'
|
||||
});
|
||||
|
||||
console.log(`The update ${response.ok ? 'succeeded' : 'faield'}`);
|
||||
}
|
||||
// query.select
|
||||
const selectNoOptions: Response = await client.query.select(query);
|
||||
const selectFullOptions: Response = await client.query.select(query, {
|
||||
headers,
|
||||
operation: 'postUrlencoded'
|
||||
});
|
||||
|
||||
async function oneTimeAuth() {
|
||||
const endpoint = new SparqlHttp({endpointUrl: 'http://example.com/endpoint'});
|
||||
// query.construct
|
||||
const constructNoOptions: Response = await client.query.construct(query);
|
||||
const constructFullOptions: Response = await client.query.construct(query, {
|
||||
headers,
|
||||
operation: 'get'
|
||||
});
|
||||
|
||||
// authorize a single query
|
||||
endpoint.selectQuery('query', {
|
||||
headers: {
|
||||
Authorization: 'Bearer token'
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
async function createAuthorizedCLient() {
|
||||
new SparqlHttp({
|
||||
endpointUrl: 'http://example.com/endpoint',
|
||||
defaultHeaders: {
|
||||
Authorization: 'Bearer token'
|
||||
}
|
||||
// query.update
|
||||
const updateNoOptions: Response = await client.query.update(query);
|
||||
const updateFullOptions: Response = await client.query.update(query, {
|
||||
headers,
|
||||
operation: 'get'
|
||||
});
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user