Skip to content

Commit

Permalink
feat: add cookie parser support
Browse files Browse the repository at this point in the history
  • Loading branch information
terwer committed Mar 1, 2024
1 parent 77a0ef2 commit 426f881
Show file tree
Hide file tree
Showing 2 changed files with 1 addition and 12 deletions.
2 changes: 1 addition & 1 deletion README.md
Expand Up @@ -47,7 +47,7 @@ curl --location 'http://localhost:3000/api/middleware/fetch' \

1、返回结果支持 cookie。后续所有 cors 对应的 header 统一放在 body 的 "cors-received-headers" 字段

2、直接cookie解析
2、支持 cookie 解析

3、升级 next.js 到 v14

Expand Down
11 changes: 0 additions & 11 deletions pages/api/middleware/fetch.ts
@@ -1,7 +1,6 @@
import Cors from "cors"
import initMiddleware from "../../../utils/cors/init-middleware"
import { NextApiRequest, NextApiResponse } from "next"
import cookie from "cookie-parse"

// Initialize the cors middleware
const cors = initMiddleware(
Expand Down Expand Up @@ -59,29 +58,19 @@ export default async function handler(
.then((response) => {
try {
const cookieArray = []
const cookieObjectArray = []
const myHeaders = response.headers
for (const pair of myHeaders.entries()) {
const key = pair[0]
const value = pair[1]
if (key.toLowerCase() === "set-cookie") {
cookieArray.push(value)

let cookies = {}
try {
cookies = cookie.parse(value)
cookieObjectArray.push(cookies)
} catch (e) {
console.error("Failed to parse cookie =>" + e)
}
} else {
// console.log(`Header ${key} is not allowed to expose`)
}
}

const corsHeaders = {
"Set-Cookie-Array": cookieArray,
"Set-Cookie-Object-Array": cookieObjectArray,
}
// console.log("corsHeaders =>", corsHeaders)

Expand Down

0 comments on commit 426f881

Please sign in to comment.