import { schema } from '@kbn/config-schema' ; import { IRouter } from '../../http' ; import { InternalCoreUsageDataSetup } from '../../core_usage_data' ; import { catchAndReturnBoomErrors } from './utils' ; interface RouteDependencies { coreUsageData : InternalCoreUsageDataSetup ; } export const registerCreateRoute = ( router , { coreUsageData } ) => { router . post ( { path : '/{type}/{id?}' , validate : { params : schema . object ( { type : schema . string ( ) , id : schema . maybe ( schema . string ( ) ) , } ) , query : schema . object ( { overwrite : schema . boolean ( { defaultValue : false } ) , } ) , body : schema . object ( { attributes : schema . recordOf ( schema . string ( ) , schema . any ( ) ) , migrationVersion : schema . maybe ( schema . recordOf ( schema . string ( ) , schema . string ( ) ) ) , coreMigrationVersion : schema . maybe ( schema . string ( ) ) , references : schema . maybe ( schema . arrayOf ( schema . object ( { name : schema . string ( ) , type : schema . string ( ) , id : schema . string ( ) , } ) ) ) , initialNamespaces : schema . maybe ( schema . arrayOf ( schema . string ( ) , { minSize : 1 } ) ) , } ) , } , } , catchAndReturnBoomErrors ( async ( context , req , res ) => { const { type , id } = req . params ; const { overwrite } = req . query ; const { attributes , migrationVersion , coreMigrationVersion , references , initialNamespaces } = req . body ; const usageStatsClient = coreUsageData . getClient ( ) ; usageStatsClient . incrementSavedObjectsCreate ( { request : req } ) . catch ( ( ) => { } ) ; const options = { id , overwrite , migrationVersion , coreMigrationVersion , references , initialNamespaces , } ; const result = await context . core . savedObjects . client . create ( type , attributes , options ) ; return res . ok ( { body : result } ) ; } ) ) ; } ;