This repository has been archived by the owner on Aug 22, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cookie.js
222 lines (193 loc) · 5.81 KB
/
cookie.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
/* @flow */
const reduce = require('lodash/reduce')
const kebabCase = require('lodash/kebabCase')
const get = require('lodash/get')
const isDate = require('lodash/isDate')
const pick = require('lodash/pick')
/**
* The `req` object represents the HTTP request and has properties for the
* request query string, parameters, body, HTTP headers, and so on.
*
* @external Request
* @see {@link http://expressjs.com/en/4x/api.html#req|Request}
*/
/**
* The `res` object represents the HTTP response that an Express app sends when
* it gets an HTTP request.
*
* @external Response
* @see {@link http://expressjs.com/en/4x/api.html#res|Response}
*/
/**
* {@link cookie} configuration options
* @typedef {Object} CookieOptions
* @param {Request} [req]
* @param {Response} [res]
* @param {string} [domain=req.hostname || window.location.host] - see {@link http://expressjs.com/en/4x/api.html#res.cookie|res.cookie}
* @param {Function} [encode] - see {@link http://expressjs.com/en/4x/api.html#res.cookie|res.cookie}
* @param {Date} [expires] - see {@link http://expressjs.com/en/4x/api.html#res.cookie|res.cookie}
* @param {boolean} [httpOnly] - see {@link http://expressjs.com/en/4x/api.html#res.cookie|res.cookie}
* @param {number} [maxAge] - see {@link http://expressjs.com/en/4x/api.html#res.cookie|res.cookie}
* @param {string} [path='/'] - see {@link http://expressjs.com/en/4x/api.html#res.cookie|res.cookie}
* @param {boolean} [secure] - see {@link http://expressjs.com/en/4x/api.html#res.cookie|res.cookie}
* @param {boolean} [signed] - see {@link http://expressjs.com/en/4x/api.html#res.cookie|res.cookie}
* @param {boolean | string} [sameSite] - see {@link http://expressjs.com/en/4x/api.html#res.cookie|res.cookie}
* @example
* import type { CookieOptions } from 'secondwheel/cookie'
*/
/** @namespace cookie */
/*::
import type { CookieOptions as Options, $Response, $Request } from 'express'
export type CookieOptions = {
req?: $Request;
res?: $Response;
...$Exact<Options>;
}
*/
const expires = new Date(0)
const allowed = [
'domain',
'encode',
'expires',
'httpOnly',
'maxAge',
'path',
'req',
'res',
'secure',
'signed'
]
const getConfig = (options/*: ?CookieOptions */)/*: CookieOptions */ => {
const { req, res, ...rest } = pick(options, allowed)
return {
path: '/',
domain: `.${get(req, 'hostname') || window.location.host}`,
...rest
}
}
const serialize = (options/*: ?CookieOptions */)/*: string */ => reduce(
options,
(result, value, key) =>
`${result}${kebabCase(String(key))}=${isDate(value) ? value.toUTCString() : value};`,
''
)
function set (
name/*: string */,
value/*: string */,
options/*: ?CookieOptions */
) {
const res = get(options, 'res')
const config = getConfig(options)
if (res) {
res.cookie(name, value, config)
} else {
document.cookie = `${name}=${value};${serialize(config)}`
}
}
/**
* sets a cookie
*
* @memberof cookie
* @example
* import { setCookie } from 'secondwheel/cookie'
*
* setCookie('cookie-name', 'cookie-value') // browser
* setCookie('cookie-name', 'cookie-value', { req, res }) // server
*/
const setCookie = (
name/*: string */,
value/*: string */,
options/*: ?CookieOptions */
) => set(name, value, { maxAge: 10 ** 10, ...options })
exports.setCookie = setCookie
/**
* sets a session (temporary) cookie
*
* @memberof cookie
* @example
* import { setSessionCookie } from 'secondwheel/cookie'
*
* setSessionCookie('cookie-name', 'cookie-value') // browser
* setSessionCookie('cookie-name', 'cookie-value', { req, res }) // server
*/
const setSessionCookie = (
name/*: string */,
value/*: string */,
options/*: ?CookieOptions */
) => set(name, value, options)
exports.setSessionCookie = setSessionCookie
/**
* retrieves a cookie by name
*
* @memberof cookie
* @example
* import { getCookie } from 'secondwheel/cookie'
*
* const cookieValue = getCookie('cookie-name') // browser
* const cookieValue = getCookie('cookie-name', { req, res }) // server
*/
const getCookie = (
name/*: string */,
options/*: ?CookieOptions */
)/*: string */ => {
const req = get(options, 'req')
let match
return req
? (req.cookies[name] || req.signedCookies[name])
: (
(match = document.cookie.match('(?:^|;) ?' + name + '=([^;]*)(?:;|$)')
) && match[1]) || ''
}
exports.getCookie = getCookie
/**
* removes a cookie
*
* @memberof cookie
* @example
* import { removeCookie } from 'secondwheel/cookie'
*
* removeCookie('cookie-name') // browser
* removeCookie('cookie-name', { req, res }) // server
*/
const removeCookie = (name/*: string */, options/*: ?CookieOptions */) => {
const res = get(options, 'res')
const config = getConfig({ expires, ...options })
if (res) {
res.clearCookie(name, config)
} else {
document.cookie = `${name}=;${serialize(config)}`
}
}
exports.removeCookie = removeCookie
/**
* sets the "return to" value (current URL) into a session cookie
*
* @memberof cookie
* @example
* import { setReturnTo } from 'secondwheel/cookie'
*
* setReturnTo() // browser
* setReturnTo({ req, res }) // server
*/
const setReturnTo = (options/*: ?CookieOptions */) => exports.setSessionCookie(
'return-to',
get(options, 'req.originalUrl') || window.location.href,
options
)
exports.setReturnTo = setReturnTo
/**
* returns and removes the "return to" value
*
* @memberof cookie
* @example
* import { getReturnTo } from 'secondwheel/cookie'
*
* const cookieValue = getReturnTo() // browser
* const cookieValue = getReturnTo({ req, res }) // server
*/
const getReturnTo = (options/*: ?CookieOptions */)/*: string */ => {
const value = getCookie('return-to', options)
removeCookie('return-to', options)
return value && decodeURIComponent(value)
}
exports.getReturnTo = getReturnTo