import { schema } from '@osd/config-schema' ; import stringify from 'json-stable-stringify' ; import { createPromiseFromStreams , createMapStream , createConcatStream } from '../../utils/streams' ; import { IRouter } from '../../http' ; import { SavedObjectConfig } from '../saved_objects_config' ; import { exportSavedObjectsToStream } from '../export' ; import { validateTypes , validateObjects } from './utils' ; export const registerExportRoute = ( router , config ) => { const { maxImportExportSize } = config ; router . post ( { path : '/_export' , validate : { body : schema . object ( { type : schema . maybe ( schema . oneOf ( [ schema . string ( ) , schema . arrayOf ( schema . string ( ) ) ] ) ) , objects : schema . maybe ( schema . arrayOf ( schema . object ( { type : schema . string ( ) , id : schema . string ( ) , } ) , { maxSize : maxImportExportSize } ) ) , search : schema . maybe ( schema . string ( ) ) , includeReferencesDeep : schema . boolean ( { defaultValue : false } ) , excludeExportDetails : schema . boolean ( { defaultValue : false } ) , } ) , } , } , router . handleLegacyErrors ( async ( context , req , res ) => { const savedObjectsClient = context . core . savedObjects . client ; const { type , objects , search , excludeExportDetails , includeReferencesDeep } = req . body ; const types = typeof type === 'string' ? [ type ] : type ; const supportedTypes = context . core . savedObjects . typeRegistry . getImportableAndExportableTypes ( ) . map ( ( t ) => t . name ) ; if ( types ) { const validationError = validateTypes ( types , supportedTypes ) ; if ( validationError ) { return res . badRequest ( { body : { message : validationError , } , } ) ; } } if ( objects ) { const validationError = validateObjects ( objects , supportedTypes ) ; if ( validationError ) { return res . badRequest ( { body : { message : validationError , } , } ) ; } } const exportStream = await exportSavedObjectsToStream ( { savedObjectsClient , types , search , objects , exportSizeLimit : maxImportExportSize , includeReferencesDeep , excludeExportDetails , } ) ; const docsToExport = await createPromiseFromStreams ( [ exportStream , createMapStream ( ( obj ) => { return stringify ( obj ) ; } ) , createConcatStream ( [ ] ) , ] ) ; return res . ok ( { body : docsToExport . join ( '\n' ) , headers : { 'Content-Disposition' : `attachment; filename="export.ndjson"` , 'Content-Type' : 'application/ndjson' , } , } ) ; } ) ) ; } ;