UNPKG

next-intlayer

Version:

Simplify internationalization i18n in Next.js with context providers, hooks, locale detection, and multilingual content integration.

1 lines 4.2 kB
{"version":3,"file":"multipleProxies.mjs","names":["proxyHeader: Headers[]"],"sources":["../../../src/proxy/multipleProxies.ts"],"sourcesContent":["import {\n type NextFetchEvent,\n type NextRequest,\n NextResponse,\n} from 'next/server';\n\n/**\n * Utility to combine multiple Next.js proxies into one.\n *\n * It executes proxies in order, merges headers, and correctly handles\n * redirects and rewrites.\n *\n * @example\n * import { multipleProxies, intlayerProxy } from \"next-intlayer/proxy\";\n * import { NextResponse } from \"next/server\";\n *\n * const authMiddleware = (req: NextRequest) => {\n * if (!req.cookies.get(\"token\")) {\n * return NextResponse.redirect(new URL(\"/login\", req.url));\n * }\n * return NextResponse.next();\n * };\n *\n * export default multipleProxies([\n * intlayerProxy,\n * authMiddleware,\n * ]);\n *\n * @param proxies - An array of proxy functions to execute in order.\n * @returns A single proxy function that runs all provided proxies.\n */\nexport const multipleProxies =\n (\n proxies: ((\n req: NextRequest,\n event?: NextFetchEvent,\n response?: NextResponse\n ) => NextResponse | Promise<NextResponse>)[]\n ) =>\n async (req: NextRequest, event?: NextFetchEvent, response?: NextResponse) => {\n // Array to store proxy headers\n const proxyHeader: Headers[] = [];\n\n // Loop through proxy functions\n for (const proxy of proxies) {\n // Execute proxy function and await the result\n const result = await proxy(req, event, response);\n\n // Check if the result is not okay and return it\n if (!result.ok) {\n return result;\n }\n\n // Push proxy headers to the array\n proxyHeader.push(result.headers);\n }\n\n // Merge all the headers to check if there is a redirection or rewrite\n const mergedHeaders = new Headers();\n\n // Merge all the custom headers added by the proxies\n const transmittedHeaders = new Headers();\n\n // Merge headers\n proxyHeader.forEach((header) => {\n for (const [key, value] of header.entries()) {\n mergedHeaders.append(key, value);\n\n // check if it's a custom header added by one of the proxies\n if (key.startsWith('x-middleware-request-')) {\n // remove the prefix to get the original key\n const fixedKey = key.replace('x-middleware-request-', '');\n\n // add the original key to the transmitted headers\n transmittedHeaders.append(fixedKey, value);\n }\n }\n });\n\n // Look for the 'x-middleware-request-redirect' header\n const redirect = mergedHeaders.get('x-middleware-request-redirect');\n\n // If a redirection is required based on the proxy headers\n if (redirect) {\n // Perform the redirection\n return NextResponse.redirect(new URL(redirect, req.url), {\n status: 307, // Temporary redirect\n });\n }\n\n // Look for the 'x-middleware-rewrite' header\n const rewrite = mergedHeaders.get('x-middleware-rewrite');\n if (rewrite) {\n // Perform the rewrite\n return NextResponse.rewrite(new URL(rewrite, req.url), {\n request: {\n headers: transmittedHeaders,\n },\n });\n }\n\n // Default: continue to next proxy\n return NextResponse.next({\n request: {\n headers: transmittedHeaders,\n },\n });\n };\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;AA+BA,MAAa,mBAET,YAMF,OAAO,KAAkB,OAAwB,aAA4B;CAE3E,MAAMA,cAAyB,EAAE;AAGjC,MAAK,MAAM,SAAS,SAAS;EAE3B,MAAM,SAAS,MAAM,MAAM,KAAK,OAAO,SAAS;AAGhD,MAAI,CAAC,OAAO,GACV,QAAO;AAIT,cAAY,KAAK,OAAO,QAAQ;;CAIlC,MAAM,gBAAgB,IAAI,SAAS;CAGnC,MAAM,qBAAqB,IAAI,SAAS;AAGxC,aAAY,SAAS,WAAW;AAC9B,OAAK,MAAM,CAAC,KAAK,UAAU,OAAO,SAAS,EAAE;AAC3C,iBAAc,OAAO,KAAK,MAAM;AAGhC,OAAI,IAAI,WAAW,wBAAwB,EAAE;IAE3C,MAAM,WAAW,IAAI,QAAQ,yBAAyB,GAAG;AAGzD,uBAAmB,OAAO,UAAU,MAAM;;;GAG9C;CAGF,MAAM,WAAW,cAAc,IAAI,gCAAgC;AAGnE,KAAI,SAEF,QAAO,aAAa,SAAS,IAAI,IAAI,UAAU,IAAI,IAAI,EAAE,EACvD,QAAQ,KACT,CAAC;CAIJ,MAAM,UAAU,cAAc,IAAI,uBAAuB;AACzD,KAAI,QAEF,QAAO,aAAa,QAAQ,IAAI,IAAI,SAAS,IAAI,IAAI,EAAE,EACrD,SAAS,EACP,SAAS,oBACV,EACF,CAAC;AAIJ,QAAO,aAAa,KAAK,EACvB,SAAS,EACP,SAAS,oBACV,EACF,CAAC"}