import { Router } from "@fastly/expressly";

const backend = "origin_0";
const router = new Router({ parseCookies: true });

// Configure middleware that runs on all requests.
router.use(async (req, res) => {
  console.log(`Original Cookie header is "${req.headers.get('cookie')}"`);

  // Remove a cookie from the request.
  req.cookies.delete("myCookie");
  console.log(`Removed myCookie. Updated Cookie is "${req.headers.get('cookie')}"`);
  // Remove all cookies from the request:
  // req.cookies.clear();

  let beresp = await fetch(req, { backend });

  // Set a cookie in the response.
  res.cookie('myCookie', 'foo', { path: "/", maxAge: 60 });
  // Set another cookie.
  res.cookie('mySecondCookie', 'bar', { httpOnly: true });

  // It is usually a good idea to prevent downstream caching of
  // responses that set cookies.
  res.headers.set('Cache-Control', 'no-store, private');

  res.send(beresp);
});

router.listen();