2018-10-05 01:50:45 +03:00
|
|
|
const debug = require('ghost-ignition').debug('api:shared:headers');
|
|
|
|
const INVALIDATE_ALL = '/*';
|
|
|
|
|
|
|
|
const cacheInvalidate = (result, options = {}) => {
|
|
|
|
let value = options.value;
|
|
|
|
|
|
|
|
return {
|
|
|
|
'X-Cache-Invalidate': value || INVALIDATE_ALL
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
const disposition = {
|
|
|
|
csv(result, options = {}) {
|
2018-10-27 19:39:39 +03:00
|
|
|
let value = options.value;
|
|
|
|
|
|
|
|
if (typeof options.value === 'function') {
|
|
|
|
value = options.value();
|
|
|
|
}
|
|
|
|
|
2018-10-05 01:50:45 +03:00
|
|
|
return {
|
2018-10-27 19:39:39 +03:00
|
|
|
'Content-Disposition': value,
|
2018-10-05 01:50:45 +03:00
|
|
|
'Content-Type': 'text/csv'
|
|
|
|
};
|
|
|
|
},
|
|
|
|
|
|
|
|
json(result, options = {}) {
|
|
|
|
return {
|
|
|
|
'Content-Disposition': options.value,
|
|
|
|
'Content-Type': 'application/json',
|
2018-10-25 05:18:36 +03:00
|
|
|
'Content-Length': Buffer.byteLength(JSON.stringify(result))
|
2018-10-05 01:50:45 +03:00
|
|
|
};
|
|
|
|
},
|
|
|
|
|
|
|
|
yaml(result, options = {}) {
|
|
|
|
return {
|
|
|
|
'Content-Disposition': options.value,
|
|
|
|
'Content-Type': 'application/yaml',
|
2018-10-25 05:18:36 +03:00
|
|
|
'Content-Length': Buffer.byteLength(JSON.stringify(result))
|
2018-10-05 01:50:45 +03:00
|
|
|
};
|
2018-12-17 14:47:19 +03:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* ### Content Disposition Header
|
|
|
|
* create a header that invokes the 'Save As' dialog in the browser when exporting the database to file. The 'filename'
|
|
|
|
* parameter is governed by [RFC6266](http://tools.ietf.org/html/rfc6266#section-4.3).
|
|
|
|
*
|
|
|
|
* For encoding whitespace and non-ISO-8859-1 characters, you MUST use the "filename*=" attribute, NOT "filename=".
|
|
|
|
* Ideally, both. Examples: http://tools.ietf.org/html/rfc6266#section-5
|
|
|
|
*
|
|
|
|
* We'll use ISO-8859-1 characters here to keep it simple.
|
|
|
|
*
|
|
|
|
* @see http://tools.ietf.org/html/rfc598
|
|
|
|
*/
|
|
|
|
file(result, options = {}) {
|
|
|
|
return Promise.resolve()
|
|
|
|
.then(() => {
|
|
|
|
let value = options.value;
|
|
|
|
|
|
|
|
if (typeof options.value === 'function') {
|
|
|
|
value = options.value();
|
|
|
|
}
|
|
|
|
|
|
|
|
return value;
|
|
|
|
})
|
|
|
|
.then((filename) => {
|
|
|
|
return {
|
|
|
|
'Content-Disposition': `Attachment; filename="${filename}"`
|
|
|
|
};
|
|
|
|
});
|
2018-10-05 01:50:45 +03:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
get(result, apiConfig = {}) {
|
|
|
|
let headers = {};
|
|
|
|
|
2018-12-17 14:47:19 +03:00
|
|
|
return Promise.resolve()
|
|
|
|
.then(() => {
|
|
|
|
let header;
|
2018-10-05 01:50:45 +03:00
|
|
|
|
2018-12-17 14:47:19 +03:00
|
|
|
if (apiConfig.disposition) {
|
|
|
|
header = disposition[apiConfig.disposition.type](result, apiConfig.disposition);
|
|
|
|
}
|
|
|
|
|
|
|
|
return header;
|
|
|
|
})
|
|
|
|
.then((header) => {
|
|
|
|
if (header) {
|
|
|
|
Object.assign(headers, header);
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.then(() => {
|
|
|
|
let header;
|
|
|
|
|
|
|
|
if (apiConfig.cacheInvalidate) {
|
|
|
|
header = cacheInvalidate(result, apiConfig.cacheInvalidate);
|
|
|
|
}
|
2018-10-05 01:50:45 +03:00
|
|
|
|
2018-12-17 14:47:19 +03:00
|
|
|
return header;
|
|
|
|
})
|
|
|
|
.then((header) => {
|
|
|
|
if (header) {
|
|
|
|
Object.assign(headers, header);
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.then(() => {
|
|
|
|
debug(headers);
|
|
|
|
return headers;
|
|
|
|
});
|
2018-10-05 01:50:45 +03:00
|
|
|
}
|
|
|
|
};
|