From 9485087d51c1fe1754c83433cefd3408296bd2c7 Mon Sep 17 00:00:00 2001 From: Jonas Erbe Date: Mon, 25 Mar 2019 16:42:21 +0100 Subject: [PATCH] Added typings for jsonld.js https://www.npmjs.com/package/jsonld --- types/jsonld/index.d.ts | 217 ++++++++++++++++++++++++++++++++++ types/jsonld/jsonld-spec.d.ts | 40 +++++++ types/jsonld/jsonld-tests.ts | 178 ++++++++++++++++++++++++++++ types/jsonld/tsconfig.json | 24 ++++ types/jsonld/tslint.json | 1 + 5 files changed, 460 insertions(+) create mode 100644 types/jsonld/index.d.ts create mode 100644 types/jsonld/jsonld-spec.d.ts create mode 100644 types/jsonld/jsonld-tests.ts create mode 100644 types/jsonld/tsconfig.json create mode 100644 types/jsonld/tslint.json diff --git a/types/jsonld/index.d.ts b/types/jsonld/index.d.ts new file mode 100644 index 0000000000..0769eae4dd --- /dev/null +++ b/types/jsonld/index.d.ts @@ -0,0 +1,217 @@ +// Type definitions for jsonld 1.5 +// Project: https://github.com/digitalbazaar/jsonld.js +// Definitions by: Jonas Erbe +// 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 = (err: Error, res: T) => void; +type DocCallback = Callback; + +/* + * 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; + } + + 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; + +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; + +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; + +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; + +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; + +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; + +export function toRDF(input: Document, callback: Callback): void; +export function toRDF(input: Document, options: Options.ToRdf, callback: Callback): void; +export function toRDF(input: Document, options?: Options.ToRdf): Promise; + +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; + +// Not exported because of experimental state +// export function createNodeMapn(input: Document, options: Options.CreateNodeMap, callback: Callback): void; +// export function createNodeMapn(input: Document, callback: Callback): void; +// export function createNodeMapn(input: Document, options: Options.CreateNodeMap): Promise; + +// 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; + +/* +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; + +// export function get (url: Url, options: Options.Get): Promise; + +// export function processContext(activeCtx: Context, localCtx: Context, options: Options.ProcessContext): Promise; + +// 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) => 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'; + +// ****************************************************************************** diff --git a/types/jsonld/jsonld-spec.d.ts b/types/jsonld/jsonld-spec.d.ts new file mode 100644 index 0000000000..bb290fc00a --- /dev/null +++ b/types/jsonld/jsonld-spec.d.ts @@ -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 ; + +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; + expand(input: Document, options?: Options): Promise; + flatten(input: Document, context?: Context|null, options?: Options): Promise; +} + +export interface RemoteDocument { + contextUrl?: Url; + documentUrl: Url; + document: JsonLd; +} + +export {}; diff --git a/types/jsonld/jsonld-tests.ts b/types/jsonld/jsonld-tests.ts new file mode 100644 index 0000000000..9e2fcf10e9 --- /dev/null +++ b/types/jsonld/jsonld-tests.ts @@ -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 = + `_: "Manu Sporny" . + _: . + _: .`; + +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); +}); diff --git a/types/jsonld/tsconfig.json b/types/jsonld/tsconfig.json new file mode 100644 index 0000000000..4d9c179ebc --- /dev/null +++ b/types/jsonld/tsconfig.json @@ -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" + ] +} diff --git a/types/jsonld/tslint.json b/types/jsonld/tslint.json new file mode 100644 index 0000000000..3db14f85ea --- /dev/null +++ b/types/jsonld/tslint.json @@ -0,0 +1 @@ +{ "extends": "dtslint/dt.json" }