Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NextJS app router support #435

Open
AashJ opened this issue Jan 30, 2024 · 4 comments
Open

NextJS app router support #435

AashJ opened this issue Jan 30, 2024 · 4 comments

Comments

@AashJ
Copy link

AashJ commented Jan 30, 2024

Any plans to support the NextJS app router or an example of how to do so? I think the NextResponse and NextRequest objects are slightly different than the NextAPiResponse/NextAPIRequest objects, so the current iteration doesn't work without some messing around of the objects.

@AashJ AashJ changed the title NextJS app router NextJS app router support Jan 30, 2024
@ccssmnn
Copy link

ccssmnn commented Feb 26, 2024

This should be fairly easy to set up once a fetch adapter is implemented #188

@Vercjames
Copy link

Forked and support added here:
https://github.com/Vercjames/package-trpc-swagger

@kdy1
Copy link

kdy1 commented Apr 14, 2024

@Mehulparekh144
Copy link

.npmjs.com/package/next-trpc-openapi

I tried using ur framework I am getting errors like
for each HTTP method instead.
⨯ No HTTP methods exported in 'C:\Users\Mehul\Desktop\testT3\demo4\src\app\api[...trpc]\route.ts'. Export a named export for each HTTP method.
GET /api/say-hello 405 in 40ms
⨯ Detected default export in 'C:\Users\Mehul\Desktop\testT3\demo4\src\app\api[...trpc]\route.ts'. Export a named export for each HTTP method instead.
⨯ No HTTP methods exported in 'C:\Users\Mehul\Desktop\testT3\demo4\src\app\api[...trpc]\route.ts'. Export a named export for each HTTP method.

`
[trpc]/route.ts
import { inferRouterContext } from "@trpc/server";
import { FetchCreateContextFn, FetchCreateContextFnOptions, fetchRequestHandler } from "@trpc/server/adapters/fetch";
import { type NextRequest } from "next/server";

import { env } from "/env";
import { appRouter } from "
/server/api/root";
import { createTRPCContext } from "/server/api/trpc";
import { db } from "
/server/db";

/**

  • This wraps the createTRPCContext helper and provides the required context for the tRPC API when
  • handling a HTTP request (e.g. when you make requests from Client Components).
    */
    // export const createContext = async (req: NextRequest) => {
    // return createTRPCContext({
    // headers: req.headers,
    // });
    // };
    export const createContext: FetchCreateContextFn = async (opts: FetchCreateContextFnOptions) => {
    const req = opts.req; // Access request object from options
    // Your logic to create context
    const headers = req.headers;

return { headers, db } as inferRouterContext; // Return the context object
};

const trpcApiRouteHandler = (req: Request) =>
fetchRequestHandler({
endpoint: "/api",
req,
// Your trpc router
router: appRouter,
// Your trpc createContext
createContext,
});

export {
trpcApiRouteHandler as DELETE,
trpcApiRouteHandler as GET,
trpcApiRouteHandler as HEAD,
trpcApiRouteHandler as PATCH,
trpcApiRouteHandler as POST,
trpcApiRouteHandler as PUT,
};
`

`
[...trpc]\route.ts

import { createOpenApiNextAppHandler } from "next-trpc-openapi";
import { appRouter } from "~/server/api/root";
import { createContext } from "../trpc/[trpc]/route";

const handler = createOpenApiNextAppHandler({
// Your trpc router
router: appRouter,
// Your trpc createContext
createContext : createContext,
responseMeta: undefined,
onError: undefined,
});

export default handler;
`

Can you help with this. Appreciate it. Thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants