Skip to content

Commit

Permalink
Macrotasks decode, encode ids (#126)
Browse files Browse the repository at this point in the history
* Macrotasks decode, encode ids

* add deprecated text

* install @types/lodash

* avoid setImemdiate for first chunk

* change @types/lodash version

* install lodash
  • Loading branch information
jhoncool authored Jun 10, 2024
1 parent 2274f35 commit edf307d
Show file tree
Hide file tree
Showing 19 changed files with 188 additions and 74 deletions.
5 changes: 4 additions & 1 deletion api/components.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@ export {

export {OrganizationPermission, ProjectPermission} from '../src/components/iam';

export {default as prepareResponse} from '../src/components/response-presenter';
export {
default as prepareResponse,
prepareResponseAsync,
} from '../src/components/response-presenter';
export {default as prepareErrorResponse} from '../src/components/error-response-presenter';

export {isEnabledFeature, Feature} from '../src/components/features';
Expand Down
8 changes: 8 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
"axios-retry": "^3.1.9",
"crc-32": "1.2.0",
"dotenv": "^8.2.0",
"lodash": "^4.17.21",
"minimist": "^1.2.5",
"moment": "^2.29.4",
"pg-error-constants": "^1.0.0",
Expand All @@ -34,6 +35,7 @@
"@trendyol/jest-testcontainers": "^2.1.1",
"@types/express": "^4.17.15",
"@types/jest": "^29.5.12",
"@types/lodash": "^4.17.4",
"@types/node": "^18.14.4",
"@types/pg": "^7.14.11",
"@types/supertest": "^2.0.10",
Expand Down
10 changes: 7 additions & 3 deletions src/components/middlewares/decode-id.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import {Request, Response, NextFunction} from '@gravity-ui/expresskit';
import {AppError} from '@gravity-ui/nodekit';
import US_ERRORS from '../../const/us-error-constants';

export const decodeId = (req: Request, _res: Response, next: NextFunction) => {
export const decodeId = async (req: Request, _res: Response, next: NextFunction) => {
try {
for (const idVariable of Utils.idVariables) {
if (req.params && req.params[idVariable]) {
Expand All @@ -15,7 +15,9 @@ export const decodeId = (req: Request, _res: Response, next: NextFunction) => {
const entity = req.query[idVariable] as string | string[];

if (Array.isArray(entity)) {
req.query[idVariable] = entity.map((encodedId) => Utils.decodeId(encodedId));
req.query[idVariable] = await Utils.macrotasksMap(entity, (encodedId) =>
Utils.decodeId(encodedId),
);
} else {
const encodedId = entity;
req.query[idVariable] = Utils.decodeId(encodedId);
Expand All @@ -26,7 +28,9 @@ export const decodeId = (req: Request, _res: Response, next: NextFunction) => {
const entity = req.body[idVariable] as string | string[];

if (Array.isArray(entity)) {
req.body[idVariable] = entity.map((encodedId) => Utils.decodeId(encodedId));
req.body[idVariable] = await Utils.macrotasksMap(entity, (encodedId) =>
Utils.decodeId(encodedId),
);
} else {
const encodedId = req.body[idVariable];
req.body[idVariable] = Utils.decodeId(encodedId);
Expand Down
29 changes: 29 additions & 0 deletions src/components/response-presenter.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import * as ST from '../types/services.types';
import Utils from '../utils';

/** @deprecated use prepareResponseAsync */
export default ({data}: {data: any}): ST.ServiceResponse => {
const response = Utils.encodeData(data);

Expand Down Expand Up @@ -28,3 +29,31 @@ export default ({data}: {data: any}): ST.ServiceResponse => {
response,
};
};

export async function prepareResponseAsync({data}: {data: any}): Promise<ST.ServiceResponse> {
const response = await Utils.macrotasksEncodeData(data);

if (response.results) {
response.results = await Utils.macrotasksEncodeData(response.results);
}
if (response.entries) {
response.entries = await Utils.macrotasksEncodeData(response.entries);
}
if (response.workbooks) {
response.workbooks = await Utils.macrotasksEncodeData(response.workbooks);
}
if (response.collections) {
response.collections = await Utils.macrotasksEncodeData(response.collections);
}
if (response.embed) {
response.embed = await Utils.macrotasksEncodeData(response.embed);
}
if (response.chart) {
response.chart = await Utils.macrotasksEncodeData(response.chart);
}

return {
code: 200,
response,
};
}
20 changes: 10 additions & 10 deletions src/controllers/collections.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {Request, Response} from '@gravity-ui/expresskit';
import prepareResponse from '../components/response-presenter';
import {prepareResponseAsync} from '../components/response-presenter';
import Utils from '../utils';
import {
createCollection,
Expand Down Expand Up @@ -42,7 +42,7 @@ export default {
result.operation,
);

const {code, response} = prepareResponse({data: formattedResponse});
const {code, response} = await prepareResponseAsync({data: formattedResponse});

res.status(code).send(response);
},
Expand All @@ -60,7 +60,7 @@ export default {

const formattedResponse = formatCollection(result);

const {code, response} = prepareResponse({data: formattedResponse});
const {code, response} = await prepareResponseAsync({data: formattedResponse});

res.status(code).send(response);
},
Expand Down Expand Up @@ -104,15 +104,15 @@ export default {

const formattedResponse = formatCollectionContent(result);

const {code, response} = prepareResponse({data: formattedResponse});
const {code, response} = await prepareResponseAsync({data: formattedResponse});

res.status(code).send(response);
},

getRootPermissions: async (req: Request, res: Response) => {
const result = await getRootCollectionPermissions({ctx: req.ctx});

const {code, response} = prepareResponse({data: result});
const {code, response} = await prepareResponseAsync({data: result});

res.status(code).send(response);
},
Expand All @@ -130,7 +130,7 @@ export default {

const formattedResponse = formatGetCollectionBreadcrumbs(result);

const {code, response} = prepareResponse({data: formattedResponse});
const {code, response} = await prepareResponseAsync({data: formattedResponse});

res.status(code).send(response);
},
Expand All @@ -147,7 +147,7 @@ export default {

const formattedResponse = formatCollectionModelsList(result);

const {code, response} = prepareResponse({data: formattedResponse});
const {code, response} = await prepareResponseAsync({data: formattedResponse});

res.status(code).send(response);
},
Expand All @@ -166,7 +166,7 @@ export default {

const formattedResponse = formatCollectionModel(result);

const {code, response} = prepareResponse({data: formattedResponse});
const {code, response} = await prepareResponseAsync({data: formattedResponse});

res.status(code).send(response);
},
Expand All @@ -184,7 +184,7 @@ export default {

const formattedResponse = formatCollectionModelsList(result);

const {code, response} = prepareResponse({data: formattedResponse});
const {code, response} = await prepareResponseAsync({data: formattedResponse});

res.status(code).send(response);
},
Expand All @@ -202,7 +202,7 @@ export default {

const formattedResponse = formatCollectionModel(result);

const {code, response} = prepareResponse({data: formattedResponse});
const {code, response} = await prepareResponseAsync({data: formattedResponse});

res.status(code).send(response);
},
Expand Down
10 changes: 5 additions & 5 deletions src/controllers/color-palettes.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {Request, Response} from '@gravity-ui/expresskit';
import prepareResponse from '../components/response-presenter';
import {prepareResponseAsync} from '../components/response-presenter';
import {
deleteColorPalette,
getColorPalettesList,
Expand All @@ -25,7 +25,7 @@ export default {

const formattedResponse = formatColorPalette(result);

const {code, response} = prepareResponse({data: formattedResponse});
const {code, response} = await prepareResponseAsync({data: formattedResponse});

res.status(code).send(response);
},
Expand All @@ -48,7 +48,7 @@ export default {

const formattedResponse = formatColorPalette(result);

const {code, response} = prepareResponse({data: formattedResponse});
const {code, response} = await prepareResponseAsync({data: formattedResponse});

res.status(code).send(response);
},
Expand All @@ -63,7 +63,7 @@ export default {

const formattedResponse = formatColorPaletteList(result);

const {code, response} = prepareResponse({data: formattedResponse});
const {code, response} = await prepareResponseAsync({data: formattedResponse});

res.status(code).send(response);
},
Expand All @@ -78,7 +78,7 @@ export default {

const formattedResponse = formatColorPaletteList(result);

const {code, response} = prepareResponse({data: formattedResponse});
const {code, response} = await prepareResponseAsync({data: formattedResponse});

res.status(code).send(response);
},
Expand Down
30 changes: 15 additions & 15 deletions src/controllers/entries.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import EntryService from '../services/entry.service';
import {Request, Response} from '@gravity-ui/expresskit';
import prepareResponse from '../components/response-presenter';
import {prepareResponseAsync} from '../components/response-presenter';
import Utils from '../utils';
import {US_MASTER_TOKEN_HEADER} from '../const';
import NavigationService from '../services/navigation.service';
Expand Down Expand Up @@ -48,7 +48,7 @@ export default {
);
const formattedResponse = formatGetEntryResponse(req.ctx, result);

const {code, response} = prepareResponse({data: formattedResponse});
const {code, response} = await prepareResponseAsync({data: formattedResponse});

res.status(code).send(response);
},
Expand All @@ -65,7 +65,7 @@ export default {
);
const formattedResponse = formatGetEntryMetaResponse(result);

const {code, response} = prepareResponse({data: formattedResponse});
const {code, response} = await prepareResponseAsync({data: formattedResponse});

res.status(code).send(response);
},
Expand All @@ -86,7 +86,7 @@ export default {
);
const formattedResponse = formatGetEntryMetaPrivateResponse(result);

const {code, response} = prepareResponse({data: formattedResponse});
const {code, response} = await prepareResponseAsync({data: formattedResponse});

res.status(code).send(response);
},
Expand All @@ -106,7 +106,7 @@ export default {
},
);

const {code, response} = prepareResponse({data: result});
const {code, response} = await prepareResponseAsync({data: result});

res.status(code).send(response);
},
Expand Down Expand Up @@ -134,7 +134,7 @@ export default {
ctx: req.ctx,
});

const {code, response} = prepareResponse({data: result});
const {code, response} = await prepareResponseAsync({data: result});

res.status(code).send(response);
},
Expand All @@ -161,7 +161,7 @@ export default {
ctx: req.ctx,
});

const {code, response} = prepareResponse({data: result});
const {code, response} = await prepareResponseAsync({data: result});

res.status(code).send(response);
},
Expand All @@ -184,7 +184,7 @@ export default {
skipSyncLinks: body.skipSyncLinks,
});

const {code, response} = prepareResponse({data: result});
const {code, response} = await prepareResponseAsync({data: result});

res.status(code).send(response);
},
Expand All @@ -200,7 +200,7 @@ export default {
},
);

const {code, response} = prepareResponse({data: result});
const {code, response} = await prepareResponseAsync({data: result});

res.status(code).send(response);
},
Expand All @@ -213,7 +213,7 @@ export default {
name: body.name,
});

const {code, response} = prepareResponse({data: result});
const {code, response} = await prepareResponseAsync({data: result});

res.status(code).send(response);
},
Expand All @@ -227,7 +227,7 @@ export default {
},
);

const {code, response} = prepareResponse({data: result});
const {code, response} = await prepareResponseAsync({data: result});
res.status(code).send(response);
},

Expand All @@ -243,7 +243,7 @@ export default {
},
);

const {code, response} = prepareResponse({data: result});
const {code, response} = await prepareResponseAsync({data: result});

res.status(code).send(response);
},
Expand All @@ -262,7 +262,7 @@ export default {

const formattedResponse = formatEntryModel(result);

const {code, response} = prepareResponse({data: formattedResponse});
const {code, response} = await prepareResponseAsync({data: formattedResponse});
res.status(code).send(response);
},

Expand All @@ -277,7 +277,7 @@ export default {
},
);

const {code, response} = prepareResponse({data: result});
const {code, response} = await prepareResponseAsync({data: result});
res.status(code).send(response);
},

Expand All @@ -302,7 +302,7 @@ export default {
ctx: req.ctx,
});

const {code, response} = prepareResponse({data: result});
const {code, response} = await prepareResponseAsync({data: result});

res.status(code).send(response);
},
Expand Down
Loading

0 comments on commit edf307d

Please sign in to comment.