forked from get-set-fetch/scraper
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Plugin.ts
65 lines (56 loc) · 2.05 KB
/
Plugin.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
/* eslint-disable no-prototype-builtins */
/* eslint-disable no-param-reassign */
/* eslint-disable no-restricted-syntax */
import { JSONSchema7 } from 'json-schema';
import SchemaHelper from '../schema/SchemaHelper';
import Project from '../storage/base/Project';
import Resource from '../storage/base/Resource';
import BrowserClient from '../browserclient/BrowserClient';
import { IDomClientConstructor } from '../domclient/DomClient';
export type PluginOpts = {
name: string;
domRead?: boolean;
domWrite?: boolean;
[key: string]: unknown;
// position options within a plugin list
before?: string;
replace?: string;
after?: string;
path?: string;
}
/** All plugins should extend this class implementing the test and apply methods. */
export default abstract class Plugin {
static get schema() {
return {};
}
opts: Partial<PluginOpts>;
constructor(opts:Partial<PluginOpts> = {}) {
const { schema } = <typeof Plugin> this.constructor;
this.opts = SchemaHelper.instantiate(schema, opts);
}
/**
* Relevant for a pipeline plugin responsible for actual content scraping.
* @returns keys the scraped data will be exported under
*/
getContentKeys():string[] {
return undefined;
}
/**
* Tests if the plugin should be executed or not against the current resource.
* @param project - current scrape project
* @param resource - current scrape resource
*/
abstract test(project: Project, resource: Resource): Promise<boolean> | boolean;
/**
* Executes the plugin against the current resource, either in node.js or browser environment.
* The result will be merged into the currently scraped resource at scraper level.
* @param project - current scrape project
* @param resource - current scrape resource
* @param client - current browser client
*/
abstract apply(project: Project, resource: Resource, client: BrowserClient|IDomClientConstructor): Promise<void | Partial<Resource>> | void | Partial<Resource>;
}
export interface IPlugin {
new(kwArgs: Partial<PluginOpts>): Plugin;
schema: JSONSchema7;
}