-
Notifications
You must be signed in to change notification settings - Fork 558
/
useReadinessCheck.ts
67 lines (65 loc) 路 2.1 KB
/
useReadinessCheck.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import { FetchAPI } from '../types.js'
import { Plugin } from './types.js'
export interface ReadinessCheckPluginOptions {
/**
* Under which endpoint do you want the readiness check to be?
*
* @default /ready
*/
endpoint?: string
/**
* The check for whether the service is ready to perform.
*
* You should check here whether the services Yoga depends on
* are ready and working, for example: is the database up and running?
*
* - Returning `true` or nothing will respond with a 200 OK.
* - Returning `false` or throwing an error will respond with a 503 Service Unavailable.
* - Returning a `Response` will have the readiness check respond with it.
*
* Beware that if an instance of `Error` is thrown, its message will be present in the
* response body. Be careful which information you expose.
*/
check: (payload: {
request: Request
fetchAPI: FetchAPI
}) => void | boolean | Response | Promise<void | boolean | Response>
}
/**
* Adds a readiness check for Yoga by simply implementing the `check` option.
*/
export function useReadinessCheck({
endpoint = '/ready',
check,
}: ReadinessCheckPluginOptions): Plugin {
let urlPattern: URLPattern
return {
async onRequest({ request, endResponse, fetchAPI }) {
if (!urlPattern) {
urlPattern = new fetchAPI.URLPattern({ pathname: endpoint })
}
if (urlPattern.test(request.url)) {
let response: Response
try {
const readyOrResponse = await check({ request, fetchAPI })
if (typeof readyOrResponse === 'object') {
response = readyOrResponse
} else {
response = new fetchAPI.Response(null, {
status: readyOrResponse === false ? 503 : 200,
})
}
} catch (err) {
const isError = err instanceof Error
response = new fetchAPI.Response(isError ? err.message : null, {
status: 503,
headers: isError
? { 'content-type': 'text/plain; charset=utf-8' }
: {},
})
}
endResponse(response)
}
},
}
}