Skip to content

Commit 072b200

Browse files
committed
fix: type overloads for command
1 parent caab9b7 commit 072b200

File tree

4 files changed

+16
-25
lines changed

4 files changed

+16
-25
lines changed

src/cmap/connection.ts

Lines changed: 10 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ import type { ClientMetadata } from './handshake/client_metadata';
6262
import { StreamDescription, type StreamDescriptionOptions } from './stream_description';
6363
import { type CompressorName, decompressResponse } from './wire_protocol/compression';
6464
import { onData } from './wire_protocol/on_data';
65-
import { MongoDBResponse } from './wire_protocol/responses';
65+
import { MongoDBResponse, type MongoDBResponseConstructor } from './wire_protocol/responses';
6666
import { getReadPreference, isSharded } from './wire_protocol/shared';
6767

6868
/** @internal */
@@ -416,7 +416,7 @@ export class Connection extends TypedEventEmitter<ConnectionEvents> {
416416
private async *sendWire(
417417
message: WriteProtocolMessageType,
418418
options: CommandOptions,
419-
responseType?: typeof MongoDBResponse
419+
responseType?: MongoDBResponseConstructor
420420
): AsyncGenerator<MongoDBResponse> {
421421
this.throwIfAborted();
422422

@@ -462,8 +462,8 @@ export class Connection extends TypedEventEmitter<ConnectionEvents> {
462462
private async *sendCommand(
463463
ns: MongoDBNamespace,
464464
command: Document,
465-
options: CommandOptions = {},
466-
responseType?: typeof MongoDBResponse
465+
options: CommandOptions,
466+
responseType?: MongoDBResponseConstructor
467467
) {
468468
const message = this.prepareCommand(ns.db, command, options);
469469

@@ -574,32 +574,24 @@ export class Connection extends TypedEventEmitter<ConnectionEvents> {
574574
}
575575
}
576576

577-
public async command<T extends typeof MongoDBResponse>(
577+
public async command<T extends MongoDBResponseConstructor>(
578578
ns: MongoDBNamespace,
579579
command: Document,
580580
options: CommandOptions | undefined,
581581
responseType: T | undefined
582-
): Promise<T extends undefined ? Document : InstanceType<T>>;
583-
584-
public async command(
585-
ns: MongoDBNamespace,
586-
command: Document,
587-
options?: undefined
588-
): Promise<Document>;
582+
): Promise<typeof responseType extends undefined ? Document : InstanceType<T>>;
589583

590584
public async command(
591585
ns: MongoDBNamespace,
592586
command: Document,
593-
options: CommandOptions
587+
options?: CommandOptions
594588
): Promise<Document>;
595589

596-
public async command(ns: MongoDBNamespace, command: Document): Promise<Document>;
597-
598590
public async command(
599591
ns: MongoDBNamespace,
600592
command: Document,
601593
options: CommandOptions = {},
602-
responseType?: typeof MongoDBResponse
594+
responseType?: MongoDBResponseConstructor
603595
): Promise<Document> {
604596
this.throwIfAborted();
605597
for await (const document of this.sendCommand(ns, command, options, responseType)) {
@@ -728,7 +720,7 @@ export class CryptoConnection extends Connection {
728720
this.autoEncrypter = options.autoEncrypter;
729721
}
730722

731-
public override async command<T extends typeof MongoDBResponse>(
723+
public override async command<T extends MongoDBResponseConstructor>(
732724
ns: MongoDBNamespace,
733725
command: Document,
734726
options: CommandOptions | undefined,
@@ -741,9 +733,7 @@ export class CryptoConnection extends Connection {
741733
options?: CommandOptions
742734
): Promise<Document>;
743735

744-
public override async command(ns: MongoDBNamespace, command: Document): Promise<Document>;
745-
746-
override async command<T extends typeof MongoDBResponse>(
736+
override async command<T extends MongoDBResponseConstructor>(
747737
ns: MongoDBNamespace,
748738
cmd: Document,
749739
options?: CommandOptions,

src/cmap/wire_protocol/responses.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@ import { type BSONSerializeOptions, BSONType, type Document, type Timestamp } fr
22
import { type ClusterTime } from '../../sdam/common';
33
import { OnDemandDocument } from './on_demand/document';
44

5+
/** @internal */
6+
export type MongoDBResponseConstructor = {
7+
new (bson: Uint8Array, offset?: number, isArray?: boolean): MongoDBResponse;
8+
};
9+
510
/** @internal */
611
export class MongoDBResponse extends OnDemandDocument {
712
// {ok:1}

src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -290,7 +290,7 @@ export type { ConnectionPoolMetrics } from './cmap/metrics';
290290
export type { StreamDescription, StreamDescriptionOptions } from './cmap/stream_description';
291291
export type { CompressorName } from './cmap/wire_protocol/compression';
292292
export type { JSTypeOf, OnDemandDocument } from './cmap/wire_protocol/on_demand/document';
293-
export type { MongoDBResponse } from './cmap/wire_protocol/responses';
293+
export type { MongoDBResponse, MongoDBResponseConstructor } from './cmap/wire_protocol/responses';
294294
export type { CollectionOptions, CollectionPrivate, ModifyResult } from './collection';
295295
export type {
296296
COMMAND_FAILED,

test/types/connection.test-d.ts

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,6 @@ declare const connection: Connection;
77
expectType<Document>(await connection.command(ns('a'), { cmd: 1 }));
88
expectType<Document>(await connection.command(ns('a'), { cmd: 1 }, undefined));
99
expectType<Document>(await connection.command(ns('a'), { cmd: 1 }, { socketTimeoutMS: 1 }));
10-
// TODO fix TS or simplify arguments
11-
// expectType<Document>(
12-
// await connection.command<undefined>(ns('a'), { cmd: 1 }, { socketTimeoutMS: 1 }, undefined)
13-
// );
1410

1511
class A extends MongoDBResponse {
1612
myProperty = 0n;

0 commit comments

Comments
 (0)