-
Notifications
You must be signed in to change notification settings - Fork 8.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Joel Griffith
committed
Jun 4, 2020
1 parent
7ada624
commit a284f1b
Showing
1 changed file
with
81 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License; | ||
* you may not use this file except in compliance with the Elastic License. | ||
*/ | ||
|
||
import { schema } from '@kbn/config-schema'; | ||
import querystring from 'querystring'; | ||
import { authorizedUserPreRoutingFactory } 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'; | ||
|
||
const BASE_GENERATE = `${API_BASE_URL}/generate`; | ||
|
||
export function registerLegacy( | ||
reporting: ReportingCore, | ||
handler: HandlerFunction, | ||
handleError: HandlerErrorFunction, | ||
logger: LevelLogger | ||
) { | ||
const { router } = reporting.getPluginSetupDeps(); | ||
const userHandler = authorizedUserPreRoutingFactory(reporting); | ||
|
||
function createLegacyPdfRoute({ path, objectType }: { path: string; objectType: string }) { | ||
const exportTypeId = 'printablePdf'; | ||
|
||
router.post( | ||
{ | ||
path, | ||
validate: { | ||
params: schema.object({ | ||
savedObjectId: schema.string({ minLength: 3 }), | ||
}), | ||
query: schema.any(), | ||
}, | ||
}, | ||
|
||
userHandler(async (user, context, req, res) => { | ||
const message = `The following URL is deprecated and will stop working in the next major version: ${req.url.path}`; | ||
logger.warn(message, ['deprecation']); | ||
|
||
try { | ||
const { savedObjectId }: { savedObjectId: string } = req.params as any; | ||
const queryString = querystring.stringify(req.query as any); | ||
|
||
return await handler( | ||
user, | ||
exportTypeId, | ||
{ | ||
objectType, | ||
savedObjectId, | ||
queryString, | ||
}, | ||
context, | ||
req, | ||
res | ||
); | ||
} catch (err) { | ||
throw handleError(res, err); | ||
} | ||
}) | ||
); | ||
} | ||
|
||
createLegacyPdfRoute({ | ||
path: `${BASE_GENERATE}/visualization/{savedId}`, | ||
objectType: 'visualization', | ||
}); | ||
|
||
createLegacyPdfRoute({ | ||
path: `${BASE_GENERATE}/search/{savedId}`, | ||
objectType: 'search', | ||
}); | ||
|
||
createLegacyPdfRoute({ | ||
path: `${BASE_GENERATE}/dashboard/{savedId}`, | ||
objectType: 'dashboard', | ||
}); | ||
} |