import { SecureCookieOptions , parseCookieOptions , sameSiteOptions } from "./utils" ; import { setCommonCookie , commonCookieOptions , Cookie } from "./cookie" ; export class SecureCookie { options : SecureCookieOptions ; constructor ( options ) { this . options = options ; } adonis ( response , name , value ) { setCommonCookie ( response , name , value , this . options ) ; } express ( res , name , value ) { setCommonCookie ( res , name , value , this . options ) ; } fastify ( res , name , value ) { const cookieValue = new Cookie ( value ) . secureCookie ( this . options ) ; res . header ( "Set-Cookie" , `${ name }=${ cookieValue }` ) ; } hapi ( h , name , value ) { const options = parseCookieOptions ( this . options ) ; const cookieOptions = { isSecure : options . secure , isHttpOnly : options . httpOnly , path : options . path } ; if ( options . expires ) { if ( typeof options . expires == "number" ) { const expireMilliseconds = options . expires * 60 * 60 * 1000 ; cookieOptions . ttl = expireMilliseconds ; } } if ( options . sameSite ) { const sameSiteValue = sameSiteOptions ( options . sameSite ) ; cookieOptions . isSameSite = sameSiteValue ; } h . state ( name , value , cookieOptions ) ; } koa ( ctx , name , value ) { const options = parseCookieOptions ( this . options ) ; const cookieOptions = commonCookieOptions ( options ) ; ctx . cookies . set ( name , value , cookieOptions ) ; } polka ( res , name , value ) { const cookieValue = new Cookie ( value ) . secureCookie ( this . options ) ; res . setHeader ( "Set-Cookie" , `${ name }=${ cookieValue }` ) ; } nest ( res , name , value ) { setCommonCookie ( res , name , value , this . options ) ; } restify ( res , name , value ) { const cookieValue = new Cookie ( value ) . secureCookie ( this . options ) ; res . header ( "Set-Cookie" , `${ name }=${ cookieValue }` ) ; } sails ( res , name , value ) { setCommonCookie ( res , name , value , this . options ) ; } total ( res , name , value ) { setCommonCookie ( res , name , value , this . options ) ; } } export interface CommonCookie { expires ? : Date ; httpOnly ? : boolean ; path ? : string ; secure ? : boolean ; sameSite ? : boolean | string ; [ key ] : boolean | string | undefined | Date ; } interface HapiCookie { isSecure ? : boolean ; isHttpOnly ? : boolean ; isSameSite ? : string | boolean ; path ? : string ; ttl ? : number ; [ key ] : boolean | string | undefined | number ; }