import { schema } from '@kbn/config-schema' ; import type { Space } from '../../../../common' ; import { wrapError } from '../../../lib/errors' ; import { createLicensedRouteHandler } from '../../lib' ; import type { ExternalRouteDeps } from './' ; export function initGetAllSpacesApi ( deps ) { const { externalRouter , log , getSpacesService } = deps ; externalRouter . get ( { path : '/api/spaces/space' , validate : { query : schema . object ( { purpose : schema . maybe ( schema . oneOf ( [ schema . literal ( 'any' ) , schema . literal ( 'copySavedObjectsIntoSpace' ) , schema . literal ( 'shareSavedObjectsIntoSpace' ) , ] ) ) , include_authorized_purposes : schema . conditional ( schema . siblingRef ( 'purpose' ) , schema . string ( ) , schema . maybe ( schema . literal ( false ) ) , schema . maybe ( schema . boolean ( ) ) ) , } ) , } , } , createLicensedRouteHandler ( async ( context , request , response ) => { log . debug ( `Inside GET /api/spaces/space` ) ; const { purpose , include_authorized_purposes : includeAuthorizedPurposes } = request . query ; const spacesClient = getSpacesService ( ) . createSpacesClient ( request ) ; let spaces ; try { log . debug ( `Attempting to retrieve all spaces for ${ purpose } purpose with includeAuthorizedPurposes=${ includeAuthorizedPurposes }` ) ; spaces = await spacesClient . getAll ( { purpose , includeAuthorizedPurposes } ) ; log . debug ( `Retrieved ${ spaces . length } spaces for ${ purpose } purpose with includeAuthorizedPurposes=${ includeAuthorizedPurposes }` ) ; } catch ( error ) { log . debug ( `Error retrieving spaces for ${ purpose } purpose with includeAuthorizedPurposes=${ includeAuthorizedPurposes }: ${ error }` ) ; return response . customError ( wrapError ( error ) ) ; } return response . ok ( { body : spaces } ) ; } ) ) ; }