Skip to content

Commit 7f5f1a3

Browse files
author
Thomas Reggi
authored
fix: adds interfaces for EJSON objects
NODE-2723
1 parent f55eeed commit 7f5f1a3

12 files changed

+92
-35
lines changed

src/binary.ts

+17-12
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,17 @@ import type { EJSONOptions } from './extended_json';
44

55
type BinarySequence = Uint8Array | Buffer | number[];
66

7-
export type BinaryEJSON = {
8-
$type?: string;
9-
$binary:
10-
| string
11-
| {
12-
subType: string;
13-
base64: string;
14-
};
15-
};
7+
export interface BinaryExtendedLegacy {
8+
$type: string;
9+
$binary: string;
10+
}
11+
12+
export interface BinaryExtended {
13+
$binary: {
14+
subType: string;
15+
base64: string;
16+
};
17+
}
1618

1719
/** A class representation of the BSON Binary type. */
1820
export class Binary {
@@ -200,7 +202,7 @@ export class Binary {
200202
}
201203

202204
/** @internal */
203-
toExtendedJSON(options?: EJSONOptions): BinaryEJSON {
205+
toExtendedJSON(options?: EJSONOptions): BinaryExtendedLegacy | BinaryExtended {
204206
options = options || {};
205207
const base64String = this.buffer.toString('base64');
206208

@@ -220,11 +222,14 @@ export class Binary {
220222
}
221223

222224
/** @internal */
223-
static fromExtendedJSON(doc: BinaryEJSON, options?: EJSONOptions): Binary {
225+
static fromExtendedJSON(
226+
doc: BinaryExtendedLegacy | BinaryExtended,
227+
options?: EJSONOptions
228+
): Binary {
224229
options = options || {};
225230
let data: Buffer | undefined;
226231
let type;
227-
if (options.legacy && typeof doc.$binary === 'string') {
232+
if (options.legacy && typeof doc.$binary === 'string' && '$type' in doc) {
228233
type = doc.$type ? parseInt(doc.$type, 16) : 0;
229234
data = Buffer.from(doc.$binary, 'base64');
230235
} else {

src/code.ts

+7-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
import type { Document } from './bson';
22

3+
export interface CodeExtended {
4+
$code: string | Function;
5+
$scope?: Document;
6+
}
7+
38
/** A class representation of the BSON Code type. */
49
export class Code {
510
_bsontype!: 'Code';
@@ -21,7 +26,7 @@ export class Code {
2126
}
2227

2328
/** @internal */
24-
toExtendedJSON(): { $code: string | Function; $scope?: Document } {
29+
toExtendedJSON(): CodeExtended {
2530
if (this.scope) {
2631
return { $code: this.code, $scope: this.scope };
2732
}
@@ -30,7 +35,7 @@ export class Code {
3035
}
3136

3237
/** @internal */
33-
static fromExtendedJSON(doc: { $code: string | Function; $scope?: Document }): Code {
38+
static fromExtendedJSON(doc: CodeExtended): Code {
3439
return new Code(doc.$code, doc.$scope);
3540
}
3641
}

src/decimal128.ts

+7-3
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,10 @@ function invalidErr(string: string, message: string) {
153153
throw new TypeError(`"${string}" is not a valid Decimal128 string - ${message}`);
154154
}
155155

156+
export interface Decimal128Extended {
157+
$numberDecimal: string;
158+
}
159+
156160
/** A class representation of the BSON Decimal128 type. */
157161
export class Decimal128 {
158162
_bsontype!: 'Decimal128';
@@ -766,17 +770,17 @@ export class Decimal128 {
766770
return string.join('');
767771
}
768772

769-
toJSON(): { $numberDecimal: string } {
773+
toJSON(): Decimal128Extended {
770774
return { $numberDecimal: this.toString() };
771775
}
772776

773777
/** @internal */
774-
toExtendedJSON(): { $numberDecimal: string } {
778+
toExtendedJSON(): Decimal128Extended {
775779
return { $numberDecimal: this.toString() };
776780
}
777781

778782
/** @internal */
779-
static fromExtendedJSON(doc: { $numberDecimal: string }): Decimal128 {
783+
static fromExtendedJSON(doc: Decimal128Extended): Decimal128 {
780784
return Decimal128.fromString(doc.$numberDecimal);
781785
}
782786
}

src/double.ts

+6-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
import type { EJSONOptions } from './extended_json';
22

3+
export interface DoubleExtended {
4+
$numberDouble: string;
5+
}
6+
37
/**
48
* A class representation of the BSON Double type.
59
*/
@@ -35,7 +39,7 @@ export class Double {
3539
}
3640

3741
/** @internal */
38-
toExtendedJSON(options?: EJSONOptions): number | { $numberDouble: string } {
42+
toExtendedJSON(options?: EJSONOptions): number | DoubleExtended {
3943
if (options && (options.legacy || (options.relaxed && isFinite(this.value)))) {
4044
return this.value;
4145
}
@@ -60,7 +64,7 @@ export class Double {
6064
}
6165

6266
/** @internal */
63-
static fromExtendedJSON(doc: { $numberDouble: string }, options?: EJSONOptions): number | Double {
67+
static fromExtendedJSON(doc: DoubleExtended, options?: EJSONOptions): number | Double {
6468
const doubleValue = parseFloat(doc.$numberDouble);
6569
return options && options.relaxed ? doubleValue : new Double(doubleValue);
6670
}

src/int_32.ts

+6-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
import type { EJSONOptions } from './extended_json';
22

3+
export interface Int32Extended {
4+
$numberInt: string;
5+
}
6+
37
/** A class representation of a BSON Int32 type. */
48
export class Int32 {
59
_bsontype!: 'Int32';
@@ -33,13 +37,13 @@ export class Int32 {
3337
}
3438

3539
/** @internal */
36-
toExtendedJSON(options?: EJSONOptions): number | { $numberInt: string } {
40+
toExtendedJSON(options?: EJSONOptions): number | Int32Extended {
3741
if (options && (options.relaxed || options.legacy)) return this.value;
3842
return { $numberInt: this.value.toString() };
3943
}
4044

4145
/** @internal */
42-
static fromExtendedJSON(doc: { $numberInt: string }, options?: EJSONOptions): number | Int32 {
46+
static fromExtendedJSON(doc: Int32Extended, options?: EJSONOptions): number | Int32 {
4347
return options && options.relaxed ? parseInt(doc.$numberInt, 10) : new Int32(doc.$numberInt);
4448
}
4549
}

src/long.ts

+5-1
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,10 @@ const INT_CACHE: { [key: number]: Long } = {};
5050
/** A cache of the Long representations of small unsigned integer values. */
5151
const UINT_CACHE: { [key: number]: Long } = {};
5252

53+
export interface LongExtended {
54+
$numberLong: string;
55+
}
56+
5357
/**
5458
* A class representing a 64-bit integer
5559
* @remarks
@@ -913,7 +917,7 @@ export class Long {
913917
* BSON SPECIFIC ADDITIONS *
914918
****************************************************************
915919
*/
916-
toExtendedJSON(options?: EJSONOptions): number | { $numberLong: string } {
920+
toExtendedJSON(options?: EJSONOptions): number | LongExtended {
917921
if (options && options.relaxed) return this.toNumber();
918922
return { $numberLong: this.toString() };
919923
}

src/max_key.ts

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
1+
interface MaxKeyExtended {
2+
$maxKey: 1;
3+
}
4+
15
/**
26
* A class representation of the BSON MaxKey type.
37
*/
48
export class MaxKey {
59
_bsontype!: 'MaxKey';
610

711
/** @internal */
8-
toExtendedJSON(): { $maxKey: 1 } {
12+
toExtendedJSON(): MaxKeyExtended {
913
return { $maxKey: 1 };
1014
}
1115

src/min_key.ts

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
1+
interface MinKeyExtended {
2+
$minKey: 1;
3+
}
4+
15
/**
26
* A class representation of the BSON MinKey type.
37
*/
48
export class MinKey {
59
_bsontype!: 'MinKey';
610

711
/** @internal */
8-
toExtendedJSON(): { $minKey: 1 } {
12+
toExtendedJSON(): MinKeyExtended {
913
return { $minKey: 1 };
1014
}
1115

src/objectid.ts

+6-2
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,10 @@ export interface ObjectIdLike {
3030
toHexString(): string;
3131
}
3232

33+
export interface ObjectIdExtended {
34+
$oid: string;
35+
}
36+
3337
const kId = Symbol('id');
3438

3539
/**
@@ -331,13 +335,13 @@ export class ObjectId {
331335
}
332336

333337
/** @internal */
334-
toExtendedJSON(): { $oid: string } {
338+
toExtendedJSON(): ObjectIdExtended {
335339
if (this.toHexString) return { $oid: this.toHexString() };
336340
return { $oid: this.toString('hex') };
337341
}
338342

339343
/** @internal */
340-
static fromExtendedJSON(doc: { $oid: string }): ObjectId {
344+
static fromExtendedJSON(doc: ObjectIdExtended): ObjectId {
341345
return new ObjectId(doc.$oid);
342346
}
343347
}

src/regexp.ts

+13-5
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,17 @@ function alphabetize(str: string): string {
44
return str.split('').sort().join('');
55
}
66

7-
export type BSONRegExpEJSON =
8-
| { $regex: string | BSONRegExp; $options: string }
9-
| { $regularExpression: { pattern: string; options: string } };
7+
export interface BSONRegExpExtendedLegacy {
8+
$regex: string | BSONRegExp;
9+
$options: string;
10+
}
11+
12+
export interface BSONRegExpExtended {
13+
$regularExpression: {
14+
pattern: string;
15+
options: string;
16+
};
17+
}
1018

1119
/** A class representation of the BSON RegExp type. */
1220
export class BSONRegExp {
@@ -46,7 +54,7 @@ export class BSONRegExp {
4654
}
4755

4856
/** @internal */
49-
toExtendedJSON(options?: EJSONOptions): BSONRegExpEJSON {
57+
toExtendedJSON(options?: EJSONOptions): BSONRegExpExtendedLegacy | BSONRegExpExtended {
5058
options = options || {};
5159
if (options.legacy) {
5260
return { $regex: this.pattern, $options: this.options };
@@ -55,7 +63,7 @@ export class BSONRegExp {
5563
}
5664

5765
/** @internal */
58-
static fromExtendedJSON(doc: BSONRegExpEJSON): BSONRegExp {
66+
static fromExtendedJSON(doc: BSONRegExpExtendedLegacy | BSONRegExpExtended): BSONRegExp {
5967
if ('$regex' in doc) {
6068
if (typeof doc.$regex !== 'string') {
6169
// This is for $regex query operators that have extended json values.

src/symbol.ts

+6-2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
export interface BSONSymbolExtended {
2+
$symbol: string;
3+
}
4+
15
/** A class representation of the BSON Symbol type. */
26
export class BSONSymbol {
37
_bsontype!: 'Symbol';
@@ -31,12 +35,12 @@ export class BSONSymbol {
3135
}
3236

3337
/** @internal */
34-
toExtendedJSON(): { $symbol: string } {
38+
toExtendedJSON(): BSONSymbolExtended {
3539
return { $symbol: this.value };
3640
}
3741

3842
/** @internal */
39-
static fromExtendedJSON(doc: { $symbol: string }): BSONSymbol {
43+
static fromExtendedJSON(doc: BSONSymbolExtended): BSONSymbol {
4044
return new BSONSymbol(doc.$symbol);
4145
}
4246
}

src/timestamp.ts

+9-2
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,13 @@ type LongWithoutOverrides = new (low: number | Long, high?: number, unsigned?: b
66
};
77
const LongWithoutOverridesClass: LongWithoutOverrides = (Long as unknown) as LongWithoutOverrides;
88

9+
export interface TimestampExtended {
10+
$timestamp: {
11+
t: number;
12+
i: number;
13+
};
14+
}
15+
916
export class Timestamp extends LongWithoutOverridesClass {
1017
_bsontype!: 'Timestamp';
1118

@@ -71,12 +78,12 @@ export class Timestamp extends LongWithoutOverridesClass {
7178
}
7279

7380
/** @internal */
74-
toExtendedJSON(): { $timestamp: { t: number; i: number } } {
81+
toExtendedJSON(): TimestampExtended {
7582
return { $timestamp: { t: this.high >>> 0, i: this.low >>> 0 } };
7683
}
7784

7885
/** @internal */
79-
static fromExtendedJSON(doc: { $timestamp: { t: number; i: number } }): Timestamp {
86+
static fromExtendedJSON(doc: TimestampExtended): Timestamp {
8087
return new Timestamp(doc.$timestamp.i, doc.$timestamp.t);
8188
}
8289
}

0 commit comments

Comments
 (0)