{"size": 1570, "ext": "js", "max_stars_repo_path": "nuxt/plugins/cookie.js", "max_stars_repo_name": "shunsukeaihara/my-django-scaffold", "max_stars_repo_stars_event_min_datetime": "2019-01-29T08:15:31.000Z", "max_stars_repo_stars_event_max_datetime": "2019-01-29T08:15:31.000Z", "max_issues_repo_path": "nuxt/plugins/cookie.js", "max_issues_repo_name": "shunsukeaihara/my-django-scaffold", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "nuxt/plugins/cookie.js", "max_forks_repo_name": "shunsukeaihara/my-django-scaffold", "max_forks_repo_forks_event_min_datetime": null, "max_forks_repo_forks_event_max_datetime": null}
{"max_stars_count": 1, "max_issues_count": null, "max_forks_count": null, "avg_line_length": 27.5438596491, "max_line_length": 117, "alphanum_fraction": 0.6343949045}
// requierd: js-cookie, cookies, moment
import moment from 'moment'

function cookieStringify(val) {
  return encodeURIComponent(String(JSON.stringify(val)))
    .replace(/%(23|24|26|2B|3A|3C|3E|3D|2F|3F|40|5B|5D|5E|60|7B|7D|7C)/g, decodeURIComponent)
}

class CookieWrap {
  constructor(cookie) {
    this._cookie = cookie
    this.options = {
      secure: process.env.NODE_ENV === "production",
      domain: null,
      httpOnly: false,
    }
  }

  mergeOptions(obj) { return Object.assign(obj, this.options) }

  get(key) { return this._cookie.get(key, this.options) }

  set(key, value) {
    if (process.server) {
      this._cookie.set(key, value, this.mergeOptions({ expires: moment().add(7, 'days').toDate() }))
    } else {
      this._cookie.set(key, value, this.mergeOptions({ expires: 7 }))
    }
  }

  setJson(key, value) {
    if (process.server) {
      this._cookie.set(key, cookieStringify(value), this.mergeOptions({ expires: moment().add(7, 'days').toDate() }))
    } else {
      this._cookie.set(key, value, this.mergeOptions({ expires: 7 }))
    }
  }

  remove(key) {
    if (process.server) {
      this._cookie.set(key, '', this.mergeOptions({ expires: new Date(0) }))
    } else {
      this._cookie.remove(key, this.options)
    }
  }
}

export default ({ req, res }, inject) => {
  let cookieInterface
  if (process.server) {
    const Cookies = require('cookies')
    cookieInterface = new Cookies(req, res, { secure: true })
  } else {
    cookieInterface = require('js-cookie')
  }
  inject('cookie', new CookieWrap(cookieInterface))
}