Skip to content

Commit

Permalink
refactor/reflatten server routes
Browse files Browse the repository at this point in the history
  • Loading branch information
tsullivan committed Sep 1, 2021
1 parent 72f6700 commit 1d5ddbb
Show file tree
Hide file tree
Showing 17 changed files with 330 additions and 206 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ describe('POST /diagnose/screenshot', () => {
toPromise: () => (resp instanceof Error ? Promise.reject(resp) : Promise.resolve(resp)),
}),
}));
(generatePngObservableFactory as any).mockResolvedValue(generateMock);
(generatePngObservableFactory as jest.Mock).mockResolvedValue(generateMock);
};

const config = createMockConfigSchema({ queue: { timeout: 120000 } });
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,16 @@
* 2.0.
*/

import { Writable } from 'stream';
import { schema } from '@kbn/config-schema';
import { KibanaRequest } from 'src/core/server';
import { ReportingCore } from '../';
import { runTaskFnFactory } from '../export_types/csv_searchsource_immediate/execute_job';
import { JobParamsDownloadCSV } from '../export_types/csv_searchsource_immediate/types';
import { LevelLogger as Logger } from '../lib';
import { TaskRunResult } from '../lib/tasks';
import { authorizedUserPreRouting } from './lib/authorized_user_pre_routing';
import { HandlerErrorFunction } from './types';
import { Writable } from 'stream';
import { ReportingCore } from '../../';
import { runTaskFnFactory } from '../../export_types/csv_searchsource_immediate/execute_job';
import { JobParamsDownloadCSV } from '../../export_types/csv_searchsource_immediate/types';
import { LevelLogger as Logger } from '../../lib';
import { TaskRunResult } from '../../lib/tasks';
import { authorizedUserPreRouting } from '../lib/authorized_user_pre_routing';
import { handleError } from '../lib/handle_request';

const API_BASE_URL_V1 = '/api/reporting/v1';
const API_BASE_GENERATE_V1 = `${API_BASE_URL_V1}/generate`;
Expand All @@ -32,7 +32,6 @@ export type CsvFromSavedObjectRequest = KibanaRequest<unknown, unknown, JobParam
*/
export function registerGenerateCsvFromSavedObjectImmediate(
reporting: ReportingCore,
handleError: HandlerErrorFunction,
parentLogger: Logger
) {
const setupDeps = reporting.getPluginSetupDeps();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,16 @@

import { schema } from '@kbn/config-schema';
import rison from 'rison-node';
import { ReportingCore } from '../';
import { API_BASE_URL } from '../../common/constants';
import { BaseParams } from '../types';
import { authorizedUserPreRouting } from './lib/authorized_user_pre_routing';
import { HandlerErrorFunction, HandlerFunction } from './types';
import { ReportingCore } from '../..';
import { API_BASE_URL } from '../../../common/constants';
import { LevelLogger } from '../../lib';
import { BaseParams } from '../../types';
import { authorizedUserPreRouting } from '../lib/authorized_user_pre_routing';
import { handleError, handleGenerateRequest } from '../lib/handle_request';

const BASE_GENERATE = `${API_BASE_URL}/generate`;

export function registerGenerateFromJobParams(
reporting: ReportingCore,
handler: HandlerFunction,
handleError: HandlerErrorFunction
) {
export function registerJobGenerationRoutes(reporting: ReportingCore, logger: LevelLogger) {
const setupDeps = reporting.getPluginSetupDeps();
const { router } = setupDeps;

Expand Down Expand Up @@ -62,7 +59,6 @@ export function registerGenerateFromJobParams(
});
}

const { exportType } = req.params;
let jobParams;

try {
Expand All @@ -81,7 +77,16 @@ export function registerGenerateFromJobParams(
}

try {
return await handler(user, exportType, jobParams, context, req, res);
return await handleGenerateRequest(
reporting,
user,
req.params.exportType,
jobParams,
context,
req,
res,
logger
);
} catch (err) {
return handleError(res, err);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,15 @@ import { of } from 'rxjs';
import { ElasticsearchClient } from 'kibana/server';
import { setupServer } from 'src/core/server/test_utils';
import supertest from 'supertest';
import { ReportingCore } from '..';
import { ExportTypesRegistry } from '../lib/export_types_registry';
import { createMockLevelLogger, createMockReportingCore } from '../test_helpers';
import { ReportingCore } from '../..';
import { ExportTypesRegistry } from '../../lib/export_types_registry';
import { createMockLevelLogger, createMockReportingCore } from '../../test_helpers';
import {
createMockConfigSchema,
createMockPluginSetup,
} from '../test_helpers/create_mock_reportingplugin';
import { registerJobGenerationRoutes } from './generation';
import type { ReportingRequestHandlerContext } from '../types';
} from '../../test_helpers/create_mock_reportingplugin';
import type { ReportingRequestHandlerContext } from '../../types';
import { registerJobGenerationRoutes } from './generate_from_jobparams';

type SetupServerReturn = UnwrapPromise<ReturnType<typeof setupServer>>;

Expand Down
10 changes: 10 additions & 0 deletions x-pack/plugins/reporting/server/routes/generate/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/

export { registerGenerateCsvFromSavedObjectImmediate } from './csv_searchsource_immediate'; // FIXME: should not need to register each immediate export type separately
export { registerJobGenerationRoutes } from './generate_from_jobparams';
export { registerLegacy } from './legacy';
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,16 @@
*/

import { schema } from '@kbn/config-schema';
import querystring from 'querystring';
import { authorizedUserPreRouting } from './lib/authorized_user_pre_routing';
import { API_BASE_URL } from '../../common/constants';
import { HandlerErrorFunction, HandlerFunction } from './types';
import { ReportingCore } from '../core';
import { LevelLogger } from '../lib';
import querystring, { ParsedUrlQueryInput } from 'querystring';
import { API_BASE_URL } from '../../../common/constants';
import { ReportingCore } from '../../core';
import { LevelLogger } from '../../lib';
import { authorizedUserPreRouting } from '../lib/authorized_user_pre_routing';
import { handleError, handleGenerateRequest } from '../lib/handle_request';

const BASE_GENERATE = `${API_BASE_URL}/generate`;

export function registerLegacy(
reporting: ReportingCore,
handler: HandlerFunction,
handleError: HandlerErrorFunction,
logger: LevelLogger
) {
export function registerLegacy(reporting: ReportingCore, logger: LevelLogger) {
const { router } = reporting.getPluginSetupDeps();

function createLegacyPdfRoute({ path, objectType }: { path: string; objectType: string }) {
Expand All @@ -32,8 +27,10 @@ export function registerLegacy(
validate: {
params: schema.object({
savedObjectId: schema.string({ minLength: 3 }),
title: schema.string(),
browserTimezone: schema.string(),
}),
query: schema.any(),
query: schema.maybe(schema.string()),
},
},

Expand All @@ -46,10 +43,11 @@ export function registerLegacy(
title,
savedObjectId,
browserTimezone,
}: { title: string; savedObjectId: string; browserTimezone: string } = req.params as any;
const queryString = querystring.stringify(req.query as any);
}: { title: string; savedObjectId: string; browserTimezone: string } = req.params;
const queryString = querystring.stringify(req.query as ParsedUrlQueryInput | undefined);

return await handler(
return await handleGenerateRequest(
reporting,
user,
exportTypeId,
{
Expand All @@ -62,7 +60,8 @@ export function registerLegacy(
},
context,
req,
res
res,
logger
);
} catch (err) {
throw handleError(res, err);
Expand Down
92 changes: 0 additions & 92 deletions x-pack/plugins/reporting/server/routes/generation.ts

This file was deleted.

23 changes: 11 additions & 12 deletions x-pack/plugins/reporting/server/routes/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,22 @@
* 2.0.
*/

import { LevelLogger as Logger } from '../lib';
import { ReportingCore } from '..';
import { LevelLogger } from '../lib';
import { registerDeprecationsRoutes } from './deprecations';
import { registerDiagnosticRoutes } from './diagnostic';
import { registerJobGenerationRoutes } from './generation';
import { registerJobInfoRoutes } from './jobs';
import { ReportingCore } from '../core';
import {
registerGenerateCsvFromSavedObjectImmediate,
registerJobGenerationRoutes,
registerLegacy,
} from './generate';
import { registerJobInfoRoutes } from './management';

export function registerRoutes(reporting: ReportingCore, logger: Logger) {
export function registerRoutes(reporting: ReportingCore, logger: LevelLogger) {
registerDeprecationsRoutes(reporting, logger);
registerDiagnosticRoutes(reporting, logger);
registerGenerateCsvFromSavedObjectImmediate(reporting, logger);
registerJobGenerationRoutes(reporting, logger);
registerLegacy(reporting, logger);
registerJobInfoRoutes(reporting);
}

export interface ReportingRequestPre {
management: {
jobTypes: string[];
};
user: string;
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ interface Payload {
statusCode: number;
content: string | Stream | ErrorFromPayload;
contentType: string | null;
headers: Record<string, any>;
headers: Record<string, string | number>;
}

type TaskRunResult = Required<ReportApiJSON>['output'];
Expand Down
Loading

0 comments on commit 1d5ddbb

Please sign in to comment.