diff --git a/src/routes/focuser.ts b/src/routes/focuser.ts index 1aa449f..2954969 100644 --- a/src/routes/focuser.ts +++ b/src/routes/focuser.ts @@ -132,5 +132,30 @@ export const focuser = ( data ) } + }, + { + name: 'setPosition', + action: < + T = { + position: number + } + >(body: { + position: number + }) => { + const url = new URL('focuser/position', base) + + const data = JSON.stringify(body) + + return dispatchRequest( + url, + { + ...init, + method: 'PUT', + body: JSON.stringify(body) + }, + headers, + data + ) + } } ] as const diff --git a/tests/focuserRoutes.spec.ts b/tests/focuserRoutes.spec.ts index e2eb227..1024eef 100644 --- a/tests/focuserRoutes.spec.ts +++ b/tests/focuserRoutes.spec.ts @@ -90,5 +90,13 @@ suite('@observerly/hyper Fiber API Focuser Client', () => { if (!isDataResult(connect)) return expect(connect).toStrictEqual({ connected: false }) }) + + it('should be able to set the position of the focuser', async () => { + const client = setupClient(getURL('/api/v1/')) + const position = await client.focuser.setPosition({ position: 11500 }) + expect(isDataResult(position)).toBe(true) + if (!isDataResult(position)) return + expect(position).toStrictEqual({ position: 11500 }) + }) }) }) diff --git a/tests/mocks/focuser.ts b/tests/mocks/focuser.ts index d6cfc0a..d398a34 100644 --- a/tests/mocks/focuser.ts +++ b/tests/mocks/focuser.ts @@ -34,15 +34,6 @@ export const focuserHandlers: Handler[] = [ } }) }, - { - method: 'GET', - url: '/api/v1/focuser/position', - handler: eventHandler(_event => { - return { - position: 10000 - } - }) - }, { method: 'GET', url: '/api/v1/focuser/temperature', @@ -107,5 +98,44 @@ export const focuserHandlers: Handler[] = [ connected: body.connect } }) + }, + { + method: ['GET', 'PUT', 'DELETE'], + url: '/api/v1/focuser/position', + handler: eventHandler(async event => { + const method = getMethod(event) + + if (!['GET', 'PUT', 'DELETE'].includes(method)) { + return new Response('Method Not Allowed', { + status: 405, + statusText: 'Method Not Allowed' + }) + } + + if (method === 'GET') { + return { + position: 10000 + } + } + + if (method === 'DELETE') { + return { + position: 0 + } + } + + const body = await readBody<{ position: number }>(event) + + if (!body) { + return new Response('Bad Request', { + status: 400, + statusText: 'Bad Request' + }) + } + + return { + position: body.position + } + }) } ] diff --git a/tests/shared/handler.ts b/tests/shared/handler.ts index f695072..4f49f41 100644 --- a/tests/shared/handler.ts +++ b/tests/shared/handler.ts @@ -18,7 +18,7 @@ export interface Handler extends Record { * * */ - method: string + method: string | string[] /** * *