mirror of
https://github.com/zhigang1992/DefinitelyTyped.git
synced 2026-06-04 19:42:46 +08:00
Add declarations for 'sparqljs' (#20226)
This commit is contained in:
committed by
Ryan Cavanaugh
parent
64ea75d1c8
commit
a6954cc035
247
types/sparqljs/index.d.ts
vendored
Normal file
247
types/sparqljs/index.d.ts
vendored
Normal file
@@ -0,0 +1,247 @@
|
||||
// Type definitions for sparqljs 1.5
|
||||
// Project: https://github.com/RubenVerborgh/SPARQL.js
|
||||
// Definitions by: Alexey Morozov <https://github.com/AlexeyMz>
|
||||
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
|
||||
// TypeScript Version: 2.1
|
||||
|
||||
export const Parser: {
|
||||
new (
|
||||
prefixes?: { [prefix: string]: string },
|
||||
baseIRI?: string,
|
||||
options?: ParserOptions,
|
||||
): SparqlParser;
|
||||
};
|
||||
|
||||
export const Generator: {
|
||||
new (options?: GeneratorOptions): SparqlGenerator;
|
||||
};
|
||||
|
||||
export interface ParserOptions {
|
||||
/** @default true */
|
||||
collapseGroups?: boolean;
|
||||
}
|
||||
|
||||
export interface GeneratorOptions {
|
||||
allPrefixes?: boolean;
|
||||
}
|
||||
|
||||
export interface SparqlParser {
|
||||
parse(query: string): SparqlQuery;
|
||||
}
|
||||
|
||||
export interface SparqlGenerator {
|
||||
stringify(query: SparqlQuery): string;
|
||||
}
|
||||
|
||||
export type SparqlQuery = Query | Update;
|
||||
|
||||
export type Query = SelectQuery | ConstructQuery | AskQuery | DescribeQuery;
|
||||
|
||||
export interface BaseQuery {
|
||||
type: 'query';
|
||||
base?: string;
|
||||
prefixes: { [prefix: string]: string; };
|
||||
where?: Pattern[];
|
||||
values?: ValuePatternRow[];
|
||||
}
|
||||
|
||||
export interface SelectQuery extends BaseQuery {
|
||||
queryType: 'SELECT';
|
||||
variables: Variable[] | ['*'];
|
||||
distinct?: boolean;
|
||||
from?: {
|
||||
default: string[];
|
||||
named: string[];
|
||||
};
|
||||
reduced?: boolean;
|
||||
group?: Grouping[];
|
||||
having?: Expression[];
|
||||
order?: Ordering[];
|
||||
limit?: number;
|
||||
offset?: number;
|
||||
}
|
||||
|
||||
export interface Grouping {
|
||||
expression: Expression;
|
||||
}
|
||||
|
||||
export interface Ordering {
|
||||
expression: Expression;
|
||||
descending?: boolean;
|
||||
}
|
||||
|
||||
export interface ConstructQuery extends BaseQuery {
|
||||
queryType: 'CONSTRUCT';
|
||||
template?: Triple[];
|
||||
}
|
||||
|
||||
export interface AskQuery extends BaseQuery {
|
||||
queryType: 'ASK';
|
||||
}
|
||||
|
||||
export interface DescribeQuery extends BaseQuery {
|
||||
queryType: 'DESCRIBE';
|
||||
variables: Variable[] | ['*'];
|
||||
}
|
||||
|
||||
export interface Update {
|
||||
type: 'update';
|
||||
prefixes: { [prefix: string]: string; };
|
||||
updates: UpdateOperation[];
|
||||
}
|
||||
|
||||
export type UpdateOperation = InsertDeleteOperation | ManagementOperation;
|
||||
|
||||
export interface InsertDeleteOperation {
|
||||
updateType: 'insert' | 'delete' | 'deletewhere' | 'insertdelete';
|
||||
insert?: Quads[];
|
||||
delete?: Quads[];
|
||||
where?: Pattern[];
|
||||
}
|
||||
|
||||
export type Quads = BgpPattern | GraphQuads;
|
||||
|
||||
export interface ManagementOperation {
|
||||
type: 'load' | 'copy' | 'move' | 'add';
|
||||
silent: boolean;
|
||||
source: string | {
|
||||
type: 'graph';
|
||||
default: boolean;
|
||||
};
|
||||
destination?: string | {
|
||||
type: 'graph';
|
||||
name: string;
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Examples: '?var', '*',
|
||||
* SELECT (?a as ?b) ... ==> { expression: '?a', variable: '?b' }
|
||||
*/
|
||||
export type Variable = VariableExpression | Term;
|
||||
|
||||
export interface VariableExpression {
|
||||
expression: Expression;
|
||||
variable: Term;
|
||||
}
|
||||
|
||||
export type Pattern =
|
||||
| BgpPattern
|
||||
| BlockPattern
|
||||
| GraphPattern
|
||||
| ServicePattern
|
||||
| FilterPattern
|
||||
| BindPattern
|
||||
| ValuesPattern
|
||||
| SelectQuery;
|
||||
|
||||
/**
|
||||
* Basic Graph Pattern
|
||||
*/
|
||||
export interface BgpPattern {
|
||||
type: 'bgp';
|
||||
triples: Triple[];
|
||||
}
|
||||
|
||||
export interface GraphQuads {
|
||||
type: 'graph';
|
||||
name: Term;
|
||||
triples: Triple[];
|
||||
}
|
||||
|
||||
export interface BlockPattern {
|
||||
type: 'optional' | 'union' | 'group' | 'minus' | 'graph' | 'service';
|
||||
patterns: Pattern[];
|
||||
}
|
||||
|
||||
export interface GroupPattern extends BlockPattern {
|
||||
type: 'group';
|
||||
}
|
||||
|
||||
export interface GraphPattern extends BlockPattern {
|
||||
type: 'graph';
|
||||
name: Term;
|
||||
}
|
||||
|
||||
export interface ServicePattern extends BlockPattern {
|
||||
type: 'service';
|
||||
name: Term;
|
||||
silent: boolean;
|
||||
}
|
||||
|
||||
export interface FilterPattern {
|
||||
type: 'filter';
|
||||
expression: Expression;
|
||||
}
|
||||
|
||||
export interface BindPattern {
|
||||
type: 'bind';
|
||||
expression: Expression;
|
||||
variable: Term;
|
||||
}
|
||||
|
||||
export interface ValuesPattern {
|
||||
type: 'values';
|
||||
values: ValuePatternRow[];
|
||||
}
|
||||
|
||||
export interface ValuePatternRow {
|
||||
[variable: string]: Term;
|
||||
}
|
||||
|
||||
/**
|
||||
* Either '?var', 'schema:iri', '_:blankNode',
|
||||
* '"literal"^^<schema:datatype>' or '{undefined}'.
|
||||
*
|
||||
* Term is a nominal type based on string.
|
||||
*/
|
||||
export type Term = string & { __termBrand: string; };
|
||||
|
||||
export interface Triple {
|
||||
subject: Term;
|
||||
predicate: PropertyPath | Term;
|
||||
object: Term;
|
||||
}
|
||||
|
||||
export interface PropertyPath {
|
||||
type: 'path';
|
||||
pathType: '|' | '/' | '^' | '+' | '*' | '!';
|
||||
items: Array<PropertyPath | Term>;
|
||||
}
|
||||
|
||||
export type Expression =
|
||||
| OperationExpression
|
||||
| FunctionCallExpression
|
||||
| AggregateExpression
|
||||
| BgpPattern
|
||||
| GroupPattern
|
||||
| Tuple
|
||||
| Term;
|
||||
|
||||
// allow Expression circularly reference itself
|
||||
// tslint:disable-next-line no-empty-interface
|
||||
export interface Tuple extends Array<Expression> {}
|
||||
|
||||
export interface BaseExpression {
|
||||
type: string;
|
||||
distinct?: boolean;
|
||||
}
|
||||
|
||||
export interface OperationExpression extends BaseExpression {
|
||||
type: 'operation';
|
||||
operator: string;
|
||||
args: Expression[];
|
||||
}
|
||||
|
||||
export interface FunctionCallExpression extends BaseExpression {
|
||||
type: 'functionCall';
|
||||
function: string;
|
||||
args: Expression[];
|
||||
}
|
||||
|
||||
export interface AggregateExpression extends BaseExpression {
|
||||
type: 'aggregate';
|
||||
expression: Expression;
|
||||
aggregation: string;
|
||||
separator?: string;
|
||||
}
|
||||
124
types/sparqljs/sparqljs-tests.ts
Normal file
124
types/sparqljs/sparqljs-tests.ts
Normal file
@@ -0,0 +1,124 @@
|
||||
import * as SparqlJs from 'sparqljs';
|
||||
|
||||
/**
|
||||
* Examples from the project's README
|
||||
*/
|
||||
function officialExamples() {
|
||||
// Parse a SPARQL query to a JSON object
|
||||
const SparqlParser = SparqlJs.Parser;
|
||||
const parser = new SparqlParser();
|
||||
const parsedQuery = parser.parse(
|
||||
'PREFIX foaf: <http://xmlns.com/foaf/0.1/> ' +
|
||||
'SELECT * { ?mickey foaf:name "Mickey Mouse"@en; foaf:knows ?other. }',
|
||||
);
|
||||
|
||||
// Regenerate a SPARQL query from a JSON object
|
||||
const SparqlGenerator = SparqlJs.Generator;
|
||||
const generator = new SparqlGenerator();
|
||||
if (parsedQuery.type === 'query' && parsedQuery.queryType === 'SELECT') {
|
||||
parsedQuery.variables = ['?mickey' as SparqlJs.Term];
|
||||
}
|
||||
|
||||
// $ExpectType string
|
||||
generator.stringify(parsedQuery);
|
||||
}
|
||||
|
||||
function advancedOptions() {
|
||||
const parser = new SparqlJs.Parser(
|
||||
{rdf: 'http://www.w3.org/1999/02/22-rdf-syntax-ns#'},
|
||||
'http://example.com',
|
||||
{collapseGroups: true}
|
||||
);
|
||||
const generator = new SparqlJs.Generator({allPrefixes: false});
|
||||
}
|
||||
|
||||
/**
|
||||
* Basic query structure
|
||||
*/
|
||||
function basicQueries() {
|
||||
const foo = 'example:foo' as SparqlJs.Term;
|
||||
const bar = 'example:bar' as SparqlJs.Term;
|
||||
const qux = 'example:qux' as SparqlJs.Term;
|
||||
|
||||
const prefixes = {rdf: 'http://www.w3.org/1999/02/22-rdf-syntax-ns#'};
|
||||
|
||||
const bgpPattern: SparqlJs.BgpPattern = {
|
||||
type: 'bgp',
|
||||
triples: [
|
||||
{subject: foo, predicate: qux, object: bar},
|
||||
{
|
||||
subject: foo,
|
||||
predicate: {
|
||||
type: 'path',
|
||||
pathType: '|',
|
||||
items: [qux, bar],
|
||||
},
|
||||
object: bar,
|
||||
}
|
||||
],
|
||||
};
|
||||
|
||||
const select: SparqlJs.SelectQuery = {
|
||||
type: 'query',
|
||||
queryType: 'SELECT',
|
||||
prefixes,
|
||||
variables: ['*'],
|
||||
distinct: true,
|
||||
from: {
|
||||
default: ['http://example.com/'],
|
||||
named: ['http://example.com/foo', 'http://example.com/bar'],
|
||||
},
|
||||
reduced: false,
|
||||
group: [
|
||||
{expression: foo},
|
||||
{expression: bar},
|
||||
],
|
||||
having: [{
|
||||
type: 'functionCall',
|
||||
function: 'isIRI',
|
||||
args: [foo],
|
||||
}],
|
||||
order: [{
|
||||
expression: bar,
|
||||
descending: true,
|
||||
}],
|
||||
limit: 100,
|
||||
offset: 10,
|
||||
where: [bgpPattern],
|
||||
values: [
|
||||
{x: foo, y: bar},
|
||||
{x: foo},
|
||||
]
|
||||
};
|
||||
|
||||
const construct: SparqlJs.ConstructQuery = {
|
||||
type: 'query',
|
||||
queryType: 'CONSTRUCT',
|
||||
base: 'http://example.com',
|
||||
prefixes,
|
||||
template: bgpPattern.triples,
|
||||
};
|
||||
|
||||
const ask: SparqlJs.AskQuery = {
|
||||
type: 'query',
|
||||
queryType: 'ASK',
|
||||
prefixes,
|
||||
};
|
||||
|
||||
const describe: SparqlJs.DescribeQuery = {
|
||||
type: 'query',
|
||||
queryType: 'DESCRIBE',
|
||||
prefixes,
|
||||
variables: [
|
||||
foo,
|
||||
{
|
||||
variable: bar,
|
||||
expression: {
|
||||
type: 'operation',
|
||||
operator: '+',
|
||||
args: [foo, bar],
|
||||
}
|
||||
}
|
||||
],
|
||||
};
|
||||
}
|
||||
22
types/sparqljs/tsconfig.json
Normal file
22
types/sparqljs/tsconfig.json
Normal file
@@ -0,0 +1,22 @@
|
||||
{
|
||||
"compilerOptions": {
|
||||
"module": "commonjs",
|
||||
"lib": [
|
||||
"es6"
|
||||
],
|
||||
"noImplicitAny": true,
|
||||
"noImplicitThis": true,
|
||||
"strictNullChecks": true,
|
||||
"baseUrl": "../",
|
||||
"typeRoots": [
|
||||
"../"
|
||||
],
|
||||
"types": [],
|
||||
"noEmit": true,
|
||||
"forceConsistentCasingInFileNames": true
|
||||
},
|
||||
"files": [
|
||||
"index.d.ts",
|
||||
"sparqljs-tests.ts"
|
||||
]
|
||||
}
|
||||
1
types/sparqljs/tslint.json
Normal file
1
types/sparqljs/tslint.json
Normal file
@@ -0,0 +1 @@
|
||||
{ "extends": "dtslint/dt.json" }
|
||||
Reference in New Issue
Block a user