Skip to content

Commit

Permalink
Merge pull request #103 from observerly/feature/client/camera/connect
Browse files Browse the repository at this point in the history
feat: add client.camera.connect() route handler to @observerly/hyper.
  • Loading branch information
michealroberts authored Apr 12, 2024
2 parents 7f014ec + 287765a commit d2a3cbf
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 0 deletions.
23 changes: 23 additions & 0 deletions src/routes/camera.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,29 @@ export const camera = (base: URL, init?: RequestInit, headers?: () => Promise<He
return dispatchRequest<T>(url, { ...init, method: 'PUT' }, headers)
}
},
{
name: 'connect',
action: <
T = {
connected: boolean
}
>() => {
const url = new URL('camera/connect', base)

const data = JSON.stringify({ connect: true })

return dispatchRequest<T>(
url,
{
...init,
method: 'PUT',
body: JSON.stringify({ connect: true })
},
headers,
data
)
}
},
{
name: 'turnCoolerOn',
action: <
Expand Down
8 changes: 8 additions & 0 deletions tests/camera.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,14 @@ suite('@observerly/hyper Fiber API Observing Camera Client', () => {
expect(isConnected).toStrictEqual({ connected: true })
})

it('should be able to connect the camera', async () => {
const client = setupClient(getURL('/api/v1/'))
const status = await client.camera.connect()
expect(isDataResult(status)).toBe(true)
if (!isDataResult(status)) return
expect(status).toStrictEqual({ connected: true })
})

it('should be able to get the configuration of the camera', async () => {
const client = setupClient(getURL('/api/v1/'))
const configuration = await client.camera.getConfiguration()
Expand Down
27 changes: 27 additions & 0 deletions tests/mocks/camera.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,33 @@ export const cameraHandlers: Handler[] = [
}
})
},
{
method: 'PUT',
url: '/api/v1/camera/connect',
handler: eventHandler(async event => {
const method = getMethod(event)

if (method !== 'PUT') {
return new Response('Method Not Allowed', {
status: 405,
statusText: 'Method Not Allowed'
})
}

const body = await readBody<{ connect: boolean }>(event)

if (!body) {
return new Response('Bad Request', {
status: 400,
statusText: 'Bad Request'
})
}

return {
connected: body.connect
}
})
},
{
method: ['PUT', 'DELETE'],
url: '/api/v1/camera/cooler',
Expand Down

0 comments on commit d2a3cbf

Please sign in to comment.