From 8f11dd042943149211a7f504b961054e2d0e2e3c Mon Sep 17 00:00:00 2001 From: Andrew Min Date: Tue, 10 Sep 2024 22:50:37 -0400 Subject: [PATCH] attempt to use nextjs libraries --- packages/sdk-server/src/sdk-client.ts | 36 +++++++++++++++------------ 1 file changed, 20 insertions(+), 16 deletions(-) diff --git a/packages/sdk-server/src/sdk-client.ts b/packages/sdk-server/src/sdk-client.ts index 5db28ff1..8220df85 100644 --- a/packages/sdk-server/src/sdk-client.ts +++ b/packages/sdk-server/src/sdk-client.ts @@ -10,11 +10,7 @@ import type { import { TurnkeySDKClientBase } from "./__generated__/sdk-client-base"; import type { Request, Response, RequestHandler } from "express"; -import type { - NextApiRequest, - NextApiResponse, - NextApiHandler, -} from "./__types__/base"; +import { NextRequest, NextResponse } from "next/server"; const DEFAULT_API_PROXY_ALLOWED_METHODS = [ "oauth", @@ -85,32 +81,40 @@ export class TurnkeyServerSDK { }; }; - nextProxyHandler = (config: TurnkeyProxyHandlerConfig): NextApiHandler => { + nextProxyHandler = (config: TurnkeyProxyHandlerConfig) => { const allowedMethods = config.allowedMethods ?? DEFAULT_API_PROXY_ALLOWED_METHODS; - return async ( - request: NextApiRequest, - response: NextApiResponse - ): Promise => { - const { methodName, params } = request.body; + return async (request: NextRequest): Promise => { + const body = await request.json(); + const { methodName, params } = body; + if (!methodName || !params) { - response.status(400).send("methodName and params are required."); + NextResponse.json( + { error: "methodName and params are required." }, + { status: 400 } + ); } try { if (allowedMethods.includes(methodName)) { const result = await this.apiProxy(methodName, params); - response.json(result); + NextResponse.json(result); } else { - response.status(401).send("Unauthorized proxy method"); + NextResponse.json( + { error: "Unauthorized proxy method." }, + { status: 401 } + ); } return; } catch (error) { if (error instanceof Error) { - response.status(500).send(error.message); + NextResponse.json({ error: error.message }, { status: 500 }); } else { - response.status(500).send("An unexpected error occurred"); + NextResponse.json( + { error: "An unexpected error occurred." }, + { status: 500 } + ); } return; }