|
1 | 1 | import type {NextFunction, Request, Response} from '@gravity-ui/expresskit';
|
2 | 2 | import {REQUEST_ID_PARAM_NAME} from '@gravity-ui/nodekit';
|
3 | 3 |
|
| 4 | +import {onFail} from '../callbacks'; |
4 | 5 | import {registry} from '../registry';
|
5 | 6 | import type {DatalensGatewaySchemas} from '../types/gateway';
|
6 | 7 |
|
7 | 8 | export function getTenantSettingsMiddleware() {
|
8 |
| - return async function tenantSettingsMiddleware( |
9 |
| - req: Request, |
10 |
| - res: Response, |
11 |
| - next: NextFunction, |
12 |
| - ) { |
| 9 | + async function resolveTenantSettings(req: Request, res: Response, next: NextFunction) { |
13 | 10 | const {ctx} = req;
|
14 | 11 |
|
15 |
| - try { |
16 |
| - const requestId = req.ctx.get(REQUEST_ID_PARAM_NAME); |
17 |
| - const currentTenantId = 'common'; |
| 12 | + const requestId = req.ctx.get(REQUEST_ID_PARAM_NAME); |
| 13 | + const currentTenantId = 'common'; |
18 | 14 |
|
19 |
| - const {gatewayApi} = registry.getGatewayApi<DatalensGatewaySchemas>(); |
20 |
| - const {getAuthArgsUSPrivate} = registry.common.auth.getAll(); |
21 |
| - const authArgsUSPrivate = getAuthArgsUSPrivate(req, res); |
| 15 | + const {gatewayApi} = registry.getGatewayApi<DatalensGatewaySchemas>(); |
| 16 | + const {getAuthArgsUSPrivate} = registry.common.auth.getAll(); |
| 17 | + const authArgsUSPrivate = getAuthArgsUSPrivate(req, res); |
22 | 18 |
|
23 |
| - const tenantDetailsResponce = await gatewayApi.usPrivate._getTenantDetails({ |
24 |
| - ctx, |
25 |
| - headers: req.headers, |
26 |
| - requestId: requestId ?? '', |
27 |
| - authArgs: authArgsUSPrivate, |
28 |
| - args: {tenantId: currentTenantId}, |
29 |
| - }); |
30 |
| - const resolvedTenant = tenantDetailsResponce.responseData; |
31 |
| - res.locals.tenantDefaultColorPaletteId = resolvedTenant.settings?.defaultColorPaletteId; |
| 19 | + const tenantDetailsResponce = await gatewayApi.usPrivate._getTenantDetails({ |
| 20 | + ctx, |
| 21 | + headers: req.headers, |
| 22 | + requestId: requestId ?? '', |
| 23 | + authArgs: authArgsUSPrivate, |
| 24 | + args: {tenantId: currentTenantId}, |
| 25 | + }); |
| 26 | + const resolvedTenant = tenantDetailsResponce.responseData; |
| 27 | + res.locals.tenantDefaultColorPaletteId = resolvedTenant.settings?.defaultColorPaletteId; |
32 | 28 |
|
33 |
| - next(); |
34 |
| - } catch (error) { |
35 |
| - ctx.logError('TENANT_RESOLVED_FAILED', error); |
| 29 | + next(); |
| 30 | + } |
36 | 31 |
|
37 |
| - next(error); |
38 |
| - } |
| 32 | + return function resolveTenantSettingsMiddleware( |
| 33 | + req: Request, |
| 34 | + res: Response, |
| 35 | + next: NextFunction, |
| 36 | + ) { |
| 37 | + resolveTenantSettings(req, res, next) |
| 38 | + .catch((error) => { |
| 39 | + req.ctx.logError('TENANT_RESOLVED_FAILED', error); |
| 40 | + onFail(req, res); |
| 41 | + }) |
| 42 | + .catch((error) => next(error)); |
39 | 43 | };
|
40 | 44 | }
|
0 commit comments