import { IncomingMessage , ServerResponse } from 'http' ; import { CookieSerializeOptions , parse , serialize } from 'cookie' ; export const getAll = ( req ) : { [ key ] : string ; } => { return parse ( req . headers . cookie || '' ) ; } ; export const get = ( req , name ) : string => { const cookies = getAll ( req ) ; return cookies [ name ] ; } ; export const set = ( res , name , value , options = { } ) : void => { const strCookie = serialize ( name , value , options ) ; let previousCookies = res . getHeader ( 'Set-Cookie' ) || [ ] ; if ( ! Array . isArray ( previousCookies ) ) { previousCookies = [ previousCookies as string ] ; } res . setHeader ( 'Set-Cookie' , [ ... previousCookies , strCookie ] ) ; } ; export const clear = ( res , name , options = { } ) : void => { const { domain , path , secure , sameSite } = options ; const clearOptions = { domain , path , maxAge : 0 } ; if ( sameSite === 'none' ) { clearOptions . secure = secure ; clearOptions . sameSite = sameSite ; } set ( res , name , '' , clearOptions ) ; } ;