From b398805089841f77ca89afe65d1d50908fa68d2f Mon Sep 17 00:00:00 2001 From: screendriver Date: Tue, 1 Nov 2016 14:01:53 +0100 Subject: [PATCH] Added type definitions for website-scraper v1.2.x (#12125) * Added type definitions for website-scraper v1.2.x * Renamed test file * Fixed "files" in tsconfig.json * Renamed test file * Replaced module with namespace --- website-scraper/index.d.ts | 50 ++++++++++++++++++++++++ website-scraper/tsconfig.json | 19 +++++++++ website-scraper/website-scraper-tests.ts | 29 ++++++++++++++ 3 files changed, 98 insertions(+) create mode 100644 website-scraper/index.d.ts create mode 100644 website-scraper/tsconfig.json create mode 100644 website-scraper/website-scraper-tests.ts diff --git a/website-scraper/index.d.ts b/website-scraper/index.d.ts new file mode 100644 index 0000000000..bcc718f693 --- /dev/null +++ b/website-scraper/index.d.ts @@ -0,0 +1,50 @@ +// Type definitions for website-scraper v1.2.x +// Project: https://github.com/s0ph1e/node-website-scraper +// Definitions by: Christian Rackerseder +// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped + +import * as request from 'request'; + +declare namespace websiteScraper { + interface Url { + url: string; + filename: string; + } + interface SubDirectory { + directory: string; + extensions: string[]; + } + interface Source { + selector: string; + attr: string; + } + interface RequestOptions { + headers: request.Headers + } + interface Options { + urls: Array; + directory: string; + urlFilter?: (url: string) => boolean; + filenameGenerator?: string; + defaultFilename?: string; + prettifyUrls?: boolean; + sources?: Source[]; + subdirectories?: SubDirectory[] | null; + request?: RequestOptions; + recursive?: boolean; + maxDepth?: number; + ignoreErrors?: boolean; + } + interface Resource { + url: string; + filename: string; + assets: Resource[]; + } + interface Callback { + (error: any | null, result: Resource[] | null): void; + } + function scrape(options: Options, callback: Callback): void; + function scrape(options: Options): Promise; +} + +export = websiteScraper; diff --git a/website-scraper/tsconfig.json b/website-scraper/tsconfig.json new file mode 100644 index 0000000000..4b973f9167 --- /dev/null +++ b/website-scraper/tsconfig.json @@ -0,0 +1,19 @@ +{ + "compilerOptions": { + "module": "commonjs", + "target": "es6", + "noImplicitAny": true, + "strictNullChecks": true, + "baseUrl": "../", + "typeRoots": [ + "../" + ], + "types": [], + "noEmit": true, + "forceConsistentCasingInFileNames": true + }, + "files": [ + "index.d.ts", + "website-scraper-tests.ts" + ] +} diff --git a/website-scraper/website-scraper-tests.ts b/website-scraper/website-scraper-tests.ts new file mode 100644 index 0000000000..4bd762adf8 --- /dev/null +++ b/website-scraper/website-scraper-tests.ts @@ -0,0 +1,29 @@ +import * as scraper from 'website-scraper'; + +scraper.scrape({ + urls: [ + 'http://nodejs.org/', + {url: 'http://nodejs.org/about', filename: 'about.html'}, + {url: 'http://blog.nodejs.org/', filename: 'blog.html'} + ], + directory: '/path/to/save', + subdirectories: [ + {directory: 'img', extensions: ['.jpg', '.png', '.svg']}, + {directory: 'js', extensions: ['.js']}, + {directory: 'css', extensions: ['.css']} + ], + sources: [ + {selector: 'img', attr: 'src'}, + {selector: 'link[rel="stylesheet"]', attr: 'href'}, + {selector: 'script', attr: 'src'} + ], + request: { + headers: { + 'User-Agent': 'Mozilla/5.0 (Linux; Android 4.2.1; en-us; Nexus 4 Build/JOP40D) AppleWebKit/535.19 (KHTML, like Gecko) Chrome/18.0.1025.166 Mobile Safari/535.19' + } + } +}).then(function (result) { + console.log(result); +}).catch(function(err){ + console.log(err); +});