Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add optional nftEndpoint param #586

Merged
merged 2 commits into from
Jul 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/@magic-ext/auth/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ interface SDKEnvironment {
version: string;
platform: 'web' | 'react-native';
defaultEndpoint: string;
defaultNFTEndpoint: string;
ViewController: ConstructorOf<ViewController>;
configureStorage: () => Promise<typeof localForage>;
bundleId?: string | null;
Expand Down
1 change: 1 addition & 0 deletions packages/@magic-sdk/provider/src/core/sdk-environment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export interface SDKEnvironment {
version: string;
platform: 'web' | 'react-native';
defaultEndpoint: string;
defaultNFTEndpoint: string;
ViewController: ConstructorOf<ViewController>;
configureStorage: () => Promise<typeof localForage>;
bundleId?: string | null;
Expand Down
4 changes: 3 additions & 1 deletion packages/@magic-sdk/provider/src/core/sdk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ export interface MagicSDKAdditionalConfiguration<
TExt extends MagicSDKExtensionsOption<TCustomExtName> = any,
> {
endpoint?: string;
nftEndpoint?: string;
locale?: SupportedLocale;
network?: EthNetworkConfiguration;
extensions?: TExt;
Expand Down Expand Up @@ -142,7 +143,7 @@ export class SDKBase {
createReactNativeEndpointConfigurationWarning().log();
}

const { defaultEndpoint, version } = SDKEnvironment;
const { defaultEndpoint, defaultNFTEndpoint, version } = SDKEnvironment;
this.testMode = !!options?.testMode;
this.endpoint = createURL(options?.endpoint ?? defaultEndpoint).origin;

Expand All @@ -161,6 +162,7 @@ export class SDKBase {
API_KEY: this.apiKey,
DOMAIN_ORIGIN: window.location ? window.location.origin : '',
ETH_NETWORK: options?.network,
nftEndpoint: options?.nftEndpoint ?? defaultNFTEndpoint,
host: createURL(this.endpoint).host,
sdk: sdkNameToEnvName[SDKEnvironment.sdkName],
version,
Expand Down
1 change: 1 addition & 0 deletions packages/@magic-sdk/provider/test/constants.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export const MAGIC_RELAYER_FULL_URL = 'https://auth.magic.link';
export const MAGIC_NFT_API_URL = 'https://nft-api.magic.link';
export const TEST_API_KEY = 'pk_test_123';
export const LIVE_API_KEY = 'pk_live_123';
export const ENCODED_QUERY_PARAMS = 'testqueryparams';
Expand Down
3 changes: 2 additions & 1 deletion packages/@magic-sdk/provider/test/factories.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as memoryDriver from 'localforage-driver-memory';
import localForage from 'localforage';
import { MAGIC_RELAYER_FULL_URL, ENCODED_QUERY_PARAMS, TEST_API_KEY } from './constants';
import { MAGIC_RELAYER_FULL_URL, ENCODED_QUERY_PARAMS, TEST_API_KEY, MAGIC_NFT_API_URL } from './constants';
import { ViewController } from '../src';
import type { SDKEnvironment } from '../src/core/sdk-environment';

Expand Down Expand Up @@ -41,6 +41,7 @@ export function createMagicSDKCtor(environment: { [P in keyof SDKEnvironment]?:
platform: 'web',
version: '1.0.0-test',
defaultEndpoint: MAGIC_RELAYER_FULL_URL,
defaultNFTEndpoint: MAGIC_NFT_API_URL,
ViewController: TestViewController,
configureStorage: async () => {
const lf = localForage.createInstance({});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
/* eslint-disable no-new, class-methods-use-this, global-require */

import browserEnv from '@ikscodes/browser-env';
import { MAGIC_RELAYER_FULL_URL, TEST_API_KEY } from '../../../constants';
import { MAGIC_NFT_API_URL, MAGIC_RELAYER_FULL_URL, TEST_API_KEY } from '../../../constants';
import { createMagicSDKCtor } from '../../../factories';
import { AuthModule } from '../../../../src/modules/auth';
import { UserModule } from '../../../../src/modules/user';
Expand All @@ -18,6 +18,7 @@ function assertEncodedQueryParams(parameters: string, expectedParams: any = {})
const defaultExpectedParams = {
API_KEY: TEST_API_KEY,
DOMAIN_ORIGIN: 'null',
nftEndpoint: MAGIC_NFT_API_URL,
host: 'auth.magic.link',
sdk: 'magic-sdk',
version: '1.0.0-test',
Expand Down Expand Up @@ -67,6 +68,15 @@ test('Initialize `MagicSDK` with custom endpoint', () => {
assertModuleInstanceTypes(magic);
});

test('Initialize `MagicSDK` with custom nftEnpoint', () => {
const Ctor = createMagicSDKCtor();
const magic = new Ctor(TEST_API_KEY, { nftEndpoint: 'https://example.com' });

expect(magic.apiKey).toBe(TEST_API_KEY);
assertEncodedQueryParams(magic.parameters, { nftEndpoint: 'https://example.com' });
assertModuleInstanceTypes(magic);
});

test('Initialize `MagicSDK` when `window.location` is missing', () => {
browserEnv.stub('location', undefined);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,16 @@ test('Generate JSON RPC request payload with method `magic_get_info` and the act
expect(requestPayload.method).toBe('magic_get_info');
expect(requestPayload.params).toEqual([{ walletType: 'metamask' }]);
});

test('Generate JSON RPC request payload with method `magic_get_info`', async () => {
const magic = createMagicSDK();
magic.user.request = jest.fn();

await storage.setItem('mc_active_wallet', 'metamask');

await magic.user.getInfo();

const requestPayload = magic.user.request.mock.calls[0][0];
expect(requestPayload.method).toBe('magic_get_info');
expect(requestPayload.params).toEqual([{ walletType: 'metamask' }]);
});
Comment on lines +25 to +36
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

This code is not related to this PR, but it helps this PR to pass the coverage threshold.

1 change: 1 addition & 0 deletions packages/@magic-sdk/react-native-bare/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ export const Magic = createSDK(SDKBaseReactNative, {
version: process.env.BARE_REACT_NATIVE_VERSION!,
bundleId: getBundleId(),
defaultEndpoint: 'https://box.magic.link/',
defaultNFTEndpoint: 'https://nft-api.magic.link/',
ViewController: ReactNativeWebViewController,
configureStorage: /* istanbul ignore next */ async () => {
const lf = localForage.createInstance({
Expand Down
1 change: 1 addition & 0 deletions packages/@magic-sdk/react-native-expo/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ export const Magic = createSDK(SDKBaseReactNative, {
version: process.env.EXPO_REACT_NATIVE_VERSION!,
bundleId: Application.applicationId,
defaultEndpoint: 'https://box.magic.link/',
defaultNFTEndpoint: 'https://nft-api.magic.link/',
ViewController: ReactNativeWebViewController,
configureStorage: /* istanbul ignore next */ async () => {
const lf = localForage.createInstance({
Expand Down
1 change: 1 addition & 0 deletions packages/@magic-sdk/types/src/core/query-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,5 @@ export interface QueryParameters {
ext?: any;
locale?: string;
bundleId?: string;
nftEndpoint?: string;
}
1 change: 1 addition & 0 deletions packages/magic-sdk/src/index.cdn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ const Magic = Object.assign(
sdkName: 'magic-sdk',
version: process.env.WEB_VERSION!,
defaultEndpoint: 'https://auth.magic.link/',
defaultNFTEndpoint: 'https://nft-api.magic.link/',
ViewController: IframeController,
configureStorage: /* istanbul ignore next */ async () => {
const lf = localForage.createInstance({
Expand Down
1 change: 1 addition & 0 deletions packages/magic-sdk/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export const Magic = createSDK(SDKBase, {
sdkName: 'magic-sdk',
version: process.env.WEB_VERSION!,
defaultEndpoint: 'https://auth.magic.link/',
defaultNFTEndpoint: 'https://nft-api.magic.link/',
ViewController: IframeController,
configureStorage: async () => {
const lf = localForage.createInstance({
Expand Down