Skip to content

Commit 00af054

Browse files
committed
feat(interfaces): fix devtools command typing
1 parent 1bec22a commit 00af054

File tree

6 files changed

+25
-27
lines changed

6 files changed

+25
-27
lines changed

client/lib/Agent.ts

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -336,13 +336,6 @@ export default class Agent extends AwaitedEventTarget<{ close: void }> {
336336
throw err;
337337
}
338338
}
339-
340-
public toJSON(): any {
341-
// return empty so we can
342-
return {
343-
type: 'Agent',
344-
};
345-
}
346339
}
347340

348341
// This class will lazily connect to core on first access of the tab properties

client/lib/FrameEnvironment.ts

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -185,13 +185,6 @@ export default class FrameEnvironment {
185185
const coreFrame = await getCoreFrameEnvironment(this);
186186
await coreFrame.waitForLocation(trigger, options);
187187
}
188-
189-
public toJSON(): any {
190-
// return empty so we can
191-
return {
192-
type: 'FrameEnvironment',
193-
};
194-
}
195188
}
196189

197190
export function getFrameState(object: any): IState {

client/lib/Tab.ts

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -221,13 +221,6 @@ export default class Tab extends AwaitedEventTarget<IEventType> {
221221
connection.closeTab(this);
222222
return coreTab.then(x => x.close());
223223
}
224-
225-
public toJSON(): any {
226-
// return empty so we can
227-
return {
228-
type: 'Tab',
229-
};
230-
}
231224
}
232225

233226
async function getOrCreateFrameEnvironment(

interfaces/AllowedNames.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,4 @@ type FilterOutFlags<Base, Condition> = {
99
type OnlyProperties<Base> = FilterOutFlags<Base, (...args: any[]) => any>[keyof Base];
1010
type AllowedNames<Base, Condition> = FilterFlags<Base, Condition>[keyof Base];
1111

12-
export { FilterFlags, AllowedNames, OnlyProperties };
12+
export { FilterOutFlags, FilterFlags, AllowedNames, OnlyProperties };

interfaces/IDevtoolsSession.ts

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,28 @@
11
import { ProtocolMapping } from 'devtools-protocol/types/protocol-mapping';
2+
import ITypedEventEmitter from './ITypedEventEmitter';
3+
import { FilterFlags, FilterOutFlags } from './AllowedNames';
24

3-
export default interface IDevtoolsSession {
5+
export declare type DevtoolsEvents = {
6+
[Key in keyof ProtocolMapping.Events]: ProtocolMapping.Events[Key][0];
7+
} & { disconnected: void };
8+
9+
type DevtoolsCommandParams = {
10+
[Key in keyof ProtocolMapping.Commands]: ProtocolMapping.Commands[Key]['paramsType'][0];
11+
};
12+
type OptionalParamsCommands = keyof FilterFlags<DevtoolsCommandParams, void | never>;
13+
type RequiredParamsCommands = keyof FilterOutFlags<DevtoolsCommandParams, void | never>;
14+
15+
export default interface IDevtoolsSession
16+
extends Omit<ITypedEventEmitter<DevtoolsEvents>, 'waitOn'> {
417
id: string;
5-
send<T extends keyof ProtocolMapping.Commands>(
18+
send<T extends RequiredParamsCommands>(
19+
method: T,
20+
params: DevtoolsCommandParams[T],
21+
sendInitiator?: object,
22+
): Promise<ProtocolMapping.Commands[T]['returnType']>;
23+
send<T extends OptionalParamsCommands>(
624
method: T,
7-
params: ProtocolMapping.Commands[T]['paramsType'][0],
25+
params?: DevtoolsCommandParams[T],
826
sendInitiator?: object,
927
): Promise<ProtocolMapping.Commands[T]['returnType']>;
1028

puppet-chrome/lib/DevtoolsSession.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import { TypedEventEmitter } from '@secret-agent/commons/eventUtils';
2323
import IResolvablePromise from '@secret-agent/interfaces/IResolvablePromise';
2424
import { createPromise } from '@secret-agent/commons/utils';
2525
import IDevtoolsSession, {
26+
DevtoolsEvents,
2627
IDevtoolsEventMessage,
2728
IDevtoolsResponseMessage,
2829
} from '@secret-agent/interfaces/IDevtoolsSession';
@@ -35,7 +36,7 @@ import RemoteObject = Protocol.Runtime.RemoteObject;
3536
*
3637
* https://chromedevtools.github.io/devtools-protocol/
3738
*/
38-
export class DevtoolsSession extends EventEmitter implements IDevtoolsSession {
39+
export class DevtoolsSession extends TypedEventEmitter<DevtoolsEvents> implements IDevtoolsSession {
3940
public connection: Connection;
4041
public messageEvents = new TypedEventEmitter<IMessageEvents>();
4142
public get id() {
@@ -88,7 +89,7 @@ export class DevtoolsSession extends EventEmitter implements IDevtoolsSession {
8889
onMessage(object: IDevtoolsResponseMessage & IDevtoolsEventMessage): void {
8990
this.messageEvents.emit('receive', { ...object });
9091
if (!object.id) {
91-
this.emit(object.method, object.params);
92+
this.emit(object.method as any, object.params);
9293
return;
9394
}
9495

0 commit comments

Comments
 (0)