From af5b21291deebf95400956f1c9451dc5c40ba800 Mon Sep 17 00:00:00 2001 From: Florian Keller Date: Thu, 28 Feb 2019 16:55:33 +0100 Subject: [PATCH] Add types for open-graph --- types/open-graph/index.d.ts | 74 ++++++++++++++++++++++++++++ types/open-graph/open-graph-tests.ts | 18 +++++++ types/open-graph/tsconfig.json | 23 +++++++++ types/open-graph/tslint.json | 1 + 4 files changed, 116 insertions(+) create mode 100644 types/open-graph/index.d.ts create mode 100644 types/open-graph/open-graph-tests.ts create mode 100644 types/open-graph/tsconfig.json create mode 100644 types/open-graph/tslint.json diff --git a/types/open-graph/index.d.ts b/types/open-graph/index.d.ts new file mode 100644 index 0000000000..14a3fe3ce6 --- /dev/null +++ b/types/open-graph/index.d.ts @@ -0,0 +1,74 @@ +// Type definitions for open-graph 0.2 +// Project: https://github.com/samholmes/node-open-graph +// Definitions by: Florian Keller +// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped +// TypeScript Version: 2.2 + +/// + +declare namespace og { + interface Metadata { + [key: string]: string | string[] | undefined; + /** An alternate url to use if the webpage requires HTTPS. */ + secure_url?: string | string[]; + /** A MIME type for this image. */ + type?: string | string[]; + } + + interface ImageVideoMetadata extends Metadata { + /** A description of what is in the image (not a caption). If the page specifies an `og:image` it should specify `og:image:alt`. */ + alt?: string | string[]; + /** The number of pixels high. */ + height?: string | string[]; + /** The */ + url?: string | string[]; + /** The number of pixels wide. */ + width?: string | string[]; + } + + interface Data { + [key: string]: string | string[] | ImageVideoMetadata | Metadata | undefined; + /** A URL to an audio file to accompany this object. */ + audio?: string | string[] | Metadata; + /** A one to two sentence description of your object. */ + description?: string | string[]; + /** + * The word that appears before this object's title in a sentence. An enum of (a, an, the, "", auto). + * If auto is chosen, the consumer of your data should chose between "a" or "an". Default is "" (blank). + */ + determiner?: string | string[]; + /** An image URL which should represent your object within the graph. */ + image?: string | string[] | ImageVideoMetadata; + /** The locale these tags are marked up in. Of the format `language_TERRITORY`. Default is `en_US`. */ + locale?: + | string | string[] + | { + /** An array of other locales this page is available in. */ + alternate?: string | string[]; + }; + /** If your object is part of a larger web site, the name which should be displayed for the overall site. e.g., "IMDb". */ + site_name?: string | string[]; + /** The title of your object as it should appear within the graph, e.g., "The Rock". */ + title: string | string[]; + /** The type of your object, e.g., "video.movie". Depending on the type you specify, other properties may also be required. */ + type?: string | string[]; + /** The canonical URL of your object that will be used as its permanent ID in the graph, e.g., "http://www.imdb.com/title/tt0117500/". */ + url?: string | string[]; + /** A URL to a video file that complements this object. */ + video?: string | string[] | ImageVideoMetadata; + } + + type DataCallback = (err: Error | null, data: Data | undefined) => void; + type RequestCallback = (err: Error | null, data: string | undefined) => void; + + interface Options { + strict?: boolean; + } + + function parse(websiteContent: string, options?: Options): Data; + function getHTML(url: string | Cheerio, callback: RequestCallback): void; +} + +declare function og(url: string, callback: og.DataCallback, options?: og.Options): void; + +export = og; diff --git a/types/open-graph/open-graph-tests.ts b/types/open-graph/open-graph-tests.ts new file mode 100644 index 0000000000..4e3db10438 --- /dev/null +++ b/types/open-graph/open-graph-tests.ts @@ -0,0 +1,18 @@ +import og = require('open-graph'); + +const url = 'http://github.com/samholmes/node-open-graph/raw/master/test.html'; + +og(url, (err, meta) => { + if (meta) { + meta.custom; + meta.description; // $ExpectType string | string[] | undefined + } +}); + +og.parse('content', { + strict: true, +}); + +og.getHTML('http://github.com/samholmes/node-open-graph/raw/master/test.html', (err, data) => { + data; // $ExpectType string | undefined +}); diff --git a/types/open-graph/tsconfig.json b/types/open-graph/tsconfig.json new file mode 100644 index 0000000000..72fa10a12d --- /dev/null +++ b/types/open-graph/tsconfig.json @@ -0,0 +1,23 @@ +{ + "compilerOptions": { + "module": "commonjs", + "lib": [ + "es6" + ], + "noImplicitAny": true, + "noImplicitThis": true, + "strictNullChecks": true, + "strictFunctionTypes": true, + "baseUrl": "../", + "typeRoots": [ + "../" + ], + "types": [], + "noEmit": true, + "forceConsistentCasingInFileNames": true + }, + "files": [ + "index.d.ts", + "open-graph-tests.ts" + ] +} diff --git a/types/open-graph/tslint.json b/types/open-graph/tslint.json new file mode 100644 index 0000000000..3db14f85ea --- /dev/null +++ b/types/open-graph/tslint.json @@ -0,0 +1 @@ +{ "extends": "dtslint/dt.json" }