Skip to content

Issues with creating API endpoint with different middleware for GET and POST #4490

Closed
@m5p3nc3r

Description

@m5p3nc3r

Which project does this relate to?

Router

Describe the bug

I am trying to create an API endpoint using createServerFileRoute where the GET and POST endpoint have different middleware. When using this example code as a reference, and implementing this code

import { createMiddleware, json } from "@tanstack/react-start";
import { createServerFileRoute } from "@tanstack/react-start/server";

function allow(ok: boolean) {
  return createMiddleware({ type: "request" }).server(
    async ({ next, request }) => {
      const result = await next();
      // If access denied, respond with a 403 error code
      if (!ok) {
        throw new Response(null, {
          status: 403,
        });
      }

      return result;
    }
  );
}

export const ServerRoute = createServerFileRoute("/api/reproducer/$id").methods(
  (api) => ({
    GET: api.middleware([allow(true)]).handler(async ({ params }) => {
      let response = { id: params.id };
      return json(response);
    }),
    POST: api.middleware([allow(false)]).handler(async ({ params }) => {
      let response = { id: params.id };
      return json(response);
    }),
  })
);

I get the runtime error TypeError: handler is not a function

Your Example Website or App

Details in reproducer

Steps to Reproduce the Bug or Issue

Use the reproducer code above.

Expected behavior

The GET method succeeds and the POST fails.

But I get the error "TypeError: handler is not a function"

Screenshots or Videos

No response

Platform

  • OS: Ubuntu 24.04
  • Browser: does not matter - server side issue
  • Version: @tanstack/react-router v1.121.27

Additional context

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions