Teixeira
`FST_ERR_REP_ALREADY_SENT` when using `res.send()`
I've setup context as such:
And a procedure like this:
import { CreateFastifyContextOptions } from '@trpc/server/adapters/fastify';
import models from '../database/models.js';
interface CreateInnerContextOptions extends Partial<CreateFastifyContextOptions> {
models: typeof models;
}
export async function createInnerContext(opts: CreateInnerContextOptions) {
return {
models: opts.models
};
}
export async function createContext(fastifyOptions: CreateFastifyContextOptions) {
const innerContext = await createInnerContext({ models });
return { ...fastifyOptions, ...innerContext };
}
export type Context = Awaited<typeof createContext>;
import { CreateFastifyContextOptions } from '@trpc/server/adapters/fastify';
import models from '../database/models.js';
interface CreateInnerContextOptions extends Partial<CreateFastifyContextOptions> {
models: typeof models;
}
export async function createInnerContext(opts: CreateInnerContextOptions) {
return {
models: opts.models
};
}
export async function createContext(fastifyOptions: CreateFastifyContextOptions) {
const innerContext = await createInnerContext({ models });
return { ...fastifyOptions, ...innerContext };
}
export type Context = Awaited<typeof createContext>;
import generateCampaignReport from '../../../../xlsx_reports/generateCampaignReport.js';
import { publicProcedure } from '../../../init.js';
import objectIdSchema from '../../../zod_schemas/ObjectId.js';
import { z } from 'zod';
import { Types } from 'mongoose';
export default publicProcedure
.input(
z.object({
campaignId: objectIdSchema
})
)
.mutation(async ({ input, ctx }) => {
const { campaignId } = input;
const { res } = ctx;
res.header('Content-Type', 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
res.header('Content-Disposition', `attachment; filename="clipx_workbook.xlsx"`);
const buffer = await generateCampaignReport(new Types.ObjectId(campaignId));
res.send(buffer);
});
import generateCampaignReport from '../../../../xlsx_reports/generateCampaignReport.js';
import { publicProcedure } from '../../../init.js';
import objectIdSchema from '../../../zod_schemas/ObjectId.js';
import { z } from 'zod';
import { Types } from 'mongoose';
export default publicProcedure
.input(
z.object({
campaignId: objectIdSchema
})
)
.mutation(async ({ input, ctx }) => {
const { campaignId } = input;
const { res } = ctx;
res.header('Content-Type', 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
res.header('Content-Disposition', `attachment; filename="clipx_workbook.xlsx"`);
const buffer = await generateCampaignReport(new Types.ObjectId(campaignId));
res.send(buffer);
});
7 replies
Route not found when using Fastify adapter
I'm currently running into the following error when attempting to issue a request to my server which is leveraging Fastify:
I'm instantiating the server as such:
I have triple checked that the client and server versions of tRPC match (
[19:04:02.888] INFO (57024): Route POST:/associateSocialMediaAccount not found
reqId: "req-1"
[19:04:02.892] INFO (57024): request completed
reqId: "req-1"
res: {
"statusCode": 404
}
responseTime: 5.059000000357628
[19:04:02.888] INFO (57024): Route POST:/associateSocialMediaAccount not found
reqId: "req-1"
[19:04:02.892] INFO (57024): request completed
reqId: "req-1"
res: {
"statusCode": 404
}
responseTime: 5.059000000357628
const fastifyServer = fastify({
logger: true,
maxParamLength: 5000
});
fastifyServer.register(fastifyTRPCPlugin, {
prefix: '/trpc',
trpcOptions: {
router: appRouter,
createContext,
onError({ path, error }) {
logger.error(`Error in tRPC handler on path '${path}':`, error);
}
} satisfies FastifyTRPCPluginOptions<AppRouter>['trpcOptions']
});
try {
await fastifyServer.listen({
port: environment.API_PORT,
host: environment.isDev ? 'localhost' : '0.0.0.0'
});
} catch (reason: unknown) {
logger.error(reason);
}
const fastifyServer = fastify({
logger: true,
maxParamLength: 5000
});
fastifyServer.register(fastifyTRPCPlugin, {
prefix: '/trpc',
trpcOptions: {
router: appRouter,
createContext,
onError({ path, error }) {
logger.error(`Error in tRPC handler on path '${path}':`, error);
}
} satisfies FastifyTRPCPluginOptions<AppRouter>['trpcOptions']
});
try {
await fastifyServer.listen({
port: environment.API_PORT,
host: environment.isDev ? 'localhost' : '0.0.0.0'
});
} catch (reason: unknown) {
logger.error(reason);
}
appRouter
looks like this:
(...)
import associateSocialMediaAccount from './procedures/associateSocialMediaAccount.js';
export const appRouter = router({
associateSocialMediaAccount
});
(...)
import associateSocialMediaAccount from './procedures/associateSocialMediaAccount.js';
export const appRouter = router({
associateSocialMediaAccount
});
associateSocialMediaAccount.ts:
export default publicProcedure
.input(socialMediaAccountSchema)
.mutation(async (opts) => {
// ...
});
export default publicProcedure
.input(socialMediaAccountSchema)
.mutation(async (opts) => {
// ...
});
^11.0.0-rc.700
), I'm on "typescript": "^5.7.3"
on both server and client, Node version v22.11.0
4 replies