Refactored the pipeline execution to async fn

Having the code use `async/await` make it more readable, and extracting the
execution to a separate function make its easier to run in the background in the
future
This commit is contained in:
Fabien "egg" O'Carroll 2024-01-17 11:28:45 +07:00 committed by Fabien 'egg' O'Carroll
parent c60dd779c9
commit ef999c4fd4

View File

@ -245,28 +245,22 @@ const pipeline = (apiController, apiUtils, apiType) => {
}
}
return Promise.resolve()
.then(() => {
return STAGES.validation.input(apiUtils, apiConfig, apiImpl, frame);
})
.then(() => {
return STAGES.serialisation.input(apiUtils, apiConfig, apiImpl, frame);
})
.then(() => {
return STAGES.permissions(apiUtils, apiConfig, apiImpl, frame);
})
.then(() => {
return STAGES.query(apiUtils, apiConfig, apiImpl, frame);
})
.then((response) => {
return STAGES.serialisation.output(response, apiUtils, apiConfig, apiImpl, frame);
})
.then(async () => {
if (apiImpl.cache) {
await apiImpl.cache.set(cacheKey, frame.response);
}
async function getResponse() {
await STAGES.validation.input(apiUtils, apiConfig, apiImpl, frame);
await STAGES.serialisation.input(apiUtils, apiConfig, apiImpl, frame);
await STAGES.permissions(apiUtils, apiConfig, apiImpl, frame);
const response = await STAGES.query(apiUtils, apiConfig, apiImpl, frame);
await STAGES.serialisation.output(response, apiUtils, apiConfig, apiImpl, frame);
return frame.response;
});
}
const response = await getResponse();
if (apiImpl.cache) {
await apiImpl.cache.set(cacheKey, response);
}
return response;
};
Object.assign(obj[method], apiImpl);