Skip to content

Commit

Permalink
feat: custom base urls. (#1103)
Browse files Browse the repository at this point in the history
* feat: custom base urls.
  • Loading branch information
b4rtaz authored Apr 24, 2023
1 parent 700a64f commit d6f6476
Show file tree
Hide file tree
Showing 17 changed files with 157 additions and 21 deletions.
6 changes: 6 additions & 0 deletions .changeset/clean-dodos-decide.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@moralisweb3/evm-api': patch
'moralis': patch
---

Added the optional `evmApiBaseUrl` config option. You may replace the default base URL of the EVM API with your own.
6 changes: 6 additions & 0 deletions .changeset/five-cooks-sleep.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@moralisweb3/sol-api': patch
'moralis': patch
---

Added the optional `solApiBaseUrl` config option. You may replace the default base URL of the Solana API with your own.
20 changes: 20 additions & 0 deletions packages/auth/src/Auth.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { ApiUtils } from '@moralisweb3/api-utils';
import { Core } from '@moralisweb3/common-core';
import { Auth } from './Auth';

describe('Auth', () => {
function setupAuth() {
const core = Core.create();
const apiUtils = ApiUtils.create(core);
const auth = Auth.create(core);
core.registerModules([apiUtils, auth]);

return { core, auth };
}

it('returns default baseUrl', () => {
const { auth } = setupAuth();

expect(auth.baseUrl).toBe('https://authapi.moralis.io');
});
});
2 changes: 1 addition & 1 deletion packages/auth/src/Auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ export class Auth extends ApiModule {
}

private constructor(core: Core) {
super(Auth.moduleName, core, BASE_URL);
super(Auth.moduleName, core, () => BASE_URL);
}

public setup() {
Expand Down
2 changes: 2 additions & 0 deletions packages/common/core/src/Config/MoralisConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,13 @@ export interface EvmUtilsConfigValues {
// @moralisweb3/evm-api
export interface EvmApiConfigValues {
defaultEvmApiChain: EvmChainish;
evmApiBaseUrl: string;
}

// @moralisweb3/sol-api
export interface SolApiConfigValues {
defaultSolNetwork: SolNetworkish;
solApiBaseUrl: string;
}

export type MoralisCoreConfigValues =
Expand Down
16 changes: 14 additions & 2 deletions packages/common/core/src/Modules/ApiModule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,23 @@ import { Core } from '../Core';
* It should always be created with:
* - `name`: name of the module (should be unique)
* - `core`: the Core instance
* - `baseUrl`: the base url where of the api
* - `baseUrlProvider`: the provider of the base URL.
*/
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export abstract class ApiModule extends Module {
public constructor(name: string, core: Core, public readonly baseUrl: string) {
private _baseUrl?: string;

/**
* @description The base URL of the API.
*/
public get baseUrl(): string {
if (!this._baseUrl) {
this._baseUrl = this.baseUrlProvider();
}
return this._baseUrl;
}

public constructor(name: string, core: Core, private readonly baseUrlProvider: () => string) {
super(name, core, ModuleType.API);
}
}
2 changes: 1 addition & 1 deletion packages/common/core/src/Modules/Modules.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class TestModule extends Module {

class TestApiModule extends ApiModule {
public constructor(core: Core) {
super(TEST_API_MODULE_NAME, core, 'http://foo');
super(TEST_API_MODULE_NAME, core, () => 'http://foo');
}
public setup(): void {
// Nothing
Expand Down
34 changes: 27 additions & 7 deletions packages/evmApi/src/EvmApi.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,17 @@ import { ApiUtils, ApiUtilsConfig } from '@moralisweb3/api-utils';
import { EvmApi } from './EvmApi';

describe('EvmApi', () => {
function setupEvmApi() {
const core = Core.create();
const apiUtils = ApiUtils.create(core);
const evmApi = EvmApi.create(core);
core.registerModules([apiUtils, evmApi]);
return { core, evmApi };
}

it('supports multi-tenancy', async () => {
function createEvmApi(apiKey: string): EvmApi {
const core = Core.create();
const apiUtils = ApiUtils.create(core);
const evmApi = EvmApi.create(core);
core.registerModules([apiUtils, evmApi]);
function setupMultiTenancyEvmApi(apiKey: string): EvmApi {
const { core, evmApi } = setupEvmApi();
core.config.set(ApiUtilsConfig.apiKey, apiKey);
return evmApi;
}
Expand All @@ -30,9 +35,9 @@ describe('EvmApi', () => {
});

const alfaApiKey = 'alfa-api-key';
const alfa = createEvmApi(alfaApiKey);
const alfa = setupMultiTenancyEvmApi(alfaApiKey);
const betaApiKey = 'beta-api-key';
const beta = createEvmApi(betaApiKey);
const beta = setupMultiTenancyEvmApi(betaApiKey);

const alfaVersion = await alfa.utils.web3ApiVersion();
expect(alfaVersion.result.version).toEqual(mockedVersion);
Expand All @@ -44,4 +49,19 @@ describe('EvmApi', () => {

jest.restoreAllMocks();
});

it('returns default baseUrl', () => {
const { evmApi } = setupEvmApi();

expect(evmApi.baseUrl).toBe('https://deep-index.moralis.io/api/v2');
});

it('supports custom baseUrl', () => {
const customBaseUrl = 'https://custom-evm-api-url.com';

const { core, evmApi } = setupEvmApi();
core.config.set('evmApiBaseUrl', customBaseUrl);

expect(evmApi.baseUrl).toBe(customBaseUrl);
});
});
5 changes: 2 additions & 3 deletions packages/evmApi/src/EvmApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@ import { Core, CoreProvider } from '@moralisweb3/common-core';
import { EvmApiConfigSetup } from './config/EvmApiConfigSetup';

import { ClientEvmApi } from './generated/ClientEvmApi';

const BASE_URL = 'https://deep-index.moralis.io/api/v2';
import { EvmApiConfig } from './config/EvmApiConfig';

export class EvmApi extends ClientEvmApi {
public static readonly moduleName = 'evmApi';
Expand All @@ -13,7 +12,7 @@ export class EvmApi extends ClientEvmApi {
}

private constructor(core: Core) {
super(EvmApi.moduleName, core, BASE_URL);
super(EvmApi.moduleName, core, () => core.config.get(EvmApiConfig.evmApiBaseUrl));
}

public setup() {
Expand Down
8 changes: 6 additions & 2 deletions packages/evmApi/src/config/EvmApiConfig.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import { ConfigKey } from '@moralisweb3/common-core';
import { EvmChainish } from '@moralisweb3/common-evm-utils';
import { ConfigKey, EvmChainish } from '@moralisweb3/common-core';

export const EvmApiConfig = {
evmApiBaseUrl: {
name: 'evmApiBaseUrl',
defaultValue: 'https://deep-index.moralis.io/api/v2',
} as ConfigKey<string>,

defaultEvmApiChain: {
name: 'defaultEvmApiChain',
defaultValue: '0x1',
Expand Down
1 change: 1 addition & 0 deletions packages/evmApi/src/config/EvmApiConfigSetup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { EvmApiConfig } from './EvmApiConfig';

export class EvmApiConfigSetup {
public static register(config: Config) {
config.registerKey(EvmApiConfig.evmApiBaseUrl);
config.registerKey(EvmApiConfig.defaultEvmApiChain);
}
}
30 changes: 30 additions & 0 deletions packages/solApi/src/SolApi.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { ApiUtils } from '@moralisweb3/api-utils';
import { Core } from '@moralisweb3/common-core';
import { SolApi } from './SolApi';

describe('SolApi', () => {
function setupSolApi() {
const core = Core.create();
const apiUtils = ApiUtils.create(core);
const solApi = SolApi.create(core);
core.registerModules([apiUtils, solApi]);

return { core, solApi };
}

it('returns default baseUrl', () => {
const { solApi } = setupSolApi();

expect(solApi.baseUrl).toBe('https://solana-gateway.moralis.io');
});

it('supports custom baseUrl', () => {
const customBaseUrl = 'https://custom-sol-api-url.com';

const { core, solApi } = setupSolApi();

core.config.set('solApiBaseUrl', customBaseUrl);

expect(solApi.baseUrl).toBe(customBaseUrl);
});
});
8 changes: 4 additions & 4 deletions packages/solApi/src/SolApi.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Core, CoreProvider } from '@moralisweb3/common-core';
import { ClientSolApi } from './generated/ClientSolApi';

const BASE_URL = 'https://solana-gateway.moralis.io';
import { EvmSolApiConfigSetup } from './config/SolApiConfigSetup';
import { SolApiConfig } from './config/SolApiConfig';

export class SolApi extends ClientSolApi {
public static readonly moduleName = 'solApi';
Expand All @@ -11,11 +11,11 @@ export class SolApi extends ClientSolApi {
}

private constructor(core: Core) {
super(SolApi.moduleName, core, BASE_URL);
super(SolApi.moduleName, core, () => core.config.get(SolApiConfig.solApiBaseUrl));
}

public setup() {
// Nothing
EvmSolApiConfigSetup.register(this.core.config);
}

public start() {
Expand Down
8 changes: 8 additions & 0 deletions packages/solApi/src/config/SolApiConfig.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { ConfigKey } from '@moralisweb3/common-core';

export const SolApiConfig = {
solApiBaseUrl: {
name: 'solApiBaseUrl',
defaultValue: 'https://solana-gateway.moralis.io',
} as ConfigKey<string>,
};
8 changes: 8 additions & 0 deletions packages/solApi/src/config/SolApiConfigSetup.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { Config } from '@moralisweb3/common-core';
import { SolApiConfig } from './SolApiConfig';

export class EvmSolApiConfigSetup {
public static register(config: Config) {
config.registerKey(SolApiConfig.solApiBaseUrl);
}
}
20 changes: 20 additions & 0 deletions packages/streams/src/Streams.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { ApiUtils } from '@moralisweb3/api-utils';
import { Core } from '@moralisweb3/common-core';
import { Streams } from './Streams';

describe('Streams', () => {
function setupStreams() {
const core = Core.create();
const apiUtils = ApiUtils.create(core);
const streams = Streams.create(core);
core.registerModules([apiUtils, streams]);

return { core, streams };
}

it('returns default baseUrl', () => {
const { streams } = setupStreams();

expect(streams.baseUrl).toBe('https://api.moralis-streams.com');
});
});
2 changes: 1 addition & 1 deletion packages/streams/src/Streams.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ export class Streams extends ApiModule {
}

private constructor(core: Core) {
super(Streams.moduleName, core, BASE_URL);
super(Streams.moduleName, core, () => BASE_URL);
}

public setup() {
Expand Down

1 comment on commit d6f6476

@github-actions
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Test coverage

Title Lines Statements Branches Functions
api-utils Coverage: 30%
30.34% (61/201) 30.35% (17/56) 30.76% (12/39)
auth Coverage: 89%
92.45% (98/106) 83.33% (20/24) 86.66% (26/30)
evm-api Coverage: 100%
100% (91/91) 66.66% (6/9) 100% (58/58)
common-aptos-utils Coverage: 4%
4.5% (149/3305) 4.64% (25/538) 5.52% (45/814)
common-evm-utils Coverage: 70%
71.11% (1423/2001) 32.04% (314/980) 48.22% (393/815)
sol-api Coverage: 96%
97.5% (39/40) 66.66% (6/9) 93.33% (14/15)
common-sol-utils Coverage: 74%
74.55% (167/224) 66.66% (18/27) 65.38% (51/78)
common-streams-utils Coverage: 91%
91.51% (1229/1343) 76.13% (418/549) 82.22% (444/540)
streams Coverage: 88%
88.2% (576/653) 68.81% (64/93) 88.02% (125/142)

Please sign in to comment.