This repository was archived by the owner on Aug 9, 2023. It is now read-only.
Repository navigation
Expand file tree
/
Copy pathtypes.ts
More file actions
148 lines (118 loc) · 3.44 KB
/
Copy pathtypes.ts
File metadata and controls
148 lines (118 loc) · 3.44 KB
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
/*----------------------------------
- CONST
----------------------------------*/
export const allowedMethods = ["GET", "POST"] as const;
export const bodyTypes = ["form", "json"] as const;
export const dataFilters = ["url", 'price'] as const;
export const devices = ['desktop', 'tablet', 'mobile'] as const;
/*----------------------------------
- GLOBAL CONFIGURATION TYPES
----------------------------------*/
export type TAdapter = (options: {
method: HttpMethod,
url: string,
headers: {[k: string]: string},
body: {
requests: TRequestWithExtractors[]
}
}) => Promise<TScrapeResult[]>;
export type TGlobalOptions = {
adapter?: TAdapter
}
/*----------------------------------
- REQUEST CONFIGURATION TYPES
----------------------------------*/
export type TBasicRequest = {
url: string,
method?: HttpMethod,
cookies?: string,
device?: typeof devices[number]
}
export type HttpMethod = typeof allowedMethods[number];
export type TRequestWithBody = TBasicRequest & {
body: { [key: string]: any },
bodyType: typeof bodyTypes[number]
}
export type TRequest = TBasicRequest | TRequestWithBody;
export type TRequestWithExtractors = TRequest & {
extract?: TExtractor,
withBody?: boolean,
withHeaders?: boolean,
}
/*----------------------------------
- SCRAPER
----------------------------------*/
export type TExtractor = TItemsExtractor | TValueExtractor | ValueExtractor | TItemsIterator;
export type TSelector = "this" | string;
export type TAttribute = "text" | "html" | string;
export type TFilter = typeof dataFilters[number];
export type TValueExtractor = {
select: TSelector,
attr?: TAttribute,
required?: boolean,
filters?: TFilter[]
}
export type TItemsIterator = {
$foreach?: string,
items: {
[name: string]: TExtractor
}
}
export type TItemsExtractor = {
[name: string]: TExtractor
}
export class ValueExtractor {
public options: TValueExtractor;
public constructor( select: TSelector ) {
this.options = {
select
}
}
public attr( attribute: TAttribute ) {
this.options.attr = attribute;
return this;
}
public each( values: { [name: string]: TExtractor } ): TItemsIterator {
return { $foreach: this.options.select, items: values };
}
public filter( ...filterNames: TFilter[] ) {
if (this.options.filters === undefined)
this.options.filters = filterNames;
else
for (const filterName of filterNames) {
if (this.options.filters.includes( filterName ))
throw new Error(`The ${this.filter} filter has already be set for this selector.`);
else
this.options.filters.push(filterName);
}
return this;
}
public optional( isOptional: boolean = true ) {
this.options.required = !isOptional;
return this;
}
public html() {
this.options.attr = 'html';
return this;
}
public text() {
this.options.attr = 'html';
return this;
}
}
/*----------------------------------
- RESPONSE
----------------------------------*/
export type TExtractedPrice = {
amount: number,
currency: string
}
export type TScrapeResult<TData extends any = any> = {
url: string,
status: number,
headers?: { [key: string]: string },
body?: string,
data?: TData,
time: number,
bandwidth: number,
}