diff --git a/src/util.ts b/src/util.ts index 649ee02..7976248 100644 --- a/src/util.ts +++ b/src/util.ts @@ -1,20 +1,18 @@ export function instanceKeepAlive(instanceId: string, token: string) { - fetch(`https://api.rerobots.net/instance/${instanceId}/ka`, { + return fetch(`https://api.rerobots.net/instance/${instanceId}/ka`, { method: 'POST', headers: { Authorization: 'Bearer ' + token, 'Content-Type': 'application/json', }, - }) - .then((res) => { - if (res.ok) { - setTimeout(() => { - instanceKeepAlive(instanceId, token); - }, 45000); - return; - } - }) - .catch((err) => { - console.log(err); - }); + }).then((res) => { + if (res.ok) { + setTimeout(() => { + instanceKeepAlive(instanceId, token); + }, 45000); + return; + } else if (res.status < 404) { + throw new Error(`${res.status}`); + } + }); } diff --git a/tests/instanceKeepAlive.test.ts b/tests/instanceKeepAlive.test.ts new file mode 100644 index 0000000..d6aea08 --- /dev/null +++ b/tests/instanceKeepAlive.test.ts @@ -0,0 +1,7 @@ +import { instanceKeepAlive } from '../src/util'; + +test('unauthenticated calls fail', () => { + return expect( + instanceKeepAlive('1926b858-0d00-4b84-b7ca-5c56be880525', ''), + ).rejects.toThrow(); +});