diff --git a/package.json b/package.json index 25442b5f7..3f19401bb 100644 --- a/package.json +++ b/package.json @@ -8,7 +8,7 @@ "@iconscout/react-unicons": "^1.1.6", "@internxt/css-config": "1.1.0", "@internxt/lib": "1.4.1", - "@internxt/sdk": "=1.12.0", + "@internxt/sdk": "=1.12.4", "@internxt/ui": "0.1.1", "@phosphor-icons/react": "^2.1.7", "@popperjs/core": "^2.11.6", diff --git a/src/app/core/factory/sdk/index.test.ts b/src/app/core/factory/sdk/index.test.ts index 020018c13..f4cc870df 100644 --- a/src/app/core/factory/sdk/index.test.ts +++ b/src/app/core/factory/sdk/index.test.ts @@ -7,9 +7,11 @@ import { STORAGE_KEYS } from 'services/storage-keys'; import { Share, Users } from '@internxt/sdk/dist/drive'; import packageJson from '../../../../../package.json'; import { Auth } from '@internxt/sdk/dist/auth'; +import { Location } from '@internxt/sdk'; const MOCKED_NEW_API = 'https://api.internxt.com'; const MOCKED_PAYMENTS = 'https://payments.internxt.com'; +const MOCKED_LOCATION = 'https://location.internxt.com'; vi.mock('@internxt/sdk/dist/drive', () => ({ Users: { @@ -26,11 +28,18 @@ vi.mock('@internxt/sdk/dist/auth', () => ({ }, })); +vi.mock('@internxt/sdk', () => ({ + Location: { + client: vi.fn(), + }, +})); + vi.mock('services/env.service', () => ({ default: { getVariable: vi.fn((key: string) => { if (key === 'newApi') return MOCKED_NEW_API; if (key === 'payments') return MOCKED_PAYMENTS; + if (key === 'location') return MOCKED_LOCATION; return ''; }), }, @@ -338,5 +347,30 @@ describe('SdkFactory', () => { ); }); }); + + describe('Creating the location client', () => { + test('When the Location client is created, then it uses the location API URL and default app details', () => { + const mockToken = 'test-token'; + const mockWorkspace = Workspace.Individuals; + + vi.spyOn(mockLocalStorage, 'getWorkspace').mockReturnValue(mockWorkspace); + vi.spyOn(mockLocalStorage, 'get').mockImplementation((key: string) => { + if (key === 'xNewToken') return mockToken; + return null; + }); + + const instance = SdkFactory.getNewApiInstance(); + instance.createLocationClient(); + + expect(Location.client).toHaveBeenCalledWith( + MOCKED_LOCATION, + { + clientName: packageJson.name, + clientVersion: packageJson.version, + }, + expect.any(Object), + ); + }); + }); }); }); diff --git a/src/app/core/factory/sdk/index.ts b/src/app/core/factory/sdk/index.ts index fffb2569d..59216df33 100644 --- a/src/app/core/factory/sdk/index.ts +++ b/src/app/core/factory/sdk/index.ts @@ -10,6 +10,7 @@ import { Workspace } from '../../types'; import { Checkout } from '@internxt/sdk/dist/payments'; import envService from 'services/env.service'; import { STORAGE_KEYS } from 'services/storage-keys'; +import { Location } from '@internxt/sdk'; export class SdkFactory { private static sdk: { @@ -113,6 +114,13 @@ export class SdkFactory { return Backups.client(apiUrl, appDetails, apiSecurity); } + public createLocationClient(): Location { + const apiUrl = envService.getVariable('location'); + const appDetails = SdkFactory.getAppDetails(); + const apiSecurity = this.getNewApiSecurity(); + return Location.client(apiUrl, appDetails, apiSecurity); + } + /** Helpers **/ private getNewApiSecurity(unauthorizedCallback?: () => void): ApiSecurity { diff --git a/src/utils/userLocation.test.ts b/src/utils/userLocation.test.ts index 4a9682c0e..1882a1fde 100644 --- a/src/utils/userLocation.test.ts +++ b/src/utils/userLocation.test.ts @@ -1,21 +1,10 @@ -// tests/userLocation.test.ts -import { describe, it, expect, vi, beforeEach, Mock } from 'vitest'; -import * as sdk from '@internxt/sdk'; +import { describe, it, expect, vi, beforeEach } from 'vitest'; import { userLocation } from './userLocation'; +import { SdkFactory } from 'app/core/factory/sdk'; -vi.mock('@internxt/sdk', async () => { - const actual = await vi.importActual('@internxt/sdk'); - return { - ...actual, - getUserLocation: vi.fn(), - }; -}); - -vi.mock('../../config', () => ({ - envConfig: { - api: { - location: 'https://mocked-location-api.com', - }, +vi.mock('app/core/factory/sdk', () => ({ + SdkFactory: { + getNewApiInstance: vi.fn(), }, })); @@ -25,15 +14,21 @@ describe('User location function', () => { location: 'ES', }; + const mockGetUserLocation = vi.fn(); + const mockCreateLocationClient = vi.fn(); + beforeEach(() => { vi.clearAllMocks(); - vi.resetModules(); - (sdk.getUserLocation as Mock).mockResolvedValue(mockedLocation); + mockGetUserLocation.mockResolvedValue(mockedLocation); + mockCreateLocationClient.mockReturnValue({ getUserLocation: mockGetUserLocation }); + vi.mocked(SdkFactory.getNewApiInstance).mockReturnValue({ + createLocationClient: mockCreateLocationClient, + } as unknown as SdkFactory); }); it('When the user location is requested, then the IP and country are returned', async () => { const result = await userLocation(); - expect(result).toEqual(mockedLocation); + expect(result).toStrictEqual(mockedLocation); }); }); diff --git a/src/utils/userLocation.ts b/src/utils/userLocation.ts index 24d3d0e6c..eaf35f6bc 100644 --- a/src/utils/userLocation.ts +++ b/src/utils/userLocation.ts @@ -1,6 +1,7 @@ -import { getUserLocation, UserLocation } from '@internxt/sdk'; -import envService from 'services/env.service'; +import { UserLocation } from '@internxt/sdk'; +import { SdkFactory } from 'app/core/factory/sdk'; export const userLocation = (): Promise => { - return getUserLocation(envService.getVariable('location')); + const client = SdkFactory.getNewApiInstance().createLocationClient(); + return client.getUserLocation(); }; diff --git a/yarn.lock b/yarn.lock index fd3714a8d..92db71b54 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1906,10 +1906,10 @@ version "1.0.2" resolved "https://codeload.github.com/internxt/prettier-config/tar.gz/9fa74e9a2805e1538b50c3809324f1c9d0f3e4f9" -"@internxt/sdk@=1.12.0": - version "1.12.0" - resolved "https://registry.yarnpkg.com/@internxt/sdk/-/sdk-1.12.0.tgz#cf9c2f0ca8864a688e4c161f470e171997bff7bb" - integrity sha512-QrjH2yJP7MjxAVvkOe6quqX7RYzC6e3M0XcXralJEFybDpimJBJbvRTPUe7+9XRQ6gHdmYi1u3ySDVoZyZpkug== +"@internxt/sdk@=1.12.4": + version "1.12.4" + resolved "https://registry.yarnpkg.com/@internxt/sdk/-/sdk-1.12.4.tgz#5874d5b2705919a27f4e621e8179443e5bbdc43f" + integrity sha512-amADmUpOubu2wnbGq+DZqgK03JCKJcLuijiigY29SwBSrdknTlAqAetQSpbl/RXnBpeqnTTZtYXMJPYB14qbHw== dependencies: axios "1.13.2" uuid "11.1.0"