From a1d6b03ec98abbc073b5b73b631da6ea3eae4eb9 Mon Sep 17 00:00:00 2001 From: Julien Dessaux Date: Wed, 27 Mar 2024 15:20:14 +0100 Subject: [node] finished the great typescript rewrite --- nodejs/lib/contracts.ts | 36 ++++++++++++++++-------------------- 1 file changed, 16 insertions(+), 20 deletions(-) (limited to 'nodejs/lib/contracts.ts') diff --git a/nodejs/lib/contracts.ts b/nodejs/lib/contracts.ts index fb62813..0adb5f6 100644 --- a/nodejs/lib/contracts.ts +++ b/nodejs/lib/contracts.ts @@ -12,21 +12,17 @@ import * as libShips from '../lib/ships.ts'; export async function accept(contract: Contract): Promise { if (contract.accepted) return contract; const response = await api.send<{agent: Agent, contract: Contract, type: ''}>({endpoint: `/my/contracts/${contract.id}/accept`, method: 'POST'}); - if ('apiError' in response) { + if (response.error) { api.debugLog(response); throw response; } - dbAgents.setAgent(response.agent); - dbContracts.setContract(response.contract); - return response.contract; + dbAgents.setAgent(response.data.agent); + dbContracts.setContract(response.data.contract); + return response.data.contract; } export async function contracts(): Promise> { - const response = await api.sendPaginated({endpoint: '/my/contracts', page: 1}); - if ('apiError' in response) { - api.debugLog(response); - throw response; - } + const response = await api.sendPaginated({endpoint: '/my/contracts'}); response.forEach(contract => dbContracts.setContract(contract)); return response; } @@ -44,8 +40,8 @@ export async function deliver(contract: Contract, ship: Ship): Promise tradeSymbol: tradeSymbol, units: units, }}); - if ('apiError' in response) { - switch(response.code) { + if (response.error) { + switch(response.error.code) { case 4509: // contract delivery terms have been met return await fulfill(contract); default: // yet unhandled error @@ -53,22 +49,22 @@ export async function deliver(contract: Contract, ship: Ship): Promise throw response; } } - dbContracts.setContract(response.contract); - dbShips.setShipCargo(ship.symbol, response.cargo); - if(response.contract.terms.deliver[0].unitsRequired >= response.contract.terms.deliver[0].unitsFulfilled) { - return await fulfill(contract); + dbContracts.setContract(response.data.contract); + dbShips.setShipCargo(ship.symbol, response.data.cargo); + if(response.data.contract.terms.deliver[0].unitsRequired >= response.data.contract.terms.deliver[0].unitsFulfilled) { + return await fulfill(response.data.contract); } - return response.contract; + return response.data.contract; } export async function fulfill(contract: Contract): Promise { if (contract.fulfilled) return contract; const response = await api.send<{agent: Agent, contract: Contract}>({ endpoint: `/my/contracts/${contract.id}/fulfill`, method: 'POST'}); - if ('apiError' in response) { + if (response.error) { api.debugLog(response); throw response; } - dbAgents.setAgent(response.agent); - dbContracts.setContract(response.contract); - return response.contract; + dbAgents.setAgent(response.data.agent); + dbContracts.setContract(response.data.contract); + return response.data.contract; } -- cgit v1.2.3