Skip to content

Instantly share code, notes, and snippets.

@ppulwey
Created September 9, 2022 11:15
Show Gist options
  • Save ppulwey/42ea59603815f365079deeb893e6e96d to your computer and use it in GitHub Desktop.
Save ppulwey/42ea59603815f365079deeb893e6e96d to your computer and use it in GitHub Desktop.
import { Request } from 'express';
const getCookies = <T extends Record<string, string>>(req: Request) => {
const cookieString = req.headers.cookie;
if (cookieString === undefined) {
return null;
}
const parsedCookies: Record<string, string> = {};
cookieString.split(';').map((x) => {
const cookieVals = x.trim().split('=');
parsedCookies[cookieVals[0]] = cookieVals[1];
});
return parsedCookies as T;
};
export { getCookies };
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment