-
Notifications
You must be signed in to change notification settings - Fork 585
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
RJS-2732: Support updating the
App
's base URL via experimental expo…
…rt (#6518) * Add experimental file to extend 'App'. * Expose experimental module. * Add getter for base url. * Test experimental module. * Rename module to 'base-url'. * Add TS error for setting the property. * Add experimental to TS docs. * Add implementation to the added functions. * Rename to 'updateBaseUrl()'. * Return the promise directly instead of using 'async await'. * Update expected base url from 'realm..' to 'services.cloud..'. * Update public API docs. * Update arg name. * Separate tests. * Help Metro resolve the correct file for RN. * Isolate test for baseUrl getter. * Remove `unstable_enablePackageExports` from metro config. Co-authored-by: Kræn Hansen <kraen.hansen@mongodb.com> * Update comment about the need of 'realm/experimental/base-url.js'. * Elaborate in API docs. * Add skipped empty test for resetting URL. * Allow 'null' as arg to reset the URL. * Add CHANGELOG entry. --------- Co-authored-by: Kræn Hansen <kraen.hansen@mongodb.com>
- Loading branch information
1 parent
56112c7
commit 7f6535f
Showing
12 changed files
with
160 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
55 changes: 55 additions & 0 deletions
55
integration-tests/tests/src/tests/experimental/base-url.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
//////////////////////////////////////////////////////////////////////////// | ||
// | ||
// Copyright 2024 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 { expect } from "chai"; | ||
import "realm/experimental/base-url"; | ||
|
||
import { baseUrl as originalBaseUrl, importAppBefore } from "../../hooks"; | ||
import { buildAppConfig } from "../../utils/build-app-config"; | ||
|
||
describe.skipIf(environment.missingServer, "Base URL", () => { | ||
importAppBefore(buildAppConfig("with-anon").anonAuth()); | ||
|
||
it("returns the base URL used", function (this: AppContext) { | ||
expect(this.app.baseUrl).equals(originalBaseUrl); | ||
}); | ||
|
||
// TODO: Should implement when I've got a working fetch mock. | ||
it.skip("updates the URL", async function (this: AppContext) { | ||
// TODO | ||
}); | ||
|
||
it.skip("resets to default URL", async function (this: AppContext) { | ||
// TODO | ||
}); | ||
|
||
it("throws when assigning via setter", function (this: AppContext) { | ||
// @ts-expect-error Assigning to read-only property. | ||
expect(() => (this.app.baseUrl = "new URL")).to.throw("Cannot assign the base URL, please use 'updateBaseUrl()'"); | ||
|
||
expect(this.app.baseUrl).equals(originalBaseUrl); | ||
}); | ||
|
||
it("rejects when updating to invalid URL", async function (this: AppContext) { | ||
await expect(this.app.updateBaseUrl("https://invalid")).to.be.rejectedWith( | ||
"request to https://invalid/api/client/", | ||
); | ||
|
||
expect(this.app.baseUrl).equals(originalBaseUrl); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,4 +4,4 @@ | |
{ "path": "./tsconfig.common.json" }, | ||
{ "path": "./tsconfig.node.json" } | ||
] | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
//////////////////////////////////////////////////////////////////////////// | ||
// | ||
// Copyright 2024 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. | ||
// | ||
//////////////////////////////////////////////////////////////////////////// | ||
|
||
// Our use of `exports` in `packages/realm/package.json` is not enabled by | ||
// default when using Metro and RN. In these cases, modules imported from | ||
// "realm/experimental" will search for the file in the same path, rather | ||
// than what is pointed to under `exports`. Thus, we use this .js file to | ||
// in turn import the necessary module. | ||
|
||
// (Enabling `unstable_enablePackageExports` in the metro config unexpectedly | ||
// does not work.) | ||
|
||
/* eslint-env commonjs */ | ||
module.exports = require("../dist/experimental/base-url"); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
//////////////////////////////////////////////////////////////////////////// | ||
// | ||
// Copyright 2024 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 { App } from "../app-services/App"; | ||
|
||
declare module "../app-services/App" { | ||
interface App { | ||
/** | ||
* Get the current base URL used for sending requests to Atlas App Services. | ||
* | ||
* If an {@link App.updateBaseUrl | updateBaseUrl} operation is currently in | ||
* progress, this value will not be updated with the new value until that | ||
* operation has completed. | ||
* @experimental This feature is experimental and may be changed or removed. | ||
*/ | ||
get baseUrl(): string; | ||
|
||
/** | ||
* Update the base URL used for sending requests to Atlas App Services. If this is | ||
* set to an empty string or `null`, it will reset the base URL to the default one. | ||
* | ||
* If this operation fails, the app will continue to use the original base URL. | ||
* If another {@link App} operation is started while this function is in progress, | ||
* that request will use the original base URL location information. | ||
* @experimental This feature is experimental and may be changed or removed. | ||
*/ | ||
updateBaseUrl(newUrl: string | null): Promise<void>; | ||
} | ||
} | ||
|
||
Object.defineProperty(App.prototype, "baseUrl", { | ||
get(this: App) { | ||
return this.internal.getBaseUrl(); | ||
}, | ||
set() { | ||
throw new Error("Cannot assign the base URL, please use 'updateBaseUrl()'."); | ||
}, | ||
}); | ||
|
||
App.prototype.updateBaseUrl = function (this: App, newUrl: string | null) { | ||
return this.internal.updateBaseUrl(newUrl ?? undefined); | ||
}; |