diff --git a/.github/CODEOWNERS b/.github/CODEOWNERS index c6beae627cb53..cbffef0027017 100644 --- a/.github/CODEOWNERS +++ b/.github/CODEOWNERS @@ -629,6 +629,7 @@ test/common/plugins/newsfeed @elastic/kibana-core src/plugins/no_data_page @elastic/appex-sharedux x-pack/plugins/notifications @elastic/appex-sharedux packages/kbn-object-versioning @elastic/appex-sharedux +packages/kbn-object-versioning-utils @elastic/appex-sharedux x-pack/plugins/observability_solution/observability_ai_assistant_app @elastic/obs-ai-assistant x-pack/plugins/observability_solution/observability_ai_assistant_management @elastic/obs-ai-assistant x-pack/plugins/observability_solution/observability_ai_assistant @elastic/obs-ai-assistant diff --git a/package.json b/package.json index ce1f760416eb0..3bcbda6c44346 100644 --- a/package.json +++ b/package.json @@ -662,6 +662,7 @@ "@kbn/no-data-page-plugin": "link:src/plugins/no_data_page", "@kbn/notifications-plugin": "link:x-pack/plugins/notifications", "@kbn/object-versioning": "link:packages/kbn-object-versioning", + "@kbn/object-versioning-utils": "link:packages/kbn-object-versioning-utils", "@kbn/observability-ai-assistant-app-plugin": "link:x-pack/plugins/observability_solution/observability_ai_assistant_app", "@kbn/observability-ai-assistant-management-plugin": "link:x-pack/plugins/observability_solution/observability_ai_assistant_management", "@kbn/observability-ai-assistant-plugin": "link:x-pack/plugins/observability_solution/observability_ai_assistant", diff --git a/packages/kbn-object-versioning-utils/README.md b/packages/kbn-object-versioning-utils/README.md new file mode 100644 index 0000000000000..65b4bdd1206ec --- /dev/null +++ b/packages/kbn-object-versioning-utils/README.md @@ -0,0 +1,3 @@ +# @kbn/object-versioning-utils + +Exports common utils from `@kbn/object-versioning` that are safe to be used both in the server and the browser. diff --git a/packages/kbn-object-versioning-utils/index.ts b/packages/kbn-object-versioning-utils/index.ts new file mode 100644 index 0000000000000..650ac1804edec --- /dev/null +++ b/packages/kbn-object-versioning-utils/index.ts @@ -0,0 +1,9 @@ +/* + * 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 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 or the Server + * Side Public License, v 1. + */ + +export { validateVersion } from './lib/validate_version'; diff --git a/packages/kbn-object-versioning-utils/jest.config.js b/packages/kbn-object-versioning-utils/jest.config.js new file mode 100644 index 0000000000000..2a127f38d568d --- /dev/null +++ b/packages/kbn-object-versioning-utils/jest.config.js @@ -0,0 +1,13 @@ +/* + * 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 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 or the Server + * Side Public License, v 1. + */ + +module.exports = { + preset: '@kbn/test', + rootDir: '../..', + roots: ['/packages/kbn-object-versioning-utils'], +}; diff --git a/packages/kbn-object-versioning-utils/kibana.jsonc b/packages/kbn-object-versioning-utils/kibana.jsonc new file mode 100644 index 0000000000000..deba859afd364 --- /dev/null +++ b/packages/kbn-object-versioning-utils/kibana.jsonc @@ -0,0 +1,5 @@ +{ + "type": "shared-common", + "id": "@kbn/object-versioning-utils", + "owner": "@elastic/appex-sharedux" +} diff --git a/packages/kbn-object-versioning-utils/lib/validate_version.ts b/packages/kbn-object-versioning-utils/lib/validate_version.ts new file mode 100644 index 0000000000000..8941e0ef9fdd1 --- /dev/null +++ b/packages/kbn-object-versioning-utils/lib/validate_version.ts @@ -0,0 +1,39 @@ +/* + * 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 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 or the Server + * Side Public License, v 1. + */ + +// import type { Version } from '@kbn/object-versioning'; +// Declaring it directly to avoid circular dependencies. +type Version = number; + +export const validateVersion = ( + version: unknown +): { result: true; value: Version } | { result: false; value: null } => { + if (typeof version === 'string') { + const isValid = /^\d+$/.test(version); + if (isValid) { + const parsed = parseInt(version, 10); + if (Number.isNaN(parsed)) { + return { result: false, value: null }; + } + return { result: true, value: parsed }; + } + return { result: false, value: null }; + } else { + const isValid = Number.isInteger(version); + if (isValid) { + return { + result: true, + value: version as Version, + }; + } + return { + result: false, + value: null, + }; + } +}; diff --git a/packages/kbn-object-versioning-utils/package.json b/packages/kbn-object-versioning-utils/package.json new file mode 100644 index 0000000000000..f608c88ab44aa --- /dev/null +++ b/packages/kbn-object-versioning-utils/package.json @@ -0,0 +1,6 @@ +{ + "name": "@kbn/object-versioning-utils", + "private": true, + "version": "1.0.0", + "license": "SSPL-1.0 OR Elastic License 2.0" +} \ No newline at end of file diff --git a/packages/kbn-object-versioning-utils/tsconfig.json b/packages/kbn-object-versioning-utils/tsconfig.json new file mode 100644 index 0000000000000..a9e9e3ad3baac --- /dev/null +++ b/packages/kbn-object-versioning-utils/tsconfig.json @@ -0,0 +1,20 @@ +{ + "extends": "../../tsconfig.base.json", + "compilerOptions": { + "outDir": "target/types", + "types": [ + "jest", + "node", + "react" + ] + }, + "include": [ + "**/*.ts", + "**/*.tsx", + ], + "exclude": [ + "target/**/*" + ], + "kbn_references": [ + ] +} diff --git a/packages/kbn-object-versioning/kibana.jsonc b/packages/kbn-object-versioning/kibana.jsonc index c79023fdec49d..00f87ac804f47 100644 --- a/packages/kbn-object-versioning/kibana.jsonc +++ b/packages/kbn-object-versioning/kibana.jsonc @@ -1,5 +1,5 @@ { - "type": "shared-common", + "type": "shared-server", "id": "@kbn/object-versioning", "owner": "@elastic/appex-sharedux" } diff --git a/packages/kbn-object-versioning/lib/utils.ts b/packages/kbn-object-versioning/lib/utils.ts index e5531578c55bb..2a75bca650de2 100644 --- a/packages/kbn-object-versioning/lib/utils.ts +++ b/packages/kbn-object-versioning/lib/utils.ts @@ -7,7 +7,6 @@ */ import type { Type, ValidationError } from '@kbn/config-schema'; -import { Version } from './types'; /** * Validate an object based on a schema. @@ -29,30 +28,4 @@ export const validateObj = (obj: unknown, objSchema?: Type): ValidationErro } }; -export const validateVersion = ( - version: unknown -): { result: true; value: Version } | { result: false; value: null } => { - if (typeof version === 'string') { - const isValid = /^\d+$/.test(version); - if (isValid) { - const parsed = parseInt(version, 10); - if (Number.isNaN(parsed)) { - return { result: false, value: null }; - } - return { result: true, value: parsed }; - } - return { result: false, value: null }; - } else { - const isValid = Number.isInteger(version); - if (isValid) { - return { - result: true, - value: version as Version, - }; - } - return { - result: false, - value: null, - }; - } -}; +export { validateVersion } from '@kbn/object-versioning-utils'; diff --git a/packages/kbn-object-versioning/tsconfig.json b/packages/kbn-object-versioning/tsconfig.json index 35c4b501af791..42c549aef3ce6 100644 --- a/packages/kbn-object-versioning/tsconfig.json +++ b/packages/kbn-object-versioning/tsconfig.json @@ -18,5 +18,6 @@ "kbn_references": [ "@kbn/config-schema", "@kbn/safer-lodash-set", + "@kbn/object-versioning-utils", ] } diff --git a/src/plugins/content_management/common/rpc/constants.ts b/src/plugins/content_management/common/rpc/constants.ts index 0a04ae308899b..08bc8dcb45152 100644 --- a/src/plugins/content_management/common/rpc/constants.ts +++ b/src/plugins/content_management/common/rpc/constants.ts @@ -6,7 +6,7 @@ * Side Public License, v 1. */ import { schema } from '@kbn/config-schema'; -import { validateVersion } from '@kbn/object-versioning/lib/utils'; +import { validateVersion } from '@kbn/object-versioning-utils'; export const procedureNames = [ 'get', diff --git a/src/plugins/content_management/public/content_client/content_client.tsx b/src/plugins/content_management/public/content_client/content_client.tsx index caf7181472f03..e4c4323ff2e9e 100644 --- a/src/plugins/content_management/public/content_client/content_client.tsx +++ b/src/plugins/content_management/public/content_client/content_client.tsx @@ -7,7 +7,7 @@ */ import { QueryClient } from '@tanstack/react-query'; -import { validateVersion } from '@kbn/object-versioning/lib/utils'; +import { validateVersion } from '@kbn/object-versioning-utils'; import type { Version } from '@kbn/object-versioning'; import { createQueryObservable } from './query_observable'; import type { CrudClient } from '../crud_client'; diff --git a/src/plugins/content_management/public/registry/registry.ts b/src/plugins/content_management/public/registry/registry.ts index 97020f205843f..8f44c7fc42ddc 100644 --- a/src/plugins/content_management/public/registry/registry.ts +++ b/src/plugins/content_management/public/registry/registry.ts @@ -6,7 +6,7 @@ * Side Public License, v 1. */ -import { validateVersion } from '@kbn/object-versioning/lib/utils'; +import { validateVersion } from '@kbn/object-versioning-utils'; import type { ContentTypeDefinition } from './content_type_definition'; import { ContentType } from './content_type'; diff --git a/src/plugins/content_management/tsconfig.json b/src/plugins/content_management/tsconfig.json index fbbe897292ded..c23ac37ec58c1 100644 --- a/src/plugins/content_management/tsconfig.json +++ b/src/plugins/content_management/tsconfig.json @@ -17,6 +17,7 @@ "@kbn/saved-objects-settings", "@kbn/core-http-server", "@kbn/content-management-favorites-server", + "@kbn/object-versioning-utils", ], "exclude": [ "target/**/*", diff --git a/tsconfig.base.json b/tsconfig.base.json index ab942aab15550..44bb897bea1c2 100644 --- a/tsconfig.base.json +++ b/tsconfig.base.json @@ -1252,6 +1252,8 @@ "@kbn/notifications-plugin/*": ["x-pack/plugins/notifications/*"], "@kbn/object-versioning": ["packages/kbn-object-versioning"], "@kbn/object-versioning/*": ["packages/kbn-object-versioning/*"], + "@kbn/object-versioning-utils": ["packages/kbn-object-versioning-utils"], + "@kbn/object-versioning-utils/*": ["packages/kbn-object-versioning-utils/*"], "@kbn/observability-ai-assistant-app-plugin": ["x-pack/plugins/observability_solution/observability_ai_assistant_app"], "@kbn/observability-ai-assistant-app-plugin/*": ["x-pack/plugins/observability_solution/observability_ai_assistant_app/*"], "@kbn/observability-ai-assistant-management-plugin": ["x-pack/plugins/observability_solution/observability_ai_assistant_management"], diff --git a/yarn.lock b/yarn.lock index 25bcf4d4d9253..acc904dd83ec8 100644 --- a/yarn.lock +++ b/yarn.lock @@ -5754,6 +5754,10 @@ version "0.0.0" uid "" +"@kbn/object-versioning-utils@link:packages/kbn-object-versioning-utils": + version "0.0.0" + uid "" + "@kbn/object-versioning@link:packages/kbn-object-versioning": version "0.0.0" uid ""