context item seems not to persist from middleware to middleware and from middleware to function

I found this behaviour trying to set up a session object and attach it to the context in the root middleware, expecting to see it bubble up the middleware chain and eventually into the "leaf" function. Tagging @HardlyWorkin' after asking consent.

In the root middleware I attach a function to context and add a field:

import { HCContextSetup } from "./libs/HorseCollar";


async function setupHorsecollarSession(context){
console.log("setting up HC in the root middleware");
context.hc = "saddled";
console.log("we are still in the root middleware: is the context saddled? [printing context.hc]", context.hc);
const res = await context.next();
return res;
}

// export const onRequest = [setupHorsecollarSession,errorHandling];
export const onRequest = [setupHorsecollarSession];

Then, in the /api/ middleware:

async function authentication(context) {

console.log("we are in the /api/ middleware: is the context saddled? [printing context.hc]", context.hc);

return context.next();
}

export const onRequest = [authentication];

Then in the /api/user/token function:

export async function onRequestGet(context) {
console.log("we are in the /api/user/token onRequestGet function: is the context saddled? [printing context.hc]", context.hc);

return new Response("foo ["+context.hc+"]", { status: 200 });
}

output in next message
Was this page helpful?