diff --git a/src/device-api/actions.ts b/src/device-api/actions.ts index ee1aa765..fd000f0e 100644 --- a/src/device-api/actions.ts +++ b/src/device-api/actions.ts @@ -518,3 +518,12 @@ export const getContainerIds = async ( throw new Error(`Could not find service with name '${serviceName}'`); } }; + +/** + * Get device type & arch + * Used by: + * - GET /v2/local/device-info + */ +export const getDeviceInfo = async () => { + return await config.getMany(['deviceType', 'deviceArch']); +}; diff --git a/src/device-api/v2.ts b/src/device-api/v2.ts index 200c92e5..9e85b8d9 100644 --- a/src/device-api/v2.ts +++ b/src/device-api/v2.ts @@ -330,10 +330,7 @@ router.post('/v2/local/target-state', async (req, res) => { router.get('/v2/local/device-info', async (_req, res) => { try { - const { deviceType, deviceArch } = await config.getMany([ - 'deviceType', - 'deviceArch', - ]); + const { deviceType, deviceArch } = await actions.getDeviceInfo(); return res.status(200).json({ status: 'success', @@ -342,10 +339,10 @@ router.get('/v2/local/device-info', async (_req, res) => { deviceType, }, }); - } catch (e: any) { - res.status(500).json({ + } catch (e: unknown) { + return res.status(500).json({ status: 'failed', - message: e.message, + message: (e as Error).message ?? e, }); } }); diff --git a/test/integration/device-api/actions.spec.ts b/test/integration/device-api/actions.spec.ts index f4e0da70..81fd1130 100644 --- a/test/integration/device-api/actions.spec.ts +++ b/test/integration/device-api/actions.spec.ts @@ -992,3 +992,25 @@ describe('gets service container ids', () => { } }); }); + +describe('gets device type and arch', () => { + let configGetManyStub: SinonStub; + before(() => { + // @ts-expect-error + configGetManyStub = stub(config, 'getMany').resolves({ + deviceType: 'test-type', + deviceArch: 'test-arch', + }); + }); + + after(() => { + configGetManyStub.restore(); + }); + + it('returns device type and arch', async () => { + expect(await actions.getDeviceInfo()).to.deep.equal({ + deviceType: 'test-type', + deviceArch: 'test-arch', + }); + }); +}); diff --git a/test/integration/device-api/v2.spec.ts b/test/integration/device-api/v2.spec.ts index 57ab5b80..3500ecca 100644 --- a/test/integration/device-api/v2.spec.ts +++ b/test/integration/device-api/v2.spec.ts @@ -896,4 +896,39 @@ describe('device-api/v2', () => { .expect(503); }); }); + + describe('GET /v2/local/device-info', () => { + let getDeviceInfoStub: SinonStub; + beforeEach(() => { + getDeviceInfoStub = stub(actions, 'getDeviceInfo'); + }); + afterEach(() => { + getDeviceInfoStub.restore(); + }); + + it('responds with 200 and device info', async () => { + getDeviceInfoStub.resolves({ + deviceArch: 'aarch64', + deviceType: 'raspberrypi4-64', + }); + await request(api) + .get('/v2/local/device-info') + .set('Authorization', `Bearer ${await deviceApi.getGlobalApiKey()}`) + .expect(200, { + status: 'success', + info: { + arch: 'aarch64', + deviceType: 'raspberrypi4-64', + }, + }); + }); + + it('responds with 500 if an error occurred', async () => { + getDeviceInfoStub.throws(new Error()); + await request(api) + .get('/v2/local/device-info') + .set('Authorization', `Bearer ${await deviceApi.getGlobalApiKey()}`) + .expect(500); + }); + }); });