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.3",
"version": "1.12.4",
"description": "An sdk for interacting with Internxt's services",
"repository": {
"type": "git",
Expand Down
32 changes: 28 additions & 4 deletions src/misc/location/index.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,35 @@
import { basicHeaders } from '../../shared/headers';
import { ApiSecurity, ApiUrl, AppDetails } from '../../shared';
import { HttpClient } from '../../shared/http/client';

export interface UserLocation {
ip: string;
location: string;
}

export const getUserLocation = async (apiUrl: string): Promise<UserLocation> => {
const client = HttpClient.create(apiUrl);
return client.get<UserLocation>(`${apiUrl}`, {});
};
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;
}

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

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,
});
}
}
14 changes: 8 additions & 6 deletions src/shared/headers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,13 @@ export interface CustomHeaders {
[key: string]: string;
}

export interface BasicHeadersPayload {
clientName: string;
clientVersion: string;
customHeaders?: Record<string, string>;
desktopToken?: Token;
}

type InternxtHeaders = {
'content-type': string;
'internxt-version': string;
Expand All @@ -20,12 +27,7 @@ export function basicHeaders({
clientVersion,
customHeaders,
desktopToken,
}: {
clientName: string;
clientVersion: string;
customHeaders?: Record<string, string>;
desktopToken?: Token;
}): InternxtHeaders {
}: BasicHeadersPayload): InternxtHeaders {
const extra: ExtraHeaders = {};
if (desktopToken) {
extra['x-internxt-desktop-header'] = desktopToken;
Expand Down
108 changes: 108 additions & 0 deletions test/misc/location/index.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
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', () => {
beforeEach(() => {
sinon.stub(HttpClient, 'create').returns(httpClient);
});

afterEach(() => {
sinon.restore();
});

describe('Get user location', () => {
it('should call with right params & return user location', async () => {
// Arrange
const mockLocation: UserLocation = {
ip: '1.1.1.1',
location: 'ES',
};
const callStub = sinon.stub(httpClient, 'get').resolves(mockLocation);
const { client, headers } = 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, appDetails);
const headers = basicHeaders({
clientName,
clientVersion,
desktopToken: desktopHeader,
customHeaders,
});
return { client, headers };
}
Loading