* get or set cookie * @param {String} name * @param {String} value * @param {Object} options
(name, value, options = {})
| 257 | * @param {Object} options |
| 258 | */ |
| 259 | cookie(name, value, options = {}) { |
| 260 | assert(name && helper.isString(name), 'cookie.name must be a string'); |
| 261 | options = Object.assign({}, this.config('cookie'), options); |
| 262 | const instance = new Cookies(this.req, this.res, { |
| 263 | keys: options.keys, |
| 264 | secure: this.request.secure |
| 265 | }); |
| 266 | |
| 267 | if (!this[COOKIE_STORE]) this[COOKIE_STORE] = {}; |
| 268 | |
| 269 | // get cookie |
| 270 | if (value === undefined) { |
| 271 | if (this[COOKIE_STORE][name] !== undefined) return this[COOKIE_STORE][name]; |
| 272 | return instance.get(name, options); |
| 273 | } |
| 274 | // remove cookie |
| 275 | if (value === null) { |
| 276 | delete this[COOKIE_STORE][name]; |
| 277 | // If the value is omitted, an outbound header with an expired date is used to delete the cookie. |
| 278 | // https://github.com/pillarjs/cookies#cookiesset-name--value---options-- |
| 279 | return instance.set(name, undefined, options); |
| 280 | } |
| 281 | assert(helper.isString(value), 'cookie value must be a string'); |
| 282 | // http://browsercookielimits.squawky.net/ |
| 283 | if (value.length >= 4094) { |
| 284 | this.app.emit('cookieLimit', {name, value, ctx: this}); |
| 285 | } |
| 286 | this[COOKIE_STORE][name] = value; |
| 287 | // set cookie |
| 288 | return instance.set(name, value, options); |
| 289 | }, |
| 290 | /** |
| 291 | * get service |
| 292 | * @param {String} name |