diff --git a/lib/core/endpoint.ts b/lib/core/endpoint.ts index fae12eb..9deafd8 100644 --- a/lib/core/endpoint.ts +++ b/lib/core/endpoint.ts @@ -4,12 +4,12 @@ abstract class Endpoint { abstract lacuna: Lacuna; abstract url: string; - callWithSession(method: string, params: any) { - return this.lacuna.server.call({ module: this.url, method, params, addSession: true }); + callWithSession(method: string, params: any) { + return this.lacuna.server.call({ module: this.url, method, params, addSession: true }); } - callWithoutSession(method: string, params: any) { - return this.lacuna.server.call({ module: this.url, method, params, addSession: false }); + callWithoutSession(method: string, params: any) { + return this.lacuna.server.call({ module: this.url, method, params, addSession: false }); } } diff --git a/lib/core/server.ts b/lib/core/server.ts index 1257f53..b4a62fa 100644 --- a/lib/core/server.ts +++ b/lib/core/server.ts @@ -3,20 +3,25 @@ import _ from 'lodash'; import axios from 'axios'; import { fixNumbers } from './util'; -interface ServerRequest { +export interface ServerRequest { module: string; method: string; params: object | Array; addSession: boolean; } -interface ServerError { +export interface ServerResponse { + result?: T; + error?: ServerError; +} + +export interface ServerError { code: number; message: string; data: any; } -interface RequestBody { +export interface RequestBody { jsonrpc: '2.0'; id: number; method: string; @@ -64,11 +69,16 @@ class Server { return new URL(options.module, this.lacuna.config.serverUrl).href; } - async sendRequest(url: string, data: string, options: ServerRequest, retry: Function) { + async sendRequest( + url: string, + data: string, + options: ServerRequest, + retry: Function + ): Promise> { this.lacuna.log.info('Calling', `${options.module}/${options.method}`, options.params); try { - const response = await axios({ + const response = await axios>({ url, data: data, method: 'post', @@ -88,7 +98,7 @@ class Server { } } - return result; + return { result }; } } catch (e) { // MenuStore.hideLoader(); @@ -105,12 +115,14 @@ class Server { // TODO console.error(error.code, error.message, error.data); } + + return { error }; } - return null; + return {}; } - call(obj: ServerRequest) { + call(obj: ServerRequest) { // MenuStore.showLoader(); const options = this.addSession(obj); @@ -122,7 +134,7 @@ class Server { await this.call(obj); }; - return this.sendRequest(url, data, options, retry); + return this.sendRequest(url, data, options, retry); } } diff --git a/lib/endpoints/body.test.ts b/lib/endpoints/body.test.ts index 814b20e..bb40647 100644 --- a/lib/endpoints/body.test.ts +++ b/lib/endpoints/body.test.ts @@ -7,30 +7,30 @@ let empire: EmpireBlock; beforeAll(async () => { lacuna = await getLacuna(); - const res = await lacuna.empire.getStatus(); - empire = res.empire; + const { result } = await lacuna.empire.getStatus(); + empire = result?.empire; }); test('getBuildings', async () => { - const res = await lacuna.body.getBuildings({ body_id: empire.home_planet_id }); - expect(res.buildings).toBeDefined(); + const { result } = await lacuna.body.getBuildings({ body_id: empire.home_planet_id }); + expect(result?.buildings).toBeDefined(); }); test('rearrangeBuildings', async () => { console.warn = jest.fn(); - const res = await lacuna.body.rearrangeBuildings({ + const { result } = await lacuna.body.rearrangeBuildings({ body_id: empire.home_planet_id, arrangement: [], }); - expect(res.body).toBeDefined(); - expect(res.moved).toStrictEqual([]); + expect(result?.body).toBeDefined(); + expect(result?.moved).toStrictEqual([]); }); test('getStatus', async () => { - const res = await lacuna.body.getStatus({ body_id: empire.home_planet_id }); - expect(res.body).toBeDefined(); - expect(res.empire).toBeDefined(); - expect(res.server).toBeDefined(); + const { result } = await lacuna.body.getStatus({ body_id: empire.home_planet_id }); + expect(result?.body).toBeDefined(); + expect(result?.empire).toBeDefined(); + expect(result?.server).toBeDefined(); }); diff --git a/lib/endpoints/body.ts b/lib/endpoints/body.ts index a29cf11..0bb57d8 100644 --- a/lib/endpoints/body.ts +++ b/lib/endpoints/body.ts @@ -12,16 +12,19 @@ class Body extends Endpoint { this.url = url; } - getBuildings(params: B.GetBuildingsParams): Promise { - return this.callWithSession('get_buildings', [params.body_id]); + getBuildings(params: B.GetBuildingsParams) { + return this.callWithSession('get_buildings', [params.body_id]); } - getStatus(params: B.GetStatusParams): Promise { - return this.callWithSession('get_status', [params.body_id]); + getStatus(params: B.GetStatusParams) { + return this.callWithSession('get_status', [params.body_id]); } - rearrangeBuildings(params: B.RearrangeBuildingsParams): Promise { - return this.callWithSession('rearrange_buildings', [params.body_id, params.arrangement]); + rearrangeBuildings(params: B.RearrangeBuildingsParams) { + return this.callWithSession('rearrange_buildings', [ + params.body_id, + params.arrangement, + ]); } } diff --git a/lib/endpoints/building.test.ts b/lib/endpoints/building.test.ts index 553815e..d47614e 100644 --- a/lib/endpoints/building.test.ts +++ b/lib/endpoints/building.test.ts @@ -7,8 +7,12 @@ let pccId: number; beforeAll(async () => { lacuna = await getLacuna(); - const { empire } = await lacuna.empire.getStatus(); - const { buildings } = await lacuna.body.getBuildings({ body_id: empire.home_planet_id }); + const { + result: { empire }, + } = await lacuna.empire.getStatus(); + const { + result: { buildings }, + } = await lacuna.body.getBuildings({ body_id: empire?.home_planet_id }); pccId = _.chain(buildings) .keys() .filter((key) => buildings[key].name === 'Planetary Command Center') @@ -18,7 +22,7 @@ beforeAll(async () => { }); test('view', async () => { - const res = await lacuna.planetaryCommand.view({ building_id: pccId }); - expect(res.building).toBeDefined(); - expect(res.building.name).toBe('Planetary Command Center'); + const { result } = await lacuna.planetaryCommand.view({ building_id: pccId }); + expect(result?.building).toBeDefined(); + expect(result?.building.name).toBe('Planetary Command Center'); }); diff --git a/lib/endpoints/building.ts b/lib/endpoints/building.ts index 0a6cff9..8db38a4 100644 --- a/lib/endpoints/building.ts +++ b/lib/endpoints/building.ts @@ -12,20 +12,20 @@ class Building extends Endpoint { this.url = url; } - view(params: B.ViewParams): Promise { - return this.callWithSession('view', [params.building_id]); + view(params: B.ViewParams) { + return this.callWithSession('view', [params.building_id]); } - upgrade(params: B.UpgradeParams): Promise { - return this.callWithSession('upgrade', [params.building_id]); + upgrade(params: B.UpgradeParams) { + return this.callWithSession('upgrade', [params.building_id]); } - downgrade(params: B.DowngradeParams): Promise { - return this.callWithSession('downgrade', [params.building_id]); + downgrade(params: B.DowngradeParams) { + return this.callWithSession('downgrade', [params.building_id]); } - demolish(params: B.DemolishParams): Promise { - return this.callWithSession('demolish', [params.building_id]); + demolish(params: B.DemolishParams) { + return this.callWithSession('demolish', [params.building_id]); } } diff --git a/lib/endpoints/buildings/essentia-vein.test.ts b/lib/endpoints/buildings/essentia-vein.test.ts index 556fcb3..8d8aee7 100644 --- a/lib/endpoints/buildings/essentia-vein.test.ts +++ b/lib/endpoints/buildings/essentia-vein.test.ts @@ -10,7 +10,8 @@ beforeAll(async () => { test('drain', async () => { console.error = jest.fn(); - await lacuna.essentiaVein.drain({ buildingId: 123456789, times: 123456789 }); + const { error } = await lacuna.essentiaVein.drain({ buildingId: 123456789, times: 123456789 }); expect(console.error).toHaveBeenCalled(); + expect(error).toBeDefined(); }); diff --git a/lib/endpoints/buildings/essentia-vein.ts b/lib/endpoints/buildings/essentia-vein.ts index da2a42f..727911c 100644 --- a/lib/endpoints/buildings/essentia-vein.ts +++ b/lib/endpoints/buildings/essentia-vein.ts @@ -7,8 +7,8 @@ class EssentiaVein extends Building { super(lacuna, url); } - drain(params: EV.DrainParams): Promise { - return this.callWithSession('drain', [params.buildingId, params.times]); + drain(params: EV.DrainParams) { + return this.callWithSession('drain', [params.buildingId, params.times]); } } diff --git a/lib/endpoints/buildings/shipyard.test.ts b/lib/endpoints/buildings/shipyard.test.ts index 0bcb533..4042be7 100644 --- a/lib/endpoints/buildings/shipyard.test.ts +++ b/lib/endpoints/buildings/shipyard.test.ts @@ -8,8 +8,12 @@ let buildingId: string; beforeAll(async () => { lacuna = await getLacuna(); - const { empire } = await lacuna.empire.getStatus(); - const { buildings } = await lacuna.body.getBuildings({ body_id: empire.home_planet_id }); + const { + result: { empire }, + } = await lacuna.empire.getStatus(); + const { + result: { buildings }, + } = await lacuna.body.getBuildings({ body_id: empire.home_planet_id }); buildingId = _.chain(buildings) .keys() .filter((key) => buildings[key].name === 'Shipyard') @@ -18,26 +22,26 @@ beforeAll(async () => { }); test.skip('view', async () => { - const res = await lacuna.shipyard.view({ building_id: int(buildingId) }); + const { result } = await lacuna.shipyard.view({ building_id: int(buildingId) }); - expect(res.building).toBeDefined(); - expect(res.building.name).toBe('Shipyard'); + expect(result?.building).toBeDefined(); + expect(result?.building.name).toBe('Shipyard'); }); test.skip('viewBuildQueue', async () => { - const res = await lacuna.shipyard.viewBuildQueue({ building_id: int(buildingId) }); + const { result } = await lacuna.shipyard.viewBuildQueue({ building_id: int(buildingId) }); - expect(res.cost_to_subsidize).toBeDefined(); - expect(res.fleets_building).toBeDefined(); - expect(res.number_of_fleets_building).toBeDefined(); - expect(res.number_of_ships_building).toBeDefined(); + expect(result?.cost_to_subsidize).toBeDefined(); + expect(result?.fleets_building).toBeDefined(); + expect(result?.number_of_fleets_building).toBeDefined(); + expect(result?.number_of_ships_building).toBeDefined(); }); test.skip('getBuildable', async () => { - const res = await lacuna.shipyard.getBuildable({ building_id: int(buildingId) }); + const { result } = await lacuna.shipyard.getBuildable({ building_id: int(buildingId) }); - expect(res.buildable).toBeDefined(); - expect(res.build_queue_max).toBeDefined(); - expect(res.build_queue_used).toBeDefined(); - expect(res.docks_available).toBeDefined(); + expect(result?.buildable).toBeDefined(); + expect(result?.build_queue_max).toBeDefined(); + expect(result?.build_queue_used).toBeDefined(); + expect(result?.docks_available).toBeDefined(); }); diff --git a/lib/endpoints/buildings/shipyard.ts b/lib/endpoints/buildings/shipyard.ts index f7250a5..1dac947 100644 --- a/lib/endpoints/buildings/shipyard.ts +++ b/lib/endpoints/buildings/shipyard.ts @@ -7,22 +7,20 @@ class Shipyard extends Building { super(lacuna, url); } - viewBuildQueue(params: SY.ViewBuildQueueParams): Promise { - return this.callWithSession('view_build_queue', params); + viewBuildQueue(params: SY.ViewBuildQueueParams) { + return this.callWithSession('view_build_queue', params); } - subsidizeBuildQueue( - params: SY.SubsidizeBuildQueueParams - ): Promise { - return this.callWithSession('subsidize_build_queue', params); + subsidizeBuildQueue(params: SY.SubsidizeBuildQueueParams) { + return this.callWithSession('subsidize_build_queue', params); } - getBuildable(params: SY.GetBuildableParams): Promise { - return this.callWithSession('get_buildable', params); + getBuildable(params: SY.GetBuildableParams) { + return this.callWithSession('get_buildable', params); } - buildFleet(params: SY.BuildFleetParams): Promise { - return this.callWithSession('build_fleet', params); + buildFleet(params: SY.BuildFleetParams) { + return this.callWithSession('build_fleet', params); } } diff --git a/lib/endpoints/buildings/space-port.test.ts b/lib/endpoints/buildings/space-port.test.ts index 5b6ce56..ea635f8 100644 --- a/lib/endpoints/buildings/space-port.test.ts +++ b/lib/endpoints/buildings/space-port.test.ts @@ -10,9 +10,13 @@ let testPlanetId: number; beforeAll(async () => { lacuna = await getLacuna(); - const { empire } = await lacuna.empire.getStatus(); + const { + result: { empire }, + } = await lacuna.empire.getStatus(); homePlanetId = empire.home_planet_id; - const { buildings } = await lacuna.body.getBuildings({ body_id: homePlanetId }); + const { + result: { buildings }, + } = await lacuna.body.getBuildings({ body_id: homePlanetId }); buildingId = _.chain(buildings) .keys() @@ -29,45 +33,49 @@ beforeAll(async () => { }); test.skip('view', async () => { - const res = await lacuna.spacePort.view({ building_id: int(buildingId) }); + const { result } = await lacuna.spacePort.view({ building_id: int(buildingId) }); - expect(res.building).toBeDefined(); - expect(res.building.name).toBe('Space Port'); + expect(result?.building).toBeDefined(); + expect(result?.building.name).toBe('Space Port'); }); test.skip('viewAllFleets', async () => { - const res = await lacuna.spacePort.viewAllFleets({ building_id: int(buildingId) }); + const { result } = await lacuna.spacePort.viewAllFleets({ building_id: int(buildingId) }); - expect(res.fleets).toBeDefined(); - expect(res.number_of_fleets).toBeDefined(); + expect(result?.fleets).toBeDefined(); + expect(result?.number_of_fleets).toBeDefined(); }); test.skip('viewAvailableFleets', async () => { - const res = await lacuna.spacePort.viewAvailableFleets({ + const { result } = await lacuna.spacePort.viewAvailableFleets({ target: { body_id: testPlanetId }, body_id: homePlanetId, }); - expect(res).toBeDefined(); + expect(result).toBeDefined(); }); test.skip('viewUnavailableFleets', async () => { - const res = await lacuna.spacePort.viewUnavailableFleets({ + const { result } = await lacuna.spacePort.viewUnavailableFleets({ target: { body_id: testPlanetId }, body_id: homePlanetId, }); - expect(res).toBeDefined(); + expect(result).toBeDefined(); }); test.skip('viewOrbitingFleets', async () => { - const res = await lacuna.spacePort.viewOrbitingFleets({ target: { body_id: testPlanetId } }); + const { result } = await lacuna.spacePort.viewOrbitingFleets({ + target: { body_id: testPlanetId }, + }); - expect(res).toBeDefined(); + expect(result).toBeDefined(); }); test.skip('viewIncomingFleets', async () => { - const res = await lacuna.spacePort.viewIncomingFleets({ target: { body_id: testPlanetId } }); + const { result } = await lacuna.spacePort.viewIncomingFleets({ + target: { body_id: testPlanetId }, + }); - expect(res).toBeDefined(); + expect(result).toBeDefined(); }); diff --git a/lib/endpoints/buildings/space-port.ts b/lib/endpoints/buildings/space-port.ts index 287f049..137979d 100644 --- a/lib/endpoints/buildings/space-port.ts +++ b/lib/endpoints/buildings/space-port.ts @@ -7,38 +7,35 @@ class SpacePort extends Building { super(lacuna, url); } - viewAllFleets(params: SP.ViewAllFleetsParams): Promise { - return this.callWithSession('view_all_fleets', params); + viewAllFleets(params: SP.ViewAllFleetsParams) { + return this.callWithSession('view_all_fleets', params); } - viewTravellingFleets( - params: SP.ViewTravellingFleetsParams - ): Promise { - return this.callWithSession('view_travelling_fleets', params); + viewTravellingFleets(params: SP.ViewTravellingFleetsParams) { + return this.callWithSession('view_travelling_fleets', params); } - viewAvailableFleets( - params: SP.ViewAvailableFleetsParams - ): Promise { - return this.callWithSession('view_available_fleets', params); + viewAvailableFleets(params: SP.ViewAvailableFleetsParams) { + return this.callWithSession('view_available_fleets', params); } - viewUnavailableFleets( - params: SP.ViewUnavailableFleetsParams - ): Promise { - return this.callWithSession('view_unavailable_fleets', params); + viewUnavailableFleets(params: SP.ViewUnavailableFleetsParams) { + return this.callWithSession( + 'view_unavailable_fleets', + params + ); } - viewOrbitingFleets(params: SP.ViewOrbitingFleetsParams): Promise { - return this.callWithSession('view_orbiting_fleets', params); + viewOrbitingFleets(params: SP.ViewOrbitingFleetsParams) { + return this.callWithSession('view_orbiting_fleets', params); } - viewIncomingFleets(params: SP.ViewIncomingFleetsParams): Promise { - return this.callWithSession('view_incoming_fleets', params); + viewIncomingFleets(params: SP.ViewIncomingFleetsParams) { + return this.callWithSession('view_incoming_fleets', params); } - sendFleet(params: SP.SendFleetParams): Promise { - return this.callWithSession('send_fleet', params); + sendFleet(params: SP.SendFleetParams) { + return this.callWithSession('send_fleet', params); } } diff --git a/lib/endpoints/captcha.test.ts b/lib/endpoints/captcha.test.ts index 54448a0..75ff360 100644 --- a/lib/endpoints/captcha.test.ts +++ b/lib/endpoints/captcha.test.ts @@ -2,22 +2,22 @@ import { getLacuna } from '../__utils__/get-lacuna'; import Lacuna from '../lacuna'; let lacuna: Lacuna; -let universityId: string; beforeAll(async () => { lacuna = await getLacuna(); }); test('fetch', async () => { - const res = await lacuna.captcha.fetch(); - expect(res.guid).toBeDefined(); - expect(res.url).toBeDefined(); + const { result } = await lacuna.captcha.fetch(); + expect(result?.guid).toBeDefined(); + expect(result?.url).toBeDefined(); }); test('solve', async () => { console.error = jest.fn(); - await lacuna.captcha.solve({ guid: 'test', solution: 'test' }); + const { error } = await lacuna.captcha.solve({ guid: 'test', solution: 'test' }); expect(console.error).toHaveBeenCalled(); + expect(error).toBeDefined(); }); diff --git a/lib/endpoints/captcha.ts b/lib/endpoints/captcha.ts index 7110979..a15ae55 100644 --- a/lib/endpoints/captcha.ts +++ b/lib/endpoints/captcha.ts @@ -12,12 +12,12 @@ class Captcha extends Endpoint { this.url = url; } - fetch(params: C.FetchParams = {}): Promise { - return this.callWithSession('fetch', []); + fetch(params: C.FetchParams = {}) { + return this.callWithSession('fetch', []); } - solve(params: C.SolveParams): Promise { - return this.callWithSession('solve', [params.guid, params.solution]); + solve(params: C.SolveParams) { + return this.callWithSession('solve', [params.guid, params.solution]); } } diff --git a/lib/endpoints/empire.test.ts b/lib/endpoints/empire.test.ts index 01b3d70..33f28d7 100644 --- a/lib/endpoints/empire.test.ts +++ b/lib/endpoints/empire.test.ts @@ -5,7 +5,7 @@ test('login with errors', async () => { const lacuna = new Lacuna({ serverUrl: 'http://localhost:8080/' }); console.error = jest.fn(); - await lacuna.empire.login({ + const { error } = await lacuna.empire.login({ name: 'non-existant-empire', password: 'incorrect-password', browser: 'todo', @@ -13,64 +13,66 @@ test('login with errors', async () => { }); expect(console.error).toHaveBeenCalledWith(1002, 'Empire does not exist.', 'non-existant-empire'); + expect(error).toBeDefined(); }); test('login', async () => { const lacuna = new Lacuna({ serverUrl: 'http://localhost:8080/' }); - const res = await lacuna.empire.login({ + const { result } = await lacuna.empire.login({ name: 'natalie', password: '1234qwer', browser: 'todo', api_key: 'anonymous', }); - expect(res.session_id).toBeDefined(); + expect(result?.session_id).toBeDefined(); }); test('logout', async () => { const lacuna = await getLacuna(); - const res = await lacuna.empire.logout(); - expect(res).toBe(1); + const response = await lacuna.empire.logout(); + expect(response?.result).toBe(1); }); test('viewBoosts', async () => { const lacuna = await getLacuna(); - const { boosts } = await lacuna.empire.viewBoosts(); + const { result } = await lacuna.empire.viewBoosts(); - expect(boosts.food).toBeDefined(); - expect(boosts.ore).toBeDefined(); - expect(boosts.water).toBeDefined(); - expect(boosts.energy).toBeDefined(); - expect(boosts.storage).toBeDefined(); - expect(boosts.spy_training).toBeDefined(); - expect(boosts.building).toBeDefined(); - expect(boosts.happiness).toBeDefined(); + expect(result?.boosts.food).toBeDefined(); + expect(result?.boosts.ore).toBeDefined(); + expect(result?.boosts.water).toBeDefined(); + expect(result?.boosts.energy).toBeDefined(); + expect(result?.boosts.storage).toBeDefined(); + expect(result?.boosts.spy_training).toBeDefined(); + expect(result?.boosts.building).toBeDefined(); + expect(result?.boosts.happiness).toBeDefined(); }); test('getStatus', async () => { const lacuna = await getLacuna(); - const res = await lacuna.empire.getStatus(); + const { result } = await lacuna.empire.getStatus(); - expect(res.empire).toBeDefined(); - expect(res.server).toBeDefined(); - expect(res.empire.name).toBe('natalie'); + expect(result?.empire).toBeDefined(); + expect(result?.server).toBeDefined(); + expect(result?.empire.name).toBe('natalie'); }); test('setBoost', async () => { const lacuna = await getLacuna(); console.error = jest.fn(); - await lacuna.empire.setBoost({ type: 'invalidtype', weeks: -0 }); + const { error } = await lacuna.empire.setBoost({ type: 'invalidtype', weeks: -0 }); expect(console.error).toHaveBeenCalled(); + expect(error).toBeDefined(); }); test('fetchCaptcha', async () => { const lacuna = await getLacuna(); - const res = await lacuna.empire.fetchCaptcha(); + const { result } = await lacuna.empire.fetchCaptcha(); - expect(res.guid).toBeDefined(); - expect(res.url).toBeDefined(); + expect(result?.guid).toBeDefined(); + expect(result?.url).toBeDefined(); }); diff --git a/lib/endpoints/empire.ts b/lib/endpoints/empire.ts index 0389ba3..c3b5399 100644 --- a/lib/endpoints/empire.ts +++ b/lib/endpoints/empire.ts @@ -12,20 +12,20 @@ class Empire extends Endpoint { this.url = url; } - fetchCaptcha(params: E.FetchCaptchaParams = {}): Promise { - return this.callWithSession('fetch_captcha', []); + fetchCaptcha(params: E.FetchCaptchaParams = {}) { + return this.callWithSession('fetch_captcha', []); } - viewBoosts(params: E.ViewBoostsParams = {}): Promise { - return this.callWithSession('view_boosts', []); + viewBoosts(params: E.ViewBoostsParams = {}) { + return this.callWithSession('view_boosts', []); } - getStatus(params: E.GetStatusParams = {}): Promise { - return this.callWithSession('get_status', []); + getStatus(params: E.GetStatusParams = {}) { + return this.callWithSession('get_status', []); } - login(params: E.LoginParams): Promise { - return this.callWithoutSession('login', [ + login(params: E.LoginParams) { + return this.callWithoutSession('login', [ params.name, params.password, params.api_key, @@ -33,12 +33,12 @@ class Empire extends Endpoint { ]); } - logout(): Promise { - return this.callWithSession('logout', []); + logout() { + return this.callWithSession('logout', []); } - setBoost(params: E.BoostParams): Promise { - return this.callWithSession('set_boost', [params.type, params.weeks]); + setBoost(params: E.BoostParams) { + return this.callWithSession('set_boost', [params.type, params.weeks]); } } diff --git a/lib/endpoints/stats.test.ts b/lib/endpoints/stats.test.ts index 9d67013..f660ec8 100644 --- a/lib/endpoints/stats.test.ts +++ b/lib/endpoints/stats.test.ts @@ -3,8 +3,8 @@ import { getLacuna } from '../__utils__/get-lacuna'; test('credits', async () => { const lacuna = await getLacuna(); - const res = await lacuna.stats.credits(); + const { result } = await lacuna.stats.credits(); - expect(res).toBeDefined(); - expect(res['0']['Game Design']).toStrictEqual(['JT Smith', 'Jamie Vrbsky']); + expect(result).toBeDefined(); + expect(result['0']['Game Design']).toStrictEqual(['JT Smith', 'Jamie Vrbsky']); }); diff --git a/lib/endpoints/stats.ts b/lib/endpoints/stats.ts index 39ee191..8fbfc89 100644 --- a/lib/endpoints/stats.ts +++ b/lib/endpoints/stats.ts @@ -12,8 +12,8 @@ class Empire extends Endpoint { this.url = url; } - credits(): Promise { - return this.callWithSession('credits', []); + credits() { + return this.callWithSession('credits', []); } } diff --git a/lib/lacuna.ts b/lib/lacuna.ts index 0ade9ed..b4f774a 100644 --- a/lib/lacuna.ts +++ b/lib/lacuna.ts @@ -53,15 +53,19 @@ class Lacuna { } async authenticate(name: string, password: string) { - const res = await this.empire.login({ + const { result, error } = await this.empire.login({ name, password, api_key: this.config.apiKey, browser: this.config.fingerprintToken, }); - this.session.set(res.session_id); - this.log.info(`Logged in as ${name}`); + if (result) { + this.session.set(result.session_id); + this.log.info(`Logged in as ${name}`); + } else if (error) { + this.log.error('Could not log in', error); + } } buildingFromUrl(url: string) {