UNPKG

8.03 kBMarkdownView Raw
1# cookie-universal-nuxt
2[![npm (scoped with tag)](https://img.shields.io/npm/v/cookie-universal-nuxt/latest.svg?style=flat-square)](https://npmjs.com/package/cookie-universal-nuxt)
3[![npm](https://img.shields.io/npm/dt/cookie-universal-nuxt.svg?style=flat-square)](https://npmjs.com/package/cookie-universal-nuxt)
4[![js-standard-style](https://img.shields.io/badge/code_style-standard-brightgreen.svg?style=flat-square)](http://standardjs.com)
5
6> Universal cookie plugin for Nuxt, perfect for SSR
7
8You can use `cookie-universal-nuxt` to set, get and remove cookies in both client and server side nuxt apps.
9`cookie-universal-nuxt` parse cookies with the popular [cookie node module](https://github.com/jshttp/cookie).
10
11## Install
12- yarn: `yarn add cookie-universal-nuxt`
13- npm: `npm i --save cookie-universal-nuxt`
14
15Add `cookie-universal-nuxt` to `nuxt.config.js`:
16
17```js
18{
19 modules: [
20 // Simple usage
21 'cookie-universal-nuxt',
22
23 // With options
24 ['cookie-universal-nuxt', { alias: 'cookiz' }],
25 ]
26}
27```
28
29## ParseJSON
30
31By default cookie-universal will try to parse to JSON, however you can disable this
32functionality in several ways:
33
34---
35
36<details><summary>Disable globally</summary><p>
37
38- Disable from the plugin options:
39
40```
41{
42 modules: [
43 ['cookie-universal-nuxt', { parseJSON: false }],
44 ]
45}
46```
47</p></details>
48
49---
50
51<details><summary>Disable globally on the fly</summary><p>
52
53```js
54// server
55app.$cookies.parseJSON = false
56
57// client
58this.$cookies.parseJSON = false
59```
60</p></details>
61
62---
63
64<details><summary>Disable on a specific get request</summary><p>
65
66```js
67// server
68app.$cookies.get('cookie-name', { parseJSON: false })
69
70// client
71this.$cookies.get('cookie-name', { parseJSON: false })
72```
73</p></details>
74
75## Api
76
77<details><summary><code>set(name, value, opts)</code></summary><p>
78
79- `name` (string): Cookie name to set.
80- `value` (string|object): Cookie value.
81- `opts` (object): Same as the [cookie node module](https://github.com/jshttp/cookie).
82 - `path` (string): Specifies the value for the Path Set-Cookie attribute. By default, the path is considered the "default path".
83 - `expires` (date): Specifies the Date object to be the value for the Expires Set-Cookie attribute.
84 - `maxAge` (number): Specifies the number (in seconds) to be the value for the Max-Age Set-Cookie attribute.
85 - `httpOnly` (boolean): Specifies the boolean value for the [HttpOnly Set-Cookie attribute][rfc-6265-5.2.6].
86 - `domain` (string): specifies the value for the Domain Set-Cookie attribute.
87 - `encode` (function): Specifies a function that will be used to encode a cookie's value.
88 - `sameSite` (boolean|string): Specifies the value for the Path Set-Cookie attribute. By default, the path is considered the "default path".
89 - `secure` (boolean): Specifies the boolean value for the Secure Set-Cookie attribute.
90
91```js
92const cookieValObject = { param1: 'value1', param2: 'value2' }
93
94// server
95app.$cookies.set('cookie-name', 'cookie-value', {
96 path: '/',
97 maxAge: 60 * 60 * 24 * 7
98})
99app.$cookies.set('cookie-name', cookieValObject, {
100 path: '/',
101 maxAge: 60 * 60 * 24 * 7
102})
103
104// client
105this.$cookies.set('cookie-name', 'cookie-value', {
106 path: '/',
107 maxAge: 60 * 60 * 24 * 7
108})
109this.$cookies.set('cookie-name', cookieValObject, {
110 path: '/',
111 maxAge: 60 * 60 * 24 * 7
112})
113```
114</p></details>
115
116---
117
118<details><summary><code>setAll(cookieArray)</code></summary><p>
119
120- cookieArray (array)
121 - `name` (string): Cookie name to set.
122 - `value` (string|object): Cookie value.
123 - `opts` (object): Same as the [cookie node module](https://github.com/jshttp/cookie)
124 - `path` (string): Specifies the value for the Path Set-Cookie attribute. By default, the path is considered the "default path".
125 - `expires` (date): Specifies the Date object to be the value for the Expires Set-Cookie attribute.
126 - `maxAge` (number): Specifies the number (in seconds) to be the value for the Max-Age Set-Cookie attribute.
127 - `httpOnly` (boolean): Specifies the boolean value for the [HttpOnly Set-Cookie attribute][rfc-6265-5.2.6].
128 - `domain` (string): specifies the value for the Domain Set-Cookie attribute.
129 - `encode` (function): Specifies a function that will be used to encode a cookie's value.
130 - `sameSite` (boolean|string): Specifies the value for the Path Set-Cookie attribute. By default, the path is considered the "default path".
131 - `secure` (boolean): Specifies the boolean value for the Secure Set-Cookie attribute.
132
133```js
134const options = {
135 path: '/',
136 maxAge: 60 * 60 * 24 * 7
137}
138const cookieList = [
139 { name: 'cookie-name1', value: 'value1', opts: options },
140 { name: 'cookie-name2', value: 'value2', opts: options },
141 { name: 'cookie-name3', value: 'value3', opts: options },
142 { name: 'cookie-name4', value: 'value4', opts: options }
143]
144
145// server
146app.$cookies.setAll(cookieList)
147
148// client
149this.$cookies.setAll(cookieList)
150```
151</p></details>
152
153---
154
155<details><summary><code>get(name, opts)</code></summary><p>
156
157- `name` (string): Cookie name to get.
158- `opts`
159 - `fromRes` (boolean): Get cookies from res instead of req.
160 - `parseJSON` (boolean): Parse json, true by default unless overridden globally or locally.
161
162```js
163// server
164const cookieRes = app.$cookies.get('cookie-name')
165const cookieRes = app.$cookies.get('cookie-name', { fromRes: true }) // get from res instead of req
166// returns the cookie value or undefined
167
168// client
169const cookieRes = this.$cookies.get('cookie-name')
170// returns the cookie value or undefined
171```
172</p></details>
173
174---
175
176<details><summary><code>getAll(opts)</code></summary><p>
177
178- `opts`
179 - `fromRes` (boolean): Get cookies from res instead of req.
180 - `parseJSON` (boolean): Parse json, true by default unless overridden globally or locally.
181
182```js
183// server
184const cookiesRes = app.$cookies.getAll()
185const cookiesRes = app.$cookies.getAll({ fromRes: true }) // get from res instead of req
186// returns all cookies or {}
187{
188 "cookie-1": "value1",
189 "cookie-2": "value2",
190}
191
192// client
193const cookiesRes = this.$cookies.getAll()
194// returns all cookies or {}
195{
196 "cookie-1": "value1",
197 "cookie-2": "value2",
198}
199```
200</p></details>
201
202---
203
204<details><summary><code>remove(name, opts)</code></summary><p>
205
206- `name` (string): Cookie name to remove.
207- `opts`
208 - `path` (object): Use it to remove the cookie from a specific location.
209
210```js
211// server
212app.$cookies.remove('cookie-name')
213app.$cookies.remove('cookie-name', {
214 // this will allow you to remove a cookie
215 // from a different path
216 path: '/my-path'
217})
218
219// client
220this.$cookies.remove('cookie-name')
221```
222</p></details>
223
224---
225
226<details><summary><code>removeAll()</code></summary><p>
227
228```js
229// note that removeAll does not currently allow you
230// to remove cookies that have a
231// path different from '/'
232
233// server
234app.$cookies.removeAll()
235
236// client
237this.$cookies.removeAll()
238```
239</p></details>
240
241---
242
243<details><summary><code>nodeCookie</code></summary><p>
244
245This property will expose the [cookie node module](https://github.com/jshttp/cookie) so you don't have to include it yourself.
246
247```js
248
249// server
250const cookieRes = app.$cookies.nodeCookie.parse('cookie-name', 'cookie-value')
251cookieRes['cookie-name'] // returns 'cookie-value'
252
253// client
254const cookieRes = this.$cookies.nodeCookie.parse('cookie-name', 'cookie-value')
255cookieRes['cookie-name'] // returns 'cookie-value'
256```
257</p></details>
258
259---
260
261<details><summary>Plugin options</summary><p>
262
263- `alias` (string): Specifies the plugin alias to use.
264- `parseJSON` (boolean): Disable JSON parsing.
265
266```js
267{
268 modules: [
269 ['cookie-universal-nuxt', { alias: 'cookiz', parseJSON: false }],
270 ]
271}
272
273
274// usage
275this.$cookiz.set('cookie-name', 'cookie-value')
276```
277</p></details>
278
279## License
280
281[MIT License](./LICENSE)
282
283Copyright (c) Salvatore Tedde <microcipcip@gmail.com>