前言
安利一个自家封装的基于web的cookie
库,主要用途是对浏览器本地cookie
的进行一些基本操作,包括:增、删、查
。
issue
,谢谢您的 Star
cookie-web
让cookie更简单!
安装
npm install cookie-web
yarn add cookie-web
方法
const cookie = require('cookie-web')
import cookie from 'cookie-web'
void cookie.setOne (key, val, options = {})
通过key-val(键值对)添加一个cookie
当然,这儿还可以添加其他设置项options = { expires, // 过期时间 path, // 允许访问的路由 domain, // 允许访问的主机名 secure // 加密传输 (true / false)}
使用它:
cookie.setOne('myCookie', 'test')cookie.setOne(2, 'test') // TypeError: key must be a string !cookie.setOne('test') // TypeError: key can't be undefined !cookie.setOne('myCookie', 2) // TypeError: val must be a string !cookie.setOne('myCookie') // TypeError: val can't be undefined !cookie.setOne('myCookie', 'test', { expires: 1 * 1000 * 60, // 一分钟})cookie.setOne('myCookie', 'test', { expires: 1 * 1000 * 60 * 60, // 一小时 path: '/', // 也可以是: /someRoute domain: '.google.com', secure: true})
void cookie.set(obj)
传入一个对象数组,添加多个cookie, 当传入的是一个对象时,则添加一个cookie
[{key, val, options}] 或 {key, val, options}cookie.set([{ key: 'cookie_1', val: 'value_1' }, { key: 'cookie_2', val: 'value_2', options: { path: '/home' } }, { key: 'cookie_3', val: 'value_3', options: { path: '/home', expires: 1 * 1000 * 60 * 60, secure: true } }, { key: 'cookie_4', val:'value_4', options: { path: '/menu', expires: 1 * 1000 * 60 * 60 } }]) cookie.set({ key: 'cookie_1', val: 'value_1' })
string cookie.getOne(key)
通过键值名获取一个cookie, 当不传入参数时,获取所有可获取的cookie
use it !cookie.setOne('myCookie_1', 'hello')cookie.setOne('myCookie_2', 'my-cookie')cookie.getOne('myCookie_1') // 'hello'cookie.getOne('myCookie_1', true) // {'myCookie_1': 'hello'}cookie.getOne('myOtherCookie') // undefinedcookie.getOne(true) // { myCookie_1: 'hello', myCookie_2: 'my-cookie' }cookie.getOne() // ['hello', 'my-cookie']
cookie.get(keys)
从字符串数组或字符串中获取多个cookie,并选择返回结果的方式(true: JSON键值对格式数组; false: 只包含值的数组
)
cookie.set([{ key: 'cookie_1', val: 'value_1' }, { key: 'cookie_2', val: 'value_2', }])cookie.get(['cookie_1', 'cookie_2'], true) // [{'cookie_1': 'value_1'}, {'cookie_2': 'value_2'}]cookie.get() // ['value_1', 'value_2']cookie.get('cookie_1', true) // {'cookie_1', 'value_1'}cookie.get('cookie_1') // 'value_1'
void cookie.deleteOne(key, options)
通过键值名删除一个cookie
当然,在创建之前, 如果你设置了路径/域等属性你应该传入创建时的设置信息(路径/域等属性),它必须与你创建它一样!但是只需要path
和domain
就行 options = { path, domain}
use it !
cookie.setOne('myCookie_1', 'hello')cookie.setOne('myCookie_2', 'my-cookie')cookie.getOne() // ['hello', 'my-cookie']cookie.deleteOne('myCookie_1')cookie.getOne() // ['my-cookie']cookie.deleteOne()cookie.getOne() // undefined
但是当你在创建它之前设置 path / domain 属性时,删除时,你必须使用相同的路径和域来正确删除它!
cookie.setOne('myCookie_1', 'hello', { path: '/home', domain: '.google.com'})cookie.deleteOne('myCookie_1', { path: '/home', domain: '.google.com'}) // delete 'myCookie_1'cookie.deleteOne(undefined, { path: '/home', domain: '.google.com'}) // 删除所有 path: '/home' , domain: '.google.com'
void cookie.delete(keys)
从字符串数组中删除多个cookie,或从字符串删除单个cookie
cookie.set([{ key: 'cookie_1', val: 'value_1' }, { key: 'cookie_2', val: 'value_2' }, { key: 'cookie_3', val: 'value_3', options: { path: '/home' } }, { key: 'cookie_4', val:'value_4' }])cookie.delete([ 'cookie_1', 'cookie_2', { key: 'cookie_3', options: { path: '/home' } }])cookie.delete('cookie_4')cookie.delete() // 删除所有 path: '/'
Thanks !
您的star是我最大的鼓励 ^_^
也希望大佬们纠正我的错误