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';
4
4
5
5
type BinarySequence = Uint8Array | Buffer | number [ ] ;
6
6
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
+ }
16
18
17
19
/** A class representation of the BSON Binary type. */
18
20
export class Binary {
@@ -200,7 +202,7 @@ export class Binary {
200
202
}
201
203
202
204
/** @internal */
203
- toExtendedJSON ( options ?: EJSONOptions ) : BinaryEJSON {
205
+ toExtendedJSON ( options ?: EJSONOptions ) : BinaryExtendedLegacy | BinaryExtended {
204
206
options = options || { } ;
205
207
const base64String = this . buffer . toString ( 'base64' ) ;
206
208
@@ -220,11 +222,14 @@ export class Binary {
220
222
}
221
223
222
224
/** @internal */
223
- static fromExtendedJSON ( doc : BinaryEJSON , options ?: EJSONOptions ) : Binary {
225
+ static fromExtendedJSON (
226
+ doc : BinaryExtendedLegacy | BinaryExtended ,
227
+ options ?: EJSONOptions
228
+ ) : Binary {
224
229
options = options || { } ;
225
230
let data : Buffer | undefined ;
226
231
let type ;
227
- if ( options . legacy && typeof doc . $binary === 'string' ) {
232
+ if ( options . legacy && typeof doc . $binary === 'string' && '$type' in doc ) {
228
233
type = doc . $type ? parseInt ( doc . $type , 16 ) : 0 ;
229
234
data = Buffer . from ( doc . $binary , 'base64' ) ;
230
235
} else {
Original file line number Diff line number Diff line change 1
1
import type { Document } from './bson' ;
2
2
3
+ export interface CodeExtended {
4
+ $code : string | Function ;
5
+ $scope ?: Document ;
6
+ }
7
+
3
8
/** A class representation of the BSON Code type. */
4
9
export class Code {
5
10
_bsontype ! : 'Code' ;
@@ -21,7 +26,7 @@ export class Code {
21
26
}
22
27
23
28
/** @internal */
24
- toExtendedJSON ( ) : { $code : string | Function ; $scope ?: Document } {
29
+ toExtendedJSON ( ) : CodeExtended {
25
30
if ( this . scope ) {
26
31
return { $code : this . code , $scope : this . scope } ;
27
32
}
@@ -30,7 +35,7 @@ export class Code {
30
35
}
31
36
32
37
/** @internal */
33
- static fromExtendedJSON ( doc : { $code : string | Function ; $scope ?: Document } ) : Code {
38
+ static fromExtendedJSON ( doc : CodeExtended ) : Code {
34
39
return new Code ( doc . $code , doc . $scope ) ;
35
40
}
36
41
}
Original file line number Diff line number Diff line change @@ -153,6 +153,10 @@ function invalidErr(string: string, message: string) {
153
153
throw new TypeError ( `"${ string } " is not a valid Decimal128 string - ${ message } ` ) ;
154
154
}
155
155
156
+ export interface Decimal128Extended {
157
+ $numberDecimal : string ;
158
+ }
159
+
156
160
/** A class representation of the BSON Decimal128 type. */
157
161
export class Decimal128 {
158
162
_bsontype ! : 'Decimal128' ;
@@ -766,17 +770,17 @@ export class Decimal128 {
766
770
return string . join ( '' ) ;
767
771
}
768
772
769
- toJSON ( ) : { $numberDecimal : string } {
773
+ toJSON ( ) : Decimal128Extended {
770
774
return { $numberDecimal : this . toString ( ) } ;
771
775
}
772
776
773
777
/** @internal */
774
- toExtendedJSON ( ) : { $numberDecimal : string } {
778
+ toExtendedJSON ( ) : Decimal128Extended {
775
779
return { $numberDecimal : this . toString ( ) } ;
776
780
}
777
781
778
782
/** @internal */
779
- static fromExtendedJSON ( doc : { $numberDecimal : string } ) : Decimal128 {
783
+ static fromExtendedJSON ( doc : Decimal128Extended ) : Decimal128 {
780
784
return Decimal128 . fromString ( doc . $numberDecimal ) ;
781
785
}
782
786
}
Original file line number Diff line number Diff line change 1
1
import type { EJSONOptions } from './extended_json' ;
2
2
3
+ export interface DoubleExtended {
4
+ $numberDouble : string ;
5
+ }
6
+
3
7
/**
4
8
* A class representation of the BSON Double type.
5
9
*/
@@ -35,7 +39,7 @@ export class Double {
35
39
}
36
40
37
41
/** @internal */
38
- toExtendedJSON ( options ?: EJSONOptions ) : number | { $numberDouble : string } {
42
+ toExtendedJSON ( options ?: EJSONOptions ) : number | DoubleExtended {
39
43
if ( options && ( options . legacy || ( options . relaxed && isFinite ( this . value ) ) ) ) {
40
44
return this . value ;
41
45
}
@@ -60,7 +64,7 @@ export class Double {
60
64
}
61
65
62
66
/** @internal */
63
- static fromExtendedJSON ( doc : { $numberDouble : string } , options ?: EJSONOptions ) : number | Double {
67
+ static fromExtendedJSON ( doc : DoubleExtended , options ?: EJSONOptions ) : number | Double {
64
68
const doubleValue = parseFloat ( doc . $numberDouble ) ;
65
69
return options && options . relaxed ? doubleValue : new Double ( doubleValue ) ;
66
70
}
Original file line number Diff line number Diff line change 1
1
import type { EJSONOptions } from './extended_json' ;
2
2
3
+ export interface Int32Extended {
4
+ $numberInt : string ;
5
+ }
6
+
3
7
/** A class representation of a BSON Int32 type. */
4
8
export class Int32 {
5
9
_bsontype ! : 'Int32' ;
@@ -33,13 +37,13 @@ export class Int32 {
33
37
}
34
38
35
39
/** @internal */
36
- toExtendedJSON ( options ?: EJSONOptions ) : number | { $numberInt : string } {
40
+ toExtendedJSON ( options ?: EJSONOptions ) : number | Int32Extended {
37
41
if ( options && ( options . relaxed || options . legacy ) ) return this . value ;
38
42
return { $numberInt : this . value . toString ( ) } ;
39
43
}
40
44
41
45
/** @internal */
42
- static fromExtendedJSON ( doc : { $numberInt : string } , options ?: EJSONOptions ) : number | Int32 {
46
+ static fromExtendedJSON ( doc : Int32Extended , options ?: EJSONOptions ) : number | Int32 {
43
47
return options && options . relaxed ? parseInt ( doc . $numberInt , 10 ) : new Int32 ( doc . $numberInt ) ;
44
48
}
45
49
}
Original file line number Diff line number Diff line change @@ -50,6 +50,10 @@ const INT_CACHE: { [key: number]: Long } = {};
50
50
/** A cache of the Long representations of small unsigned integer values. */
51
51
const UINT_CACHE : { [ key : number ] : Long } = { } ;
52
52
53
+ export interface LongExtended {
54
+ $numberLong : string ;
55
+ }
56
+
53
57
/**
54
58
* A class representing a 64-bit integer
55
59
* @remarks
@@ -913,7 +917,7 @@ export class Long {
913
917
* BSON SPECIFIC ADDITIONS *
914
918
****************************************************************
915
919
*/
916
- toExtendedJSON ( options ?: EJSONOptions ) : number | { $numberLong : string } {
920
+ toExtendedJSON ( options ?: EJSONOptions ) : number | LongExtended {
917
921
if ( options && options . relaxed ) return this . toNumber ( ) ;
918
922
return { $numberLong : this . toString ( ) } ;
919
923
}
Original file line number Diff line number Diff line change
1
+ interface MaxKeyExtended {
2
+ $maxKey : 1 ;
3
+ }
4
+
1
5
/**
2
6
* A class representation of the BSON MaxKey type.
3
7
*/
4
8
export class MaxKey {
5
9
_bsontype ! : 'MaxKey' ;
6
10
7
11
/** @internal */
8
- toExtendedJSON ( ) : { $maxKey : 1 } {
12
+ toExtendedJSON ( ) : MaxKeyExtended {
9
13
return { $maxKey : 1 } ;
10
14
}
11
15
Original file line number Diff line number Diff line change
1
+ interface MinKeyExtended {
2
+ $minKey : 1 ;
3
+ }
4
+
1
5
/**
2
6
* A class representation of the BSON MinKey type.
3
7
*/
4
8
export class MinKey {
5
9
_bsontype ! : 'MinKey' ;
6
10
7
11
/** @internal */
8
- toExtendedJSON ( ) : { $minKey : 1 } {
12
+ toExtendedJSON ( ) : MinKeyExtended {
9
13
return { $minKey : 1 } ;
10
14
}
11
15
Original file line number Diff line number Diff line change @@ -30,6 +30,10 @@ export interface ObjectIdLike {
30
30
toHexString ( ) : string ;
31
31
}
32
32
33
+ export interface ObjectIdExtended {
34
+ $oid : string ;
35
+ }
36
+
33
37
const kId = Symbol ( 'id' ) ;
34
38
35
39
/**
@@ -331,13 +335,13 @@ export class ObjectId {
331
335
}
332
336
333
337
/** @internal */
334
- toExtendedJSON ( ) : { $oid : string } {
338
+ toExtendedJSON ( ) : ObjectIdExtended {
335
339
if ( this . toHexString ) return { $oid : this . toHexString ( ) } ;
336
340
return { $oid : this . toString ( 'hex' ) } ;
337
341
}
338
342
339
343
/** @internal */
340
- static fromExtendedJSON ( doc : { $oid : string } ) : ObjectId {
344
+ static fromExtendedJSON ( doc : ObjectIdExtended ) : ObjectId {
341
345
return new ObjectId ( doc . $oid ) ;
342
346
}
343
347
}
Original file line number Diff line number Diff line change @@ -4,9 +4,17 @@ function alphabetize(str: string): string {
4
4
return str . split ( '' ) . sort ( ) . join ( '' ) ;
5
5
}
6
6
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
+ }
10
18
11
19
/** A class representation of the BSON RegExp type. */
12
20
export class BSONRegExp {
@@ -46,7 +54,7 @@ export class BSONRegExp {
46
54
}
47
55
48
56
/** @internal */
49
- toExtendedJSON ( options ?: EJSONOptions ) : BSONRegExpEJSON {
57
+ toExtendedJSON ( options ?: EJSONOptions ) : BSONRegExpExtendedLegacy | BSONRegExpExtended {
50
58
options = options || { } ;
51
59
if ( options . legacy ) {
52
60
return { $regex : this . pattern , $options : this . options } ;
@@ -55,7 +63,7 @@ export class BSONRegExp {
55
63
}
56
64
57
65
/** @internal */
58
- static fromExtendedJSON ( doc : BSONRegExpEJSON ) : BSONRegExp {
66
+ static fromExtendedJSON ( doc : BSONRegExpExtendedLegacy | BSONRegExpExtended ) : BSONRegExp {
59
67
if ( '$regex' in doc ) {
60
68
if ( typeof doc . $regex !== 'string' ) {
61
69
// This is for $regex query operators that have extended json values.
You can’t perform that action at this time.
0 commit comments