UNPKG

@plone/volto

Version:
45 lines (39 loc) 1.44 kB
/** * Sitemap helper. * @module helpers/Sitemap */ import superagent from 'superagent'; import config from '@plone/volto/registry'; import { addHeadersFactory } from '@plone/volto/helpers/Proxy/Proxy'; import { stripSubpathPrefix } from '@plone/volto/helpers/Url/Url'; /** * Get a resource image/file with authenticated (if token exist) API headers * @function getAPIResourceWithAuth * @param {Object} req Request object * @return {string} The response with the image */ export const getAPIResourceWithAuth = (req) => new Promise((resolve, reject) => { const { settings } = config; const apiSuffix = settings.legacyTraverse ? '' : '/++api++'; let apiPath = ''; if (settings.internalApiPath && __SERVER__) { apiPath = settings.internalApiPath; } else if (__DEVELOPMENT__ && settings.devProxyToApiPath) { apiPath = settings.devProxyToApiPath; } else { apiPath = settings.apiPath; } //strip subpath if any const contentPath = stripSubpathPrefix(req.path); const request = superagent .get(`${apiPath}${__DEVELOPMENT__ ? '' : apiSuffix}${contentPath}`) .maxResponseSize(settings.maxResponseSize) .responseType('blob'); const authToken = req.universalCookies.get('auth_token'); if (authToken) { request.set('Authorization', `Bearer ${authToken}`); } request.use(addHeadersFactory(req)); request.then(resolve).catch(reject); });