-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmiddleware.ts
More file actions
58 lines (51 loc) · 1.85 KB
/
Copy pathmiddleware.ts
File metadata and controls
58 lines (51 loc) · 1.85 KB
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
import { NextRequest, NextFetchEvent, NextResponse } from "next/server";
export default async function middleware(req: NextRequest) {
switch (req.nextUrl.pathname.split("/").pop()) {
case "strippedEtag":
return new NextResponse("Hello from middleware!", {
headers: { sonos: "rocks", etag: "666" },
});
case "brokenEtag":
return NextResponse.next({ headers: { sonos: "rocks", etag: "666" } });
case "ifMatch":
return handleIfMatch(req);
default:
return new NextResponse(undefined, { status: 404 });
}
}
async function handleIfMatch(req: NextRequest) {
if (req.method !== "GET" && req.method !== "POST")
return new NextResponse(undefined, { status: 405 });
const upstreamUrl = req.nextUrl.searchParams.get("upstream-url");
if (!upstreamUrl)
return new NextResponse("Missing upstreamUrl", { status: 400 });
const upstreamResponse =
req.method === "GET"
? await fetch(upstreamUrl, {
headers: {
authorization: req.headers.get("authorization") ?? "asdf",
},
})
: await sendUpstreamPost(req, upstreamUrl);
return new NextResponse(upstreamResponse.body, {
headers: getDownstreamHeaders(upstreamResponse),
});
}
const sendUpstreamPost = async (req: NextRequest, upstreamUrl: string) => {
return fetch(upstreamUrl, {
method: "POST",
body: req.body ? JSON.stringify(await req.json()) : "no body",
headers: {
authorization: req.headers.get("authorization") ?? "asdf",
"Content-type": "application/json",
"if-match": req.headers.get("if-match") ?? "asdf",
},
});
};
const getDownstreamHeaders = (upstreamResponse: Response) => {
const headers = new Headers(upstreamResponse.headers);
headers.delete("content-length");
headers.delete("content-encoding");
headers.delete("transfer-encoding");
return headers;
};