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
23 changes: 23 additions & 0 deletions src/routes/monitor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,5 +60,28 @@ export const monitor = (
const url = new URL('monitor/init', base)
return dispatchRequest<T>(url, { ...init, method: 'PUT' }, headers)
}
},
{
name: 'connect',
action: <
T = {
connected: boolean
}
>() => {
const url = new URL('telescope/connect', base)

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

return dispatchRequest<T>(
url,
{
...init,
method: 'PUT',
body: JSON.stringify({ connect: true })
},
headers,
data
)
}
}
] as const
29 changes: 28 additions & 1 deletion tests/mocks/monitor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

/*****************************************************************************************************************/

import { eventHandler } from 'h3'
import { eventHandler, getMethod, readBody } from 'h3'

import { type Handler } from '../shared/handler'

Expand Down Expand Up @@ -49,5 +49,32 @@ export const monitorHandlers: Handler[] = [
connected: true
}
})
},
{
method: 'PUT',
url: '/api/v1/monitor/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
}
})
}
]
8 changes: 8 additions & 0 deletions tests/monitorRoutes.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,5 +49,13 @@ suite('@observerly/hyper Fiber API Monitor Client', () => {
if (!isDataResult(initialise)) return
expect(initialise).toStrictEqual({ connected: true })
})

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