Skip to content

Commit

Permalink
Moved global injecting code to a separate file
Browse files Browse the repository at this point in the history
  • Loading branch information
kraenhansen committed May 26, 2023
1 parent d709f3b commit 9c3968c
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 310 deletions.
310 changes: 0 additions & 310 deletions packages/realm/src/Realm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,6 @@ import {
mapTo,
normalizeObjectSchema,
normalizeRealmSchema,
safeGlobalThis,
toArrayBuffer,
toBindingLoggerLevel,
toBindingSchema,
Expand Down Expand Up @@ -1725,315 +1724,6 @@ export declare namespace Realm {
}
}

// Exporting a deprecated global for backwards compatibility
const RealmConstructor = Realm;
declare global {
/** @deprecated Will be removed in v13.0.0. Please use named imports */
export class Realm extends RealmConstructor {}
// IMPORTANT: This needs to match the namespace above!
// eslint-disable-next-line @typescript-eslint/no-namespace
export namespace Realm {
// TODO: Decide if we want to deprecate this as well
export type Object<T = DefaultObject> = RealmObject<T>;
export {
// Pure type exports below
/** @deprecated Please use named imports */
AppType as App,
/** @deprecated Please use named imports */
AppChangeCallback,
/** @deprecated Please use named imports */
AppConfiguration,
/** @deprecated Please use named imports */
BaseConfiguration,
/** @deprecated Please use named imports */
BaseObjectSchema,
/** @deprecated Please use named imports */
BaseSyncConfiguration,
/** @deprecated Please use named imports */
BSONType as BSON,
/** @deprecated Please use named imports */
CanonicalObjectSchema,
/** @deprecated Will be removed in v13.0.0. Please use {@link CanonicalPropertySchema} as a named import */
CanonicalObjectSchemaProperty,
/** @deprecated Please use named imports */
CanonicalPropertySchema,
/** @deprecated Please use named imports */
CanonicalPropertiesTypes,
/** @deprecated Please use named imports */
ClientResetModeType as ClientResetMode,
/** @deprecated Please use named imports */
ClientResetFallbackCallback,
/** @deprecated Please use named imports */
ClientResetBeforeCallback,
/** @deprecated Please use named imports */
ClientResetAfterCallback,
/** @deprecated Please use named imports */
ClientResetManualConfiguration,
/** @deprecated Please use named imports */
ClientResetDiscardUnsyncedChangesConfiguration,
/** @deprecated Please use named imports */
ClientResetRecoverUnsyncedChangesConfiguration,
/**
* This type got renamed to {@link ClientResetRecoverUnsyncedChangesConfiguration}
* @deprecated Please use named imports
*/
ClientResetRecoverUnsyncedChangesConfiguration as ClientResetRecoveryConfiguration,
/** @deprecated Please use named imports */
ClientResetRecoverOrDiscardUnsyncedChangesConfiguration,
/** @deprecated Please use named imports */
ClientResetConfig,
/** @deprecated Please use named imports */
CollectionChangeCallback,
/** @deprecated Please use named imports */
CollectionChangeSet,
/** @deprecated Please use named imports */
CollectionType as Collection,
/** @deprecated Please use named imports */
ConfigurationWithoutSync,
/** @deprecated Please use named imports */
ConfigurationWithSync,
/** @deprecated Please use named imports */
Configuration,
/** @deprecated Please use named imports */
ConnectionNotificationCallback,
/** @deprecated Please use named imports */
ConnectionStateType as ConnectionState,
/** @deprecated Please use named imports */
CredentialsType as Credentials,
/** @deprecated Please use named imports */
DefaultFunctionsFactory,
/** @deprecated Please use named imports */
DefaultUserProfileData,
/** @deprecated Please use named imports */
DictionaryType as Dictionary,
/** @deprecated Please use named imports */
DictionaryChangeCallback,
/** @deprecated Please use named imports */
DictionaryChangeSet,
/** @deprecated Please use named imports */
ErrorCallback,
/** @deprecated Please use named imports */
FlexibleSyncConfiguration,
/** @deprecated Please use named imports */
ListType as List,
/** @deprecated Please use named imports */
LocalAppConfiguration,
/** @deprecated Please use named imports */
MigrationCallback,
/** @deprecated Please use named imports */
Mixed,
/** @deprecated Please use named imports */
ObjectChangeCallback,
/** @deprecated Please use named imports */
ObjectChangeSet,
/** @deprecated Please use named imports */
ObjectSchema,
/**
* @deprecated Will be removed in v13.0.0. Please use {@link PropertySchema}.
*/
ObjectSchemaProperty,
/** @deprecated Please use named imports */
ObjectType,
/** @deprecated Please use named imports */
OpenRealmBehaviorConfiguration,
/** @deprecated Please use named imports */
OpenRealmBehaviorTypeType as OpenRealmBehaviorType,
/** @deprecated Please use named imports */
OpenRealmTimeOutBehaviorType as OpenRealmTimeOutBehavior,

/** @deprecated Please use named imports */
PartitionSyncConfiguration,
/** @deprecated Please use named imports */
PrimaryKey,
/** @deprecated Please use named imports */
ProgressDirectionType as ProgressDirection,
/** @deprecated Please use named imports */
ProgressModeType as ProgressMode,
/** @deprecated Please use named imports */
ProgressNotificationCallback,
/** @deprecated Please use named imports */
PropertiesTypes,
/** @deprecated Please use named imports */
PropertySchema,
/** @deprecated Please use named imports */
PropertySchemaShorthand,
/** @deprecated Please use named imports */
ProviderTypeType as ProviderType,
/** @deprecated Please use named imports */
RealmFunction,
/** @deprecated Please use named imports */
RealmObjectConstructor,
/**
* This type got renamed to RealmObjectConstructor
* @deprecated Please use named imports
*/
RealmObjectConstructor as ObjectClass,
/** @deprecated Please use named imports */
ResultsType as Results,
/** @deprecated Please use named imports */
SessionStateType as SessionState,
/** @deprecated Please use named imports */
SessionStopPolicyType as SessionStopPolicy,
/** @deprecated Please use named imports */
SetType as Set,
// TODO: Add these once we've implemented the SSL config for the sync client
// SSLVerifyObject,
// SSLVerifyCallback,
// SSLConfiguration,
/** @deprecated Please use named imports */
SortDescriptor,
/** @deprecated Please use named imports */
SyncConfiguration,
/** @deprecated Please use named imports */
SyncErrorType as SyncError,
/** @deprecated Please use named imports */
TypesType as Types,
/** @deprecated Please use named imports */
UpdateModeType as UpdateMode,
/** @deprecated Please use named imports */
UserType as User,
/** @deprecated Please use named imports */
UserChangeCallback,
/** @deprecated Please use named imports */
UserStateType as UserState,
};

/** @deprecated Please use named imports */
// eslint-disable-next-line @typescript-eslint/no-namespace
export namespace App {
/** @deprecated Please use named imports */
export type Credentials = CredentialsType;
/** @deprecated Please use named imports */
// eslint-disable-next-line @typescript-eslint/no-namespace
export namespace Sync {
/** @deprecated Please use named imports */
export type BaseSubscriptionSet = BaseSubscriptionSetType;
/** @deprecated Please use named imports */
export type LogLevel = LogLevelType;
/** @deprecated Please use named imports */
export type NumericLogLevel = NumericLogLevelType;
/** @deprecated Please use named imports */
export type MutableSubscriptionSet = MutableSubscriptionSetType;
/** @deprecated Please use named imports */
export type PartitionValue = PartitionValueType;
/** @deprecated Please use named imports */
export type SubscriptionOptions = SubscriptionOptionsType;
/** @deprecated Please use named imports */
export type SubscriptionSet = SubscriptionSetType;
/** @deprecated Please use named imports */
export type SubscriptionSetState = SubscriptionSetStateType;
/** @deprecated Please use {@link SubscriptionSetState} as a named import */
export type SubscriptionsState = SubscriptionSetStateType;
/** @deprecated Please use named imports */
export type Subscription = SubscriptionType;
/** @deprecated Please use named imports */
export type SyncSession = SyncSessionType;
/**
* @deprecated Got renamed to {@SyncSession} and please use named imports
*/
export type Session = SyncSessionType;
}
}

/** @deprecated Please use named imports */
// eslint-disable-next-line @typescript-eslint/no-namespace
export namespace BSON {
/** @deprecated Please use named imports */
export type ObjectId = ObjectIdType;
/** @deprecated Please use named imports */
export type Decimal128 = Decimal128Type;
/** @deprecated Please use named imports */
export type UUID = UUIDType;
}

/** @deprecated Please use named imports */
// eslint-disable-next-line @typescript-eslint/no-namespace
export namespace Auth {
/**
* @deprecated Got renamed to {@link EmailPasswordAuth} and please use named imports
*/
export type EmailPasswordAuth = EmailPasswordAuthType;
/** @deprecated Please use named imports */
export type ApiKey = ApiKeyType;
/**
* @deprecated Got renamed to {@link ApiKeyAuth} and please use named imports
*/
export type ApiKeyAuth = ApiKeyAuthType;
}

/** @deprecated Please use named imports */
// eslint-disable-next-line @typescript-eslint/no-namespace
export namespace Services {
// TODO: Fill in once the MongoDB client has stabilized

/**
* @deprecated Got renamed to {@link PushClient} and please use named imports
*/
export type Push = PushClient;
}

/** @deprecated Please use named imports */
// eslint-disable-next-line @typescript-eslint/no-namespace
export namespace Types {
/** @deprecated Please use named imports */
export type Bool = boolean;
/** @deprecated Please use named imports */
export type String = string;
/** @deprecated Please use named imports */
export type Int = number;
/** @deprecated Please use named imports */
export type Float = number;
/** @deprecated Please use named imports */
export type Double = number;
/** @deprecated Please use named imports */
export type Decimal128 = Realm.BSON.Decimal128;
/** @deprecated Please use named imports */
export type ObjectId = Realm.BSON.ObjectId;
/** @deprecated Please use named imports */
export type UUID = Realm.BSON.UUID;
/** @deprecated Please use named imports */
export type Date = GlobalDate;
/** @deprecated Please use named imports */
export type Data = ArrayBuffer;
/** @deprecated Please use named imports */
export type List<T> = Realm.List<T>;
/** @deprecated Please use named imports */
export type Set<T> = Realm.Set<T>;
/** @deprecated Please use named imports */
export type Dictionary<T> = Realm.Dictionary<T>;
/** @deprecated Please use named imports */
export type Mixed = unknown;
/** @deprecated Please use named imports */
export type LinkingObjects<ObjectTypeT, LinkingPropertyName> = Realm.Results<ObjectTypeT>;
}
}
}

//Set default logger and log level.
Realm.setLogger(defaultLogger);
Realm.setLogLevel(defaultLoggerLevel);

// Patch the global at runtime
let warnedAboutGlobalRealmUse = false;
Object.defineProperty(safeGlobalThis, "Realm", {
get() {
if (flags.THROW_ON_GLOBAL_REALM) {
throw new Error(
"Accessed global Realm, please update your code to ensure you import Realm via a named import:\nimport { Realm } from 'realm';",
);
} else if (!warnedAboutGlobalRealmUse) {
// eslint-disable-next-line no-console
console.warn(
"Your app is relying on a Realm global, which will be removed in realm-js v13, please update your code to ensure you import Realm via a named import:\n\n",
'import { Realm } from "realm"; // For ES Modules\n',
'const { Realm } = require("realm"); // For CommonJS\n\n',
"To determine where, put this in the top of your index file:\n",
`import { flags } from "realm";\n`,
`flags.THROW_ON_GLOBAL_REALM = true`,
);
warnedAboutGlobalRealmUse = true;
}
return RealmConstructor;
},
configurable: false,
});
54 changes: 54 additions & 0 deletions packages/realm/src/global.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
////////////////////////////////////////////////////////////////////////////
//
// Copyright 2023 Realm Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
////////////////////////////////////////////////////////////////////////////

import { Realm as RealmConstructor, flags, safeGlobalThis } from "./internal";

declare global {
/**
* @deprecated Use default import of Realm, this global will be removed in v13.
* @example
* import Realm from "realm";
*/
export type Realm = RealmConstructor;
export const Realm: typeof RealmConstructor;
}

// Patch the global at runtime
let warnedAboutGlobalRealmUse = false;
Object.defineProperty(safeGlobalThis, "Realm", {
get() {
if (flags.THROW_ON_GLOBAL_REALM) {
throw new Error(
"Accessed global Realm, please update your code to ensure you import Realm via a default import:\nimport Realm from 'realm';",
);
} else if (!warnedAboutGlobalRealmUse) {
// eslint-disable-next-line no-console
console.warn(
"Your app is relying on a Realm global, which will be removed in realm-js v13, please update your code to ensure you import Realm via a default import:\n\n",
'import Realm from "realm"; // For ES Modules\n',
'const Realm = require("realm"); // For CommonJS\n\n',
"To determine where, put this in the top of your index file:\n",
`import { flags } from "realm";\n`,
`flags.THROW_ON_GLOBAL_REALM = true`,
);
warnedAboutGlobalRealmUse = true;
}
return RealmConstructor;
},
configurable: false,
});
2 changes: 2 additions & 0 deletions packages/realm/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,8 @@ export {
UserTypeName,
} from "./internal";

import "./global";

import { Realm, RealmObjectConstructor } from "./internal";

export type Mixed = unknown;
Expand Down

0 comments on commit 9c3968c

Please sign in to comment.