File tree 5 files changed +49
-13
lines changed
5 files changed +49
-13
lines changed Original file line number Diff line number Diff line change @@ -2,6 +2,7 @@ import { ValidationMetadata } from './ValidationMetadata';
2
2
import { ConstraintMetadata } from './ConstraintMetadata' ;
3
3
import { ValidationSchema } from '../validation-schema/ValidationSchema' ;
4
4
import { ValidationSchemaToMetadataTransformer } from '../validation-schema/ValidationSchemaToMetadataTransformer' ;
5
+ import { getGlobal } from '../utils' ;
5
6
6
7
/**
7
8
* Storage all metadatas.
@@ -114,11 +115,11 @@ export class MetadataStorage {
114
115
* Metadata storage follows the best practices and stores metadata in a global variable.
115
116
*/
116
117
export function getMetadataStorage ( ) : MetadataStorage {
117
- if ( typeof window !== 'undefined' ) {
118
- window . global = window ;
118
+ const global = getGlobal ( ) ;
119
+
120
+ if ( ! global . classValidatorMetadataStorage ) {
121
+ global . classValidatorMetadataStorage = new MetadataStorage ( ) ;
119
122
}
120
- if ( ! ( global as any ) . classValidatorMetadataStorage )
121
- ( global as any ) . classValidatorMetadataStorage = new MetadataStorage ( ) ;
122
123
123
124
return ( global as any ) . classValidatorMetadataStorage ;
124
125
}
Original file line number Diff line number Diff line change
1
+ /**
2
+ * Convert Map, Set to Array
3
+ */
4
+ export function convertToArray < T > ( val : Array < T > | Set < T > | Map < any , T > ) : Array < T > {
5
+ if ( val instanceof Map ) {
6
+ return Array . from ( val . values ( ) ) ;
7
+ }
8
+ return Array . isArray ( val ) ? val : Array . from ( val ) ;
9
+ }
Original file line number Diff line number Diff line change
1
+ /**
2
+ * This function returns the global object across Node and browsers.
3
+ *
4
+ * Note: `globalThis` is the standardized approach however it has been added to
5
+ * Node.js in version 12. We need to include this snippet until Node 12 EOL.
6
+ */
7
+ export function getGlobal ( ) {
8
+ if ( typeof globalThis !== 'undefined' ) {
9
+ return globalThis ;
10
+ }
11
+
12
+ if ( typeof global !== 'undefined' ) {
13
+ return global ;
14
+ }
15
+
16
+ // eslint-disable-next-line @typescript-eslint/ban-ts-comment
17
+ // @ts -ignore: Cannot find name 'window'.
18
+ if ( typeof window !== 'undefined' ) {
19
+ // eslint-disable-next-line @typescript-eslint/ban-ts-comment
20
+ // @ts -ignore: Cannot find name 'window'.
21
+ return window ;
22
+ }
23
+
24
+ // eslint-disable-next-line @typescript-eslint/ban-ts-comment
25
+ // @ts -ignore: Cannot find name 'self'.
26
+ if ( typeof self !== 'undefined' ) {
27
+ // eslint-disable-next-line @typescript-eslint/ban-ts-comment
28
+ // @ts -ignore: Cannot find name 'self'.
29
+ return self ;
30
+ }
31
+ }
Original file line number Diff line number Diff line change
1
+ export * from './convert-to-array.util' ;
2
+ export * from './get-global.util' ;
3
+ export * from './is-promise.util'
Original file line number Diff line number Diff line change @@ -4,12 +4,4 @@ export function isPromise<T = any>(p: any): p is Promise<T> {
4
4
return p !== null && typeof p === 'object' && typeof p . then === 'function' ;
5
5
}
6
6
7
- /**
8
- * Convert Map, Set to Array
9
- */
10
- export function convertToArray < T > ( val : Array < T > | Set < T > | Map < any , T > ) : Array < T > {
11
- if ( val instanceof Map ) {
12
- return Array . from ( val . values ( ) ) ;
13
- }
14
- return Array . isArray ( val ) ? val : Array . from ( val ) ;
15
- }
7
+
You can’t perform that action at this time.
0 commit comments