import uuidv1 from 'uuid/v1' ; import uuidv5 from 'uuid/v5' ; import { SavedObjectsFindOptions } from '../../types' ; import { SavedObjectsFindResponse } from '..' ; export const DEFAULT_NAMESPACE_STRING = 'default' ; export const ALL_NAMESPACES_STRING = '*' ; export const FIND_DEFAULT_PAGE = 1 ; export const FIND_DEFAULT_PER_PAGE = 20 ; const UUID_REGEX = /^(?:[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}|00000000-0000-0000-0000-000000000000)$/i ; public export class SavedObjectsUtils { param namespace public static namespaceIdToString = ( namespace ? ) => { if ( namespace === '' ) { throw new TypeError ( 'namespace cannot be an empty string' ) ; } return namespace ?? DEFAULT_NAMESPACE_STRING ; } ; param namespace public static namespaceStringToId = ( namespace ) => { if ( ! namespace ) { throw new TypeError ( 'namespace must be a non-empty string' ) ; } return namespace !== DEFAULT_NAMESPACE_STRING ? namespace : undefined ; } ; /**
     * Creates an empty response for a find operation. This is only intended to be used by saved objects client wrappers.
     */ public static createEmptyFindResponse = < T , A > ( { page = FIND_DEFAULT_PAGE , perPage = FIND_DEFAULT_PER_PAGE , } ) : SavedObjectsFindResponse < T , A > => ( { page , per_page : perPage , total : 0 , saved_objects : [ ] , } ) ; /**
     * Generates a random ID for a saved objects.
     */ public static generateId ( ) { return uuidv1 ( ) ; } param { string } id todo public static isRandomId ( id ) { return typeof id === 'string' && UUID_REGEX . test ( id ) ; } param { string } namespace param { string } type param { string } id returns { string } public static getConvertedObjectId ( namespace , type , id ) { if ( SavedObjectsUtils . namespaceIdToString ( namespace ) === DEFAULT_NAMESPACE_STRING ) { return id ; } return uuidv5 ( `${ namespace }:${ type }:${ id }` , uuidv5 . DNS ) ; } }