Skip to content

Commit

Permalink
refactor: refactor steelseries api to point that it work only with lo…
Browse files Browse the repository at this point in the history
…calhost (#12)
  • Loading branch information
meskill committed Sep 17, 2021
1 parent b18aca9 commit e863a4f
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 5 deletions.
8 changes: 6 additions & 2 deletions src/api.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { GameSenseDescription, GameSenseEvent, GameSenseRegisterEvents, GameSenseStopEvent } from './types';
import { getPortFromUrl } from './utils/getPortFromUrl';

interface Events {
game_metadata: GameSenseDescription;
Expand All @@ -8,10 +9,13 @@ interface Events {
}

export class SteelSeriesApi {
constructor(public apiUrl: string) {}
private readonly port: number;
constructor(address: string) {
this.port = getPortFromUrl(address);
}

async send<Method extends keyof Events>(method: Method, body: Events[Method]): Promise<void> {
await fetch(`http://${this.apiUrl}/${method}`, {
await fetch(`http://localhost:${this.port}/${method}`, {
method: 'POST',
body: JSON.stringify(body),
headers: {
Expand Down
4 changes: 2 additions & 2 deletions src/steelseriesConfig.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { STEEL_SERIES_CONFIG_PATH } from './constants/steelseries';

export const resolveSteelseriesAddress = () => {
return new Promise<string>((resolve, reject) => {
export const resolveSteelseriesAddress = (): Promise<string> => {
return new Promise((resolve, reject) => {
const xhr = new XMLHttpRequest();

xhr.open('GET', STEEL_SERIES_CONFIG_PATH);
Expand Down
2 changes: 1 addition & 1 deletion src/storage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export const getAddressWithStorage = async (): Promise<string | undefined> => {
});
};

export const writeAddressIntoStorage = async (address: string) => {
export const writeAddressIntoStorage = async (address: string): Promise<void> => {
await chrome.storage.local.set({
[STEEL_SERIES_API_STORAGE_KEY]: address,
});
Expand Down
9 changes: 9 additions & 0 deletions src/utils/getPortFromUrl.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
export const getPortFromUrl = (address: string): number => {
const match = address.match(/:(\d+)$/);

if (!match) {
throw new Error(`Cannot get port from address "${address}"`);
}

return +match[1];
};

0 comments on commit e863a4f

Please sign in to comment.