@crawlee/core 3.17.1-beta.65 → 3.17.1-beta.67

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. package/package.json +5 -5
  2. package/router.d.ts +24 -8
  3. package/router.js +3 -2
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@crawlee/core",
3
- "version": "3.17.1-beta.65",
3
+ "version": "3.17.1-beta.67",
4
4
  "description": "The scalable web crawling and scraping library for JavaScript/Node.js. Enables development of data extraction and web automation jobs (not only) with headless Chrome and Puppeteer.",
5
5
  "engines": {
6
6
  "node": ">=16.0.0"
@@ -59,9 +59,9 @@
59
59
  "@apify/pseudo_url": "^2.0.30",
60
60
  "@apify/timeout": "^0.4.0",
61
61
  "@apify/utilities": "^2.7.10",
62
- "@crawlee/memory-storage": "3.17.1-beta.65",
63
- "@crawlee/types": "3.17.1-beta.65",
64
- "@crawlee/utils": "3.17.1-beta.65",
62
+ "@crawlee/memory-storage": "3.17.1-beta.67",
63
+ "@crawlee/types": "3.17.1-beta.67",
64
+ "@crawlee/utils": "3.17.1-beta.67",
65
65
  "@sapphire/async-queue": "^1.5.1",
66
66
  "@standard-schema/spec": "^1.0.0",
67
67
  "@vladfrangu/async_event_emitter": "^2.2.2",
@@ -84,5 +84,5 @@
84
84
  }
85
85
  }
86
86
  },
87
- "gitHead": "5f275abc2a1e8234df28bddffd12c162c211234e"
87
+ "gitHead": "f4621717f7e09ef21e1730bb0a817fa869f4a8f9"
88
88
  }
package/router.d.ts CHANGED
@@ -24,14 +24,28 @@ export type RouterHandlerContext<Context, UserData extends Dictionary> = Omit<Co
24
24
  export type RouteSchemas = Record<string, StandardSchemaV1> & {
25
25
  [defaultRoute]?: StandardSchemaV1;
26
26
  };
27
+ /** Infers a label's `userData` type from its schema, falling back to a plain {@link Dictionary}. */
28
+ type SchemaUserData<Schema extends StandardSchemaV1> = StandardSchemaV1.InferOutput<Schema> extends Dictionary ? StandardSchemaV1.InferOutput<Schema> : Dictionary;
27
29
  /**
28
- * Derives a route map (label → `userData` type) from a {@link RouteSchemas} map by inferring each
29
- * schema's output type. Outputs that are not object-shaped fall back to a plain {@link Dictionary}. The
30
- * {@link defaultRoute} schema drives runtime validation only, so it is excluded from the typed route map.
30
+ * Derives a route map (label → `userData` type) from a {@link RouteSchemas} map by inferring each schema's
31
+ * output type. Outputs that are not object-shaped fall back to a plain {@link Dictionary}. The
32
+ * {@link defaultRoute} schema is kept under its symbol key so {@link Router.addDefaultHandler} can pick it
33
+ * up; string labels (the ones {@link Router.addHandler} accepts) ignore it.
31
34
  */
32
35
  export type RoutesFromSchemas<Schemas extends RouteSchemas> = {
33
- [Label in Extract<keyof Schemas, string>]: StandardSchemaV1.InferOutput<Schemas[Label]> extends Dictionary ? StandardSchemaV1.InferOutput<Schemas[Label]> : Dictionary;
34
- };
36
+ [Label in Extract<keyof Schemas, string>]: SchemaUserData<Schemas[Label]>;
37
+ } & (Schemas extends {
38
+ [defaultRoute]: StandardSchemaV1;
39
+ } ? {
40
+ [defaultRoute]: SchemaUserData<Schemas[typeof defaultRoute]>;
41
+ } : {});
42
+ /**
43
+ * The `userData` type of the default route: inferred from the {@link defaultRoute} schema when the route map
44
+ * carries one, otherwise the provided `Fallback`.
45
+ */
46
+ export type DefaultRouteUserData<Routes, Fallback extends Dictionary> = Routes extends {
47
+ [defaultRoute]: infer DefaultUserData extends Dictionary;
48
+ } ? DefaultUserData : Fallback;
35
49
  /**
36
50
  * Validates `userData` against a {@link RouteSchemas|Standard Schema}, returning the parsed (and coerced)
37
51
  * value. Throws a {@link RequestValidationError} when validation fails.
@@ -182,10 +196,11 @@ export declare class Router<Context extends Omit<RestrictedCrawlingContext, 'enq
182
196
  addHandler<UserData extends Dictionary = GetUserDataFromRequest<Context['request']>>(label: RouterLabel<Routes>, handler: (ctx: RouterHandlerContext<Context, UserData>) => Awaitable<void>): void;
183
197
  /**
184
198
  * Registers default route handler. As a fallback it can receive any request (including labels not
185
- * declared in the route map), so `request.userData` defaults to the context's `userData` type
186
- * (loosely typed by default). Pass an explicit `UserData` type argument to narrow it.
199
+ * declared in the route map). When the router was created with a {@link defaultRoute} schema,
200
+ * `request.userData` is typed from it; otherwise it defaults to the context's (loosely typed) `userData`.
201
+ * Pass an explicit `UserData` type argument to narrow it.
187
202
  */
188
- addDefaultHandler<UserData extends Dictionary = GetUserDataFromRequest<Context['request']>>(handler: (ctx: RouterHandlerContext<Context, UserData>) => Awaitable<void>): void;
203
+ addDefaultHandler<UserData extends Dictionary = DefaultRouteUserData<Routes, GetUserDataFromRequest<Context['request']>>>(handler: (ctx: RouterHandlerContext<Context, UserData>) => Awaitable<void>): void;
189
204
  /**
190
205
  * Returns the {@link RouteSchemas|Standard Schema} registered for a label, if any. Used by the crawler
191
206
  * to validate `request.userData` when requests are added.
@@ -234,3 +249,4 @@ export declare class Router<Context extends Omit<RestrictedCrawlingContext, 'enq
234
249
  static create<Context extends Omit<RestrictedCrawlingContext, 'enqueueLinks'> = CrawlingContext, UserData extends Dictionary = GetUserDataFromRequest<Context['request']>>(routes?: RouterRoutes<Context, Record<string, UserData>>): RouterHandler<Context, Record<string, UserData>>;
235
250
  static create<Context extends Omit<RestrictedCrawlingContext, 'enqueueLinks'> = CrawlingContext, const Schemas extends RouteSchemas = RouteSchemas>(schemas: Schemas): RouterHandler<Context, RoutesFromSchemas<Schemas>>;
236
251
  }
252
+ export {};
package/router.js CHANGED
@@ -175,8 +175,9 @@ class Router {
175
175
  }
176
176
  /**
177
177
  * Registers default route handler. As a fallback it can receive any request (including labels not
178
- * declared in the route map), so `request.userData` defaults to the context's `userData` type
179
- * (loosely typed by default). Pass an explicit `UserData` type argument to narrow it.
178
+ * declared in the route map). When the router was created with a {@link defaultRoute} schema,
179
+ * `request.userData` is typed from it; otherwise it defaults to the context's (loosely typed) `userData`.
180
+ * Pass an explicit `UserData` type argument to narrow it.
180
181
  */
181
182
  addDefaultHandler(handler) {
182
183
  this.validate(exports.defaultRoute);