UNPKG

graphql-yoga

Version:

<div align="center"><img src="./website/public/cover.png" width="720" /></div>

44 lines (43 loc) 1.65 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.useReadinessCheck = void 0; /** * Adds a readiness check for Yoga by simply implementing the `check` option. */ function useReadinessCheck({ endpoint = '/ready', check, }) { let urlPattern; return { onYogaInit({ yoga }) { urlPattern = new yoga.fetchAPI.URLPattern({ pathname: endpoint }); }, async onRequest({ request, endResponse, fetchAPI, url }) { if (request.url.endsWith(endpoint) || url.pathname === endpoint || urlPattern.test(url)) { let 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); } }, }; } exports.useReadinessCheck = useReadinessCheck;