From 2c36a3137657ac2224936ce9c35777325eba07c4 Mon Sep 17 00:00:00 2001
From: Joe Portner <5295965+jportner@users.noreply.github.com>
Date: Fri, 4 Sep 2020 12:02:27 -0400
Subject: [PATCH] Wrap namespace utility methods in `SavedObjectsUtils` class
---
.../core/server/kibana-plugin-core-server.md | 3 +-
...na-plugin-core-server.savedobjectsutils.md | 20 +++++++
....savedobjectsutils.namespaceidtostring.md} | 6 +-
....savedobjectsutils.namespacestringtoid.md} | 6 +-
src/core/server/index.ts | 3 +-
.../server/saved_objects/service/index.ts | 3 +-
.../server/saved_objects/service/lib/index.ts | 2 +-
.../service/lib/namespace.test.ts | 53 -----------------
.../saved_objects/service/lib/namespace.ts | 48 ----------------
.../saved_objects/service/lib/repository.ts | 32 +++++++----
.../service/lib/search_dsl/query_params.ts | 2 +-
.../saved_objects/service/lib/utils.test.ts | 57 +++++++++++++++++++
.../server/saved_objects/service/lib/utils.ts | 53 +++++++++++++++++
src/core/server/server.api.md | 12 ++--
.../server/lib/utils/__mocks__/index.ts | 6 +-
.../spaces/server/lib/utils/namespace.ts | 6 +-
16 files changed, 174 insertions(+), 138 deletions(-)
create mode 100644 docs/development/core/server/kibana-plugin-core-server.savedobjectsutils.md
rename docs/development/core/server/{kibana-plugin-core-server.namespaceidtostring.md => kibana-plugin-core-server.savedobjectsutils.namespaceidtostring.md} (57%)
rename docs/development/core/server/{kibana-plugin-core-server.namespacestringtoid.md => kibana-plugin-core-server.savedobjectsutils.namespacestringtoid.md} (57%)
delete mode 100644 src/core/server/saved_objects/service/lib/namespace.test.ts
delete mode 100644 src/core/server/saved_objects/service/lib/namespace.ts
create mode 100644 src/core/server/saved_objects/service/lib/utils.test.ts
create mode 100644 src/core/server/saved_objects/service/lib/utils.ts
diff --git a/docs/development/core/server/kibana-plugin-core-server.md b/docs/development/core/server/kibana-plugin-core-server.md
index a29d550e831ae..2adeb6c8dc685 100644
--- a/docs/development/core/server/kibana-plugin-core-server.md
+++ b/docs/development/core/server/kibana-plugin-core-server.md
@@ -28,6 +28,7 @@ The plugin integrates with the core system via lifecycle events: `setup`
| [SavedObjectsErrorHelpers](./kibana-plugin-core-server.savedobjectserrorhelpers.md) | |
| [SavedObjectsRepository](./kibana-plugin-core-server.savedobjectsrepository.md) | |
| [SavedObjectsSerializer](./kibana-plugin-core-server.savedobjectsserializer.md) | A serializer that can be used to manually convert [raw](./kibana-plugin-core-server.savedobjectsrawdoc.md) or [sanitized](./kibana-plugin-core-server.savedobjectsanitizeddoc.md) documents to the other kind. |
+| [SavedObjectsUtils](./kibana-plugin-core-server.savedobjectsutils.md) | |
| [SavedObjectTypeRegistry](./kibana-plugin-core-server.savedobjecttyperegistry.md) | Registry holding information about all the registered [saved object types](./kibana-plugin-core-server.savedobjectstype.md). |
## Enumerations
@@ -224,8 +225,6 @@ The plugin integrates with the core system via lifecycle events: `setup`
| Variable | Description |
| --- | --- |
| [kibanaResponseFactory](./kibana-plugin-core-server.kibanaresponsefactory.md) | Set of helpers used to create KibanaResponse
to form HTTP response on an incoming request. Should be returned as a result of [RequestHandler](./kibana-plugin-core-server.requesthandler.md) execution. |
-| [namespaceIdToString](./kibana-plugin-core-server.namespaceidtostring.md) | Converts a given saved object namespace ID to its string representation. All namespace IDs have an identical string representation, with the exception of the undefined
namespace ID (which has a namespace string of 'default'
). |
-| [namespaceStringToId](./kibana-plugin-core-server.namespacestringtoid.md) | Converts a given saved object namespace string to its ID representation. All namespace strings have an identical ID representation, with the exception of the 'default'
namespace string (which has a namespace ID of undefined
). |
| [ServiceStatusLevels](./kibana-plugin-core-server.servicestatuslevels.md) | The current "level" of availability of a service. |
| [validBodyOutput](./kibana-plugin-core-server.validbodyoutput.md) | The set of valid body.output |
diff --git a/docs/development/core/server/kibana-plugin-core-server.savedobjectsutils.md b/docs/development/core/server/kibana-plugin-core-server.savedobjectsutils.md
new file mode 100644
index 0000000000000..e365dfbcb5142
--- /dev/null
+++ b/docs/development/core/server/kibana-plugin-core-server.savedobjectsutils.md
@@ -0,0 +1,20 @@
+
+
+[Home](./index.md) > [kibana-plugin-core-server](./kibana-plugin-core-server.md) > [SavedObjectsUtils](./kibana-plugin-core-server.savedobjectsutils.md)
+
+## SavedObjectsUtils class
+
+
+Signature:
+
+```typescript
+export declare class SavedObjectsUtils
+```
+
+## Properties
+
+| Property | Modifiers | Type | Description |
+| --- | --- | --- | --- |
+| [namespaceIdToString](./kibana-plugin-core-server.savedobjectsutils.namespaceidtostring.md) | static
| (namespace?: string | undefined) => string
| Converts a given saved object namespace ID to its string representation. All namespace IDs have an identical string representation, with the exception of the undefined
namespace ID (which has a namespace string of 'default'
). |
+| [namespaceStringToId](./kibana-plugin-core-server.savedobjectsutils.namespacestringtoid.md) | static
| (namespace: string) => string | undefined
| Converts a given saved object namespace string to its ID representation. All namespace strings have an identical ID representation, with the exception of the 'default'
namespace string (which has a namespace ID of undefined
). |
+
diff --git a/docs/development/core/server/kibana-plugin-core-server.namespaceidtostring.md b/docs/development/core/server/kibana-plugin-core-server.savedobjectsutils.namespaceidtostring.md
similarity index 57%
rename from docs/development/core/server/kibana-plugin-core-server.namespaceidtostring.md
rename to docs/development/core/server/kibana-plugin-core-server.savedobjectsutils.namespaceidtostring.md
index 5c9f093531e1c..591505892e64f 100644
--- a/docs/development/core/server/kibana-plugin-core-server.namespaceidtostring.md
+++ b/docs/development/core/server/kibana-plugin-core-server.savedobjectsutils.namespaceidtostring.md
@@ -1,13 +1,13 @@
-[Home](./index.md) > [kibana-plugin-core-server](./kibana-plugin-core-server.md) > [namespaceIdToString](./kibana-plugin-core-server.namespaceidtostring.md)
+[Home](./index.md) > [kibana-plugin-core-server](./kibana-plugin-core-server.md) > [SavedObjectsUtils](./kibana-plugin-core-server.savedobjectsutils.md) > [namespaceIdToString](./kibana-plugin-core-server.savedobjectsutils.namespaceidtostring.md)
-## namespaceIdToString variable
+## SavedObjectsUtils.namespaceIdToString property
Converts a given saved object namespace ID to its string representation. All namespace IDs have an identical string representation, with the exception of the `undefined` namespace ID (which has a namespace string of `'default'`).
Signature:
```typescript
-namespaceIdToString: (namespace?: string | undefined) => string
+static namespaceIdToString: (namespace?: string | undefined) => string;
```
diff --git a/docs/development/core/server/kibana-plugin-core-server.namespacestringtoid.md b/docs/development/core/server/kibana-plugin-core-server.savedobjectsutils.namespacestringtoid.md
similarity index 57%
rename from docs/development/core/server/kibana-plugin-core-server.namespacestringtoid.md
rename to docs/development/core/server/kibana-plugin-core-server.savedobjectsutils.namespacestringtoid.md
index 39d6a13aa7fb0..e052fe493b5ea 100644
--- a/docs/development/core/server/kibana-plugin-core-server.namespacestringtoid.md
+++ b/docs/development/core/server/kibana-plugin-core-server.savedobjectsutils.namespacestringtoid.md
@@ -1,13 +1,13 @@
-[Home](./index.md) > [kibana-plugin-core-server](./kibana-plugin-core-server.md) > [namespaceStringToId](./kibana-plugin-core-server.namespacestringtoid.md)
+[Home](./index.md) > [kibana-plugin-core-server](./kibana-plugin-core-server.md) > [SavedObjectsUtils](./kibana-plugin-core-server.savedobjectsutils.md) > [namespaceStringToId](./kibana-plugin-core-server.savedobjectsutils.namespacestringtoid.md)
-## namespaceStringToId variable
+## SavedObjectsUtils.namespaceStringToId property
Converts a given saved object namespace string to its ID representation. All namespace strings have an identical ID representation, with the exception of the `'default'` namespace string (which has a namespace ID of `undefined`).
Signature:
```typescript
-namespaceStringToId: (namespace: string) => string | undefined
+static namespaceStringToId: (namespace: string) => string | undefined;
```
diff --git a/src/core/server/index.ts b/src/core/server/index.ts
index 9753f725c7c96..257ae3710478e 100644
--- a/src/core/server/index.ts
+++ b/src/core/server/index.ts
@@ -300,11 +300,10 @@ export {
SavedObjectsTypeManagementDefinition,
SavedObjectMigrationMap,
SavedObjectMigrationFn,
+ SavedObjectsUtils,
exportSavedObjectsToStream,
importSavedObjectsFromStream,
resolveSavedObjectsImportErrors,
- namespaceIdToString,
- namespaceStringToId,
} from './saved_objects';
export {
diff --git a/src/core/server/saved_objects/service/index.ts b/src/core/server/saved_objects/service/index.ts
index 245df328db665..19ba7396911ad 100644
--- a/src/core/server/saved_objects/service/index.ts
+++ b/src/core/server/saved_objects/service/index.ts
@@ -58,8 +58,7 @@ export {
SavedObjectsErrorHelpers,
SavedObjectsClientFactory,
SavedObjectsClientFactoryProvider,
- namespaceIdToString,
- namespaceStringToId,
+ SavedObjectsUtils,
} from './lib';
export * from './saved_objects_client';
diff --git a/src/core/server/saved_objects/service/lib/index.ts b/src/core/server/saved_objects/service/lib/index.ts
index cfae6ae9b62d5..eae8c5ef2e10c 100644
--- a/src/core/server/saved_objects/service/lib/index.ts
+++ b/src/core/server/saved_objects/service/lib/index.ts
@@ -31,4 +31,4 @@ export {
export { SavedObjectsErrorHelpers } from './errors';
-export { namespaceIdToString, namespaceStringToId } from './namespace';
+export { SavedObjectsUtils } from './utils';
diff --git a/src/core/server/saved_objects/service/lib/namespace.test.ts b/src/core/server/saved_objects/service/lib/namespace.test.ts
deleted file mode 100644
index c5818deb8ef2a..0000000000000
--- a/src/core/server/saved_objects/service/lib/namespace.test.ts
+++ /dev/null
@@ -1,53 +0,0 @@
-/*
- * Licensed to Elasticsearch B.V. under one or more contributor
- * license agreements. See the NOTICE file distributed with
- * this work for additional information regarding copyright
- * ownership. Elasticsearch B.V. licenses this file to you under
- * the Apache License, Version 2.0 (the "License"); you may
- * not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied. See the License for the
- * specific language governing permissions and limitations
- * under the License.
- */
-
-import { namespaceIdToString, namespaceStringToId } from './namespace';
-
-describe('#namespaceIdToString', () => {
- it('converts `undefined` to default namespace string', () => {
- expect(namespaceIdToString(undefined)).toEqual('default');
- });
-
- it('leaves other namespace IDs as-is', () => {
- expect(namespaceIdToString('foo')).toEqual('foo');
- });
-
- it('throws an error when a namespace ID is an empty string', () => {
- expect(() => namespaceIdToString('')).toThrowError('namespace cannot be an empty string');
- });
-});
-
-describe('#namespaceStringToId', () => {
- it('converts default namespace string to `undefined`', () => {
- expect(namespaceStringToId('default')).toBeUndefined();
- });
-
- it('leaves other namespace strings as-is', () => {
- expect(namespaceStringToId('foo')).toEqual('foo');
- });
-
- it('throws an error when a namespace string is falsy', () => {
- const test = (arg: any) =>
- expect(() => namespaceStringToId(arg)).toThrowError('namespace must be a non-empty string');
-
- test(undefined);
- test(null);
- test('');
- });
-});
diff --git a/src/core/server/saved_objects/service/lib/namespace.ts b/src/core/server/saved_objects/service/lib/namespace.ts
deleted file mode 100644
index bc0691c0bb1db..0000000000000
--- a/src/core/server/saved_objects/service/lib/namespace.ts
+++ /dev/null
@@ -1,48 +0,0 @@
-/*
- * Licensed to Elasticsearch B.V. under one or more contributor
- * license agreements. See the NOTICE file distributed with
- * this work for additional information regarding copyright
- * ownership. Elasticsearch B.V. licenses this file to you under
- * the Apache License, Version 2.0 (the "License"); you may
- * not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
- * KIND, either express or implied. See the License for the
- * specific language governing permissions and limitations
- * under the License.
- */
-
-export const DEFAULT_NAMESPACE_STRING = 'default';
-
-/**
- * Converts a given saved object namespace ID to its string representation. All namespace IDs have an identical string representation, with
- * the exception of the `undefined` namespace ID (which has a namespace string of `'default'`).
- *
- * @param namespace The namespace ID, which must be either a non-empty string or `undefined`.
- */
-export const namespaceIdToString = (namespace?: string) => {
- if (namespace === '') {
- throw new TypeError('namespace cannot be an empty string');
- }
-
- return namespace ?? DEFAULT_NAMESPACE_STRING;
-};
-
-/**
- * Converts a given saved object namespace string to its ID representation. All namespace strings have an identical ID representation, with
- * the exception of the `'default'` namespace string (which has a namespace ID of `undefined`).
- *
- * @param namespace The namespace string, which must be non-empty.
- */
-export const namespaceStringToId = (namespace: string) => {
- if (!namespace) {
- throw new TypeError('namespace must be a non-empty string');
- }
-
- return namespace !== DEFAULT_NAMESPACE_STRING ? namespace : undefined;
-};
diff --git a/src/core/server/saved_objects/service/lib/repository.ts b/src/core/server/saved_objects/service/lib/repository.ts
index c73dec7797801..9cb0d415e25a9 100644
--- a/src/core/server/saved_objects/service/lib/repository.ts
+++ b/src/core/server/saved_objects/service/lib/repository.ts
@@ -67,7 +67,7 @@ import {
} from '../../types';
import { SavedObjectTypeRegistry } from '../../saved_objects_type_registry';
import { validateConvertFilterToKueryNode } from './filter_utils';
-import { namespaceIdToString, namespaceStringToId } from './namespace';
+import { SavedObjectsUtils } from './utils';
// BEWARE: The SavedObjectClient depends on the implementation details of the SavedObjectsRepository
// so any breaking changes to this repository are considered breaking changes to the SavedObjectsClient.
@@ -563,7 +563,7 @@ export class SavedObjectsRepository {
preflightResult = await this.preflightCheckIncludesNamespace(type, id, namespace);
const existingNamespaces = getSavedObjectNamespaces(undefined, preflightResult);
const remainingNamespaces = existingNamespaces?.filter(
- (x) => x !== namespaceIdToString(namespace)
+ (x) => x !== SavedObjectsUtils.namespaceIdToString(namespace)
);
if (remainingNamespaces?.length) {
@@ -887,7 +887,9 @@ export class SavedObjectsRepository {
const { originId, updated_at: updatedAt } = doc._source;
let namespaces = [];
if (!this._registry.isNamespaceAgnostic(type)) {
- namespaces = doc._source.namespaces ?? [namespaceIdToString(doc._source.namespace)];
+ namespaces = doc._source.namespaces ?? [
+ SavedObjectsUtils.namespaceIdToString(doc._source.namespace),
+ ];
}
return {
@@ -944,7 +946,9 @@ export class SavedObjectsRepository {
let namespaces: string[] = [];
if (!this._registry.isNamespaceAgnostic(type)) {
- namespaces = body._source.namespaces ?? [namespaceIdToString(body._source.namespace)];
+ namespaces = body._source.namespaces ?? [
+ SavedObjectsUtils.namespaceIdToString(body._source.namespace),
+ ];
}
return {
@@ -1020,7 +1024,9 @@ export class SavedObjectsRepository {
const { originId } = body.get._source;
let namespaces = [];
if (!this._registry.isNamespaceAgnostic(type)) {
- namespaces = body.get._source.namespaces ?? [namespaceIdToString(body.get._source.namespace)];
+ namespaces = body.get._source.namespaces ?? [
+ SavedObjectsUtils.namespaceIdToString(body.get._source.namespace),
+ ];
}
return {
@@ -1257,9 +1263,11 @@ export class SavedObjectsRepository {
});
const getNamespaceId = (objectNamespace?: string) =>
- objectNamespace !== undefined ? namespaceStringToId(objectNamespace) : namespace;
+ objectNamespace !== undefined
+ ? SavedObjectsUtils.namespaceStringToId(objectNamespace)
+ : namespace;
const getNamespaceString = (objectNamespace?: string) =>
- objectNamespace ?? namespaceIdToString(namespace);
+ objectNamespace ?? SavedObjectsUtils.namespaceIdToString(namespace);
const bulkGetDocs = expectedBulkGetResults
.filter(isRight)
@@ -1319,7 +1327,7 @@ export class SavedObjectsRepository {
};
}
namespaces = actualResult._source.namespaces ?? [
- namespaceIdToString(actualResult._source.namespace),
+ SavedObjectsUtils.namespaceIdToString(actualResult._source.namespace),
];
versionProperties = getExpectedVersionProperties(version, actualResult);
} else {
@@ -1522,7 +1530,7 @@ export class SavedObjectsRepository {
const savedObject = this._serializer.rawToSavedObject(raw);
const { namespace, type } = savedObject;
if (this._registry.isSingleNamespace(type)) {
- savedObject.namespaces = [namespaceIdToString(namespace)];
+ savedObject.namespaces = [SavedObjectsUtils.namespaceIdToString(namespace)];
}
return omit(savedObject, 'namespace') as SavedObject;
}
@@ -1545,7 +1553,7 @@ export class SavedObjectsRepository {
}
const namespaces = raw._source.namespaces;
- return namespaces?.includes(namespaceIdToString(namespace)) ?? false;
+ return namespaces?.includes(SavedObjectsUtils.namespaceIdToString(namespace)) ?? false;
}
/**
@@ -1665,7 +1673,7 @@ function getSavedObjectNamespaces(
if (document) {
return document._source?.namespaces;
}
- return [namespaceIdToString(namespace)];
+ return [SavedObjectsUtils.namespaceIdToString(namespace)];
}
/**
@@ -1673,7 +1681,7 @@ function getSavedObjectNamespaces(
* This allows `'default'` to be used interchangeably with `undefined`.
*/
const normalizeNamespace = (namespace?: string) =>
- namespace === undefined ? namespace : namespaceStringToId(namespace);
+ namespace === undefined ? namespace : SavedObjectsUtils.namespaceStringToId(namespace);
/**
* Extracts the contents of a decorated error to return the attributes for bulk operations.
diff --git a/src/core/server/saved_objects/service/lib/search_dsl/query_params.ts b/src/core/server/saved_objects/service/lib/search_dsl/query_params.ts
index 42fcc0b351fbc..3ff72a86c2f89 100644
--- a/src/core/server/saved_objects/service/lib/search_dsl/query_params.ts
+++ b/src/core/server/saved_objects/service/lib/search_dsl/query_params.ts
@@ -21,7 +21,7 @@ import { esKuery, KueryNode } from '../../../../../../plugins/data/server';
import { getRootPropertiesObjects, IndexMapping } from '../../../mappings';
import { ISavedObjectTypeRegistry } from '../../../saved_objects_type_registry';
-import { DEFAULT_NAMESPACE_STRING } from '../namespace';
+import { DEFAULT_NAMESPACE_STRING } from '../utils';
/**
* Gets the types based on the type. Uses mappings to support
diff --git a/src/core/server/saved_objects/service/lib/utils.test.ts b/src/core/server/saved_objects/service/lib/utils.test.ts
new file mode 100644
index 0000000000000..ecb0c695f0883
--- /dev/null
+++ b/src/core/server/saved_objects/service/lib/utils.test.ts
@@ -0,0 +1,57 @@
+/*
+ * Licensed to Elasticsearch B.V. under one or more contributor
+ * license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright
+ * ownership. Elasticsearch B.V. licenses this file to you under
+ * the Apache License, Version 2.0 (the "License"); you may
+ * not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+import { SavedObjectsUtils } from './utils';
+
+describe('#SavedObjectsUtils', () => {
+ const { namespaceIdToString, namespaceStringToId } = SavedObjectsUtils;
+
+ describe('#namespaceIdToString', () => {
+ it('converts `undefined` to default namespace string', () => {
+ expect(namespaceIdToString(undefined)).toEqual('default');
+ });
+
+ it('leaves other namespace IDs as-is', () => {
+ expect(namespaceIdToString('foo')).toEqual('foo');
+ });
+
+ it('throws an error when a namespace ID is an empty string', () => {
+ expect(() => namespaceIdToString('')).toThrowError('namespace cannot be an empty string');
+ });
+ });
+
+ describe('#namespaceStringToId', () => {
+ it('converts default namespace string to `undefined`', () => {
+ expect(namespaceStringToId('default')).toBeUndefined();
+ });
+
+ it('leaves other namespace strings as-is', () => {
+ expect(namespaceStringToId('foo')).toEqual('foo');
+ });
+
+ it('throws an error when a namespace string is falsy', () => {
+ const test = (arg: any) =>
+ expect(() => namespaceStringToId(arg)).toThrowError('namespace must be a non-empty string');
+
+ test(undefined);
+ test(null);
+ test('');
+ });
+ });
+});
diff --git a/src/core/server/saved_objects/service/lib/utils.ts b/src/core/server/saved_objects/service/lib/utils.ts
new file mode 100644
index 0000000000000..6101ad57cc401
--- /dev/null
+++ b/src/core/server/saved_objects/service/lib/utils.ts
@@ -0,0 +1,53 @@
+/*
+ * Licensed to Elasticsearch B.V. under one or more contributor
+ * license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright
+ * ownership. Elasticsearch B.V. licenses this file to you under
+ * the Apache License, Version 2.0 (the "License"); you may
+ * not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+export const DEFAULT_NAMESPACE_STRING = 'default';
+
+/**
+ * @public
+ */
+export class SavedObjectsUtils {
+ /**
+ * Converts a given saved object namespace ID to its string representation. All namespace IDs have an identical string representation, with
+ * the exception of the `undefined` namespace ID (which has a namespace string of `'default'`).
+ *
+ * @param namespace The namespace ID, which must be either a non-empty string or `undefined`.
+ */
+ public static namespaceIdToString = (namespace?: string) => {
+ if (namespace === '') {
+ throw new TypeError('namespace cannot be an empty string');
+ }
+
+ return namespace ?? DEFAULT_NAMESPACE_STRING;
+ };
+
+ /**
+ * Converts a given saved object namespace string to its ID representation. All namespace strings have an identical ID representation, with
+ * the exception of the `'default'` namespace string (which has a namespace ID of `undefined`).
+ *
+ * @param namespace The namespace string, which must be non-empty.
+ */
+ public static namespaceStringToId = (namespace: string) => {
+ if (!namespace) {
+ throw new TypeError('namespace must be a non-empty string');
+ }
+
+ return namespace !== DEFAULT_NAMESPACE_STRING ? namespace : undefined;
+ };
+}
diff --git a/src/core/server/server.api.md b/src/core/server/server.api.md
index 337ccf336741f..8d651e3245771 100644
--- a/src/core/server/server.api.md
+++ b/src/core/server/server.api.md
@@ -1599,12 +1599,6 @@ export function modifyUrl(url: string, urlModifier: (urlParts: URLMeaningfulPart
// @public
export type MutatingOperationRefreshSetting = boolean | 'wait_for';
-// @public
-export const namespaceIdToString: (namespace?: string | undefined) => string;
-
-// @public
-export const namespaceStringToId: (namespace: string) => string | undefined;
-
// Warning: (ae-missing-release-tag) "NodesVersionCompatibility" is exported by the package, but it is missing a release tag (@alpha, @beta, @public, or @internal)
//
// @public (undocumented)
@@ -2717,6 +2711,12 @@ export interface SavedObjectsUpdateResponse extends Omit string;
+ static namespaceStringToId: (namespace: string) => string | undefined;
+}
+
// @public
export class SavedObjectTypeRegistry {
getAllTypes(): SavedObjectsType[];
diff --git a/x-pack/plugins/spaces/server/lib/utils/__mocks__/index.ts b/x-pack/plugins/spaces/server/lib/utils/__mocks__/index.ts
index 2bb23d0304858..2b93e6d87a7af 100644
--- a/x-pack/plugins/spaces/server/lib/utils/__mocks__/index.ts
+++ b/x-pack/plugins/spaces/server/lib/utils/__mocks__/index.ts
@@ -7,8 +7,10 @@
const mockNamespaceIdToString = jest.fn();
const mockNamespaceStringToId = jest.fn();
jest.mock('../../../../../../../src/core/server', () => ({
- namespaceIdToString: mockNamespaceIdToString,
- namespaceStringToId: mockNamespaceStringToId,
+ SavedObjectsUtils: {
+ namespaceIdToString: mockNamespaceIdToString,
+ namespaceStringToId: mockNamespaceStringToId,
+ },
}));
export { mockNamespaceIdToString, mockNamespaceStringToId };
diff --git a/x-pack/plugins/spaces/server/lib/utils/namespace.ts b/x-pack/plugins/spaces/server/lib/utils/namespace.ts
index 6861d0141ea02..344da18846f3b 100644
--- a/x-pack/plugins/spaces/server/lib/utils/namespace.ts
+++ b/x-pack/plugins/spaces/server/lib/utils/namespace.ts
@@ -4,7 +4,7 @@
* you may not use this file except in compliance with the Elastic License.
*/
-import { namespaceStringToId, namespaceIdToString } from '../../../../../../src/core/server';
+import { SavedObjectsUtils } from '../../../../../../src/core/server';
/**
* Converts a Space ID string to its namespace ID representation. Note that a Space ID string is equivalent to a namespace string.
@@ -12,7 +12,7 @@ import { namespaceStringToId, namespaceIdToString } from '../../../../../../src/
* See also: {@link namespaceStringToId}.
*/
export function spaceIdToNamespace(spaceId: string) {
- return namespaceStringToId(spaceId);
+ return SavedObjectsUtils.namespaceStringToId(spaceId);
}
/**
@@ -21,5 +21,5 @@ export function spaceIdToNamespace(spaceId: string) {
* See also: {@link namespaceIdToString}.
*/
export function namespaceToSpaceId(namespace?: string) {
- return namespaceIdToString(namespace);
+ return SavedObjectsUtils.namespaceIdToString(namespace);
}