@adonisjs/framework
Advanced tools
Comparing version 4.0.3 to 4.0.4
@@ -1,3 +0,3 @@ | ||
<a name="4.0.3"></a> | ||
## [4.0.3](https://github.com/adonisjs/adonis-framework/compare/v4.0.2...v4.0.3) (2017-07-17) | ||
<a name="4.0.4"></a> | ||
## [4.0.4](https://github.com/adonisjs/adonis-framework/compare/v4.0.3...v4.0.4) (2017-07-18) | ||
@@ -7,7 +7,12 @@ | ||
* **response:** add support for clear existing cookie ([d0174f4](https://github.com/adonisjs/adonis-framework/commit/d0174f4)) | ||
* **response:** add support to disable implicit end ([998061d](https://github.com/adonisjs/adonis-framework/commit/998061d)) | ||
* **response:** add support to set cookies ([d7377f0](https://github.com/ | ||
* **response:** add support for clear existing cookie ([14de180](https://github.com/adonisjs/adonis-framework/commit/14de180)) | ||
* **response:** add support to disable implicit end ([7c117e4](https://github.com/adonisjs/adonis-framework/commit/7c117e4)) | ||
* **response:** add support to set cookies ([650b071](https://github.com/adonisjs/adonis-framework/commit/650b071)) | ||
<a name="4.0.3"></a> | ||
## [4.0.3](https://github.com/adonisjs/adonis-framework/compare/v4.0.2...v4.0.3) (2017-07-17) | ||
<a name="4.0.2"></a> | ||
@@ -14,0 +19,0 @@ ## [4.0.2](https://github.com/adonisjs/adonis-framework/compare/v4.0.1...v4.0.2) (2017-06-23) |
{ | ||
"name": "@adonisjs/framework", | ||
"version": "4.0.3", | ||
"version": "4.0.4", | ||
"description": "Adonis framework makes it easy for you to write webapps with less code", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -290,3 +290,3 @@ 'use strict' | ||
Context.getter('response', function () { | ||
return new Response(this.req, this.res) | ||
return new Response(this.req, this.res, use('Adonis/Src/Config')) | ||
}, true) | ||
@@ -293,0 +293,0 @@ } |
@@ -14,4 +14,7 @@ 'use strict' | ||
const nodeReq = require('node-req') | ||
const nodeCookie = require('node-cookie') | ||
const Macroable = require('macroable') | ||
const SECRET = 'app.secret' | ||
/** | ||
@@ -33,3 +36,3 @@ * @module Adonis | ||
class Response extends Macroable { | ||
constructor (request, response) { | ||
constructor (request, response, Config) { | ||
super() | ||
@@ -71,12 +74,11 @@ /** | ||
/** | ||
* Flag to know whether a file was sent as the response. In this | ||
* case the response will be closed immediately once stream is | ||
* finished | ||
* Implicitly end the response. If you set it | ||
* to false, calling `response.end` will | ||
* end the response. | ||
* | ||
* @attribute _sentFile | ||
* | ||
* @type {Boolean} | ||
* @private | ||
*/ | ||
this._sentFile = false | ||
this.implicitEnd = true | ||
this.Config = Config | ||
} | ||
@@ -218,3 +220,3 @@ | ||
download (filePath, options = {}) { | ||
this._sentFile = true | ||
this.implicitEnd = false | ||
nodeRes.download(this.request, this.response, filePath, options) | ||
@@ -237,3 +239,3 @@ } | ||
attachment (filePath, name, disposition, options = {}) { | ||
this._sentFile = true | ||
this.implicitEnd = false | ||
nodeRes.attachment(this.request, this.response, filePath, name, disposition, options) | ||
@@ -311,2 +313,7 @@ } | ||
send (body) { | ||
if (!this.implicitEnd) { | ||
nodeRes.send(this.request, this.response, body) | ||
return | ||
} | ||
this._lazyBody = { | ||
@@ -330,2 +337,7 @@ method: 'send', | ||
json (body) { | ||
if (!this.implicitEnd) { | ||
nodeRes.json(this.request, this.response, body) | ||
return | ||
} | ||
this._lazyBody = { | ||
@@ -351,2 +363,8 @@ method: 'json', | ||
callbackFn = callbackFn || nodeReq.get(this.request).callback || 'callback' | ||
if (!this.implicitEnd) { | ||
nodeRes.jsonp(this.request, this.response, body, callbackFn) | ||
return | ||
} | ||
this._lazyBody = { | ||
@@ -368,3 +386,3 @@ method: 'jsonp', | ||
end () { | ||
if (!this._sentFile) { | ||
if (this.implicitEnd) { | ||
const method = this._lazyBody.method || 'send' | ||
@@ -375,2 +393,45 @@ const args = [this.request, this.response, this._lazyBody.content].concat(this._lazyBody.args) | ||
} | ||
/** | ||
* Send cookie with the http response | ||
* | ||
* @method cookie | ||
* | ||
* @param {String} key | ||
* @param {Mixed} value | ||
* @param {Object} [options = {}] | ||
* | ||
* @return {void} | ||
*/ | ||
cookie (key, value, options = {}) { | ||
nodeCookie.create(this.response, key, value, options, this.Config.get(SECRET), true) | ||
} | ||
/** | ||
* Set plain cookie HTTP response | ||
* | ||
* @method plainCookie | ||
* | ||
* @param {String} key | ||
* @param {Mixed} value | ||
* @param {Object} [options = {}] | ||
* | ||
* @return {void} | ||
*/ | ||
plainCookie (key, value, options) { | ||
nodeCookie.create(this.response, key, value, options) | ||
} | ||
/** | ||
* Remove existing cookie using it's key | ||
* | ||
* @method clearCookie | ||
* | ||
* @param {String} key | ||
* | ||
* @return {void} | ||
*/ | ||
clearCookie (key) { | ||
nodeCookie.clear(this.response, key) | ||
} | ||
} | ||
@@ -392,2 +453,6 @@ | ||
Response.prototype[method] = function (content) { | ||
if (!this.implicitEnd) { | ||
nodeRes[method](this.request, this.response, content) | ||
return | ||
} | ||
this._lazyBody = { method, content, args: [] } | ||
@@ -394,0 +459,0 @@ } |
@@ -234,3 +234,3 @@ 'use strict' | ||
_endResponse (response) { | ||
if (response.isPending) { | ||
if (response.isPending && response.implicitEnd) { | ||
response.end() | ||
@@ -237,0 +237,0 @@ } |
156671
5093