Skip to content

Commit

Permalink
core: improved form data handling and better reporting of internal er…
Browse files Browse the repository at this point in the history
…rors during rest operations.
  • Loading branch information
maxott committed Oct 8, 2020
1 parent 3acc348 commit 0a98eef
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 8 deletions.
5 changes: 4 additions & 1 deletion packages/core/src/rest_client/fetch-api.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,10 @@ export function fetchApi(apiUrl, request, silent) {
const tmpRequest = request;
let contentType = null;
if (tmpRequest && tmpRequest.body && typeof (tmpRequest.body) !== 'string') {
if (method === 'GET') {
// eslint-disable-next-line no-undef
if (tmpRequest.body instanceof FormData) {
contentType = undefined; // 'multipart/form-data; boundary=`';
} else if (method === 'GET') {
tmpRequest.body = JSON.stringify(tmpRequest.body);
contentType = 'application/json';
} else {
Expand Down
4 changes: 2 additions & 2 deletions packages/core/src/rest_client/get.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ export function buildURL(parts, vars) {
if (!v) {
throw Error(`Missing assignment to url parameter '${k}'`);
}
id.forEach((i) => { a2[i] = encodeURI(v); });
id.forEach((i) => { a2[i] = encodeURIComponent(v); });
});
return a2;
}
Expand All @@ -122,7 +122,7 @@ export function buildURL(parts, vars) {
}
const qe = Object.entries(qph);
if (qe.length > 0) {
const query = qe.map(([k, v]) => `${encodeURI(k)}=${encodeURI(v)}`).join('&');
const query = qe.map(([k, v]) => `${encodeURIComponent(k)}=${encodeURIComponent(v)}`).join('&');
return `${path}?${query}`;
} else {
return path;
Expand Down
27 changes: 25 additions & 2 deletions packages/core/src/rest_client/postPut.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,17 +49,40 @@ const registerMethod = (method, opts) => {
const submitType = `${ACTION_TYPES[`${method}_SUBMITTED`]}:${name}`;
const resultType = `${ACTION_TYPES[`${method}_RESULT`]}:${name}`;
const errorType = `${ACTION_TYPES[`${method}_ERROR`]}:${name}`;
const intErrorType = `${ACTION_TYPES[`${method}_INTERNAL_ERROR`]}:${name}`;

registerReducer(trigger, (state, action) => {
if (guard) {
if (!guard(action, state)) {
return state;
}
}
const [body, vars] = request(action, state, variables);
let r;
try {
r = request(action, state, variables);
} catch (e) {
dispatchFromReducer({
type: intErrorType,
call: 'request',
// eslint-disable-next-line object-property-newline
action, state, variables,
error: e,
});
}
const [body, vars] = r;
if (body) {
const url2 = buildURL(parts, vars, variables);
runMethod(method, url2, name, body, vars, resultType, errorType, action);
try {
runMethod(method, url2, name, body, vars, resultType, errorType, action);
} catch (e) {
dispatchFromReducer({
type: intErrorType,
call: 'runMethod',
// eslint-disable-next-line object-property-newline
method, url2, name, body, vars, resultType, errorType, action,
error: e,
});
}
// eslint-disable-next-line object-curly-newline
dispatchFromReducer({
type: submitType,
Expand Down
6 changes: 3 additions & 3 deletions packages/core/src/rest_client/rest.actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ const Domain = 'REST';
export const ACTION_TYPES = registerActions(Domain,
[
'THROW_UNAUTHORISED_ERROR', 'THROW_PERMISSION_DENIED_ERROR',
'GET_SUBMITTED', 'GET_RESULT', 'GET_ERROR',
'GET_SUBMITTED', 'GET_RESULT', 'GET_ERROR', 'GET_INTERNAL_ERROR',
'GET_PERIODIC_TICK',
'POST_SUBMITTED', 'POST_RESULT', 'POST_ERROR',
'PUT_SUBMITTED', 'PUT_RESULT', 'PUT_ERROR',
'POST_SUBMITTED', 'POST_RESULT', 'POST_ERROR', 'POST_INTERNAL_ERROR',
'PUT_SUBMITTED', 'PUT_RESULT', 'PUT_ERROR', 'PUT_INTERNAL_ERROR',
]);

export function throwUnauthorisedError() {
Expand Down

0 comments on commit 0a98eef

Please sign in to comment.