Skip to content

Commit

Permalink
Don't expose core namespace conversion methods in the public contract
Browse files Browse the repository at this point in the history
This partially reverts commit fa6ae1c.
  • Loading branch information
jportner committed Aug 31, 2020
1 parent 440d987 commit 0b2f62c
Show file tree
Hide file tree
Showing 13 changed files with 53 additions and 87 deletions.
2 changes: 0 additions & 2 deletions docs/development/core/server/kibana-plugin-core-server.md
Original file line number Diff line number Diff line change
Expand Up @@ -224,8 +224,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 <code>KibanaResponse</code> 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 <code>undefined</code> namespace ID (which has a namespace string of <code>'default'</code>). |
| [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 <code>'default'</code> namespace string (which has a namespace ID of <code>undefined</code>). |
| [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 |

Expand Down

This file was deleted.

This file was deleted.

2 changes: 0 additions & 2 deletions src/core/server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -303,8 +303,6 @@ export {
exportSavedObjectsToStream,
importSavedObjectsFromStream,
resolveSavedObjectsImportErrors,
namespaceIdToString,
namespaceStringToId,
} from './saved_objects';

export {
Expand Down
2 changes: 0 additions & 2 deletions src/core/server/saved_objects/service/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,6 @@ export {
SavedObjectsErrorHelpers,
SavedObjectsClientFactory,
SavedObjectsClientFactoryProvider,
namespaceIdToString,
namespaceStringToId,
} from './lib';

export * from './saved_objects_client';
2 changes: 0 additions & 2 deletions src/core/server/saved_objects/service/lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,3 @@ export {
} from './scoped_client_provider';

export { SavedObjectsErrorHelpers } from './errors';

export { namespaceIdToString, namespaceStringToId } from './namespace';
6 changes: 0 additions & 6 deletions src/core/server/server.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -1596,12 +1596,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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import { copySavedObjectsToSpacesFactory } from './copy_to_spaces';

jest.mock('../../../../../../src/core/server', () => {
return {
...(jest.requireActual('../../../../../../src/core/server') as Record<string, unknown>),
exportSavedObjectsToStream: jest.fn(),
importSavedObjectsFromStream: jest.fn(),
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import { resolveCopySavedObjectsToSpacesConflictsFactory } from './resolve_copy_

jest.mock('../../../../../../src/core/server', () => {
return {
...(jest.requireActual('../../../../../../src/core/server') as Record<string, unknown>),
exportSavedObjectsToStream: jest.fn(),
resolveSavedObjectsImportErrors: jest.fn(),
};
Expand Down
14 changes: 0 additions & 14 deletions x-pack/plugins/spaces/server/lib/utils/__mocks__/index.ts

This file was deleted.

46 changes: 31 additions & 15 deletions x-pack/plugins/spaces/server/lib/utils/namespace.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,29 +4,45 @@
* you may not use this file except in compliance with the Elastic License.
*/

import { mockNamespaceIdToString, mockNamespaceStringToId } from './__mocks__';
import { DEFAULT_SPACE_ID } from '../../../common/constants';
import { spaceIdToNamespace, namespaceToSpaceId } from './namespace';

beforeEach(() => {
jest.clearAllMocks();
});

describe('#spaceIdToNamespace', () => {
it('returns result of namespaceStringToId', () => {
mockNamespaceStringToId.mockReturnValue('bar');
it('converts the default space to undefined', () => {
expect(spaceIdToNamespace(DEFAULT_SPACE_ID)).toBeUndefined();
});

it('returns non-default spaces as-is', () => {
expect(spaceIdToNamespace('foo')).toEqual('foo');
});

const result = spaceIdToNamespace('foo');
expect(mockNamespaceStringToId).toHaveBeenCalledWith('foo');
expect(result).toEqual('bar');
it('throws an error when a spaceId is not provided', () => {
// @ts-ignore ts knows this isn't right
expect(() => spaceIdToNamespace()).toThrowErrorMatchingInlineSnapshot(`"spaceId is required"`);

// @ts-ignore ts knows this isn't right
expect(() => spaceIdToNamespace(null)).toThrowErrorMatchingInlineSnapshot(
`"spaceId is required"`
);

expect(() => spaceIdToNamespace('')).toThrowErrorMatchingInlineSnapshot(
`"spaceId is required"`
);
});
});

describe('#namespaceToSpaceId', () => {
it('returns result of namespaceIdToString', () => {
mockNamespaceIdToString.mockReturnValue('bar');
it('returns the default space id for undefined namespaces', () => {
expect(namespaceToSpaceId(undefined)).toEqual(DEFAULT_SPACE_ID);
});

it('returns all other namespaces as-is', () => {
expect(namespaceToSpaceId('foo')).toEqual('foo');
});

const result = namespaceToSpaceId('foo');
expect(mockNamespaceIdToString).toHaveBeenCalledWith('foo');
expect(result).toEqual('bar');
it('throws an error when an empty string is provided', () => {
expect(() => namespaceToSpaceId('')).toThrowErrorMatchingInlineSnapshot(
`"namespace cannot be an empty string"`
);
});
});
36 changes: 21 additions & 15 deletions x-pack/plugins/spaces/server/lib/utils/namespace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,28 @@
* you may not use this file except in compliance with the Elastic License.
*/

import { namespaceStringToId, namespaceIdToString } from '../../../../../../src/core/server';
import { DEFAULT_SPACE_ID } from '../../../common/constants';

/**
* Converts a Space ID string to its namespace ID representation. Note that a Space ID string is equivalent to a namespace string.
*
* See also: {@link namespaceStringToId}.
*/
export function spaceIdToNamespace(spaceId: string) {
return namespaceStringToId(spaceId);
export function spaceIdToNamespace(spaceId: string): string | undefined {
if (!spaceId) {
throw new TypeError('spaceId is required');
}

if (spaceId === DEFAULT_SPACE_ID) {
return undefined;
}

return spaceId;
}

/**
* Converts a namespace ID to its Space ID string representation. Note that a Space ID string is equivalent to a namespace string.
*
* See also: {@link namespaceIdToString}.
*/
export function namespaceToSpaceId(namespace?: string) {
return namespaceIdToString(namespace);
export function namespaceToSpaceId(namespace: string | undefined): string {
if (namespace === '') {
throw new TypeError('namespace cannot be an empty string');
}

if (!namespace) {
return DEFAULT_SPACE_ID;
}

return namespace;
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,10 @@ import { securityMock } from '../../../../../security/server/mocks';
import { ObjectType } from '@kbn/config-schema';
jest.mock('../../../../../../../src/core/server', () => {
return {
...(jest.requireActual('../../../../../../../src/core/server') as Record<string, unknown>),
exportSavedObjectsToStream: jest.fn(),
importSavedObjectsFromStream: jest.fn(),
resolveSavedObjectsImportErrors: jest.fn(),
kibanaResponseFactory: jest.requireActual('src/core/server').kibanaResponseFactory,
};
});
import {
Expand Down

0 comments on commit 0b2f62c

Please sign in to comment.