Skip to content

Commit 200e7da

Browse files
committed
feat(modules): Supporting ?skip_ids param on Modules API POST requests
Filtering apps by ID to prevent unecessary (skipped) calls
1 parent e75e45d commit 200e7da

File tree

1 file changed

+37
-9
lines changed

1 file changed

+37
-9
lines changed

packages/modules/src/firebase/handle-module.ts

+37-9
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@ async function runModule(
2626
modName: string,
2727
validate: ValidateFunction,
2828
responseValidate: ValidateFunction,
29-
appId?: any,
29+
appId?: number,
30+
skipAppIds?: number[],
3031
) {
3132
const respond = (result: any[]) => res.send({
3233
result,
@@ -44,13 +45,7 @@ async function runModule(
4445
[`modules.${modName}.enabled`]: true,
4546
fields: `_id,app_id,version,data,hidden_data,modules.${modName}`,
4647
};
47-
if (
48-
appId
49-
&& (typeof appId === 'number' || (typeof appId === 'string' && /^\d+$/.test(appId)))
50-
) {
51-
if (typeof appId === 'string') {
52-
appId = parseInt(appId, 10);
53-
}
48+
if (appId) {
5449
canCache = false;
5550
listAppsParams.app_id = appId;
5651
listAppsParams.limit = 1;
@@ -104,6 +99,9 @@ async function runModule(
10499
if (!application.data) {
105100
application.data = {};
106101
}
102+
if (!appId && skipAppIds?.find((id) => id === application.app_id)) {
103+
continue;
104+
}
107105
const appModuleUrl = application.modules[modName].endpoint as string;
108106
// Handle request with big timeout if proxying one app (by ID) only
109107
const isBigTimeout = !!(appId);
@@ -203,7 +201,37 @@ export default (
203201
runModule(req.query, res, modName, validate, responseValidate);
204202
},
205203
POST() {
206-
runModule(req.body, res, modName, validate, responseValidate, req.query.app_id);
204+
let appId: undefined | number;
205+
const qAppId = req.query.app_id;
206+
if (qAppId) {
207+
if (typeof qAppId === 'string' && /^\d+$/.test(qAppId)) {
208+
appId = parseInt(qAppId, 10);
209+
}
210+
if (typeof qAppId === 'number' && qAppId > 1) {
211+
appId = qAppId;
212+
}
213+
}
214+
const skipAppIds: number[] = [];
215+
const qSkipIds = req.query.skip_ids;
216+
if (qSkipIds) {
217+
(Array.isArray(qSkipIds) ? qSkipIds : [qSkipIds]).forEach((qSkipId: any) => {
218+
if (typeof qSkipId === 'string') {
219+
qSkipId = parseInt(qSkipId, 10);
220+
}
221+
if (typeof qSkipId === 'number' && qSkipId > 1) {
222+
skipAppIds.push(qSkipId);
223+
}
224+
});
225+
}
226+
runModule(
227+
req.body,
228+
res,
229+
modName,
230+
validate,
231+
responseValidate,
232+
appId,
233+
skipAppIds,
234+
);
207235
},
208236
};
209237
};

0 commit comments

Comments
 (0)