Closed andydam closed 6 months ago
Thanks for reporting, this is a bug that has now been fixed in v5.1.6
, upgrading to that version should address your issue.
@blomqma Thanks for fixing that but I think there might be a bug with your fix, a
Next REST Framework encountered an error:
SyntaxError: Unexpected end of JSON input
error is returned if the rpcOperation
is created without an input and if a body isn't passed in the request
Example:
// src/app/rpc/[operationId]/route.ts
import { rpcOperation, rpcRoute } from 'next-rest-framework';
import { z } from 'zod';
const testRpc = rpcOperation()
.outputs([{ schema: z.string() }])
.handler(() => {
return 'asdf';
});
export const { POST } = rpcRoute({ testRpc });
@blomqma Thanks for fixing that but I think there might be a bug with your fix, a
Next REST Framework encountered an error: SyntaxError: Unexpected end of JSON input
error is returned if the
rpcOperation
is created without an input and if a body isn't passed in the requestExample:
// src/app/rpc/[operationId]/route.ts import { rpcOperation, rpcRoute } from 'next-rest-framework'; import { z } from 'zod'; const testRpc = rpcOperation() .outputs([{ schema: z.string() }]) .handler(() => { return 'asdf'; }); export const { POST } = rpcRoute({ testRpc });
@andydam You are correct, thanks for the follow-up report. The initial fix was too naive, causing this regression. I have now fixed it for good with better test coverage in v5.1.8
, hope this resolves your issue.
When using
rpcRoute()
with App Router, I noticed that calling the RPC Operation over the App Router route passes aReadableStream
instead of the actual valuesExample:
The RPC Operation works fine when called during SSR or when using
rpcApiRoute()
in Pages Router.Looks like it has something to do with the request body's
ReadableStream
being passed directly to the handler (and middlewares)?https://github.com/blomqma/next-rest-framework/blob/53999d54579d79672316a174ebe39f7256e70057/packages/next-rest-framework/src/app-router/rpc-route.ts#L113
It looks like
rpcRoute()
at least parses the body correctly for validation.https://github.com/blomqma/next-rest-framework/blob/53999d54579d79672316a174ebe39f7256e70057/packages/next-rest-framework/src/app-router/rpc-route.ts#L83
There's a workaround where I can conditionally parse the
ReadableStream
manually in the handler but doesn't seem ideal, if that's the intent the argument types for the handler is wrong.