Skip to content

Commit

Permalink
đŸ¤– Merge PR #63570 [chrome-remote-interface] add support for shorthand…
Browse files Browse the repository at this point in the history
… callback version of events by @kazarmy

* Add support for shorthand callback version of events

* Callback `params` type is undefined if there are no event params
  • Loading branch information
kazarmy committed Dec 11, 2022
1 parent 20a8be0 commit 6f97745
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 5 deletions.
18 changes: 14 additions & 4 deletions types/chrome-remote-interface/chrome-remote-interface-tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,18 +30,28 @@ function assertType<T>(value: T): T {
const loadEvent = await client['Page.loadEventFired'](); // instead of: await Page.loadEventFired();
loadEvent.timestamp;
await client['Page.interstitialHidden'](); // instead of: await Page.interstitialHidden();
// instead of: Network.requestWillBeSent((params, sessionId) => {});
const unsubscribe = client['Network.requestWillBeSent']((params, sessionId) => {
const unsubscribe = Network.requestWillBeSent((params, sessionId) => {
params.request.url;
unsubscribe();
});
const unsubscribe2 = client['Network.requestWillBeSent']((params) => {
const unsubscribeAlt = client['Network.requestWillBeSent']((params, sessionId) => {
params.request.url;
unsubscribeAlt();
});
const unsubscribe2 = Network.requestWillBeSent((params) => {
params.request.url;
unsubscribe2();
});
const unsubscribe3 = client['Page.frameResized'](() => {
const unsubscribeAlt2 = client['Network.requestWillBeSent']((params) => {
params.request.url;
unsubscribeAlt2();
});
const unsubscribe3 = Page.frameResized(() => {
unsubscribe3();
});
const unsubscribeAlt3 = client['Page.frameResized'](() => {
unsubscribeAlt3();
});
await Runtime.enable();
const loc = await Runtime.evaluate({ expression: 'window.location.toString()' });
const targets = await CDP.List(cdpPort);
Expand Down
13 changes: 12 additions & 1 deletion types/chrome-remote-interface/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,16 @@ declare namespace CDP {
// Generated content end.
/////////////////////////////////////////////////

type GetEventFromString<D extends string, S extends string> = S extends `${D}.${infer E}` ? E : never;
type GetEvent<D extends string> = GetEventFromString<D, keyof ProtocolMappingApi.Events>;
type GetReturnType<D extends string, E extends string> =
`${D}.${E}` extends keyof ProtocolMappingApi.Events ?
ProtocolMappingApi.Events[`${D}.${E}`][0] : never;
type DoEventProps<D extends string> = {
[event in GetEvent<D>]:
(listener: (params: GetReturnType<D, event>, sessionId?: string) => void) => () => Client};
type DoEventObj<D> = D extends string ? DoEventProps<D> : Record<keyof any, never>;

type IsNullableObj<T> = Record<keyof T, undefined> extends T ? true : false;
/**
* Checks whether the only parameter of `T[key]` is nullable i.e. all of
Expand All @@ -165,7 +175,8 @@ declare namespace CDP {
T[key] :
T[key]
};
type ImproveAPI<T> = {[key in keyof T]: OptIfParamNullable<T[key]>};

type ImproveAPI<T> = {[key in keyof T]: OptIfParamNullable<T[key]> & DoEventObj<key>};
interface StableDomains {
Browser: ProtocolProxyApi.BrowserApi;
Debugger: ProtocolProxyApi.DebuggerApi;
Expand Down

0 comments on commit 6f97745

Please sign in to comment.