File tree Expand file tree Collapse file tree 12 files changed +92
-35
lines changed Expand file tree Collapse file tree 12 files changed +92
-35
lines changed Original file line number Diff line number Diff line change @@ -4,15 +4,17 @@ import type { EJSONOptions } from './extended_json';
44
55type 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. */
1820export 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 {
Original file line number Diff line number Diff line change 11import 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. */
49export 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}
Original file line number Diff line number Diff 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. */
157161export 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}
Original file line number Diff line number Diff line change 11import 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 }
Original file line number Diff line number Diff line change 11import type { EJSONOptions } from './extended_json' ;
22
3+ export interface Int32Extended {
4+ $numberInt : string ;
5+ }
6+
37/** A class representation of a BSON Int32 type. */
48export 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}
Original file line number Diff line number Diff line change @@ -50,6 +50,10 @@ const INT_CACHE: { [key: number]: Long } = {};
5050/** A cache of the Long representations of small unsigned integer values. */
5151const 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 }
Original file line number Diff line number Diff line change 1+ interface MaxKeyExtended {
2+ $maxKey : 1 ;
3+ }
4+
15/**
26 * A class representation of the BSON MaxKey type.
37 */
48export class MaxKey {
59 _bsontype ! : 'MaxKey' ;
610
711 /** @internal */
8- toExtendedJSON ( ) : { $maxKey : 1 } {
12+ toExtendedJSON ( ) : MaxKeyExtended {
913 return { $maxKey : 1 } ;
1014 }
1115
Original file line number Diff line number Diff line change 1+ interface MinKeyExtended {
2+ $minKey : 1 ;
3+ }
4+
15/**
26 * A class representation of the BSON MinKey type.
37 */
48export class MinKey {
59 _bsontype ! : 'MinKey' ;
610
711 /** @internal */
8- toExtendedJSON ( ) : { $minKey : 1 } {
12+ toExtendedJSON ( ) : MinKeyExtended {
913 return { $minKey : 1 } ;
1014 }
1115
Original file line number Diff line number Diff line change @@ -30,6 +30,10 @@ export interface ObjectIdLike {
3030 toHexString ( ) : string ;
3131}
3232
33+ export interface ObjectIdExtended {
34+ $oid : string ;
35+ }
36+
3337const 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}
Original file line number Diff line number Diff 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. */
1220export 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.
You can’t perform that action at this time.
0 commit comments