2020-09-14 13:33:37 +03:00
|
|
|
const url = require('url');
|
2022-08-11 17:42:21 +03:00
|
|
|
const debug = require('@tryghost/debug')('headers');
|
2018-10-05 01:50:45 +03:00
|
|
|
const INVALIDATE_ALL = '/*';
|
|
|
|
|
|
|
|
const cacheInvalidate = (result, options = {}) => {
|
|
|
|
let value = options.value;
|
|
|
|
|
|
|
|
return {
|
|
|
|
'X-Cache-Invalidate': value || INVALIDATE_ALL
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
const disposition = {
|
2019-05-06 15:24:12 +03:00
|
|
|
/**
|
|
|
|
* @description Generate CSV header.
|
|
|
|
*
|
|
|
|
* @param {Object} result - API response
|
|
|
|
* @param {Object} options
|
|
|
|
* @return {Object}
|
|
|
|
*/
|
2018-10-05 01:50:45 +03:00
|
|
|
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 {
|
2019-01-14 21:05:16 +03:00
|
|
|
'Content-Disposition': `Attachment; filename="${value}"`,
|
2018-10-05 01:50:45 +03:00
|
|
|
'Content-Type': 'text/csv'
|
|
|
|
};
|
|
|
|
},
|
|
|
|
|
2019-05-06 15:24:12 +03:00
|
|
|
/**
|
|
|
|
* @description Generate JSON header.
|
|
|
|
*
|
|
|
|
* @param {Object} result - API response
|
|
|
|
* @param {Object} options
|
|
|
|
* @return {Object}
|
|
|
|
*/
|
2018-10-05 01:50:45 +03:00
|
|
|
json(result, options = {}) {
|
|
|
|
return {
|
2019-01-14 21:05:16 +03:00
|
|
|
'Content-Disposition': `Attachment; filename="${options.value}"`,
|
2018-10-05 01:50:45 +03:00
|
|
|
'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
|
|
|
};
|
|
|
|
},
|
|
|
|
|
2019-05-06 15:24:12 +03:00
|
|
|
/**
|
|
|
|
* @description Generate YAML header.
|
|
|
|
*
|
|
|
|
* @param {Object} result - API response
|
|
|
|
* @param {Object} options
|
|
|
|
* @return {Object}
|
|
|
|
*/
|
2018-10-05 01:50:45 +03:00
|
|
|
yaml(result, options = {}) {
|
|
|
|
return {
|
2019-01-14 21:05:16 +03:00
|
|
|
'Content-Disposition': `Attachment; filename="${options.value}"`,
|
2018-10-05 01:50:45 +03:00
|
|
|
'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
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
2019-05-06 15:24:12 +03:00
|
|
|
* @description Content Disposition Header
|
|
|
|
*
|
|
|
|
* Create a header that invokes the 'Save As' dialog in the browser when exporting the database to file. The 'filename'
|
2018-12-17 14:47:19 +03:00
|
|
|
* 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 = {
|
2019-05-06 15:24:12 +03:00
|
|
|
/**
|
|
|
|
* @description Get header based on ctrl configuration.
|
|
|
|
*
|
|
|
|
* @param {Object} result - API response
|
2020-09-14 13:33:37 +03:00
|
|
|
* @param {Object} apiConfigHeaders
|
|
|
|
* @param {Object} frame
|
2019-05-06 15:24:12 +03:00
|
|
|
* @return {Promise}
|
|
|
|
*/
|
2020-09-14 13:33:37 +03:00
|
|
|
async get(result, apiConfigHeaders = {}, frame) {
|
2018-10-05 01:50:45 +03:00
|
|
|
let headers = {};
|
|
|
|
|
2020-09-14 13:33:37 +03:00
|
|
|
if (apiConfigHeaders.disposition) {
|
|
|
|
const dispositionHeader = await disposition[apiConfigHeaders.disposition.type](result, apiConfigHeaders.disposition);
|
2018-10-05 01:50:45 +03:00
|
|
|
|
2020-09-07 06:36:06 +03:00
|
|
|
if (dispositionHeader) {
|
|
|
|
Object.assign(headers, dispositionHeader);
|
|
|
|
}
|
|
|
|
}
|
2018-12-17 14:47:19 +03:00
|
|
|
|
2020-09-14 13:33:37 +03:00
|
|
|
if (apiConfigHeaders.cacheInvalidate) {
|
|
|
|
const cacheInvalidationHeader = cacheInvalidate(result, apiConfigHeaders.cacheInvalidate);
|
2018-12-17 14:47:19 +03:00
|
|
|
|
2020-09-07 06:36:06 +03:00
|
|
|
if (cacheInvalidationHeader) {
|
|
|
|
Object.assign(headers, cacheInvalidationHeader);
|
|
|
|
}
|
|
|
|
}
|
2018-10-05 01:50:45 +03:00
|
|
|
|
2022-08-12 09:46:32 +03:00
|
|
|
const locationHeaderDisabled = apiConfigHeaders?.location === false;
|
|
|
|
const hasFrameData = frame?.method === 'add' && result[frame.docName]?.[0]?.id;
|
2020-09-14 13:33:37 +03:00
|
|
|
|
|
|
|
if (!locationHeaderDisabled && hasFrameData) {
|
|
|
|
const protocol = (frame.original.url.secure === false) ? 'http://' : 'https://';
|
|
|
|
const resourceId = result[frame.docName][0].id;
|
|
|
|
|
|
|
|
let locationURL = url.resolve(`${protocol}${frame.original.url.host}`,frame.original.url.pathname);
|
|
|
|
if (!locationURL.endsWith('/')) {
|
|
|
|
locationURL += '/';
|
|
|
|
}
|
|
|
|
locationURL += `${resourceId}/`;
|
|
|
|
|
|
|
|
const locationHeader = {
|
|
|
|
Location: locationURL
|
|
|
|
};
|
|
|
|
|
|
|
|
Object.assign(headers, locationHeader);
|
|
|
|
}
|
|
|
|
|
2020-09-07 06:36:06 +03:00
|
|
|
debug(headers);
|
|
|
|
return headers;
|
2018-10-05 01:50:45 +03:00
|
|
|
}
|
|
|
|
};
|