Wanted: A way to pass data from parts to response processors

Hi all,

I would like a way to return some data from a part/layout/page, and to access that data in a response processor.

Replacement for “headers-hack”

I want this feature as a replacement for the hack I am currently using. I use headers to pass data to response processors. The main problem for headers is that they will overwrite each other if the same key is used. (and it’s not intuitive for other developers that it’s a hack I’m using)

I find myself using this hack again-and-again, I and I was thinking there should be a better way. Some of the places I’m using this hack is:

  1. Redirecting from a part
  2. Passing a status code from a part
  3. Passing the whole body of a turbo stream from a part
  4. Passing server timings from pages/parts/layouts. (I have to use different keys for each, so that they don’t collide. It’s not pretty.)

Example of feature:

// my-form part
export function post(req: Request): Response {
  const res = externalFunction(req);

  return {
    body: render(view,  res);

    stuff: {
      status: 201
    } 
  }
}

In the response processor I want to get all values from a key in stuff as an Array (similar to page contributions).

export function responseProcessor(req: Request, res: Response): Response {
  // req.stuff.status is an array with all values from parts/layout/pages

  if (req.stuff.status) {
    res.status = getMax(forceArray(req.stuff.status)) ?? 200;
  }

  return res;
}

Have a great day guys!
– Tom Arild

3 Likes

I’m not necessarily advocating for actually using the name stuff. :smiley:

1 Like

In other words: me too :slight_smile:

1 Like