-
Notifications
You must be signed in to change notification settings - Fork 558
/
useUnhandledRoute.ts
46 lines (44 loc) 路 1.23 KB
/
useUnhandledRoute.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
import landingPageBody from '../landing-page-html.js'
import type { Plugin } from './types.js'
export function useUnhandledRoute(args: {
graphqlEndpoint: string
showLandingPage: boolean
}): Plugin {
let urlPattern: URLPattern
return {
onRequest({ request, fetchAPI, endResponse, url }) {
if (!urlPattern) {
urlPattern = new fetchAPI.URLPattern({ pathname: args.graphqlEndpoint })
}
if (!urlPattern.test(url)) {
if (
args.showLandingPage === true &&
request.method === 'GET' &&
Boolean(request.headers?.get('accept')?.includes('text/html'))
) {
endResponse(
new fetchAPI.Response(
landingPageBody
.replace(/__GRAPHIQL_LINK__/g, args.graphqlEndpoint)
.replace(/__REQUEST_PATH__/g, url.pathname),
{
status: 200,
statusText: 'OK',
headers: {
'Content-Type': 'text/html',
},
},
),
)
return
}
endResponse(
new fetchAPI.Response('', {
status: 404,
statusText: 'Not Found',
}),
)
}
},
}
}