@pantheon-systems/pcc-react-sdk
Version:
Pantheon Content Cloud React SDK
1 lines • 4.18 kB
Source Map (JSON)
{"version":3,"sources":["../../src/core/pantheon-api.ts"],"names":["NextPantheonAPI","options","api","PantheonAPI","req","res","__async","isPagesRouterResponse","appRouterParams","headers","__spreadValues","cookiesToObj","key","value","status","path","data","cookies","acc","cookie"],"mappings":"2+BAmBO,SAASA,CAAAA,CAAgBC,EAA8B,CAC5D,IAAMC,EAAMC,WAAYF,CAAAA,CAAO,EA2C/B,OAzCyB,CAAOG,EAAKC,CAAQC,GAAAA,CAAAA,CAAA,sBAC3C,GAAIC,CAAAA,CAAsBF,CAAG,CAI3B,CAAA,OAAO,KAAM,MAAMH,CAAAA,CAAIE,EAAuBC,CAAsB,CAAA,CAAA,CAItE,IAAMG,CAAkBH,CAAAA,CAAAA,CAClBI,EAAU,IAAI,OAAA,CAAQC,EAAA,EACtBF,CAAAA,CAAAA,CAAgB,SAAW,EAAC,CACjC,EAED,OAAQ,MAAMN,EACZ,CACE,KAAA,CAAOQ,IAAA,EACF,CAAA,MAAA,CAAO,YAAaN,CAAoB,CAAA,OAAA,CAAQ,YAAY,CAC5DI,CAAAA,CAAAA,CAAAA,CAAgB,QAErB,OAASG,CAAAA,CAAAA,CAAcP,EAAoB,OAAO,CACpD,EACA,CACE,SAAA,CAAYQ,GAAQH,CAAQ,CAAA,GAAA,CAAIG,CAAG,CAAK,EAAA,EAAA,CACxC,UAAW,CAACA,CAAAA,CAAKC,IAAUJ,CAAQ,CAAA,GAAA,CAAIG,EAAKC,CAAM,CAAA,QAAA,EAAU,CAC5D,CAAA,QAAA,CAAU,CAACC,CAAQC,CAAAA,CAAAA,IACjBN,EAAQ,GAAI,CAAA,UAAA,CAAYM,CAAI,CACrB,CAAA,IAAI,SAAS,IAAM,CAAA,CACxB,OAAAD,CACA,CAAA,OAAA,CAAAL,CACF,CAAC,CAAA,CAAA,CAEH,KAAOO,CACE,EAAA,QAAA,CAAS,KAAKA,CAAM,CAAA,CACzB,QAAAP,CACF,CAAC,CAEL,CACF,CACF,EAGF,CAEA,SAASF,EACPF,CACwB,CAAA,CAIxB,OAAOA,CAAO,EAAA,IAAA,EAAQ,OAAOA,CAAQ,EAAA,QAAA,EAAY,EAAE,QAAYA,GAAAA,CAAAA,CACjE,CAEA,SAASM,CAAAA,CAAaM,EAAiC,CAErD,OAAOA,EAAQ,MAAO,EAAA,CAAE,OACtB,CAACC,CAAAA,CAAKC,KACJD,CAAIC,CAAAA,CAAAA,CAAO,IAAI,CAAIA,CAAAA,CAAAA,CAAO,MACnBD,CAET,CAAA,CAAA,EACF,CACF","file":"index.mjs","sourcesContent":["import {\n PantheonAPI,\n PantheonAPIOptions,\n} from \"@pantheon-systems/pcc-sdk-core\";\nimport type { NextApiRequest, NextApiResponse } from \"next\";\nimport type { NextRequest } from \"next/server\";\n\nexport interface AppRouterParams {\n params: Record<string, string>;\n headers?: null;\n}\n\ntype Handler = {\n // In Pages routing, req and res are NextApiRequest and NextApiResponse\n (req: NextApiRequest, res: NextApiResponse): Promise<unknown>;\n // In App routing, req is NextRequest and the second argument is AppRouterParams\n (req: NextRequest, res: AppRouterParams): Promise<void | Response>;\n};\n\nexport function NextPantheonAPI(options?: PantheonAPIOptions) {\n const api = PantheonAPI(options);\n\n const handler: Handler = async (req, res) => {\n if (isPagesRouterResponse(res)) {\n // Pages router\n // Intentionally voiding the return value, page router API routes should not return a value\n // https://github.com/vercel/next.js/discussions/48951\n return void (await api(req as NextApiRequest, res as NextApiResponse));\n }\n\n // App router\n const appRouterParams = res as AppRouterParams;\n const headers = new Headers({\n ...(appRouterParams.headers || {}),\n });\n\n return (await api(\n {\n query: {\n ...Object.fromEntries((req as NextRequest).nextUrl.searchParams),\n ...appRouterParams.params,\n },\n cookies: cookiesToObj((req as NextRequest).cookies),\n },\n {\n getHeader: (key) => headers.get(key) || \"\",\n setHeader: (key, value) => headers.set(key, value.toString()),\n redirect: (status, path) => {\n headers.set(\"Location\", path);\n return new Response(null, {\n status,\n headers,\n });\n },\n json: (data) => {\n return Response.json(data, {\n headers,\n });\n },\n },\n )) as void | Response;\n };\n\n return handler;\n}\n\nfunction isPagesRouterResponse(\n res: AppRouterParams | NextApiResponse,\n): res is NextApiResponse {\n // We can differentiate between app router vs pages api\n // by checking for params\n // reference: (https://github.com/nextauthjs/next-auth/blob/v4/packages/next-auth/src/next/index.ts)\n return res != null && typeof res === \"object\" && !(\"params\" in res);\n}\n\nfunction cookiesToObj(cookies: NextRequest[\"cookies\"]) {\n // Convert to name value pairs\n return cookies.getAll().reduce(\n (acc, cookie) => {\n acc[cookie.name] = cookie.value;\n return acc;\n },\n {} as Record<string, string>,\n );\n}\n"]}