Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@internxt/sdk",
"author": "Internxt <hello@internxt.com>",
"version": "1.12.5",
"version": "1.12.6",
"description": "An sdk for interacting with Internxt's services",
"repository": {
"type": "git",
Expand Down
24 changes: 6 additions & 18 deletions src/misc/location/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { basicHeaders } from '../../shared/headers';
import { ApiSecurity, ApiUrl, AppDetails } from '../../shared';
import { ApiUrl } from '../../shared';
import { HttpClient } from '../../shared/http/client';

export interface UserLocation {
Expand All @@ -9,27 +8,16 @@ export interface UserLocation {

export class Location {
private readonly client: HttpClient;
private readonly appDetails: AppDetails;

private constructor(apiUrl: ApiUrl, appDetails: AppDetails, apiSecurity?: ApiSecurity) {
this.client = HttpClient.create(apiUrl, apiSecurity?.unauthorizedCallback);
this.appDetails = appDetails;
private constructor(apiUrl: ApiUrl) {
this.client = HttpClient.create(apiUrl);
}

public static client(apiUrl: ApiUrl, appDetails: AppDetails, apiSecurity?: ApiSecurity) {
return new Location(apiUrl, appDetails, apiSecurity);
public static client(apiUrl: ApiUrl) {
return new Location(apiUrl);
}

public async getUserLocation(): Promise<UserLocation> {
return this.client.get<UserLocation>('', this.basicUserHeaders());
}

private basicUserHeaders() {
return basicHeaders({
clientName: this.appDetails.clientName,
clientVersion: this.appDetails.clientVersion,
desktopToken: this.appDetails.desktopHeader,
customHeaders: this.appDetails.customHeaders,
});
return this.client.get<UserLocation>('/', {});
}
}
73 changes: 5 additions & 68 deletions test/misc/location/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
import sinon from 'sinon';
import { AppDetails } from '../../../src/shared';
import { basicHeaders } from '../../../src/shared/headers';
import { Location, UserLocation } from '../../../src/misc/location';
import { HttpClient } from '../../../src/shared/http/client';

const httpClient = HttpClient.create('');

describe('location service', () => {
describe('Location service', () => {
beforeEach(() => {
sinon.stub(HttpClient, 'create').returns(httpClient);
});
Expand All @@ -22,87 +20,26 @@ describe('location service', () => {
ip: '1.1.1.1',
location: 'ES',
};
const callStub = sinon.stub(httpClient, 'get').resolves(mockLocation);
const { client, headers } = clientAndHeaders();
sinon.stub(httpClient, 'get').resolves(mockLocation);
const { client } = clientAndHeaders();

// Act
const result = await client.getUserLocation();

// Assert
expect(callStub.firstCall.args).toEqual(['', headers]);
expect(result).toEqual(mockLocation);
});

it('should include desktop header when provided', async () => {
// Arrange
const mockLocation: UserLocation = {
ip: '10.0.0.1',
location: 'US',
};
const callStub = sinon.stub(httpClient, 'get').resolves(mockLocation);
const { client, headers } = clientAndHeaders({
desktopHeader: 'desktop-token',
});

// Act
const result = await client.getUserLocation();

// Assert
expect(callStub.firstCall.args).toEqual(['', headers]);
expect(result).toEqual(mockLocation);
});

it('should include custom headers when provided', async () => {
// Arrange
const mockLocation: UserLocation = {
ip: '172.16.0.1',
location: 'FR',
};
const customHeaders = { 'x-custom-header': 'custom-value' };
const callStub = sinon.stub(httpClient, 'get').resolves(mockLocation);
const { client, headers } = clientAndHeaders({
customHeaders,
});

// Act
const result = await client.getUserLocation();

// Assert
expect(callStub.firstCall.args).toEqual(['', headers]);
expect(result).toEqual(mockLocation);
});
});
});

function clientAndHeaders({
apiUrl = '',
clientName = 'internxt-client',
clientVersion = '0.1',
desktopHeader,
customHeaders,
}: {
apiUrl?: string;
clientName?: string;
clientVersion?: string;
desktopHeader?: string;
customHeaders?: Record<string, string>;
} = {}): {
client: Location;
headers: object;
} {
const appDetails: AppDetails = {
clientName,
clientVersion,
desktopHeader,
customHeaders,
};
const client = Location.client(apiUrl);

const client = Location.client(apiUrl, appDetails);
const headers = basicHeaders({
clientName,
clientVersion,
desktopToken: desktopHeader,
customHeaders,
});
return { client, headers };
return { client };
}
Loading