-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathapi.ts
168 lines (159 loc) · 5.33 KB
/
api.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
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
import { runCrawleeOne } from './lib/actor/actor';
import type {
CrawleeOneActorInst,
CrawleeOneActorDef,
CrawleeOneActorRouterCtx,
CrawleeOneCtx,
} from './lib/actor/types';
import type { AllActorInputs } from './lib/input';
import { logLevelHandlerWrapper } from './lib/log';
import type { CrawleeOneRouteHandler, CrawleeOneRoute } from './lib/router/types';
import type { CrawlerMeta, CrawlerType } from './types';
import type { MaybePromise } from './utils/types';
/** Args object passed to `crawleeOne` */
export interface CrawleeOneArgs<
TType extends CrawlerType,
T extends CrawleeOneCtx<CrawlerMeta<TType>['context']>
> {
/** Type specifying the Crawlee crawler class, input options, and more. */
type: CrawlerType;
/** Unique name of the crawler instance. The name may be used in codegen and logging. */
name?: string;
/** Crawlee crawler configuration that CANNOT be overriden via `input` and `crawlerConfigDefaults` */
crawlerConfig?: Omit<CrawlerMeta<TType>['options'], 'requestHandler'>;
/** Crawlee crawler configuration that CAN be overriden via `input` and `crawlerConfig` */
crawlerConfigDefaults?: Omit<CrawlerMeta<TType>['options'], 'requestHandler'>;
/**
* If `mergeInput` is truthy, will merge input settings from `inputDefaults`, `input`,
* and `io.getInput()`.
*
* ```js
* { ...inputDefaults, ...io.getInput(), ...input }
* ```
*
* If `mergeInput` is falsy, `io.getInput()` is ignored if `input` is provided. So the input is either:
*
* ```js
* { ...inputDefaults, ...io.getInput() } // If `input` is not defined
* ```
*
* OR
*
* ```js
* { ...inputDefaults, ...input } // If `input` is defined
* ```
*
* Alternatively, you can supply your own function that merges the sources:
*
* ```js
* {
* // `mergeInput` can be also async
* mergeInput: ({ defaults, overrides, env }) => {
* // This is same as `mergeInput: true`
* return { ...defaults, ...env, ...overrides };
* },
* }
* ```
*/
mergeInput?: boolean | ((sources: {
defaults: Partial<AllActorInputs>;
overrides: Partial<AllActorInputs>;
env: Partial<AllActorInputs>;
}) => MaybePromise<Partial<AllActorInputs>>);
/** Input configuration that CANNOT be overriden via `inputDefaults` and `io.getInput()` */
input?: Partial<AllActorInputs>;
/** Input configuration that CAN be overriden via `input` and `io.getInput()` */
inputDefaults?: Partial<AllActorInputs>;
// /////// Override services /////////
/**
* Configure the Crawlee proxy.
*
* See {@link ProxyConfiguration}
*/
proxy?: CrawleeOneActorDef<T>['proxy'];
/**
* Provide a telemetry instance that is used for tracking errors.
*
* See {@link CrawleeOneTelemetry}
*/
telemetry?: CrawleeOneActorDef<T>['telemetry'];
/**
* Provide an instance that is responsible for state management:
* - Adding scraped data to datasets
* - Adding and removing requests to/from queues
* - Cache storage
*
* This is an API based on Apify's `Actor` utility class, which is also
* the default.
*
* You don't need to override this in most of the cases.
*
* By default, the data is saved and kept locally in
* `./storage` directory. And if the cralwer runs in Apify's platform
* then it will use Apify's cloud for storage.
*
* See {@link CrawleeOneIO}
*/
io?: CrawleeOneActorDef<T>['io'];
/**
* Provide a custom router instance.
*
* By default, router is created as:
* ```ts
* import { Router } from 'crawlee';
* Router.create(),
* ```
*
* See {@link Router}
*/
router?: CrawleeOneActorDef<T>['router'];
hooks?: {
onReady?: (actor: CrawleeOneActorInst<T>) => MaybePromise<void>;
validateInput?: (input: AllActorInputs | null) => MaybePromise<void>;
onBeforeHandler?: CrawleeOneRouteHandler<T, CrawleeOneActorRouterCtx<T>>;
onAfterHandler?: CrawleeOneRouteHandler<T, CrawleeOneActorRouterCtx<T>>;
};
routes: Record<T['labels'], CrawleeOneRoute<T, CrawleeOneActorRouterCtx<T>>>;
} // prettier-ignore
export const crawleeOne = <
TType extends CrawlerType,
T extends CrawleeOneCtx<CrawlerMeta<TType>['context']> = CrawleeOneCtx<
CrawlerMeta<TType>['context']
>
>(
args: CrawleeOneArgs<TType, T>
) => {
const hookHandlerWrapper = (handler: CrawleeOneRouteHandler<T, CrawleeOneActorRouterCtx<T>>) => {
const innerHandler = async (ctx) => {
await args.hooks?.onBeforeHandler?.(ctx as any);
await handler(ctx);
await args.hooks?.onAfterHandler?.(ctx as any);
};
return innerHandler;
};
return runCrawleeOne<TType, T>({
actorName: args.name,
actorType: args.type as TType,
crawlerConfigDefaults: args.crawlerConfigDefaults,
crawlerConfigOverrides: args.crawlerConfig,
actorConfig: {
telemetry: args.telemetry,
router: args.router,
proxy: args.proxy,
io: args.io,
input: args.input,
inputDefaults: args.inputDefaults,
mergeInput: args.mergeInput,
validateInput: args.hooks?.validateInput,
routes: args.routes,
routeHandlerWrappers: ({ input }) => [
logLevelHandlerWrapper(input?.logLevel ?? 'info'),
hookHandlerWrapper as any,
],
},
onReady: async (actor) => {
const onReady = args.hooks?.onReady ?? ((actor) => actor.runCrawler());
await onReady(actor);
},
});
};