Last active
January 5, 2024 11:14
-
-
Save transcendr/050d5a43037d240970da555e634552fc to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//import { createNextApiHandler } from "@trpc/server/adapters/next"; | |
import { nodeHTTPRequestHandler } from '@trpc/server/adapters/node-http'; | |
import { appRouter, createContext } from '@wildcard/alpha-v2/trpc'; | |
import { LoggingService } from '@wildcard-nde/utils-logging'; | |
import { TRPCError } from '@trpc/server'; | |
import { NextApiHandler, NextApiRequest, NextApiResponse } from 'next'; | |
import Cors from 'cors'; | |
// Initializing the cors middleware | |
// You can read more about the available options here: | |
// https://github.com/expressjs/cors#configuration-options | |
const cors = Cors({ | |
methods: ['POST', 'PUT', 'GET', 'HEAD'], | |
}); | |
// Helper method to wait for a middleware to execute before continuing | |
// And to throw an error when an error happens in a middleware | |
function runMiddleware( | |
req: NextApiRequest, | |
res: NextApiResponse, | |
fn: { | |
( | |
req: Cors.CorsRequest, | |
res: { | |
statusCode?: number; | |
setHeader(key: string, value: string): any; | |
end(): any; | |
}, | |
next: (err?: any) => any | |
): void; | |
( | |
arg0: NextApiRequest, | |
arg1: NextApiResponse<any>, | |
arg2: (result: any) => void | |
): void; | |
} | |
) { | |
return new Promise((resolve, reject) => { | |
fn(req, res, (result: any) => { | |
if (result instanceof Error) { | |
return reject(result); | |
} | |
return resolve(result); | |
}); | |
}); | |
} | |
// https://github.com/trpc/trpc/blob/main/packages/server/src/adapters/next.ts#L19 | |
const customNextApiHandler: NextApiHandler = async (req, res) => { | |
try { | |
// Run the middleware | |
await runMiddleware(req, res, cors); | |
// Remove the /cluster prefix from the request URL | |
const path = req.url.replace(/^\/cluster/, ''); | |
// Proxy the request to the dev cluster by prefixing the dev cluster's hostname | |
const devClusterHostname = 'https://wwww2.dev.w6d.io'; // Replace with actual dev cluster hostname | |
// Our request URL | |
const url = new URL(path, devClusterHostname); | |
// Get the session cookie | |
const kratosSessionCookie = process.env['KRATOS_SESSION_COOKIE']; | |
// Make a call to the dev cluster with the cookie and the current request data | |
// and return the response | |
// result = [something from dev cluster] | |
// Return the response from the dev cluster | |
//res.status(result.status).json(result.body); | |
} catch (error) { | |
const logService = new LoggingService('[cluster endpoint]'); | |
logService.addError({ log: error }); | |
res.status(500).json({ message: 'Internal Server Error' }); | |
} | |
}; | |
export default customNextApiHandler; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment