mirror of
https://github.com/FlipsideCrypto/DefinitelyTyped.git
synced 2026-02-06 10:56:53 +00:00
Added typings for jsonld.js https://www.npmjs.com/package/jsonld
This commit is contained in:
parent
836c2fa859
commit
9485087d51
217
types/jsonld/index.d.ts
vendored
Normal file
217
types/jsonld/index.d.ts
vendored
Normal file
@ -0,0 +1,217 @@
|
||||
// Type definitions for jsonld 1.5
|
||||
// Project: https://github.com/digitalbazaar/jsonld.js
|
||||
// Definitions by: Jonas Erbe <https://github.com/jason076>
|
||||
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
|
||||
// TypeScript Version: 2.6
|
||||
|
||||
import { Context, JsonLd, Document, Url, JsonLdProcessor, RemoteDocument } from './jsonld-spec';
|
||||
|
||||
// Some typealiases for better readability and some placeholders
|
||||
type MimeNQuad = 'application/n-quads';
|
||||
type RdfDataSet = any;
|
||||
type RdfOrString = RdfDataSet|string;
|
||||
type Callback<T> = (err: Error, res: T) => void;
|
||||
type DocCallback = Callback<JsonLd>;
|
||||
|
||||
/*
|
||||
* Declares interfaces used to type the methods options object.
|
||||
* The interfaces are usefull to avoid code replication.
|
||||
*/
|
||||
|
||||
declare namespace Options {
|
||||
interface DocLoader {
|
||||
documentLoader?: (url: Url,
|
||||
callback: (err: Error, remoteDoc: RemoteDocument) => void)
|
||||
=> Promise<RemoteDocument>;
|
||||
}
|
||||
|
||||
interface Common extends DocLoader {
|
||||
base?: string;
|
||||
expandContext?: Context;
|
||||
}
|
||||
|
||||
interface ExpMap {
|
||||
// TODO: Figure out type of info
|
||||
expansionMap?: (info: any) => any;
|
||||
}
|
||||
|
||||
interface Compact extends Common, ExpMap {
|
||||
compactArrays?: boolean;
|
||||
appropriate?: boolean;
|
||||
compactToRelative?: boolean;
|
||||
graph?: boolean;
|
||||
skipExpansion?: boolean;
|
||||
expansion?: boolean;
|
||||
framing?: boolean;
|
||||
// TODO: Figure out type of info
|
||||
compactionMap?: (info: any) => void;
|
||||
}
|
||||
|
||||
interface Expand extends Common, ExpMap {
|
||||
keepFreeFloatingNodes?: boolean;
|
||||
}
|
||||
|
||||
type Flatten = Common;
|
||||
|
||||
interface Frame {
|
||||
embed?: '@last'| '@always'|'@never'|'@link';
|
||||
explicit?: boolean;
|
||||
requireAll?: boolean;
|
||||
omitDefault?: boolean;
|
||||
}
|
||||
|
||||
interface Normalize extends Common {
|
||||
algorithm?: 'URDNA2015' | `URGNA2012`;
|
||||
skipExpansion?: boolean;
|
||||
expansion?: boolean;
|
||||
inputFormat?: MimeNQuad;
|
||||
format?: MimeNQuad;
|
||||
useNative?: boolean;
|
||||
}
|
||||
|
||||
interface FromRdf {
|
||||
format?: MimeNQuad;
|
||||
rdfParser?: any;
|
||||
useRdfType?: boolean;
|
||||
useNativeTypes?: boolean;
|
||||
}
|
||||
|
||||
interface ToRdf extends Common {
|
||||
skipExpansion?: boolean;
|
||||
format?: MimeNQuad;
|
||||
produceGeneralizedRdf?: boolean;
|
||||
}
|
||||
|
||||
// TODO Complete and uncomment if needed (see comments at the end of the file)
|
||||
/* NOT USED AT THE MOMENT
|
||||
// type Link = Common;
|
||||
interface Issuer {
|
||||
|
||||
issuer?: IdentifierIssuer; // a jsonld.IdentifierIssuer to use to label blank nodes.
|
||||
}
|
||||
|
||||
type CreateNodeMap = Common&Issuer;
|
||||
|
||||
interface Merge extends Common, Issuer{
|
||||
|
||||
mergeNodes?: boolean; //true to merge properties for nodes with the same ID,
|
||||
//false to ignore new properties for nodes with the same ID once
|
||||
//the ID has been defined; note that this may not prevent merging
|
||||
//new properties where a node is in the `object` position
|
||||
//(default: true).
|
||||
}
|
||||
|
||||
interface Get {
|
||||
documentLoader?: DocLoader; // the document loader to use.
|
||||
}
|
||||
|
||||
type ProcessContext = DocLoader;
|
||||
*/
|
||||
}
|
||||
|
||||
export function compact(input: Document, ctx: Context, options: Options.Compact, callback: DocCallback): void;
|
||||
export function compact(input: Document, ctx: Context, callback: DocCallback): void;
|
||||
export function compact(input: Document, ctx: Context, options?: Options.Compact): Promise<JsonLd>;
|
||||
|
||||
export function expand(input: Document, options: Options.Expand, callback: DocCallback): void;
|
||||
export function expand(input: Document, callback: DocCallback): void;
|
||||
export function expand(input: Document, options?: Options.Expand): Promise<JsonLd>;
|
||||
|
||||
export function flatten(input: Document, ctx: Context, options: Options.Flatten, callback: DocCallback): void;
|
||||
export function flatten(input: Document, ctx: Context, callback: DocCallback): void;
|
||||
export function flatten(input: Document, ctx: Context, options?: Options.Flatten): Promise<JsonLd>;
|
||||
|
||||
export function frame(input: Document, frame: Document, options: Options.Frame, callback: DocCallback): void;
|
||||
export function frame(input: Document, frame: Document, callback: DocCallback): void;
|
||||
export function frame(input: Document, frame: Document, options?: Options.Frame): Promise<JsonLd>;
|
||||
|
||||
export function normalize(input: Document, options: Options.Normalize, callback: DocCallback): void;
|
||||
export function normalize(input: Document, callback: DocCallback): void;
|
||||
export function normalize(input: Document, options?: Options.Normalize): Promise<JsonLd>;
|
||||
|
||||
export function fromRDF(dataset: RdfOrString, options: Options.FromRdf, callback: DocCallback): void;
|
||||
export function fromRDF(dataset: RdfOrString, callback: DocCallback): void;
|
||||
export function fromRDF(dataset: RdfOrString, options?: Options.FromRdf): Promise<JsonLd>;
|
||||
|
||||
export function toRDF(input: Document, callback: Callback<RdfDataSet>): void;
|
||||
export function toRDF(input: Document, options: Options.ToRdf, callback: Callback<RdfDataSet>): void;
|
||||
export function toRDF(input: Document, options?: Options.ToRdf): Promise<RdfDataSet>;
|
||||
|
||||
export let JsonLdProcessor: JsonLdProcessor;
|
||||
|
||||
// disable autoexport
|
||||
export {};
|
||||
|
||||
// TODO: Complete and export the following types if needed!
|
||||
// ************************************************************************************
|
||||
// Not exported because of experimental state
|
||||
// export function link(input: Document, ctx: Context, options: Options.Link, callback: DocCallback): void;
|
||||
// export function link(input: Document, ctx: Context, callback: DocCallback): void;
|
||||
// export function link(input: Document, ctx: Context, options?: Options.Link): Promise<JsonLd>;
|
||||
|
||||
// Not exported because of experimental state
|
||||
// export function createNodeMapn(input: Document, options: Options.CreateNodeMap, callback: Callback<MegedNodeMap>): void;
|
||||
// export function createNodeMapn(input: Document, callback: Callback<MegedNodeMap>): void;
|
||||
// export function createNodeMapn(input: Document, options: Options.CreateNodeMap): Promise<MegedNodeMap>;
|
||||
|
||||
// Not exported because of experimental state
|
||||
// export function merge (docs: Document, ctx: Context, options: Options.Merge, callback: DocCallback): void;
|
||||
// export function merge (docs: Document, ctx: Context, callback: DocCallback): void;
|
||||
// export function merge (docs: Document, ctx: Context, options: Options.Merge): Promise<JsonLd>;
|
||||
|
||||
/*
|
||||
export namespace documentLoader {
|
||||
function get(): NormalizedDocLoader;
|
||||
function set(v: DocLoader): void;
|
||||
}
|
||||
*/
|
||||
|
||||
// default document loader not implemented
|
||||
// export function documentLoader(url: Url): DocLoader;
|
||||
|
||||
/**
|
||||
* Deprecated default document loader. Do not use or override.
|
||||
*/
|
||||
// export function loadDocument(url: Url): Promise<DocLoader>;
|
||||
|
||||
// export function get (url: Url, options: Options.Get): Promise<JsonLd>;
|
||||
|
||||
// export function processContext(activeCtx: Context, localCtx: Context, options: Options.ProcessContext): Promise<Context>;
|
||||
|
||||
// backwards compatibility
|
||||
// export function getContextValue(ctx: Context, key: string, type: JsonLDType): any;
|
||||
/**
|
||||
* Document loaders.
|
||||
*/
|
||||
/*
|
||||
export namespace documentLoaders {
|
||||
let node: DocLoader;
|
||||
let xhr: DocLoader;
|
||||
}
|
||||
|
||||
export type DocumentLoaders = 'node' | 'xhr';
|
||||
*/
|
||||
|
||||
// export function useDocumentLoader (type: DocumentLoaders, ...params: any): void;
|
||||
|
||||
// type ParserFunction = (input: string, callback: (err: Error, dataset: RdfDataSet) => Promise<RdfDataSet|null|undefined>) => void;
|
||||
// export function registerRDFParser (contentType: MimeType, parser: ParserFunction): void;
|
||||
|
||||
// export function unregisterRDFParser (contentType: MimeType): void;
|
||||
|
||||
// TODO Still originial source code. Maybe build additional types from it
|
||||
// ******************************************************************************
|
||||
/* URL API */
|
||||
// jsonld.url = require('./url');
|
||||
/* Utility API */
|
||||
// jsonld.util = util;
|
||||
// backwards compatibility
|
||||
// Object.assign(jsonld, util);
|
||||
|
||||
// reexpose API as jsonld.promises for backwards compatability
|
||||
// jsonld.promises = jsonld;
|
||||
|
||||
// backwards compatibility
|
||||
// declare module 'RequestQueue';
|
||||
|
||||
// ******************************************************************************
|
||||
40
types/jsonld/jsonld-spec.d.ts
vendored
Normal file
40
types/jsonld/jsonld-spec.d.ts
vendored
Normal file
@ -0,0 +1,40 @@
|
||||
/*
|
||||
* Types from the jsonld Specification:
|
||||
* https://www.w3.org/TR/json-ld-api/
|
||||
*
|
||||
*/
|
||||
|
||||
// Some aliases and placeholders for better readability
|
||||
export type JsonLdObj = object;
|
||||
export type JsonLdArray = [object];
|
||||
export type JsonLd = JsonLdObj|JsonLdArray;
|
||||
|
||||
type DOMString = string;
|
||||
type LoadDocumentCallback = Promise <Url>;
|
||||
|
||||
export type Url = DOMString;
|
||||
export type Iri = Url;
|
||||
export type Document = JsonLd|Url;
|
||||
export type Context = Document;
|
||||
|
||||
export interface Options {
|
||||
base?: DOMString|null;
|
||||
compactArrays?: boolean;
|
||||
documentLoader?: LoadDocumentCallback|null;
|
||||
expandContext?: Context|null;
|
||||
processingMode?: DOMString;
|
||||
}
|
||||
|
||||
export interface JsonLdProcessor {
|
||||
compact(input: Document, context: Context, options?: Options): Promise<JsonLd>;
|
||||
expand(input: Document, options?: Options): Promise<JsonLd>;
|
||||
flatten(input: Document, context?: Context|null, options?: Options): Promise<JsonLd>;
|
||||
}
|
||||
|
||||
export interface RemoteDocument {
|
||||
contextUrl?: Url;
|
||||
documentUrl: Url;
|
||||
document: JsonLd;
|
||||
}
|
||||
|
||||
export {};
|
||||
178
types/jsonld/jsonld-tests.ts
Normal file
178
types/jsonld/jsonld-tests.ts
Normal file
@ -0,0 +1,178 @@
|
||||
import * as jsonld from 'jsonld';
|
||||
|
||||
const doc = {
|
||||
"http://schema.org/name": "Manu Sporny",
|
||||
"http://schema.org/url": {"@id": "http://manu.sporny.org/"},
|
||||
"http://schema.org/image": {"@id": "http://manu.sporny.org/images/manu.png"}
|
||||
};
|
||||
|
||||
const docNQuads =
|
||||
`_: <http://schema.org/name> "Manu Sporny" .
|
||||
_: <http://schema.org/url> <http://manu.sporny.org/> .
|
||||
_: <http://schema.org/image> <http://manu.sporny.org/images/manu.png> .`;
|
||||
|
||||
const context = {
|
||||
name: "http://schema.org/name",
|
||||
homepage: {"@id": "http://schema.org/url", "@type": "@id"},
|
||||
image: {"@id": "http://schema.org/image", "@type": "@id"}
|
||||
};
|
||||
|
||||
const baseUrl = 'http://schema.org';
|
||||
|
||||
const frame = doc;
|
||||
|
||||
const docRDF = jsonld.toRDF(doc);
|
||||
|
||||
let count = 0;
|
||||
function log(doc: object) {
|
||||
count++;
|
||||
// Uncomment if testing with node.js
|
||||
// console.log(count + ": " + JSON.stringify(doc) + "\n");
|
||||
}
|
||||
|
||||
/**
|
||||
* compact() test
|
||||
*/
|
||||
jsonld.compact(doc, context, (err, compDoc) => {
|
||||
log(compDoc);
|
||||
});
|
||||
|
||||
jsonld.compact(doc, context, {appropriate: true}, (err, compDoc) => {
|
||||
log(compDoc);
|
||||
});
|
||||
|
||||
jsonld.compact(doc, context)
|
||||
.then((compDoc) => {
|
||||
log(compDoc);
|
||||
});
|
||||
|
||||
jsonld.compact(doc, context, {graph: false})
|
||||
.then((compDoc) => {
|
||||
log(compDoc);
|
||||
});
|
||||
|
||||
/**
|
||||
* expand() test
|
||||
*/
|
||||
jsonld.expand(doc, (err, res) => {
|
||||
log(res);
|
||||
});
|
||||
|
||||
jsonld.expand(doc, {keepFreeFloatingNodes: false}, (err, res) => {
|
||||
log(res);
|
||||
});
|
||||
|
||||
jsonld.expand(doc)
|
||||
.then((res) => {
|
||||
log(res);
|
||||
});
|
||||
|
||||
jsonld.expand(doc, {keepFreeFloatingNodes: false})
|
||||
.then((res) => {
|
||||
log(res);
|
||||
});
|
||||
|
||||
/**
|
||||
* flatten() test
|
||||
*/
|
||||
jsonld.flatten(doc, context, (err, res) => {
|
||||
log(res);
|
||||
});
|
||||
|
||||
jsonld.flatten(doc, context, {expandContext: context}, (err, res) => {
|
||||
log(res);
|
||||
});
|
||||
|
||||
jsonld.flatten(doc, context)
|
||||
.then((res) => {
|
||||
log(res);
|
||||
});
|
||||
|
||||
jsonld.flatten(doc, context, {base: baseUrl})
|
||||
.then((res) => {
|
||||
log(res);
|
||||
});
|
||||
|
||||
/**
|
||||
* frame() test
|
||||
*/
|
||||
jsonld.frame(doc, frame, (err, res) => {
|
||||
log(res);
|
||||
});
|
||||
|
||||
jsonld.frame(doc, frame, {embed: "@last", explicit: false}, (err, res) => {
|
||||
log(res);
|
||||
});
|
||||
|
||||
jsonld.frame(doc, frame)
|
||||
.then((res) => {
|
||||
log(res);
|
||||
});
|
||||
|
||||
jsonld.frame(doc, frame, {requireAll: true})
|
||||
.then((res) => {
|
||||
log(res);
|
||||
});
|
||||
|
||||
/**
|
||||
* normalize() test
|
||||
*/
|
||||
jsonld.normalize(doc, (err, res) => {
|
||||
log(res);
|
||||
});
|
||||
|
||||
jsonld.normalize(doc, {algorithm: 'URDNA2015', expansion: false}, (err, res) => {
|
||||
log(res);
|
||||
});
|
||||
|
||||
jsonld.normalize(doc)
|
||||
.then((res) => {
|
||||
log(res);
|
||||
});
|
||||
|
||||
jsonld.normalize(doc, {expansion: false})
|
||||
.then((res) => {
|
||||
log(res);
|
||||
});
|
||||
|
||||
/**
|
||||
* fromRDF() test
|
||||
*/
|
||||
jsonld.fromRDF(docNQuads, (err, res) => {
|
||||
log(res);
|
||||
});
|
||||
|
||||
jsonld.fromRDF(docNQuads, {format: 'application/n-quads'}, (err, res) => {
|
||||
log(res);
|
||||
});
|
||||
|
||||
jsonld.fromRDF(docRDF)
|
||||
.then((res) => {
|
||||
log(res);
|
||||
});
|
||||
|
||||
jsonld.fromRDF(docRDF, {useRdfType: false})
|
||||
.then((res) => {
|
||||
log(res);
|
||||
});
|
||||
|
||||
/**
|
||||
* toRDF() test
|
||||
*/
|
||||
jsonld.toRDF(doc, (err, res) => {
|
||||
log(res);
|
||||
});
|
||||
|
||||
jsonld.toRDF(doc, {format: 'application/n-quads'}, (err, res) => {
|
||||
log(res);
|
||||
});
|
||||
|
||||
jsonld.toRDF(doc)
|
||||
.then((res) => {
|
||||
log(res);
|
||||
});
|
||||
|
||||
jsonld.toRDF(doc, {produceGeneralizedRdf: false})
|
||||
.then((res) => {
|
||||
log(res);
|
||||
});
|
||||
24
types/jsonld/tsconfig.json
Normal file
24
types/jsonld/tsconfig.json
Normal file
@ -0,0 +1,24 @@
|
||||
{
|
||||
"compilerOptions": {
|
||||
"module": "commonjs",
|
||||
"lib": [
|
||||
"es6"
|
||||
],
|
||||
"noImplicitAny": true,
|
||||
"strictFunctionTypes": true,
|
||||
"noImplicitThis": true,
|
||||
"strictNullChecks": true,
|
||||
"baseUrl": "../",
|
||||
"typeRoots": [
|
||||
"../"
|
||||
],
|
||||
"types": [],
|
||||
"noEmit": true,
|
||||
"forceConsistentCasingInFileNames": true
|
||||
},
|
||||
"files": [
|
||||
"index.d.ts",
|
||||
"jsonld-tests.ts",
|
||||
"jsonld-spec.d.ts"
|
||||
]
|
||||
}
|
||||
1
types/jsonld/tslint.json
Normal file
1
types/jsonld/tslint.json
Normal file
@ -0,0 +1 @@
|
||||
{ "extends": "dtslint/dt.json" }
|
||||
Loading…
Reference in New Issue
Block a user