microcipcip / cookie-universal

Universal cookie plugin, perfect for SSR
525 stars 39 forks source link

set default options not work #64

Closed mean-cj closed 4 years ago

mean-cj commented 4 years ago

Hello

How i can set default option for cookie-universal-nuxt ,

this not work for me

  modules: [
    {
      src: 'cookie-universal-nuxt',
      options: {
        alias: 'cookies',
        path: '/',
        expires: new Date(Date.now() + 604800), // 7day
        // maxAge : ,
        // httpOnly : null,
        domain: '.domain.com',
        // encode : ()=>{},
        sameSite: null,
        secure: true
      }
    },

thank you.

microcipcip commented 4 years ago

Hi @mean-cj, that is not supported, you have to pass the options object when you call it directly with set, get and remove. The only supported options keys are alias and parseJSON.

microcipcip commented 4 years ago

This is untested, but I guess you could remap it with the cookie-universal library by creating a plugin yourself manually, instead of using the wrapper library which is cookie-universal-nuxt. For example:

import cookieUniversal from "cookie-universal";

export default ({ req, res }, inject) => {
  const aliasName = 'cookies'
  const parseJsonbyDefault = true

  const myOptions = {
    path: "/",
    expires: new Date(Date.now() + 604800), // 7day
    // maxAge : ,
    // httpOnly : null,
    domain: ".domain.com",
    // encode : ()=>{},
    sameSite: null,
    secure: true
  };

  const cookieLib = cookieUniversal(req, res, parseJsonbyDefault);
  const cookieLibWithDefaults = {
    get: (name, optsOverride = {}) =>
      cookieLib.get(name, { ...myOptions, ...optsOverride }),
    set: (name, value, optsOverride = {}) =>
      cookieLib.set(name, value, { ...myOptions, ...optsOverride }),
    remove: (name, optsOverride = {}) =>
      cookieLib.set(name, { ...myOptions, ...optsOverride })
  };

  inject(aliasName, cookieLibWithDefaults);
};
stale[bot] commented 4 years ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.