Skip to content

Commit

Permalink
Merge pull request #1225 from lowdefy/request-fix
Browse files Browse the repository at this point in the history
feat(engine): Set request to null and update before calling request.
  • Loading branch information
SamTolmay committed Jun 8, 2022
2 parents d570f4c + 6e06ce1 commit d091a6c
Show file tree
Hide file tree
Showing 11 changed files with 511 additions and 205 deletions.
7 changes: 5 additions & 2 deletions packages/api/src/routes/request/callRequest.js
Expand Up @@ -27,9 +27,12 @@ import getRequestConfig from './getRequestConfig.js';
import getRequestResolver from './getRequestResolver.js';
import validateSchemas from './validateSchemas.js';

async function callRequest(context, { pageId, payload, requestId }) {
async function callRequest(context, { blockId, pageId, payload, requestId }) {
const { logger } = context;
logger.debug({ route: 'request', params: { pageId, payload, requestId } }, 'Started request');
logger.debug(
{ route: 'request', params: { blockId, pageId, payload, requestId } },
'Started request'
);
const requestConfig = await getRequestConfig(context, { pageId, requestId });
const connectionConfig = await getConnectionConfig(context, { requestConfig });
authorizeRequest(context, { requestConfig });
Expand Down
71 changes: 37 additions & 34 deletions packages/engine/src/Requests.js
Expand Up @@ -30,11 +30,11 @@ class Requests {
});
}

callRequests({ actions, arrayIndices, event, params } = {}) {
callRequests({ actions, arrayIndices, blockId, event, params } = {}) {
if (type.isObject(params) && params.all === true) {
return Promise.all(
Object.keys(this.requestConfig).map((requestId) =>
this.callRequest({ requestId, event, arrayIndices })
this.callRequest({ arrayIndices, blockId, event, requestId })
)
);
}
Expand All @@ -43,68 +43,71 @@ class Requests {
if (type.isString(params)) requestIds = [params];
if (type.isArray(params)) requestIds = params;

return Promise.all(
requestIds.map((requestId) => this.callRequest({ actions, requestId, event, arrayIndices }))
const requests = requestIds.map((requestId) =>
this.callRequest({ actions, requestId, blockId, event, arrayIndices })
);
this.context._internal.update(); // update to render request reset
return Promise.all(requests);
}

callRequest({ actions, arrayIndices, event, requestId }) {
const request = this.requestConfig[requestId];
if (!request) {
async callRequest({ actions, arrayIndices, blockId, event, requestId }) {
const requestConfig = this.requestConfig[requestId];
if (!this.context.requests[requestId]) {
this.context.requests[requestId] = [];
}
if (!requestConfig) {
const error = new Error(`Configuration Error: Request ${requestId} not defined on page.`);
this.context.requests[requestId] = {
this.context.requests[requestId].unshift({
blockId: 'block_id',
error,
loading: false,
requestId,
response: null,
error: [error],
};
return Promise.reject(error);
}

if (!this.context.requests[requestId]) {
this.context.requests[requestId] = {
loading: true,
response: null,
error: [],
};
});
throw error;
}

const { output: payload, errors: parserErrors } = this.context._internal.parser.parse({
actions,
event,
arrayIndices,
input: request.payload,
input: requestConfig.payload,
location: requestId,
});

// TODO: We are throwing this error differently to the request does not exist error
if (parserErrors.length > 0) {
throw parserErrors[0];
}

return this.fetch({ requestId, payload });
const request = {
blockId,
loading: true,
payload,
requestId,
response: null,
};
this.context.requests[requestId].unshift(request);
return this.fetch(request);
}

async fetch({ requestId, payload }) {
this.context.requests[requestId].loading = true;

async fetch(request) {
request.loading = true;
try {
const response = await this.context._internal.lowdefy._internal.callRequest({
blockId: request.blockId,
pageId: this.context.pageId,
payload: serializer.serialize(payload),
requestId,
payload: serializer.serialize(request.payload),
requestId: request.requestId,
});
const deserializedResponse = serializer.deserialize(
get(response, 'response', {
default: null,
})
);
this.context.requests[requestId].response = deserializedResponse;
this.context.requests[requestId].loading = false;
request.response = deserializedResponse;
request.loading = false;
this.context._internal.update();
return deserializedResponse;
} catch (error) {
this.context.requests[requestId].error.unshift(error);
this.context.requests[requestId].loading = false;
request.error = error;
request.loading = false;
this.context._internal.update();
throw error;
}
Expand Down
56 changes: 36 additions & 20 deletions packages/engine/src/actions/createGetRequestDetails.test.js
Expand Up @@ -116,11 +116,15 @@ test('getRequestDetails params is true', async () => {
},
b: {
response: {
req_one: {
error: [],
loading: false,
response: 1,
},
req_one: [
{
blockId: 'button',
loading: false,
payload: {},
requestId: 'req_one',
response: 1,
},
],
},
index: 1,
type: 'Action',
Expand Down Expand Up @@ -177,11 +181,15 @@ test('getRequestDetails params is req_one', async () => {
type: 'Request',
},
b: {
response: {
error: [],
loading: false,
response: 1,
},
response: [
{
blockId: 'button',
loading: false,
payload: {},
requestId: 'req_one',
response: 1,
},
],
index: 1,
type: 'Action',
},
Expand Down Expand Up @@ -366,11 +374,15 @@ test('getRequestDetails params.all is true', async () => {
},
b: {
response: {
req_one: {
error: [],
loading: false,
response: 1,
},
req_one: [
{
blockId: 'button',
loading: false,
payload: {},
requestId: 'req_one',
response: 1,
},
],
},
index: 1,
type: 'Action',
Expand Down Expand Up @@ -505,11 +517,15 @@ test('getRequestDetails params.key is req_one', async () => {
type: 'Request',
},
b: {
response: {
error: [],
loading: false,
response: 1,
},
response: [
{
blockId: 'button',
loading: false,
payload: {},
requestId: 'req_one',
response: 1,
},
],
index: 1,
type: 'Action',
},
Expand Down
10 changes: 8 additions & 2 deletions packages/engine/src/actions/createRequest.js
Expand Up @@ -14,9 +14,15 @@
limitations under the License.
*/

function createRequest({ actions, arrayIndices, context, event }) {
function createRequest({ actions, arrayIndices, blockId, context, event }) {
return async function request(params) {
return await context._internal.Requests.callRequests({ actions, arrayIndices, event, params });
return await context._internal.Requests.callRequests({
actions,
arrayIndices,
blockId,
event,
params,
});
};
}

Expand Down

0 comments on commit d091a6c

Please sign in to comment.