Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(router): add userdata generic to addHandler #1547

Merged
merged 2 commits into from
Sep 19, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions packages/core/src/except.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// Do not convert this file to .d.ts

type IsEqual<T, U> =
(<G>() => G extends T ? 1 : 2) extends
(<G>() => G extends U ? 1 : 2)
? true
: false;

type Filter<KeyType, ExcludeType> = IsEqual<KeyType, ExcludeType> extends true ? never : (KeyType extends ExcludeType ? never : KeyType);

export type Except<ObjectType, KeysType extends keyof ObjectType> = {
[KeyType in keyof ObjectType as Filter<KeyType, KeysType>]: ObjectType[KeyType];
};
14 changes: 12 additions & 2 deletions packages/core/src/router.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import type { Dictionary } from '@crawlee/types';
import type { CrawlingContext } from './crawlers/crawler_commons';
import type { Awaitable } from './typedefs';
import type { Request } from './request';
import type { Except } from './except';
import { MissingRouteError } from './errors';

const defaultRoute = Symbol('default-route');
Expand All @@ -8,6 +11,8 @@ export interface RouterHandler<Context extends CrawlingContext = CrawlingContext
(ctx: Context): Awaitable<void>;
}

type GetUserDataFromRequest<T> = T extends Request<infer Y> ? Y : never;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should I move this into ./request and export it?


/**
* Simple router that works based on request labels. This instance can then serve as a `requestHandler` of your crawler.
*
Expand Down Expand Up @@ -72,15 +77,20 @@ export class Router<Context extends CrawlingContext> {
/**
* Registers new route handler for given label.
*/
addHandler(label: string | symbol, handler: (ctx: Context) => Awaitable<void>) {
addHandler<UserData extends Dictionary = GetUserDataFromRequest<Context['request']>>(
label: string | symbol,
handler: (ctx: Except<Context, 'request'> & {request: Request<UserData>}) => Awaitable<void>,
) {
this.validate(label);
this.routes.set(label, handler);
}

/**
* Registers default route handler.
*/
addDefaultHandler(handler: (ctx: Context) => Awaitable<void>) {
addDefaultHandler<UserData extends Dictionary = GetUserDataFromRequest<Context['request']>>(
handler: (ctx: Except<Context, 'request'> & {request: Request<UserData>}) => Awaitable<void>,
) {
this.validate(defaultRoute);
this.routes.set(defaultRoute, handler);
}
Expand Down
26 changes: 26 additions & 0 deletions test/core/router.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import type { CrawlingContext } from '@crawlee/core';
import { MissingRouteError, Router } from '@crawlee/core';
import { BasicCrawler } from '@crawlee/basic';

Expand Down Expand Up @@ -104,4 +105,29 @@ describe('Router', () => {
requestHandler: router,
});
});

test('addHandler accepts userdata generic', async () => {
const testType = <T>(t: T): void => {};

const router: Router<CrawlingContext<{foo: 'foo'}>> = {
addHandler: () => {},
addDefaultHandler: () => {},
} as any;

router.addHandler('1', (ctx) => {
testType<'foo'>(ctx.request.userData.foo);
});

router.addHandler<{foo: 'bar'}>('2', (ctx) => {
testType<'bar'>(ctx.request.userData.foo);
});

router.addDefaultHandler((ctx) => {
testType<'foo'>(ctx.request.userData.foo);
});

router.addDefaultHandler<{foo: 'bar'}>((ctx) => {
testType<'bar'>(ctx.request.userData.foo);
});
});
});