Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions src/routes/focuser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<T>(
url,
{
...init,
method: 'PUT',
body: JSON.stringify(body)
},
headers,
data
)
}
}
] as const
8 changes: 8 additions & 0 deletions tests/focuserRoutes.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 })
})
})
})
48 changes: 39 additions & 9 deletions tests/mocks/focuser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down Expand Up @@ -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
}
})
}
]
2 changes: 1 addition & 1 deletion tests/shared/handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export interface Handler extends Record<string, unknown> {
*
*
*/
method: string
method: string | string[]
/**
*
*
Expand Down