Skip to content

forwardRequest

forwardRequest(original, init?): Request

Defined in: packages/actions/src/forward-request.ts:87

Builds a new Request that mirrors original (URL, method, headers, cookies) with optional overrides for body, URL, method, or extra headers.

Use this when an action handler needs to dispatch a sub-action — for example, an “import CSV” action that builds many “create row” sub-action requests. Sub-actions resolved with the framework’s getContext() callback see a null user unless the request still carries the original cookies, which is exactly what forwardRequest preserves.

The returned request is a fresh Request, not a clone() of the source. That means:

  • The forwarded request has its own (drainable) body. Reading it does not affect the source request.
  • Headers are deep-copied. Mutating the returned request’s headers does not leak back into the source.
  • When body is omitted, the source request’s body stream is consumed. If you need to keep the source readable, call original.clone() before passing it in, or supply a fresh body explicitly.

Request

The incoming request to forward.

ForwardRequestInit = {}

Optional overrides for body, method, URL, and headers.

Request

A new Request ready to hand to a sub-action.

import { forwardRequest } from "@cfast/actions";
const importCsv = createAction(async (db, input, ctx) => ({
permissions: createRow.buildOperation(db, {} as never, ctx).permissions,
async run() {
for (const row of input.rows) {
const subFormData = new FormData();
subFormData.set("_action", "createRow");
subFormData.set("title", row.title);
const subRequest = forwardRequest(ctx.request, { body: subFormData });
await createRow.action({ request: subRequest, params: {}, context: undefined });
}
return { ok: true };
},
}));
const subRequest = forwardRequest(originalRequest, {
body: JSON.stringify({ title: "Hello" }),
headers: { "Content-Type": "application/json" },
});