diff --git a/x-pack/plugins/spaces/index.js b/x-pack/plugins/spaces/index.js index 3eafab6461f9e..2679a894a6661 100644 --- a/x-pack/plugins/spaces/index.js +++ b/x-pack/plugins/spaces/index.js @@ -14,6 +14,7 @@ import { createDefaultSpace } from './server/lib/create_default_space'; import { createSpacesService } from './server/lib/create_spaces_service'; import { getActiveSpace } from './server/lib/get_active_space'; import { getSpacesUsageCollector } from './server/lib/get_spaces_usage_collector'; +import { createSpacesTutorialContextFactory } from './server/lib/spaces_tutorial_context_factory'; import { wrapError } from './server/lib/errors'; import mappings from './mappings.json'; import { spacesSavedObjectsClientWrapperFactory } from './server/lib/saved_objects_client/saved_objects_client_wrapper_factory'; @@ -94,6 +95,10 @@ export const spaces = (kibana) => new kibana.Plugin({ spacesSavedObjectsClientWrapperFactory(spacesService) ); + server.addScopedTutorialContextFactory( + createSpacesTutorialContextFactory(spacesService) + ); + initPrivateApis(server); initPublicSpacesApi(server); diff --git a/x-pack/plugins/spaces/server/lib/create_spaces_service.js b/x-pack/plugins/spaces/server/lib/create_spaces_service.ts similarity index 75% rename from x-pack/plugins/spaces/server/lib/create_spaces_service.js rename to x-pack/plugins/spaces/server/lib/create_spaces_service.ts index 3ee55354da8dc..3269142a9cf17 100644 --- a/x-pack/plugins/spaces/server/lib/create_spaces_service.js +++ b/x-pack/plugins/spaces/server/lib/create_spaces_service.ts @@ -6,13 +6,16 @@ import { getSpaceIdFromPath } from './spaces_url_parser'; -export function createSpacesService(server) { +export interface SpacesService { + getSpaceId: (req: any) => string; +} +export function createSpacesService(server: any): SpacesService { const serverBasePath = server.config().get('server.basePath'); const contextCache = new WeakMap(); - function getSpaceId(request) { + function getSpaceId(request: any) { if (!contextCache.has(request)) { populateCache(request); } @@ -21,11 +24,11 @@ export function createSpacesService(server) { return spaceId; } - function populateCache(request) { + function populateCache(request: any) { const spaceId = getSpaceIdFromPath(request.getBasePath(), serverBasePath); contextCache.set(request, { - spaceId + spaceId, }); } diff --git a/x-pack/plugins/spaces/server/lib/spaces_tutorial_context_factory.test.ts b/x-pack/plugins/spaces/server/lib/spaces_tutorial_context_factory.test.ts new file mode 100644 index 0000000000000..4ed548d64b574 --- /dev/null +++ b/x-pack/plugins/spaces/server/lib/spaces_tutorial_context_factory.test.ts @@ -0,0 +1,55 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License; + * you may not use this file except in compliance with the Elastic License. + */ + +import { DEFAULT_SPACE_ID } from '../../common/constants'; +import { createSpacesService } from './create_spaces_service'; +import { createSpacesTutorialContextFactory } from './spaces_tutorial_context_factory'; + +const server = { + config: () => { + return { + get: (key: string) => { + if (key === 'server.basePath') { + return '/foo'; + } + throw new Error('unexpected key ' + key); + }, + }; + }, +}; + +describe('createSpacesTutorialContextFactory', () => { + it('should create a valid context factory', () => { + const spacesService = createSpacesService(server); + expect(typeof createSpacesTutorialContextFactory(spacesService)).toEqual('function'); + }); + + it('should create context with the current space id for space my-space-id', () => { + const spacesService = createSpacesService(server); + const contextFactory = createSpacesTutorialContextFactory(spacesService); + + const request = { + getBasePath: () => '/foo/s/my-space-id', + }; + + expect(contextFactory(request)).toEqual({ + spaceId: 'my-space-id', + }); + }); + + it('should create context with the current space id for the default space', () => { + const spacesService = createSpacesService(server); + const contextFactory = createSpacesTutorialContextFactory(spacesService); + + const request = { + getBasePath: () => '/foo', + }; + + expect(contextFactory(request)).toEqual({ + spaceId: DEFAULT_SPACE_ID, + }); + }); +}); diff --git a/x-pack/plugins/spaces/server/lib/spaces_tutorial_context_factory.ts b/x-pack/plugins/spaces/server/lib/spaces_tutorial_context_factory.ts new file mode 100644 index 0000000000000..b3254fd3b3c07 --- /dev/null +++ b/x-pack/plugins/spaces/server/lib/spaces_tutorial_context_factory.ts @@ -0,0 +1,15 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License; + * you may not use this file except in compliance with the Elastic License. + */ + +import { SpacesService } from './create_spaces_service'; + +export function createSpacesTutorialContextFactory(spacesService: SpacesService) { + return function spacesTutorialContextFactory(request: any) { + return { + spaceId: spacesService.getSpaceId(request), + }; + }; +}