cookie#CookieSerializeOptions TypeScript Examples

The following examples show how to use cookie#CookieSerializeOptions. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: get-cart-cookie.ts    From storefront-data-hooks with MIT License 7 votes vote down vote up
export default function getCartCookie(
  name: string,
  cartId?: string,
  maxAge?: number
) {
  const options: CookieSerializeOptions =
    cartId && maxAge
      ? {
          maxAge,
          expires: new Date(Date.now() + maxAge * 1000),
          secure: process.env.NODE_ENV === 'production',
          path: '/',
          sameSite: 'lax',
        }
      : { maxAge: -1, path: '/' } // Removes the cookie

  return serialize(name, cartId || '', options)
}
Example #2
Source File: controller.tsx    From farrow with MIT License 6 votes vote down vote up
/**
   * set cookie without caring whether in server or client
   * @param name
   * @param value
   * @param options
   */
  setCookie(name: string, value: string, options?: CookieSerializeOptions) {
    return Cookie.set(name, value, options, this.page.res)
  }
Example #3
Source File: cookie.ts    From farrow with MIT License 6 votes vote down vote up
getSameSite = (opts: CookieSerializeOptions) => {
  let sameSite: CookieAttributes['sameSite']

  if (typeof opts.sameSite === 'boolean') {
    if (opts.sameSite) {
      sameSite = 'strict'
    }
  } else if (opts.sameSite) {
    sameSite = opts.sameSite
  }

  return sameSite
}
Example #4
Source File: cookie.ts    From farrow with MIT License 6 votes vote down vote up
set = (name: string, value: string, options?: CookieSerializeOptions, res?: ServerResponse) => {
  const opts: CookieSerializeOptions = { ...options }
  const val = typeof value === 'object' ? `j:${JSON.stringify(value)}` : String(value)

  if (typeof opts.maxAge === 'number') {
    opts.expires = new Date(Date.now() + opts.maxAge)
    opts.maxAge /= 1000
  }

  if (!opts.path) {
    opts.path = '/'
  }

  if (!res) {
    return JSCookie.set(name, value, {
      ...opts,
      sameSite: getSameSite(opts),
    })
  }

  res.setHeader('Set-Cookie', serialize(name, String(val), opts))
}
Example #5
Source File: cookie.ts    From farrow with MIT License 6 votes vote down vote up
remove = (name: string, options?: CookieSerializeOptions, res?: ServerResponse) => {
  const opts = { expires: new Date(1), path: '/', ...options }
  if (!res) {
    return JSCookie.set(name, '', {
      ...opts,
      sameSite: getSameSite(opts),
    })
  }
  return set(name, '', opts, res)
}
Example #6
Source File: controller.tsx    From farrow with MIT License 5 votes vote down vote up
/**
   * remove cookie without caring whether in server or client
   * @param name
   * @param options
   */
  removeCookie(name: string, options?: CookieSerializeOptions) {
    return Cookie.remove(name, options, this.page.res)
  }