-
Notifications
You must be signed in to change notification settings - Fork 8.6k
[CPS] Add @kbn/cps-server-utils package #254085
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
20 changes: 20 additions & 0 deletions
20
src/platform/packages/shared/kbn-cps-server-utils/README.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| # @kbn/cps-server-utils | ||
|
|
||
| Server-side Cross-Project Search (CPS) utilities. | ||
|
|
||
| ## `getSpaceNPRE` | ||
|
|
||
| Returns the Named Project Routing Expression (NPRE) for a given space, using the convention `kibana_space_${spaceId}_default`. | ||
|
|
||
| Accepts either a `spaceId` string or a `KibanaRequest` (from which the space is derived via the request URL path, without a dependency on the `spaces` plugin). | ||
|
|
||
| ```ts | ||
| import { getSpaceNPRE } from '@kbn/cps-server-utils'; | ||
|
|
||
| // From a space ID string | ||
| getSpaceNPRE('my-space'); // 'kibana_space_my-space_default' | ||
| getSpaceNPRE(''); // 'kibana_space_default_default' | ||
|
|
||
| // From a KibanaRequest (extracts space from the URL path) | ||
| getSpaceNPRE(request); // e.g. 'kibana_space_my-space_default' | ||
| ``` |
10 changes: 10 additions & 0 deletions
10
src/platform/packages/shared/kbn-cps-server-utils/index.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| /* | ||
| * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
| * or more contributor license agreements. Licensed under the "Elastic License | ||
| * 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side | ||
| * Public License v 1"; you may not use this file except in compliance with, at | ||
| * your election, the "Elastic License 2.0", the "GNU Affero General Public | ||
| * License v3.0 only", or the "Server Side Public License, v 1". | ||
| */ | ||
|
|
||
| export { getSpaceNPRE } from './src/get_space_npre'; |
14 changes: 14 additions & 0 deletions
14
src/platform/packages/shared/kbn-cps-server-utils/jest.config.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| /* | ||
| * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
| * or more contributor license agreements. Licensed under the "Elastic License | ||
| * 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side | ||
| * Public License v 1"; you may not use this file except in compliance with, at | ||
| * your election, the "Elastic License 2.0", the "GNU Affero General Public | ||
| * License v3.0 only", or the "Server Side Public License, v 1". | ||
| */ | ||
|
|
||
| module.exports = { | ||
| preset: '@kbn/test/jest_node', | ||
| rootDir: '../../../../..', | ||
| roots: ['<rootDir>/src/platform/packages/shared/kbn-cps-server-utils'], | ||
| }; |
9 changes: 9 additions & 0 deletions
9
src/platform/packages/shared/kbn-cps-server-utils/kibana.jsonc
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| { | ||
| "type": "shared-server", | ||
| "id": "@kbn/cps-server-utils", | ||
| "owner": [ | ||
| "@elastic/kibana-core" | ||
| ], | ||
| "group": "platform", | ||
| "visibility": "shared" | ||
| } |
46 changes: 46 additions & 0 deletions
46
src/platform/packages/shared/kbn-cps-server-utils/moon.yml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,46 @@ | ||
| # This file is generated by the @kbn/moon package. Any manual edits will be erased! | ||
| # To extend this, write your extensions/overrides to 'moon.extend.yml' | ||
| # then regenerate this file with: 'node scripts/regenerate_moon_projects.js --update --filter @kbn/cps-server-utils' | ||
|
|
||
| $schema: https://moonrepo.dev/schemas/project.json | ||
| id: '@kbn/cps-server-utils' | ||
| type: unknown | ||
| owners: | ||
| defaultOwner: '@elastic/kibana-core' | ||
| toolchain: | ||
| default: node | ||
| language: typescript | ||
| project: | ||
| name: '@kbn/cps-server-utils' | ||
| description: Moon project for @kbn/cps-server-utils | ||
| channel: '' | ||
| owner: '@elastic/kibana-core' | ||
| metadata: | ||
| sourceRoot: src/platform/packages/shared/kbn-cps-server-utils | ||
| dependsOn: | ||
| - '@kbn/core-http-server' | ||
| - '@kbn/spaces-utils' | ||
| tags: | ||
| - shared-server | ||
| - package | ||
| - prod | ||
| - group-platform | ||
| - shared | ||
| - jest-unit-tests | ||
| fileGroups: | ||
| src: | ||
| - '**/*' | ||
| - '!target/**/*' | ||
| tasks: | ||
| jest: | ||
| args: | ||
| - '--config' | ||
| - $projectRoot/jest.config.js | ||
| inputs: | ||
| - '@group(src)' | ||
| jestCI: | ||
| args: | ||
| - '--config' | ||
| - $projectRoot/jest.config.js | ||
| inputs: | ||
| - '@group(src)' |
7 changes: 7 additions & 0 deletions
7
src/platform/packages/shared/kbn-cps-server-utils/package.json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| { | ||
| "name": "@kbn/cps-server-utils", | ||
| "version": "1.0.0", | ||
| "private": true, | ||
| "license": "Elastic License 2.0 OR AGPL-3.0-only OR SSPL-1.0", | ||
| "sideEffects": false | ||
| } |
42 changes: 42 additions & 0 deletions
42
src/platform/packages/shared/kbn-cps-server-utils/src/get_space_npre.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| /* | ||
| * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
| * or more contributor license agreements. Licensed under the "Elastic License | ||
| * 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side | ||
| * Public License v 1"; you may not use this file except in compliance with, at | ||
| * your election, the "Elastic License 2.0", the "GNU Affero General Public | ||
| * License v3.0 only", or the "Server Side Public License, v 1". | ||
| */ | ||
|
|
||
| import type { KibanaRequest } from '@kbn/core-http-server'; | ||
| import { getSpaceNPRE } from './get_space_npre'; | ||
|
|
||
| const mockRequest = (pathname: string) => | ||
| ({ url: new URL(`http://localhost:5601${pathname}`) } as unknown as KibanaRequest); | ||
|
|
||
| describe('getSpaceNPRE', () => { | ||
| describe('when called with a spaceId string', () => { | ||
| it('returns the NPRE for the given space', () => { | ||
| expect(getSpaceNPRE('my-space')).toBe('kibana_space_my-space_default'); | ||
| }); | ||
|
|
||
| it('uses "default" when spaceId is an empty string', () => { | ||
| expect(getSpaceNPRE('')).toBe('kibana_space_default_default'); | ||
| }); | ||
|
|
||
| it('returns the NPRE for the default space when spaceId is "default"', () => { | ||
| expect(getSpaceNPRE('default')).toBe('kibana_space_default_default'); | ||
| }); | ||
| }); | ||
|
|
||
| describe('when called with a KibanaRequest', () => { | ||
| it('extracts the space from the request URL and returns the NPRE', () => { | ||
| expect(getSpaceNPRE(mockRequest('/s/my-space/api/foo'))).toBe( | ||
| 'kibana_space_my-space_default' | ||
| ); | ||
| }); | ||
|
|
||
| it('returns the default space NPRE when the request URL has no space segment', () => { | ||
| expect(getSpaceNPRE(mockRequest('/api/foo'))).toBe('kibana_space_default_default'); | ||
| }); | ||
| }); | ||
| }); |
20 changes: 20 additions & 0 deletions
20
src/platform/packages/shared/kbn-cps-server-utils/src/get_space_npre.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| /* | ||
| * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
| * or more contributor license agreements. Licensed under the "Elastic License | ||
| * 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side | ||
| * Public License v 1"; you may not use this file except in compliance with, at | ||
| * your election, the "Elastic License 2.0", the "GNU Affero General Public | ||
| * License v3.0 only", or the "Server Side Public License, v 1". | ||
| */ | ||
|
|
||
| import type { KibanaRequest } from '@kbn/core-http-server'; | ||
| import { DEFAULT_SPACE_ID, getSpaceIdFromPath } from '@kbn/spaces-utils'; | ||
|
|
||
| export function getSpaceNPRE(spaceIdOrRequest: string | KibanaRequest): string { | ||
| const spaceId = | ||
| typeof spaceIdOrRequest === 'string' | ||
| ? spaceIdOrRequest || DEFAULT_SPACE_ID | ||
| : getSpaceIdFromPath(spaceIdOrRequest.url.pathname).spaceId; | ||
|
|
||
| return `kibana_space_${spaceId}_default`; | ||
| } | ||
17 changes: 17 additions & 0 deletions
17
src/platform/packages/shared/kbn-cps-server-utils/tsconfig.json
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| { | ||
| "extends": "@kbn/tsconfig-base/tsconfig.json", | ||
| "compilerOptions": { | ||
| "outDir": "target/types", | ||
| }, | ||
| "include": [ | ||
| "**/*", | ||
| "../../../../../typings/**/*", | ||
| ], | ||
| "kbn_references": [ | ||
| "@kbn/core-http-server", | ||
| "@kbn/spaces-utils", | ||
| ], | ||
| "exclude": [ | ||
| "target/**/*" | ||
| ] | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: can you add TSDocs?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Will add them in a follow-up PR, I don't want to retrigger a whole CI just for that.