From 2568bf655dfc3ca5dd9b1d4088ca114d5f4969f8 Mon Sep 17 00:00:00 2001 From: Florian Keller Date: Tue, 7 Jul 2020 02:24:43 +0200 Subject: [PATCH] [open-graph-scraper] Add open-graph-scraper (#45913) --- types/open-graph-scraper/index.d.ts | 81 +++++++++++++++++++ .../open-graph-scraper-tests.ts | 26 ++++++ types/open-graph-scraper/tsconfig.json | 23 ++++++ types/open-graph-scraper/tslint.json | 1 + 4 files changed, 131 insertions(+) create mode 100644 types/open-graph-scraper/index.d.ts create mode 100644 types/open-graph-scraper/open-graph-scraper-tests.ts create mode 100644 types/open-graph-scraper/tsconfig.json create mode 100644 types/open-graph-scraper/tslint.json diff --git a/types/open-graph-scraper/index.d.ts b/types/open-graph-scraper/index.d.ts new file mode 100644 index 0000000000..7fbb3d8222 --- /dev/null +++ b/types/open-graph-scraper/index.d.ts @@ -0,0 +1,81 @@ +// Type definitions for open-graph-scraper 4.3 +// Project: https://github.com/jshemas/openGraphScraper#readme +// Definitions by: Florian Keller +// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped + +/// + +import { PassThrough } from 'stream'; + +declare namespace run { + interface SuccessResult { + error: false; + result: { + ogDescription?: string; + ogImage?: { + height: string; + type: string; + url: string; + width: string; + }; + ogTitle?: string; + ogType?: string; + ogUrl?: string; + requestUrl: string; + success: true; + }; + response: PassThrough; + } + + interface ErrorResult { + error: true; + result: { + error: string; + errorDetails: Error; + success: false; + }; + } + + interface Options { + /** By default, OGS will only send back the first image/video it finds (default: `false`). */ + allMedia?: boolean; + /** Pass in an array of sites you don't want `og`s to run on. */ + blacklist?: string[]; + /** Set the accept-encoding to `gzip/deflate` (default: `true`). */ + decompress?: boolean; + /** Setting this to `null` might help with running `og`s on non english websites (default: `utf8`). */ + encoding?: string | null; + /** Defines if redirect responses should be followed automatically (default: `true`). */ + followRedirect?: boolean; + /** An object containing request headers. Useful for setting the user-agent. */ + headers?: Record; + /** You can pass in an HTML string to run `og`s on it (use without `options.url`). */ + html?: string; + /** Max number of redirects `og`s will follow (default: `10`). */ + maxRedirects?: number; + /** Fetch other images if no open graph ones are found (default: `true`). */ + ogImageFallback?: boolean; + /** Only fetch open graph info and don't fall back on anything else (default: `false`). */ + onlyGetOpenGraphInfo?: boolean; + /** Sets the peekSize for the request (default: `1024`). */ + peekSize?: number; + /** Number of times `og`s will retry the request (default: `2`). */ + retry?: number; + /** Runs charset and icons on the request payload (default: `false`). */ + runChar?: boolean; + /** Timeout of the request in ms (default: `2000`). */ + timeout?: number; + /** URL of the site. */ + url: string; + /** Returns the charset in the `og`s payload (default: `false`). */ + withCharset?: boolean; + } +} + +declare function run(options: run.Options): Promise; +declare function run( + options: run.Options, + callback: (error: boolean, results: run.SuccessResult | run.ErrorResult, response: PassThrough) => void, +): void; + +export = run; diff --git a/types/open-graph-scraper/open-graph-scraper-tests.ts b/types/open-graph-scraper/open-graph-scraper-tests.ts new file mode 100644 index 0000000000..3913e372ed --- /dev/null +++ b/types/open-graph-scraper/open-graph-scraper-tests.ts @@ -0,0 +1,26 @@ +import openGraphScraper = require('open-graph-scraper'); + +const options: openGraphScraper.Options = { + encoding: null, + url: 'http://ogp.me/', + timeout: 200, +}; + +openGraphScraper(options, (error, results, response) => { + error; // $expectType boolean + results; // $expectType SuccessResult | ErrorResult + response; // $expectType PassThrough +}); + +openGraphScraper(options).then(data => { + if (!data.error) { + const { error, result, response } = data; + error; // $expectType false + result; // $expectType SuccessResult + response; // $expectType PassThrough + } else { + const { error, result } = data; + error; // $expectType true + result; // $expectType ErrorResult + } +}); diff --git a/types/open-graph-scraper/tsconfig.json b/types/open-graph-scraper/tsconfig.json new file mode 100644 index 0000000000..eb996f3486 --- /dev/null +++ b/types/open-graph-scraper/tsconfig.json @@ -0,0 +1,23 @@ +{ + "compilerOptions": { + "module": "commonjs", + "lib": [ + "es6" + ], + "noImplicitAny": true, + "noImplicitThis": true, + "strictFunctionTypes": true, + "strictNullChecks": true, + "baseUrl": "../", + "typeRoots": [ + "../" + ], + "types": [], + "noEmit": true, + "forceConsistentCasingInFileNames": true + }, + "files": [ + "index.d.ts", + "open-graph-scraper-tests.ts" + ] +} diff --git a/types/open-graph-scraper/tslint.json b/types/open-graph-scraper/tslint.json new file mode 100644 index 0000000000..3db14f85ea --- /dev/null +++ b/types/open-graph-scraper/tslint.json @@ -0,0 +1 @@ +{ "extends": "dtslint/dt.json" }