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 = {}) {
|
|
|
|
return {
|
|
|
|
'Content-Disposition': options.value,
|
|
|
|
'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
|
|
|
};
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
get(result, apiConfig = {}) {
|
|
|
|
let headers = {};
|
|
|
|
|
|
|
|
if (apiConfig.disposition) {
|
|
|
|
Object.assign(headers, disposition[apiConfig.disposition.type](result, apiConfig.disposition));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (apiConfig.cacheInvalidate) {
|
|
|
|
Object.assign(headers, cacheInvalidate(result, apiConfig.cacheInvalidate));
|
|
|
|
}
|
|
|
|
|
|
|
|
debug(headers);
|
|
|
|
return headers;
|
|
|
|
}
|
|
|
|
};
|