Skip to content

Commit

Permalink
Override fetch for Altas App Services
Browse files Browse the repository at this point in the history
  • Loading branch information
kneth committed Sep 29, 2023
1 parent 190349b commit 493a83e
Show file tree
Hide file tree
Showing 7 changed files with 85 additions and 4 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* None

### Enhancements
* None
* Expose `Realm.App.Configuration.fetchOverride` to enable implementing a custom [`fetch`](https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API/Using_Fetch) function. ([#6159](https://github.com/realm/realm-js/issues/6159))

### Fixed
* <How to hit and notice issue? what was the impact?> ([#????](https://github.com/realm/realm-js/issues/????), since v?.?.?)
Expand Down
1 change: 1 addition & 0 deletions integration-tests/tests/src/hooks/import-app-before.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ export type AppConfigurationRelaxed = {
timeout?: number;
multiplexSessions?: boolean;
baseFilePath?: string;
fetchOverride?: any;
};

export function importAppBefore(config: AppConfig | { config: AppConfig }, sdkConfig?: AppConfigurationRelaxed): void {
Expand Down
66 changes: 66 additions & 0 deletions integration-tests/tests/src/node/custom-fetch.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
////////////////////////////////////////////////////////////////////////////
//
// Copyright 2022 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 { importAppBefore } from "../hooks";
import { buildAppConfig } from "../utils/build-app-config";
import fetch, { Request } from "node-fetch";
import Realm from "realm";

describe("custom fetch", () => {
let called = false;

const HTTP_METHOD: Record<number, string> = {
[0]: "GET",
[1]: "POST",
[2]: "PUT",
[3]: "PATCH",
[4]: "DELETE",
};

const customFetch = async (request: Request) => {
called = true;
const { url, body, ...rest } = request;
if (typeof request.method == "number") {
rest.method = HTTP_METHOD[request.method];
}
const response = await fetch(url, rest);
return response;
};

importAppBefore(buildAppConfig("with-anon").anonAuth(), { fetchOverride: customFetch });
afterEach(() => {
Realm.clearTestState();
});

it("custom fetch is called", async function (this: Mocha.Context & AppContext & RealmContext) {
let user;
called = false;
try {
expect(this.app).instanceOf(Realm.App);
const credentials = Realm.Credentials.anonymous();
user = await this.app.logIn(credentials);
expect(called).equals(true);
expect(user).instanceOf(Realm.User);
expect(user.deviceId).to.not.be.null;
expect(user.providerType).equals("anon-user");
} finally {
await user?.logOut();
}
});
});
1 change: 1 addition & 0 deletions integration-tests/tests/src/node/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,6 @@

import "./analytics";
import "./clean-exit";
import "./custom-fetch";
import "./path";
import "./sync-proxy";
2 changes: 1 addition & 1 deletion packages/realm/realm-constants.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"REALM_ANONYMIZED_BUNDLE_ID":"tkgif/+3l1e9wStGJp2TOngAK3UcQ2u7OM8ZYJU5JYo="}
{"REALM_ANONYMIZED_BUNDLE_ID":"1RmJBlqbKuzyRiPm4AsdIIxe8xlRUntGcGFEwUnUh6A="}
15 changes: 14 additions & 1 deletion packages/realm/src/app-services/App.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
//
////////////////////////////////////////////////////////////////////////////

import { inject } from "src/platform/network";
import {
AnyUser,
Credentials,
Expand All @@ -30,6 +31,7 @@ import {
createNetworkTransport,
deviceInfo,
fs,
network,
} from "../internal";

// eslint-disable-next-line @typescript-eslint/no-explicit-any
Expand Down Expand Up @@ -140,6 +142,12 @@ export type AppConfiguration = {
* @since 12.2.0
*/
metadata?: Metadata;

/**
* Specify a custom `fetch` implementation.
* @since 12.3.0
*/
fetchOverride?: any;
};

/**
Expand Down Expand Up @@ -261,7 +269,8 @@ export class App<
constructor(configOrId: AppConfiguration | string) {
const config: AppConfiguration = typeof configOrId === "string" ? { id: configOrId } : configOrId;
assert.object(config, "config");
const { id, baseUrl, app, timeout, multiplexSessions = true, baseFilePath, metadata } = config;
const { id, baseUrl, app, timeout, multiplexSessions = true, baseFilePath, metadata, fetchOverride } = config;

assert.string(id, "id");
if (timeout !== undefined) {
assert.number(timeout, "timeout");
Expand All @@ -277,6 +286,10 @@ export class App<
}
}

if (fetchOverride) {
inject({ fetch: fetchOverride });
}

fs.ensureDirectoryForFile(fs.joinPaths(baseFilePath || fs.getDefaultDirectoryPath(), "mongodb-realm"));
// TODO: This used getSharedApp in the legacy SDK, but it's failing AppTests
this.internal = binding.App.getUncachedApp(
Expand Down
2 changes: 1 addition & 1 deletion packages/realm/src/platform/network.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,5 +60,5 @@ export const network: NetworkType = {
};

export function inject(injected: NetworkType) {
Object.freeze(Object.assign(network, injected));
Object.assign(network, injected);
}

0 comments on commit 493a83e

Please sign in to comment.