Skip to content

Commit

Permalink
Fixed undefined body in Api
Browse files Browse the repository at this point in the history
  • Loading branch information
razvanMiu committed Sep 2, 2020
1 parent 6e0a1c4 commit c725bc4
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 3 deletions.
1 change: 1 addition & 0 deletions src/customizations/volto/components/theme/View/View.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,7 @@ class View extends Component {
] || null;

getRenderedView = () =>
this.props.content?.['@id'] &&
getBasePath(this.props.content?.['@id']) === this.props.pathname
? this.getViewByType() || this.getViewByLayout() || this.getViewDefault()
: null;
Expand Down
83 changes: 83 additions & 0 deletions src/customizations/volto/helpers/Api/Api.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
/**
* Api helper.
* @module helpers/Api
*/

import superagent from 'superagent';
import cookie from 'react-cookie';

import { settings } from '~/config';

const methods = ['get', 'post', 'put', 'patch', 'del'];

/**
* Format the url.
* @function formatUrl
* @param {string} path Path (or URL) to be formatted.
* @returns {string} Formatted path.
*/
function formatUrl(path) {
if (path.startsWith('http://') || path.startsWith('https://')) return path;

const adjustedPath = path[0] !== '/' ? `/${path}` : path;
let apiPath = '';
if (settings.internalApiPath && __SERVER__) {
apiPath = settings.internalApiPath;
} else {
apiPath = settings.apiPath;
}
return `${apiPath}${adjustedPath}`;
}

/**
* Api class.
* @class Api
*/
class Api {
/**
* Constructor
* @method constructor
* @constructs Api
*/
constructor() {
methods.forEach((method) => {
this[method] = (path, { params, data, type, headers = {} } = {}) => {
let request;
let promise = new Promise((resolve, reject) => {
request = superagent[method](formatUrl(path));

if (params) {
request.query(params);
}

const authToken = cookie.load('auth_token');
if (authToken) {
request.set('Authorization', `Bearer ${authToken}`);
}

request.set('Accept', 'application/json');

if (type) {
request.type(type);
}

Object.keys(headers).forEach((key) => request.set(key, headers[key]));

if (data) {
request.send(data);
}

request.end((err, resp) => {
const body = resp?.body;
const text = resp?.text;
return err ? reject(err) : resolve(body || text);
});
});
promise.request = request;
return promise;
};
});
}
}

export default Api;
6 changes: 3 additions & 3 deletions src/helpers/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@ export function getBasePath(url) {
const parseUrl = url === '' ? '/' : url;
if (parseUrl) {
return getBaseUrl(url)
.replace(settings.apiPath, '')
.replace(settings.internalApiPath, '');
.replace(settings.apiPath, '')
.replace(settings.internalApiPath, '');
}
return '/'
return '';
}

export const objectHasData = (obj) => {
Expand Down

0 comments on commit c725bc4

Please sign in to comment.