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

Middleware gives type error if using a function variable rather than defining inline #186

Open
callumgare opened this issue Jan 21, 2025 · 0 comments

Comments

@callumgare
Copy link

callumgare commented Jan 21, 2025

Doing this works fine:

...
    .outputs([
      {
        status: 400,
        contentType: 'application/json',
        body: z.string(),
      },
    ])
    .middleware(async () => TypedNextResponse.json('test', { status: 400 }))
...

But doing this:

const middlewareFunction = async () => TypedNextResponse.json('test', { status: 400 });
...
    .outputs([
      {
        status: 400,
        contentType: 'application/json',
        body: z.string(),
      },
    ])
    .middleware(middlewareFunction)

Gives this error:

Argument of type '() => Promise<TypedNextResponseType<string, 400, AnyContentTypeWithAutocompleteForMostCommonOnes>>' is not assignable to parameter of type 'RouteMiddleware<BaseOptions, BaseOptions, unknown, number, AnyContentTypeWithAutocompleteForMostCommonOnes, { ...; }[], void | ... 1 more ... | NextResponse<...>>'.
  Type 'Promise<TypedNextResponseType<string, 400, AnyContentTypeWithAutocompleteForMostCommonOnes>>' is not assignable to type 'void | BaseOptions | TypedNextResponseType<string, 400, "application/json"> | NextResponse<string> | Promise<void | BaseOptions | TypedNextResponseType<...> | NextResponse<...>>'.
    Type 'Promise<TypedNextResponseType<string, 400, AnyContentTypeWithAutocompleteForMostCommonOnes>>' is not assignable to type 'Promise<void | BaseOptions | TypedNextResponseType<string, 400, "application/json"> | NextResponse<string>>'.
      Type 'TypedNextResponseType<string, 400, AnyContentTypeWithAutocompleteForMostCommonOnes>' is not assignable to type 'void | BaseOptions | TypedNextResponseType<string, 400, "application/json"> | NextResponse<string>'.
        Type 'TypedNextResponseType<string, 400, AnyContentTypeWithAutocompleteForMostCommonOnes>' is not assignable to type 'TypedNextResponseType<string, 400, "application/json">'.
          Type 'AnyContentTypeWithAutocompleteForMostCommonOnes' is not assignable to type '"application/json"'.
            Type 'string & Record<never, never>' is not assignable to type '"application/json"'.(2345)
const middlewareFunction: () => Promise<TypedNextResponseType<string, 400, AnyContentTypeWithAutocompleteForMostCommonOnes>>

I've made a reproducible stackblitz of the issue to demonstrate the issue.

Thanks for such a helpful framework btw!


I've narrowed it down slightly/come up with a workaround. This does work okay:

const middlewareFunction = async () => TypedNextResponse.json(
  'test', 
  { status: 400, headers: {'Content-Type': 'application/json'} }
);
...
    .outputs([
      {
        status: 400,
        contentType: 'application/json',
        body: z.string(),
      },
    ])
    .middleware(middlewareFunction)

I've updated the stackblitz to include this third example.

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

1 participant