import Boom from '@hapi/boom' ; import { SavedObjectsErrorHelpers } from '../../../../../../../src/core/server' ; import { wrapError } from '../../../lib/errors' ; import { spaceSchema } from '../../../lib/space_schema' ; import { createLicensedRouteHandler } from '../../lib' ; import type { ExternalRouteDeps } from './' ; export function initPostSpacesApi ( deps ) { const { externalRouter , log , getSpacesService } = deps ; externalRouter . post ( { path : '/api/spaces/space' , validate : { body : spaceSchema , } , } , createLicensedRouteHandler ( async ( context , request , response ) => { log . debug ( `Inside POST /api/spaces/space` ) ; const spacesClient = getSpacesService ( ) . createSpacesClient ( request ) ; const space = request . body ; try { log . debug ( `Attempting to create space` ) ; const createdSpace = await spacesClient . create ( space ) ; return response . ok ( { body : createdSpace } ) ; } catch ( error ) { if ( SavedObjectsErrorHelpers . isConflictError ( error ) ) { const { body } = wrapError ( Boom . conflict ( `A space with the identifier ${ space . id } already exists.` ) ) ; return response . conflict ( { body } ) ; } log . debug ( `Error creating space: ${ error }` ) ; return response . customError ( wrapError ( error ) ) ; } } ) ) ; }