-
Notifications
You must be signed in to change notification settings - Fork 86
/
Copy pathedge.js
53 lines (50 loc) · 1.16 KB
/
edge.js
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
export const config = { runtime: 'edge' }
/**
* @param {import('next/server').NextRequest} req
*/
export default async (req) => {
const handlerName = req.nextUrl.searchParams.get('handler')
const handler = handlers.get(handlerName) || defaultHandler
return handler()
}
/**
* @type {Map<string, () => Promise<Response>>}
*/
const handlers = new Map([
[
'text-file',
async () => {
const url = new URL('../../src/text-file.txt', import.meta.url)
return fetch(url)
},
],
[
'image-file',
async () => {
const url = new URL('../../src/vercel.png', import.meta.url)
return fetch(url)
},
],
// [
// 'from-node-module',
// async () => {
// const url = new URL('my-pkg/hello/world.json', import.meta.url)
// return fetch(url)
// },
// ],
[
'remote-full',
async () => {
const url = new URL('https://example.vercel.sh')
return fetch(url)
},
],
[
'remote-with-base',
async () => {
const url = new URL('/', 'https://example.vercel.sh')
return fetch(url)
},
],
])
const defaultHandler = async () => new Response('Invalid handler', { status: 400 })