From d601dbdde8ff65566e21b302cd664dd9fd98e63e Mon Sep 17 00:00:00 2001 From: Matt Kime Date: Fri, 14 Apr 2023 23:31:47 -0500 Subject: [PATCH 01/27] abstract types --- package.json | 1 + .../kbn-content-management-utils/README.md | 3 + .../kbn-content-management-utils/index.ts | 9 ++ .../jest.config.js | 13 +++ .../kbn-content-management-utils/kibana.jsonc | 5 + .../kbn-content-management-utils/package.json | 6 ++ .../tsconfig.json | 19 ++++ .../kbn-content-management-utils/types.ts | 89 +++++++++++++++++ tsconfig.base.json | 2 + .../common/content_management/v1/types.ts | 97 +++++-------------- yarn.lock | 4 + 11 files changed, 174 insertions(+), 74 deletions(-) create mode 100644 packages/kbn-content-management-utils/README.md create mode 100644 packages/kbn-content-management-utils/index.ts create mode 100644 packages/kbn-content-management-utils/jest.config.js create mode 100644 packages/kbn-content-management-utils/kibana.jsonc create mode 100644 packages/kbn-content-management-utils/package.json create mode 100644 packages/kbn-content-management-utils/tsconfig.json create mode 100644 packages/kbn-content-management-utils/types.ts diff --git a/package.json b/package.json index 2b2f4ff6326f1..cdb158ca0bc4e 100644 --- a/package.json +++ b/package.json @@ -187,6 +187,7 @@ "@kbn/content-management-examples-plugin": "link:examples/content_management_examples", "@kbn/content-management-plugin": "link:src/plugins/content_management", "@kbn/content-management-table-list": "link:packages/content-management/table_list", + "@kbn/content-management-utils": "link:packages/kbn-content-management-utils", "@kbn/controls-example-plugin": "link:examples/controls_example", "@kbn/controls-plugin": "link:src/plugins/controls", "@kbn/core": "link:src/core", diff --git a/packages/kbn-content-management-utils/README.md b/packages/kbn-content-management-utils/README.md new file mode 100644 index 0000000000000..87a985a5addaa --- /dev/null +++ b/packages/kbn-content-management-utils/README.md @@ -0,0 +1,3 @@ +# @kbn/content-management-utils + +Empty package generated by @kbn/generate diff --git a/packages/kbn-content-management-utils/index.ts b/packages/kbn-content-management-utils/index.ts new file mode 100644 index 0000000000000..12594660136d8 --- /dev/null +++ b/packages/kbn-content-management-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 * from './types'; diff --git a/packages/kbn-content-management-utils/jest.config.js b/packages/kbn-content-management-utils/jest.config.js new file mode 100644 index 0000000000000..b1e7646521e26 --- /dev/null +++ b/packages/kbn-content-management-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-content-management-utils'], +}; diff --git a/packages/kbn-content-management-utils/kibana.jsonc b/packages/kbn-content-management-utils/kibana.jsonc new file mode 100644 index 0000000000000..9e0e955936a2e --- /dev/null +++ b/packages/kbn-content-management-utils/kibana.jsonc @@ -0,0 +1,5 @@ +{ + "type": "shared-common", + "id": "@kbn/content-management-utils", + "owner": "@elastic/appex-sharedux" +} diff --git a/packages/kbn-content-management-utils/package.json b/packages/kbn-content-management-utils/package.json new file mode 100644 index 0000000000000..8b59a10c3d429 --- /dev/null +++ b/packages/kbn-content-management-utils/package.json @@ -0,0 +1,6 @@ +{ + "name": "@kbn/content-management-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-content-management-utils/tsconfig.json b/packages/kbn-content-management-utils/tsconfig.json new file mode 100644 index 0000000000000..f7360c15153f9 --- /dev/null +++ b/packages/kbn-content-management-utils/tsconfig.json @@ -0,0 +1,19 @@ +{ + "extends": "../../tsconfig.base.json", + "compilerOptions": { + "outDir": "target/types", + "types": [ + "jest", + "node", + "react" + ] + }, + "include": [ + "**/*.ts", + "**/*.tsx", + ], + "exclude": [ + "target/**/*" + ], + "kbn_references": ["@kbn/content-management-plugin"] +} diff --git a/packages/kbn-content-management-utils/types.ts b/packages/kbn-content-management-utils/types.ts new file mode 100644 index 0000000000000..63947a01eaaf0 --- /dev/null +++ b/packages/kbn-content-management-utils/types.ts @@ -0,0 +1,89 @@ +/* + * 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 { + GetIn, + GetResult, + CreateIn, + CreateResult, + SearchIn, + SearchResult, + UpdateIn, + UpdateResult, + DeleteIn, + DeleteResult, +} from '@kbn/content-management-plugin/common'; + +interface Reference { + type: string; + id: string; + name: string; +} + +export interface CreateOptions { + /** Array of referenced saved objects. */ + references?: Reference[]; +} + +export interface SearchOptions { + /** Flag to indicate to only search the text on the "title" field */ + onlyTitle?: boolean; +} + +export interface UpdateOptions { + /** Array of referenced saved objects. */ + references?: Reference[]; +} + +export type GetResultSO = GetResult< + Item, + { + outcome: 'exactMatch' | 'aliasMatch' | 'conflict'; + aliasTargetId?: string; + aliasPurpose?: 'savedObjectConversion' | 'savedObjectImport'; + } +>; + +// I'm not sure if this is correct +export interface SOWithMetadata { + id: string; + type: string; + version?: string; + createdAt?: string; + updatedAt?: string; + error?: { + error: string; + message: string; + statusCode: number; + metadata?: Record; + }; + attributes: Attributes; + references: Reference[]; + namespaces?: string[]; + originId?: string; +} + +export interface ContentManagementCrudTypes { + Item: SOWithMetadata; + + GetIn: GetIn; + GetOut: GetResultSO>; + + CreateIn: CreateIn; + CreateOut: CreateResult>; + + SearchIn: SearchIn; + SearchOut: SearchResult>; + + UpdateIn: UpdateIn; + UpdateOut: UpdateResult>; + + DeleteIn: DeleteIn; + + DeleteOut: DeleteResult; +} diff --git a/tsconfig.base.json b/tsconfig.base.json index 85fa70f831719..c676f448141c3 100644 --- a/tsconfig.base.json +++ b/tsconfig.base.json @@ -170,6 +170,8 @@ "@kbn/content-management-plugin/*": ["src/plugins/content_management/*"], "@kbn/content-management-table-list": ["packages/content-management/table_list"], "@kbn/content-management-table-list/*": ["packages/content-management/table_list/*"], + "@kbn/content-management-utils": ["packages/kbn-content-management-utils"], + "@kbn/content-management-utils/*": ["packages/kbn-content-management-utils/*"], "@kbn/controls-example-plugin": ["examples/controls_example"], "@kbn/controls-example-plugin/*": ["examples/controls_example/*"], "@kbn/controls-plugin": ["src/plugins/controls"], diff --git a/x-pack/plugins/maps/common/content_management/v1/types.ts b/x-pack/plugins/maps/common/content_management/v1/types.ts index d5a7351226c23..d679838481d22 100644 --- a/x-pack/plugins/maps/common/content_management/v1/types.ts +++ b/x-pack/plugins/maps/common/content_management/v1/types.ts @@ -5,25 +5,10 @@ * 2.0. */ -import type { - GetIn, - GetResult, - CreateIn, - CreateResult, - SearchIn, - SearchResult, - UpdateIn, - UpdateResult, - DeleteIn, - DeleteResult, -} from '@kbn/content-management-plugin/common'; +import type { ContentManagementCrudTypes } from '@kbn/content-management-utils'; import { MapContentType } from '../types'; -interface Reference { - type: string; - id: string; - name: string; -} +export type MapCrudTypes = ContentManagementCrudTypes; /* eslint-disable-next-line @typescript-eslint/consistent-type-definitions */ export type MapAttributes = { @@ -34,77 +19,41 @@ export type MapAttributes = { uiStateJSON?: string; }; -export interface MapItem { - id: string; - type: string; - version?: string; - createdAt?: string; - updatedAt?: string; - error?: { - error: string; - message: string; - statusCode: number; - metadata?: Record; - }; - attributes: MapAttributes; - references: Reference[]; - namespaces?: string[]; - originId?: string; -} - -export type PartialMapItem = Omit & { - attributes: Partial; - references: Reference[] | undefined; -}; +export type MapItem = MapCrudTypes['Item']; // ----------- GET -------------- -export type MapGetIn = GetIn; - -export type MapGetOut = GetResult< - MapItem, - { - outcome: 'exactMatch' | 'aliasMatch' | 'conflict'; - aliasTargetId?: string; - aliasPurpose?: 'savedObjectConversion' | 'savedObjectImport'; - } ->; +export type MapGetIn = MapCrudTypes['GetIn']; +export type MapGetOut = MapCrudTypes['GetOut']; // ----------- CREATE -------------- -export interface CreateOptions { - /** Array of referenced saved objects. */ - references?: Reference[]; -} - -export type MapCreateIn = CreateIn; - -export type MapCreateOut = CreateResult; +export type MapCreateIn = MapCrudTypes['CreateIn']; +export type MapCreateOut = MapCrudTypes['CreateOut']; // ----------- UPDATE -------------- -export interface UpdateOptions { - /** Array of referenced saved objects. */ - references?: Reference[]; -} - -export type MapUpdateIn = UpdateIn; - -export type MapUpdateOut = UpdateResult; +export type MapUpdateIn = MapCrudTypes['UpdateIn']; +export type MapUpdateOut = MapCrudTypes['UpdateOut']; // ----------- DELETE -------------- -export type MapDeleteIn = DeleteIn; - -export type MapDeleteOut = DeleteResult; +export type MapDeleteIn = MapCrudTypes['DeleteIn']; +export type MapDeleteOut = MapCrudTypes['DeleteOut']; // ----------- SEARCH -------------- -export interface MapSearchOptions { - /** Flag to indicate to only search the text on the "title" field */ - onlyTitle?: boolean; -} +export type MapSearchIn = MapCrudTypes['SearchIn']; +export type MapSearchOut = MapCrudTypes['SearchOut']; -export type MapSearchIn = SearchIn; +// Might be able to factor this out, otherwise it can be added to the abstract interface +export type PartialMapItem = Omit & { + attributes: Partial; + references: Reference[] | undefined; +}; -export type MapSearchOut = SearchResult; +interface Reference { + type: string; + id: string; + name: string; +} diff --git a/yarn.lock b/yarn.lock index c480dd98324b0..8040bb8623932 100644 --- a/yarn.lock +++ b/yarn.lock @@ -3077,6 +3077,10 @@ version "0.0.0" uid "" +"@kbn/content-management-utils@link:packages/kbn-content-management-utils": + version "0.0.0" + uid "" + "@kbn/controls-example-plugin@link:examples/controls_example": version "0.0.0" uid "" From 9e06b50a6e6de7bb67f852e4d204b1d4f19a94b8 Mon Sep 17 00:00:00 2001 From: kibanamachine <42973632+kibanamachine@users.noreply.github.com> Date: Sat, 15 Apr 2023 04:39:10 +0000 Subject: [PATCH 02/27] [CI] Auto-commit changed files from 'node scripts/lint_ts_projects --fix' --- packages/kbn-content-management-utils/tsconfig.json | 2 +- x-pack/plugins/maps/tsconfig.json | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/packages/kbn-content-management-utils/tsconfig.json b/packages/kbn-content-management-utils/tsconfig.json index f7360c15153f9..dad37ec28ac76 100644 --- a/packages/kbn-content-management-utils/tsconfig.json +++ b/packages/kbn-content-management-utils/tsconfig.json @@ -10,7 +10,7 @@ }, "include": [ "**/*.ts", - "**/*.tsx", + "**/*.tsx", ], "exclude": [ "target/**/*" diff --git a/x-pack/plugins/maps/tsconfig.json b/x-pack/plugins/maps/tsconfig.json index 9e501ba66559c..398139fe00d34 100644 --- a/x-pack/plugins/maps/tsconfig.json +++ b/x-pack/plugins/maps/tsconfig.json @@ -67,6 +67,7 @@ "@kbn/core-saved-objects-api-server", "@kbn/object-versioning", "@kbn/field-types", + "@kbn/content-management-utils", ], "exclude": [ "target/**/*", From 83b1ff58a06929caa8d63961ec51e81fc3de4aa0 Mon Sep 17 00:00:00 2001 From: kibanamachine <42973632+kibanamachine@users.noreply.github.com> Date: Sat, 15 Apr 2023 04:45:22 +0000 Subject: [PATCH 03/27] [CI] Auto-commit changed files from 'node scripts/generate codeowners' --- .github/CODEOWNERS | 1 + 1 file changed, 1 insertion(+) diff --git a/.github/CODEOWNERS b/.github/CODEOWNERS index 26be09f9bb2f0..fe7bd2da49272 100644 --- a/.github/CODEOWNERS +++ b/.github/CODEOWNERS @@ -88,6 +88,7 @@ packages/content-management/content_editor @elastic/appex-sharedux examples/content_management_examples @elastic/appex-sharedux src/plugins/content_management @elastic/appex-sharedux packages/content-management/table_list @elastic/appex-sharedux +packages/kbn-content-management-utils @elastic/appex-sharedux examples/controls_example @elastic/kibana-presentation src/plugins/controls @elastic/kibana-presentation src/core @elastic/kibana-core From 86e3a85273d6e59a75144e2c813f48e9d15d5e46 Mon Sep 17 00:00:00 2001 From: Matt Kime Date: Sat, 15 Apr 2023 00:21:27 -0500 Subject: [PATCH 04/27] fix types --- x-pack/plugins/maps/common/content_management/v1/types.ts | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/x-pack/plugins/maps/common/content_management/v1/types.ts b/x-pack/plugins/maps/common/content_management/v1/types.ts index d679838481d22..a872feaa86c94 100644 --- a/x-pack/plugins/maps/common/content_management/v1/types.ts +++ b/x-pack/plugins/maps/common/content_management/v1/types.ts @@ -5,7 +5,8 @@ * 2.0. */ -import type { ContentManagementCrudTypes } from '@kbn/content-management-utils'; +import type { ContentManagementCrudTypes, SearchOptions } from '@kbn/content-management-utils'; +export type { CreateOptions, UpdateOptions } from '@kbn/content-management-utils'; import { MapContentType } from '../types'; export type MapCrudTypes = ContentManagementCrudTypes; @@ -45,6 +46,7 @@ export type MapDeleteOut = MapCrudTypes['DeleteOut']; export type MapSearchIn = MapCrudTypes['SearchIn']; export type MapSearchOut = MapCrudTypes['SearchOut']; +export type MapSearchOptions = SearchOptions; // Might be able to factor this out, otherwise it can be added to the abstract interface export type PartialMapItem = Omit & { From aea2173334e2ba4f96be3e47d49fe07433d97b41 Mon Sep 17 00:00:00 2001 From: Matt Kime Date: Sat, 15 Apr 2023 09:10:52 -0500 Subject: [PATCH 05/27] remove comment --- packages/kbn-content-management-utils/types.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/packages/kbn-content-management-utils/types.ts b/packages/kbn-content-management-utils/types.ts index 63947a01eaaf0..ea1a87adeb4f9 100644 --- a/packages/kbn-content-management-utils/types.ts +++ b/packages/kbn-content-management-utils/types.ts @@ -49,7 +49,6 @@ export type GetResultSO = GetResult< } >; -// I'm not sure if this is correct export interface SOWithMetadata { id: string; type: string; From 28548f77af20cd8e12bb463be4c4d1bd6a4a2d00 Mon Sep 17 00:00:00 2001 From: Matt Kime Date: Sat, 15 Apr 2023 11:15:56 -0500 Subject: [PATCH 06/27] fix partial types --- packages/kbn-content-management-utils/types.ts | 4 ++++ .../maps/common/content_management/v1/types.ts | 13 +------------ 2 files changed, 5 insertions(+), 12 deletions(-) diff --git a/packages/kbn-content-management-utils/types.ts b/packages/kbn-content-management-utils/types.ts index ea1a87adeb4f9..64b1604c04f59 100644 --- a/packages/kbn-content-management-utils/types.ts +++ b/packages/kbn-content-management-utils/types.ts @@ -69,6 +69,10 @@ export interface SOWithMetadata { export interface ContentManagementCrudTypes { Item: SOWithMetadata; + PartialItem: Omit, 'attributes' | 'references'> & { + attributes: Partial; + references: Reference[] | undefined; + }; GetIn: GetIn; GetOut: GetResultSO>; diff --git a/x-pack/plugins/maps/common/content_management/v1/types.ts b/x-pack/plugins/maps/common/content_management/v1/types.ts index a872feaa86c94..d0d403bff31f2 100644 --- a/x-pack/plugins/maps/common/content_management/v1/types.ts +++ b/x-pack/plugins/maps/common/content_management/v1/types.ts @@ -21,6 +21,7 @@ export type MapAttributes = { }; export type MapItem = MapCrudTypes['Item']; +export type PartialMapItem = MapCrudTypes['PartialItem']; // ----------- GET -------------- @@ -47,15 +48,3 @@ export type MapDeleteOut = MapCrudTypes['DeleteOut']; export type MapSearchIn = MapCrudTypes['SearchIn']; export type MapSearchOut = MapCrudTypes['SearchOut']; export type MapSearchOptions = SearchOptions; - -// Might be able to factor this out, otherwise it can be added to the abstract interface -export type PartialMapItem = Omit & { - attributes: Partial; - references: Reference[] | undefined; -}; - -interface Reference { - type: string; - id: string; - name: string; -} From 44ca8ee89af7b9730d9d78b298dd4ef31e2f73f2 Mon Sep 17 00:00:00 2001 From: Matt Kime Date: Sat, 15 Apr 2023 11:34:39 -0500 Subject: [PATCH 07/27] add readme content --- .../kbn-content-management-utils/README.md | 48 ++++++++++++++++++- 1 file changed, 46 insertions(+), 2 deletions(-) diff --git a/packages/kbn-content-management-utils/README.md b/packages/kbn-content-management-utils/README.md index 87a985a5addaa..ebdd2564baf47 100644 --- a/packages/kbn-content-management-utils/README.md +++ b/packages/kbn-content-management-utils/README.md @@ -1,3 +1,47 @@ -# @kbn/content-management-utils +# Content management utils -Empty package generated by @kbn/generate +Utilities to ease the implementation of the Content Management API with Saved Objects. + +```ts +import type { ContentManagementCrudTypes } from '@kbn/content-management-utils'; + +export type MapCrudTypes = ContentManagementCrudTypes; + +export type MapAttributes = { + title: string; + description?: string; + mapStateJSON?: string; + layerListJSON?: string; + uiStateJSON?: string; +}; + +export type MapItem = MapCrudTypes['Item']; +export type PartialMapItem = MapCrudTypes['PartialItem']; + +// ----------- GET -------------- + +export type MapGetIn = MapCrudTypes['GetIn']; +export type MapGetOut = MapCrudTypes['GetOut']; + +// ----------- CREATE -------------- + +export type MapCreateIn = MapCrudTypes['CreateIn']; +export type MapCreateOut = MapCrudTypes['CreateOut']; + +// ----------- UPDATE -------------- + +export type MapUpdateIn = MapCrudTypes['UpdateIn']; +export type MapUpdateOut = MapCrudTypes['UpdateOut']; + +// ----------- DELETE -------------- + +export type MapDeleteIn = MapCrudTypes['DeleteIn']; +export type MapDeleteOut = MapCrudTypes['DeleteOut']; + +// ----------- SEARCH -------------- + +export type MapSearchIn = MapCrudTypes['SearchIn']; +export type MapSearchOut = MapCrudTypes['SearchOut']; +export type MapSearchOptions = SearchOptions; + +``` From 3ab819836e117c8897a7f58149c472251b04fc10 Mon Sep 17 00:00:00 2001 From: Matt Kime Date: Sun, 16 Apr 2023 23:10:54 -0500 Subject: [PATCH 08/27] simplify type export --- .../kbn-content-management-utils/types.ts | 82 ++++++++++++++++--- .../maps/common/content_management/index.ts | 4 +- .../common/content_management/v1/index.ts | 4 +- .../common/content_management/v1/types.ts | 7 +- .../server/content_management/maps_storage.ts | 16 ++-- 5 files changed, 86 insertions(+), 27 deletions(-) diff --git a/packages/kbn-content-management-utils/types.ts b/packages/kbn-content-management-utils/types.ts index 64b1604c04f59..eb8575d6a77bf 100644 --- a/packages/kbn-content-management-utils/types.ts +++ b/packages/kbn-content-management-utils/types.ts @@ -25,22 +25,22 @@ interface Reference { name: string; } -export interface CreateOptions { +interface CreateOptions { /** Array of referenced saved objects. */ references?: Reference[]; } -export interface SearchOptions { +interface SearchOptions { /** Flag to indicate to only search the text on the "title" field */ onlyTitle?: boolean; } -export interface UpdateOptions { +interface UpdateOptions { /** Array of referenced saved objects. */ references?: Reference[]; } -export type GetResultSO = GetResult< +type GetResultSO = GetResult< Item, { outcome: 'exactMatch' | 'aliasMatch' | 'conflict'; @@ -49,6 +49,9 @@ export type GetResultSO = GetResult< } >; +/** + * Saved object with metadata + */ export interface SOWithMetadata { id: string; type: string; @@ -67,26 +70,81 @@ export interface SOWithMetadata { originId?: string; } +type PartialItem = Omit< + SOWithMetadata, + 'attributes' | 'references' +> & { + attributes: Partial; + references: Reference[] | undefined; +}; + +/** + * Types used by content management storage + * @argument ContentType - content management type. assumed to be the same as saved object type + * @argument Attributes - attributes of the saved object + */ export interface ContentManagementCrudTypes { + /** + * Complete saved object + */ Item: SOWithMetadata; - PartialItem: Omit, 'attributes' | 'references'> & { - attributes: Partial; - references: Reference[] | undefined; - }; - + /** + * Partial saved object, used as output for update + */ + PartialItem: PartialItem; + /** + * Get item params + */ GetIn: GetIn; + /** + * Get item result + */ GetOut: GetResultSO>; - + /** + * Create item params + */ CreateIn: CreateIn; + /** + * Create item result + */ CreateOut: CreateResult>; + /** + * Create options + */ + CreateOptions: CreateOptions; + /** + * Search item params + */ SearchIn: SearchIn; + /** + * Search item result + */ SearchOut: SearchResult>; + /** + * Search options + */ + SearchOptions: SearchOptions; + /** + * Update item params + */ UpdateIn: UpdateIn; - UpdateOut: UpdateResult>; + /** + * Update item result + */ + UpdateOut: UpdateResult>; + /** + * Update options + */ + UpdateOptions: UpdateOptions; + /** + * Delete item params + */ DeleteIn: DeleteIn; - + /** + * Delete item result + */ DeleteOut: DeleteResult; } diff --git a/x-pack/plugins/maps/common/content_management/index.ts b/x-pack/plugins/maps/common/content_management/index.ts index daafc194a37fc..f389e3fea36e7 100644 --- a/x-pack/plugins/maps/common/content_management/index.ts +++ b/x-pack/plugins/maps/common/content_management/index.ts @@ -17,10 +17,10 @@ export type { MapGetOut, MapCreateIn, MapCreateOut, - CreateOptions, + MapCreateOptions, MapUpdateIn, MapUpdateOut, - UpdateOptions, + MapUpdateOptions, MapDeleteIn, MapDeleteOut, MapSearchIn, diff --git a/x-pack/plugins/maps/common/content_management/v1/index.ts b/x-pack/plugins/maps/common/content_management/v1/index.ts index 272e0e1eb5f2e..535a15e5a23a6 100644 --- a/x-pack/plugins/maps/common/content_management/v1/index.ts +++ b/x-pack/plugins/maps/common/content_management/v1/index.ts @@ -13,10 +13,10 @@ export type { MapGetOut, MapCreateIn, MapCreateOut, - CreateOptions, + MapCreateOptions, MapUpdateIn, MapUpdateOut, - UpdateOptions, + MapUpdateOptions, MapDeleteIn, MapDeleteOut, MapSearchIn, diff --git a/x-pack/plugins/maps/common/content_management/v1/types.ts b/x-pack/plugins/maps/common/content_management/v1/types.ts index d0d403bff31f2..c5c8429275a63 100644 --- a/x-pack/plugins/maps/common/content_management/v1/types.ts +++ b/x-pack/plugins/maps/common/content_management/v1/types.ts @@ -5,8 +5,7 @@ * 2.0. */ -import type { ContentManagementCrudTypes, SearchOptions } from '@kbn/content-management-utils'; -export type { CreateOptions, UpdateOptions } from '@kbn/content-management-utils'; +import type { ContentManagementCrudTypes } from '@kbn/content-management-utils'; import { MapContentType } from '../types'; export type MapCrudTypes = ContentManagementCrudTypes; @@ -32,11 +31,13 @@ export type MapGetOut = MapCrudTypes['GetOut']; export type MapCreateIn = MapCrudTypes['CreateIn']; export type MapCreateOut = MapCrudTypes['CreateOut']; +export type MapCreateOptions = MapCrudTypes['CreateOptions']; // ----------- UPDATE -------------- export type MapUpdateIn = MapCrudTypes['UpdateIn']; export type MapUpdateOut = MapCrudTypes['UpdateOut']; +export type MapUpdateOptions = MapCrudTypes['UpdateOptions']; // ----------- DELETE -------------- @@ -47,4 +48,4 @@ export type MapDeleteOut = MapCrudTypes['DeleteOut']; export type MapSearchIn = MapCrudTypes['SearchIn']; export type MapSearchOut = MapCrudTypes['SearchOut']; -export type MapSearchOptions = SearchOptions; +export type MapSearchOptions = MapCrudTypes['SearchOptions']; diff --git a/x-pack/plugins/maps/server/content_management/maps_storage.ts b/x-pack/plugins/maps/server/content_management/maps_storage.ts index 266ad81007345..dc7273ca8a6de 100644 --- a/x-pack/plugins/maps/server/content_management/maps_storage.ts +++ b/x-pack/plugins/maps/server/content_management/maps_storage.ts @@ -23,10 +23,10 @@ import type { MapGetOut, MapCreateIn, MapCreateOut, - CreateOptions, + MapCreateOptions, MapUpdateIn, MapUpdateOut, - UpdateOptions, + MapUpdateOptions, MapDeleteOut, MapSearchOptions, MapSearchOut, @@ -133,7 +133,7 @@ export class MapsStorage implements ContentStorage { async create( ctx: StorageContext, data: MapCreateIn['data'], - options: CreateOptions + options: MapCreateOptions ): Promise { const { utils: { getTransforms }, @@ -150,8 +150,8 @@ export class MapsStorage implements ContentStorage { } const { value: optionsToLatest, error: optionsError } = transforms.create.in.options.up< - CreateOptions, - CreateOptions + MapCreateOptions, + MapCreateOptions >(options); if (optionsError) { throw Boom.badRequest(`Invalid options. ${optionsError.message}`); @@ -184,7 +184,7 @@ export class MapsStorage implements ContentStorage { ctx: StorageContext, id: string, data: MapUpdateIn['data'], - options: UpdateOptions + options: MapUpdateOptions ): Promise { const { utils: { getTransforms }, @@ -201,8 +201,8 @@ export class MapsStorage implements ContentStorage { } const { value: optionsToLatest, error: optionsError } = transforms.update.in.options.up< - CreateOptions, - CreateOptions + MapCreateOptions, + MapCreateOptions >(options); if (optionsError) { throw Boom.badRequest(`Invalid options. ${optionsError.message}`); From e10b29753ae6a4a40ae51bf50a65fa9e7e286423 Mon Sep 17 00:00:00 2001 From: Matt Kime Date: Sun, 16 Apr 2023 23:32:17 -0500 Subject: [PATCH 09/27] simplify type export --- packages/kbn-content-management-utils/types.ts | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/packages/kbn-content-management-utils/types.ts b/packages/kbn-content-management-utils/types.ts index eb8575d6a77bf..406c5cb6a7fa7 100644 --- a/packages/kbn-content-management-utils/types.ts +++ b/packages/kbn-content-management-utils/types.ts @@ -19,28 +19,28 @@ import type { DeleteResult, } from '@kbn/content-management-plugin/common'; -interface Reference { +export interface Reference { type: string; id: string; name: string; } -interface CreateOptions { +export interface CreateOptions { /** Array of referenced saved objects. */ references?: Reference[]; } -interface SearchOptions { +export interface SearchOptions { /** Flag to indicate to only search the text on the "title" field */ onlyTitle?: boolean; } -interface UpdateOptions { +export interface UpdateOptions { /** Array of referenced saved objects. */ references?: Reference[]; } -type GetResultSO = GetResult< +export type GetResultSO = GetResult< Item, { outcome: 'exactMatch' | 'aliasMatch' | 'conflict'; From 055679a24db10cc148886614d8af9b1ef67e0cb1 Mon Sep 17 00:00:00 2001 From: Matt Kime Date: Sun, 16 Apr 2023 23:32:35 -0500 Subject: [PATCH 10/27] simplify type export --- packages/kbn-content-management-utils/types.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/packages/kbn-content-management-utils/types.ts b/packages/kbn-content-management-utils/types.ts index 406c5cb6a7fa7..507f6ef044dbc 100644 --- a/packages/kbn-content-management-utils/types.ts +++ b/packages/kbn-content-management-utils/types.ts @@ -92,6 +92,7 @@ export interface ContentManagementCrudTypes; + /** * Get item params */ From eee1744fa513e5ae1682d3b1fcc2789c926c2814 Mon Sep 17 00:00:00 2001 From: Matt Kime Date: Tue, 18 Apr 2023 21:44:40 -0500 Subject: [PATCH 11/27] move Option types external to main interface --- .github/CODEOWNERS | 2 +- .../kbn-content-management-utils/README.md | 13 +++++++++- .../kbn-content-management-utils/types.ts | 26 ++++++------------- .../common/content_management/v1/types.ts | 15 ++++++++--- 4 files changed, 32 insertions(+), 24 deletions(-) diff --git a/.github/CODEOWNERS b/.github/CODEOWNERS index 9038ebabb3bf6..4d91ba71f67c8 100644 --- a/.github/CODEOWNERS +++ b/.github/CODEOWNERS @@ -88,7 +88,7 @@ packages/content-management/content_editor @elastic/appex-sharedux examples/content_management_examples @elastic/appex-sharedux src/plugins/content_management @elastic/appex-sharedux packages/content-management/table_list @elastic/appex-sharedux -packages/kbn-content-management-utils @elastic/appex-sharedux +packages/kbn-content-management-utils @elastic/kibana-data-discovery examples/controls_example @elastic/kibana-presentation src/plugins/controls @elastic/kibana-presentation src/core @elastic/kibana-core diff --git a/packages/kbn-content-management-utils/README.md b/packages/kbn-content-management-utils/README.md index ebdd2564baf47..32c28b9fc4807 100644 --- a/packages/kbn-content-management-utils/README.md +++ b/packages/kbn-content-management-utils/README.md @@ -3,10 +3,17 @@ Utilities to ease the implementation of the Content Management API with Saved Objects. ```ts -import type { ContentManagementCrudTypes } from '@kbn/content-management-utils'; +import type { + ContentManagementCrudTypes, + CreateOptions, + SearchOptions, + UpdateOptions, +} from '@kbn/content-management-utils'; +import { MapContentType } from '../types'; export type MapCrudTypes = ContentManagementCrudTypes; +/* eslint-disable-next-line @typescript-eslint/consistent-type-definitions */ export type MapAttributes = { title: string; description?: string; @@ -27,11 +34,13 @@ export type MapGetOut = MapCrudTypes['GetOut']; export type MapCreateIn = MapCrudTypes['CreateIn']; export type MapCreateOut = MapCrudTypes['CreateOut']; +export type MapCreateOptions = CreateOptions; // ----------- UPDATE -------------- export type MapUpdateIn = MapCrudTypes['UpdateIn']; export type MapUpdateOut = MapCrudTypes['UpdateOut']; +export type MapUpdateOptions = UpdateOptions; // ----------- DELETE -------------- @@ -45,3 +54,5 @@ export type MapSearchOut = MapCrudTypes['SearchOut']; export type MapSearchOptions = SearchOptions; ``` + + diff --git a/packages/kbn-content-management-utils/types.ts b/packages/kbn-content-management-utils/types.ts index 507f6ef044dbc..2d345a19498a9 100644 --- a/packages/kbn-content-management-utils/types.ts +++ b/packages/kbn-content-management-utils/types.ts @@ -25,23 +25,25 @@ export interface Reference { name: string; } +/** Saved Object create options - Pick and Omit to customize */ export interface CreateOptions { /** Array of referenced saved objects. */ references?: Reference[]; } -export interface SearchOptions { - /** Flag to indicate to only search the text on the "title" field */ - onlyTitle?: boolean; -} +/** Saved Object search options - Pick and Omit to customize */ +// eslint-disable-next-line @typescript-eslint/no-empty-interface +export interface SearchOptions {} +/** Saved Object update options - Pick and Omit to customize */ export interface UpdateOptions { /** Array of referenced saved objects. */ references?: Reference[]; } -export type GetResultSO = GetResult< - Item, +/** Return value for Saved Object get, T is item returned */ +export type GetResultSO = GetResult< + T, { outcome: 'exactMatch' | 'aliasMatch' | 'conflict'; aliasTargetId?: string; @@ -109,10 +111,6 @@ export interface ContentManagementCrudTypes>; - /** - * Create options - */ - CreateOptions: CreateOptions; /** * Search item params @@ -122,10 +120,6 @@ export interface ContentManagementCrudTypes>; - /** - * Search options - */ - SearchOptions: SearchOptions; /** * Update item params @@ -135,10 +129,6 @@ export interface ContentManagementCrudTypes>; - /** - * Update options - */ - UpdateOptions: UpdateOptions; /** * Delete item params diff --git a/x-pack/plugins/maps/common/content_management/v1/types.ts b/x-pack/plugins/maps/common/content_management/v1/types.ts index c5c8429275a63..033dec61cc8a0 100644 --- a/x-pack/plugins/maps/common/content_management/v1/types.ts +++ b/x-pack/plugins/maps/common/content_management/v1/types.ts @@ -5,7 +5,11 @@ * 2.0. */ -import type { ContentManagementCrudTypes } from '@kbn/content-management-utils'; +import type { + ContentManagementCrudTypes, + CreateOptions, + UpdateOptions, +} from '@kbn/content-management-utils'; import { MapContentType } from '../types'; export type MapCrudTypes = ContentManagementCrudTypes; @@ -31,13 +35,13 @@ export type MapGetOut = MapCrudTypes['GetOut']; export type MapCreateIn = MapCrudTypes['CreateIn']; export type MapCreateOut = MapCrudTypes['CreateOut']; -export type MapCreateOptions = MapCrudTypes['CreateOptions']; +export type MapCreateOptions = CreateOptions; // ----------- UPDATE -------------- export type MapUpdateIn = MapCrudTypes['UpdateIn']; export type MapUpdateOut = MapCrudTypes['UpdateOut']; -export type MapUpdateOptions = MapCrudTypes['UpdateOptions']; +export type MapUpdateOptions = UpdateOptions; // ----------- DELETE -------------- @@ -48,4 +52,7 @@ export type MapDeleteOut = MapCrudTypes['DeleteOut']; export type MapSearchIn = MapCrudTypes['SearchIn']; export type MapSearchOut = MapCrudTypes['SearchOut']; -export type MapSearchOptions = MapCrudTypes['SearchOptions']; +export interface MapSearchOptions { + /** Flag to indicate to only search the text on the "title" field */ + onlyTitle?: boolean; +} From 530c23dd27346c71767253e8a4cceabd8ff30e5d Mon Sep 17 00:00:00 2001 From: kibanamachine <42973632+kibanamachine@users.noreply.github.com> Date: Wed, 19 Apr 2023 02:51:06 +0000 Subject: [PATCH 12/27] [CI] Auto-commit changed files from 'node scripts/generate codeowners' --- .github/CODEOWNERS | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/CODEOWNERS b/.github/CODEOWNERS index 4d91ba71f67c8..9038ebabb3bf6 100644 --- a/.github/CODEOWNERS +++ b/.github/CODEOWNERS @@ -88,7 +88,7 @@ packages/content-management/content_editor @elastic/appex-sharedux examples/content_management_examples @elastic/appex-sharedux src/plugins/content_management @elastic/appex-sharedux packages/content-management/table_list @elastic/appex-sharedux -packages/kbn-content-management-utils @elastic/kibana-data-discovery +packages/kbn-content-management-utils @elastic/appex-sharedux examples/controls_example @elastic/kibana-presentation src/plugins/controls @elastic/kibana-presentation src/core @elastic/kibana-core From 8f9c9394ef8e5bef9398d7e192eb1661004b54bb Mon Sep 17 00:00:00 2001 From: Matt Kime Date: Wed, 19 Apr 2023 22:03:14 -0500 Subject: [PATCH 13/27] schema abstraction --- .../kbn-content-management-utils/index.ts | 3 +- .../kbn-content-management-utils/types.ts | 141 ------------------ .../content_management/v1/cm_services.ts | 91 +++-------- 3 files changed, 19 insertions(+), 216 deletions(-) delete mode 100644 packages/kbn-content-management-utils/types.ts diff --git a/packages/kbn-content-management-utils/index.ts b/packages/kbn-content-management-utils/index.ts index 12594660136d8..8e4751ba223ea 100644 --- a/packages/kbn-content-management-utils/index.ts +++ b/packages/kbn-content-management-utils/index.ts @@ -6,4 +6,5 @@ * Side Public License, v 1. */ -export * from './types'; +export * from './src/types'; +export * from './src/schema'; diff --git a/packages/kbn-content-management-utils/types.ts b/packages/kbn-content-management-utils/types.ts deleted file mode 100644 index 2d345a19498a9..0000000000000 --- a/packages/kbn-content-management-utils/types.ts +++ /dev/null @@ -1,141 +0,0 @@ -/* - * 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 { - GetIn, - GetResult, - CreateIn, - CreateResult, - SearchIn, - SearchResult, - UpdateIn, - UpdateResult, - DeleteIn, - DeleteResult, -} from '@kbn/content-management-plugin/common'; - -export interface Reference { - type: string; - id: string; - name: string; -} - -/** Saved Object create options - Pick and Omit to customize */ -export interface CreateOptions { - /** Array of referenced saved objects. */ - references?: Reference[]; -} - -/** Saved Object search options - Pick and Omit to customize */ -// eslint-disable-next-line @typescript-eslint/no-empty-interface -export interface SearchOptions {} - -/** Saved Object update options - Pick and Omit to customize */ -export interface UpdateOptions { - /** Array of referenced saved objects. */ - references?: Reference[]; -} - -/** Return value for Saved Object get, T is item returned */ -export type GetResultSO = GetResult< - T, - { - outcome: 'exactMatch' | 'aliasMatch' | 'conflict'; - aliasTargetId?: string; - aliasPurpose?: 'savedObjectConversion' | 'savedObjectImport'; - } ->; - -/** - * Saved object with metadata - */ -export interface SOWithMetadata { - id: string; - type: string; - version?: string; - createdAt?: string; - updatedAt?: string; - error?: { - error: string; - message: string; - statusCode: number; - metadata?: Record; - }; - attributes: Attributes; - references: Reference[]; - namespaces?: string[]; - originId?: string; -} - -type PartialItem = Omit< - SOWithMetadata, - 'attributes' | 'references' -> & { - attributes: Partial; - references: Reference[] | undefined; -}; - -/** - * Types used by content management storage - * @argument ContentType - content management type. assumed to be the same as saved object type - * @argument Attributes - attributes of the saved object - */ -export interface ContentManagementCrudTypes { - /** - * Complete saved object - */ - Item: SOWithMetadata; - /** - * Partial saved object, used as output for update - */ - PartialItem: PartialItem; - - /** - * Get item params - */ - GetIn: GetIn; - /** - * Get item result - */ - GetOut: GetResultSO>; - /** - * Create item params - */ - CreateIn: CreateIn; - /** - * Create item result - */ - CreateOut: CreateResult>; - - /** - * Search item params - */ - SearchIn: SearchIn; - /** - * Search item result - */ - SearchOut: SearchResult>; - - /** - * Update item params - */ - UpdateIn: UpdateIn; - /** - * Update item result - */ - UpdateOut: UpdateResult>; - - /** - * Delete item params - */ - DeleteIn: DeleteIn; - /** - * Delete item result - */ - DeleteOut: DeleteResult; -} diff --git a/x-pack/plugins/maps/common/content_management/v1/cm_services.ts b/x-pack/plugins/maps/common/content_management/v1/cm_services.ts index 0e430b56ced98..fb4ffacf4885d 100644 --- a/x-pack/plugins/maps/common/content_management/v1/cm_services.ts +++ b/x-pack/plugins/maps/common/content_management/v1/cm_services.ts @@ -6,24 +6,12 @@ */ import { schema } from '@kbn/config-schema'; import type { ContentManagementServicesDefinition as ServicesDefinition } from '@kbn/object-versioning'; - -const apiError = schema.object({ - error: schema.string(), - message: schema.string(), - statusCode: schema.number(), - metadata: schema.object({}, { unknowns: 'allow' }), -}); - -const referenceSchema = schema.object( - { - name: schema.maybe(schema.string()), - type: schema.string(), - id: schema.string(), - }, - { unknowns: 'forbid' } -); - -const referencesSchema = schema.arrayOf(referenceSchema); +import { + savedObjectSchema, + objectTypeToGetResultSchema, + createOptionsSchema, + createResultSchema, +} from '@kbn/content-management-utils'; const mapAttributesSchema = schema.object( { @@ -36,57 +24,24 @@ const mapAttributesSchema = schema.object( { unknowns: 'forbid' } ); -const mapSavedObjectSchema = schema.object( - { - id: schema.string(), - type: schema.string(), - version: schema.maybe(schema.string()), - createdAt: schema.maybe(schema.string()), - updatedAt: schema.maybe(schema.string()), - error: schema.maybe(apiError), - attributes: mapAttributesSchema, - references: referencesSchema, - namespaces: schema.maybe(schema.arrayOf(schema.string())), - originId: schema.maybe(schema.string()), - }, - { unknowns: 'allow' } -); +const mapSavedObjectSchema = savedObjectSchema(mapAttributesSchema); -const getResultSchema = schema.object( - { - item: mapSavedObjectSchema, - meta: schema.object( - { - outcome: schema.oneOf([ - schema.literal('exactMatch'), - schema.literal('aliasMatch'), - schema.literal('conflict'), - ]), - aliasTargetId: schema.maybe(schema.string()), - aliasPurpose: schema.maybe( - schema.oneOf([ - schema.literal('savedObjectConversion'), - schema.literal('savedObjectImport'), - ]) - ), - }, - { unknowns: 'forbid' } - ), - }, - { unknowns: 'forbid' } +const searchOptionsSchema = schema.maybe( + schema.object( + { + onlyTitle: schema.maybe(schema.boolean()), + }, + { unknowns: 'forbid' } + ) ); -const createOptionsSchema = schema.object({ - references: schema.maybe(referencesSchema), -}); - // Content management service definition. // We need it for BWC support between different versions of the content export const serviceDefinition: ServicesDefinition = { get: { out: { result: { - schema: getResultSchema, + schema: objectTypeToGetResultSchema(mapSavedObjectSchema), }, }, }, @@ -101,12 +56,7 @@ export const serviceDefinition: ServicesDefinition = { }, out: { result: { - schema: schema.object( - { - item: mapSavedObjectSchema, - }, - { unknowns: 'forbid' } - ), + schema: createResultSchema(mapSavedObjectSchema), }, }, }, @@ -123,14 +73,7 @@ export const serviceDefinition: ServicesDefinition = { search: { in: { options: { - schema: schema.maybe( - schema.object( - { - onlyTitle: schema.maybe(schema.boolean()), - }, - { unknowns: 'forbid' } - ) - ), + schema: searchOptionsSchema, }, }, }, From 5303a6b3bb17b08ccff302045d24dbc36eb40c90 Mon Sep 17 00:00:00 2001 From: Matt Kime Date: Wed, 19 Apr 2023 22:03:48 -0500 Subject: [PATCH 14/27] schema abstraction --- .../src/schema.ts | 93 ++++++++++++ .../kbn-content-management-utils/src/types.ts | 141 ++++++++++++++++++ 2 files changed, 234 insertions(+) create mode 100644 packages/kbn-content-management-utils/src/schema.ts create mode 100644 packages/kbn-content-management-utils/src/types.ts diff --git a/packages/kbn-content-management-utils/src/schema.ts b/packages/kbn-content-management-utils/src/schema.ts new file mode 100644 index 0000000000000..2a793f7c29538 --- /dev/null +++ b/packages/kbn-content-management-utils/src/schema.ts @@ -0,0 +1,93 @@ +/* + * 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 { schema, ObjectType } from '@kbn/config-schema'; + +export const apiError = schema.object({ + error: schema.string(), + message: schema.string(), + statusCode: schema.number(), + metadata: schema.object({}, { unknowns: 'allow' }), +}); + +export const referenceSchema = schema.object( + { + name: schema.maybe(schema.string()), + type: schema.string(), + id: schema.string(), + }, + { unknowns: 'forbid' } +); + +export const referencesSchema = schema.arrayOf(referenceSchema); + +/* +export const mapAttributesSchema = schema.object( + { + title: schema.string(), + description: schema.maybe(schema.string()), + mapStateJSON: schema.maybe(schema.string()), + layerListJSON: schema.maybe(schema.string()), + uiStateJSON: schema.maybe(schema.string()), + }, + { unknowns: 'forbid' } +); +*/ + +export const savedObjectSchema = (attributesSchema: ObjectType) => + schema.object( + { + id: schema.string(), + type: schema.string(), + version: schema.maybe(schema.string()), + createdAt: schema.maybe(schema.string()), + updatedAt: schema.maybe(schema.string()), + error: schema.maybe(apiError), + attributes: attributesSchema, + references: referencesSchema, + namespaces: schema.maybe(schema.arrayOf(schema.string())), + originId: schema.maybe(schema.string()), + }, + { unknowns: 'allow' } + ); + +export const objectTypeToGetResultSchema = (soSchema: ObjectType) => + schema.object( + { + item: soSchema, + meta: schema.object( + { + outcome: schema.oneOf([ + schema.literal('exactMatch'), + schema.literal('aliasMatch'), + schema.literal('conflict'), + ]), + aliasTargetId: schema.maybe(schema.string()), + aliasPurpose: schema.maybe( + schema.oneOf([ + schema.literal('savedObjectConversion'), + schema.literal('savedObjectImport'), + ]) + ), + }, + { unknowns: 'forbid' } + ), + }, + { unknowns: 'forbid' } + ); + +export const createOptionsSchema = schema.object({ + references: schema.maybe(referencesSchema), +}); + +export const createResultSchema = (soSchema: ObjectType) => + schema.object( + { + item: soSchema, + }, + { unknowns: 'forbid' } + ); diff --git a/packages/kbn-content-management-utils/src/types.ts b/packages/kbn-content-management-utils/src/types.ts new file mode 100644 index 0000000000000..2d345a19498a9 --- /dev/null +++ b/packages/kbn-content-management-utils/src/types.ts @@ -0,0 +1,141 @@ +/* + * 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 { + GetIn, + GetResult, + CreateIn, + CreateResult, + SearchIn, + SearchResult, + UpdateIn, + UpdateResult, + DeleteIn, + DeleteResult, +} from '@kbn/content-management-plugin/common'; + +export interface Reference { + type: string; + id: string; + name: string; +} + +/** Saved Object create options - Pick and Omit to customize */ +export interface CreateOptions { + /** Array of referenced saved objects. */ + references?: Reference[]; +} + +/** Saved Object search options - Pick and Omit to customize */ +// eslint-disable-next-line @typescript-eslint/no-empty-interface +export interface SearchOptions {} + +/** Saved Object update options - Pick and Omit to customize */ +export interface UpdateOptions { + /** Array of referenced saved objects. */ + references?: Reference[]; +} + +/** Return value for Saved Object get, T is item returned */ +export type GetResultSO = GetResult< + T, + { + outcome: 'exactMatch' | 'aliasMatch' | 'conflict'; + aliasTargetId?: string; + aliasPurpose?: 'savedObjectConversion' | 'savedObjectImport'; + } +>; + +/** + * Saved object with metadata + */ +export interface SOWithMetadata { + id: string; + type: string; + version?: string; + createdAt?: string; + updatedAt?: string; + error?: { + error: string; + message: string; + statusCode: number; + metadata?: Record; + }; + attributes: Attributes; + references: Reference[]; + namespaces?: string[]; + originId?: string; +} + +type PartialItem = Omit< + SOWithMetadata, + 'attributes' | 'references' +> & { + attributes: Partial; + references: Reference[] | undefined; +}; + +/** + * Types used by content management storage + * @argument ContentType - content management type. assumed to be the same as saved object type + * @argument Attributes - attributes of the saved object + */ +export interface ContentManagementCrudTypes { + /** + * Complete saved object + */ + Item: SOWithMetadata; + /** + * Partial saved object, used as output for update + */ + PartialItem: PartialItem; + + /** + * Get item params + */ + GetIn: GetIn; + /** + * Get item result + */ + GetOut: GetResultSO>; + /** + * Create item params + */ + CreateIn: CreateIn; + /** + * Create item result + */ + CreateOut: CreateResult>; + + /** + * Search item params + */ + SearchIn: SearchIn; + /** + * Search item result + */ + SearchOut: SearchResult>; + + /** + * Update item params + */ + UpdateIn: UpdateIn; + /** + * Update item result + */ + UpdateOut: UpdateResult>; + + /** + * Delete item params + */ + DeleteIn: DeleteIn; + /** + * Delete item result + */ + DeleteOut: DeleteResult; +} From 10048c5fc7c96342232b93790c2bee953fe558f2 Mon Sep 17 00:00:00 2001 From: kibanamachine <42973632+kibanamachine@users.noreply.github.com> Date: Thu, 20 Apr 2023 03:11:39 +0000 Subject: [PATCH 15/27] [CI] Auto-commit changed files from 'node scripts/lint_ts_projects --fix' --- packages/kbn-content-management-utils/tsconfig.json | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/packages/kbn-content-management-utils/tsconfig.json b/packages/kbn-content-management-utils/tsconfig.json index dad37ec28ac76..1a54419075f1e 100644 --- a/packages/kbn-content-management-utils/tsconfig.json +++ b/packages/kbn-content-management-utils/tsconfig.json @@ -15,5 +15,8 @@ "exclude": [ "target/**/*" ], - "kbn_references": ["@kbn/content-management-plugin"] + "kbn_references": [ + "@kbn/content-management-plugin", + "@kbn/config-schema", + ] } From c47354940afe8e5a5e5202abf8e1e31ad29a4a13 Mon Sep 17 00:00:00 2001 From: kibanamachine <42973632+kibanamachine@users.noreply.github.com> Date: Thu, 20 Apr 2023 03:18:01 +0000 Subject: [PATCH 16/27] [CI] Auto-commit changed files from 'node scripts/generate codeowners' --- .github/CODEOWNERS | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/CODEOWNERS b/.github/CODEOWNERS index 4d91ba71f67c8..9038ebabb3bf6 100644 --- a/.github/CODEOWNERS +++ b/.github/CODEOWNERS @@ -88,7 +88,7 @@ packages/content-management/content_editor @elastic/appex-sharedux examples/content_management_examples @elastic/appex-sharedux src/plugins/content_management @elastic/appex-sharedux packages/content-management/table_list @elastic/appex-sharedux -packages/kbn-content-management-utils @elastic/kibana-data-discovery +packages/kbn-content-management-utils @elastic/appex-sharedux examples/controls_example @elastic/kibana-presentation src/plugins/controls @elastic/kibana-presentation src/core @elastic/kibana-core From b3da60af78a48f54b90424ae2db1389e78c5a0e3 Mon Sep 17 00:00:00 2001 From: Matthew Kime Date: Thu, 20 Apr 2023 08:17:47 -0500 Subject: [PATCH 17/27] Update CODEOWNERS --- .github/CODEOWNERS | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/CODEOWNERS b/.github/CODEOWNERS index 9038ebabb3bf6..4d91ba71f67c8 100644 --- a/.github/CODEOWNERS +++ b/.github/CODEOWNERS @@ -88,7 +88,7 @@ packages/content-management/content_editor @elastic/appex-sharedux examples/content_management_examples @elastic/appex-sharedux src/plugins/content_management @elastic/appex-sharedux packages/content-management/table_list @elastic/appex-sharedux -packages/kbn-content-management-utils @elastic/appex-sharedux +packages/kbn-content-management-utils @elastic/kibana-data-discovery examples/controls_example @elastic/kibana-presentation src/plugins/controls @elastic/kibana-presentation src/core @elastic/kibana-core From f32ecb4c30e42db2770b79b93d2ba1ad3d43c236 Mon Sep 17 00:00:00 2001 From: Matthew Kime Date: Thu, 20 Apr 2023 08:18:07 -0500 Subject: [PATCH 18/27] Update kibana.jsonc --- packages/kbn-content-management-utils/kibana.jsonc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/kbn-content-management-utils/kibana.jsonc b/packages/kbn-content-management-utils/kibana.jsonc index 9e0e955936a2e..06779896a47c4 100644 --- a/packages/kbn-content-management-utils/kibana.jsonc +++ b/packages/kbn-content-management-utils/kibana.jsonc @@ -1,5 +1,5 @@ { "type": "shared-common", "id": "@kbn/content-management-utils", - "owner": "@elastic/appex-sharedux" + "owner": "@elastic/kibana-data-discovery" } From 64c4733ac05a22a3ece71cd10d214364c7882ee1 Mon Sep 17 00:00:00 2001 From: Matt Kime Date: Thu, 20 Apr 2023 16:06:06 -0500 Subject: [PATCH 19/27] more complete types for SO Options --- .../kbn-content-management-utils/types.ts | 131 +++++++++++++++++- .../common/content_management/v1/types.ts | 4 +- 2 files changed, 130 insertions(+), 5 deletions(-) diff --git a/packages/kbn-content-management-utils/types.ts b/packages/kbn-content-management-utils/types.ts index 2d345a19498a9..7185fe6311172 100644 --- a/packages/kbn-content-management-utils/types.ts +++ b/packages/kbn-content-management-utils/types.ts @@ -19,6 +19,20 @@ import type { DeleteResult, } from '@kbn/content-management-plugin/common'; +import type { + SortOrder, + AggregationsAggregationContainer, + SortResults, +} from '@elastic/elasticsearch/lib/api/typesWithBodyKey'; + +import { + MutatingOperationRefreshSetting, + SavedObjectsPitParams, + SavedObjectsFindOptionsReference, +} from '@kbn/core-saved-objects-api-server'; + +type KueryNode = any; + export interface Reference { type: string; id: string; @@ -27,18 +41,129 @@ export interface Reference { /** Saved Object create options - Pick and Omit to customize */ export interface CreateOptions { + /** (not recommended) Specify an id for the document */ + id?: string; + /** Overwrite existing documents (defaults to false) */ + overwrite?: boolean; + /** + * An opaque version number which changes on each successful write operation. + * Can be used in conjunction with `overwrite` for implementing optimistic concurrency control. + **/ + version?: string; /** Array of referenced saved objects. */ references?: Reference[]; + /** The Elasticsearch Refresh setting for this operation */ + refresh?: boolean; + /** + * Optional initial namespaces for the object to be created in. If this is defined, it will supersede the namespace ID that is in + * {@link SavedObjectsCreateOptions}. + * + * * For shareable object types (registered with `namespaceType: 'multiple'`): this option can be used to specify one or more spaces, + * including the "All spaces" identifier (`'*'`). + * * For isolated object types (registered with `namespaceType: 'single'` or `namespaceType: 'multiple-isolated'`): this option can only + * be used to specify a single space, and the "All spaces" identifier (`'*'`) is not allowed. + * * For global object types (registered with `namespaceType: 'agnostic'`): this option cannot be used. + */ + initialNamespaces?: string[]; } /** Saved Object search options - Pick and Omit to customize */ -// eslint-disable-next-line @typescript-eslint/no-empty-interface -export interface SearchOptions {} +export interface SearchOptions { + /** the page of results to return */ + page?: number; + /** the number of objects per page */ + perPage?: number; + /** which field to sort by */ + sortField?: string; + /** sort order, ascending or descending */ + sortOrder?: SortOrder; + /** + * An array of fields to include in the results + * @example + * SavedObjects.find({type: 'dashboard', fields: ['attributes.name', 'attributes.location']}) + */ + fields?: string[]; + /** Search documents using the Elasticsearch Simple Query String syntax. See Elasticsearch Simple Query String `query` argument for more information */ + search?: string; + /** The fields to perform the parsed query against. See Elasticsearch Simple Query String `fields` argument for more information */ + searchFields?: string[]; + /** + * Use the sort values from the previous page to retrieve the next page of results. + */ + searchAfter?: SortResults; + /** + * The fields to perform the parsed query against. Unlike the `searchFields` argument, these are expected to be root fields and will not + * be modified. If used in conjunction with `searchFields`, both are concatenated together. + */ + rootSearchFields?: string[]; + /** + * Search for documents having a reference to the specified objects. + * Use `hasReferenceOperator` to specify the operator to use when searching for multiple references. + */ + hasReference?: SavedObjectsFindOptionsReference | SavedObjectsFindOptionsReference[]; + /** + * The operator to use when searching by multiple references using the `hasReference` option. Defaults to `OR` + */ + hasReferenceOperator?: 'AND' | 'OR'; + /** + * Search for documents *not* having a reference to the specified objects. + * Use `hasNoReferenceOperator` to specify the operator to use when searching for multiple references. + */ + hasNoReference?: SavedObjectsFindOptionsReference | SavedObjectsFindOptionsReference[]; + /** + * The operator to use when searching by multiple references using the `hasNoReference` option. Defaults to `OR` + */ + hasNoReferenceOperator?: 'AND' | 'OR'; + /** + * The search operator to use with the provided filter. Defaults to `OR` + */ + defaultSearchOperator?: 'AND' | 'OR'; + /** filter string for the search query */ + filter?: string | KueryNode; + /** + * A record of aggregations to perform. + * The API currently only supports a limited set of metrics and bucket aggregation types. + * Additional aggregation types can be contributed to Core. + * + * @example + * Aggregating on SO attribute field + * ```ts + * const aggs = { latest_version: { max: { field: 'dashboard.attributes.version' } } }; + * return client.find({ type: 'dashboard', aggs }) + * ``` + * + * @example + * Aggregating on SO root field + * ```ts + * const aggs = { latest_update: { max: { field: 'dashboard.updated_at' } } }; + * return client.find({ type: 'dashboard', aggs }) + * ``` + * + * @alpha + */ + aggs?: Record; + /** array of namespaces to search */ + namespaces?: string[]; + /** + * Search against a specific Point In Time (PIT) that you've opened with {@link SavedObjectsClient.openPointInTimeForType}. + */ + pit?: SavedObjectsPitParams; +} /** Saved Object update options - Pick and Omit to customize */ -export interface UpdateOptions { +export interface UpdateOptions { /** Array of referenced saved objects. */ references?: Reference[]; + version?: string; + /** The Elasticsearch Refresh setting for this operation */ + refresh?: MutatingOperationRefreshSetting; + /** If specified, will be used to perform an upsert if the object doesn't exist */ + upsert?: Attributes; + /** + * The Elasticsearch `retry_on_conflict` setting for this operation. + * Defaults to `0` when `version` is provided, `3` otherwise. + */ + retryOnConflict?: number; } /** Return value for Saved Object get, T is item returned */ diff --git a/x-pack/plugins/maps/common/content_management/v1/types.ts b/x-pack/plugins/maps/common/content_management/v1/types.ts index 033dec61cc8a0..2b8935ed45ab3 100644 --- a/x-pack/plugins/maps/common/content_management/v1/types.ts +++ b/x-pack/plugins/maps/common/content_management/v1/types.ts @@ -35,13 +35,13 @@ export type MapGetOut = MapCrudTypes['GetOut']; export type MapCreateIn = MapCrudTypes['CreateIn']; export type MapCreateOut = MapCrudTypes['CreateOut']; -export type MapCreateOptions = CreateOptions; +export type MapCreateOptions = Pick; // ----------- UPDATE -------------- export type MapUpdateIn = MapCrudTypes['UpdateIn']; export type MapUpdateOut = MapCrudTypes['UpdateOut']; -export type MapUpdateOptions = UpdateOptions; +export type MapUpdateOptions = Pick; // ----------- DELETE -------------- From 907ce41572a0a9b2d5aa8034676c4bc074574d20 Mon Sep 17 00:00:00 2001 From: kibanamachine <42973632+kibanamachine@users.noreply.github.com> Date: Thu, 20 Apr 2023 21:18:55 +0000 Subject: [PATCH 20/27] [CI] Auto-commit changed files from 'node scripts/lint_ts_projects --fix' --- packages/kbn-content-management-utils/tsconfig.json | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/packages/kbn-content-management-utils/tsconfig.json b/packages/kbn-content-management-utils/tsconfig.json index dad37ec28ac76..7de04c3c13451 100644 --- a/packages/kbn-content-management-utils/tsconfig.json +++ b/packages/kbn-content-management-utils/tsconfig.json @@ -15,5 +15,8 @@ "exclude": [ "target/**/*" ], - "kbn_references": ["@kbn/content-management-plugin"] + "kbn_references": [ + "@kbn/content-management-plugin", + "@kbn/core-saved-objects-api-server", + ] } From c3d72110dacce976e807be767f28d94563556514 Mon Sep 17 00:00:00 2001 From: Matt Kime Date: Thu, 20 Apr 2023 18:07:34 -0500 Subject: [PATCH 21/27] fix SO types for config --- .../kbn-content-management-utils/types.ts | 27 +++++++++++++++---- .../common/content_management/v1/types.ts | 24 ++++++++++------- .../public/content_management/maps_client.ts | 11 ++++---- 3 files changed, 43 insertions(+), 19 deletions(-) diff --git a/packages/kbn-content-management-utils/types.ts b/packages/kbn-content-management-utils/types.ts index 7185fe6311172..1f15c69947f63 100644 --- a/packages/kbn-content-management-utils/types.ts +++ b/packages/kbn-content-management-utils/types.ts @@ -40,7 +40,7 @@ export interface Reference { } /** Saved Object create options - Pick and Omit to customize */ -export interface CreateOptions { +export interface SavedObjectCreateOptions { /** (not recommended) Specify an id for the document */ id?: string; /** Overwrite existing documents (defaults to false) */ @@ -68,7 +68,7 @@ export interface CreateOptions { } /** Saved Object search options - Pick and Omit to customize */ -export interface SearchOptions { +export interface SavedObjectSearchOptions { /** the page of results to return */ page?: number; /** the number of objects per page */ @@ -151,7 +151,7 @@ export interface SearchOptions { } /** Saved Object update options - Pick and Omit to customize */ -export interface UpdateOptions { +export interface SavedObjectUpdateOptions { /** Array of referenced saved objects. */ references?: Reference[]; version?: string; @@ -210,7 +210,13 @@ type PartialItem = Omit< * @argument ContentType - content management type. assumed to be the same as saved object type * @argument Attributes - attributes of the saved object */ -export interface ContentManagementCrudTypes { +export interface ContentManagementCrudTypes< + ContentType extends string, + Attributes extends object, + CreateOptions extends object, + UpdateOptions extends object, + SearchOptions extends object +> { /** * Complete saved object */ @@ -219,7 +225,18 @@ export interface ContentManagementCrudTypes; - + /** + * Create options + */ + CreateOptions: CreateOptions; + /** + * Update options + */ + UpdateOptions: UpdateOptions; + /** + * Search options + */ + SearchOptions: SearchOptions; /** * Get item params */ diff --git a/x-pack/plugins/maps/common/content_management/v1/types.ts b/x-pack/plugins/maps/common/content_management/v1/types.ts index 2b8935ed45ab3..c19713e641659 100644 --- a/x-pack/plugins/maps/common/content_management/v1/types.ts +++ b/x-pack/plugins/maps/common/content_management/v1/types.ts @@ -7,12 +7,21 @@ import type { ContentManagementCrudTypes, - CreateOptions, - UpdateOptions, + SavedObjectCreateOptions, + SavedObjectUpdateOptions, } from '@kbn/content-management-utils'; import { MapContentType } from '../types'; -export type MapCrudTypes = ContentManagementCrudTypes; +export type MapCrudTypes = ContentManagementCrudTypes< + MapContentType, + MapAttributes, + Pick, + Pick, + { + /** Flag to indicate to only search the text on the "title" field */ + onlyTitle?: boolean; + } +>; /* eslint-disable-next-line @typescript-eslint/consistent-type-definitions */ export type MapAttributes = { @@ -35,13 +44,13 @@ export type MapGetOut = MapCrudTypes['GetOut']; export type MapCreateIn = MapCrudTypes['CreateIn']; export type MapCreateOut = MapCrudTypes['CreateOut']; -export type MapCreateOptions = Pick; +export type MapCreateOptions = MapCrudTypes['CreateOptions']; // ----------- UPDATE -------------- export type MapUpdateIn = MapCrudTypes['UpdateIn']; export type MapUpdateOut = MapCrudTypes['UpdateOut']; -export type MapUpdateOptions = Pick; +export type MapUpdateOptions = MapCrudTypes['UpdateOptions']; // ----------- DELETE -------------- @@ -52,7 +61,4 @@ export type MapDeleteOut = MapCrudTypes['DeleteOut']; export type MapSearchIn = MapCrudTypes['SearchIn']; export type MapSearchOut = MapCrudTypes['SearchOut']; -export interface MapSearchOptions { - /** Flag to indicate to only search the text on the "title" field */ - onlyTitle?: boolean; -} +export type MapSearchOptions = MapCrudTypes['SearchOptions']; diff --git a/x-pack/plugins/maps/public/content_management/maps_client.ts b/x-pack/plugins/maps/public/content_management/maps_client.ts index 932765899da22..065d44fdc0681 100644 --- a/x-pack/plugins/maps/public/content_management/maps_client.ts +++ b/x-pack/plugins/maps/public/content_management/maps_client.ts @@ -19,18 +19,19 @@ import type { MapSearchOut, MapSearchOptions, } from '../../common/content_management'; +import { CONTENT_ID as contentTypeId } from '../../common/content_management'; import { getContentManagement } from '../kibana_services'; const get = async (id: string) => { return getContentManagement().client.get({ - contentTypeId: 'map', + contentTypeId, id, }); }; const create = async ({ data, options }: Omit) => { const res = await getContentManagement().client.create({ - contentTypeId: 'map', + contentTypeId, data, options, }); @@ -39,7 +40,7 @@ const create = async ({ data, options }: Omit) => const update = async ({ id, data, options }: Omit) => { const res = await getContentManagement().client.update({ - contentTypeId: 'map', + contentTypeId, id, data, options, @@ -49,14 +50,14 @@ const update = async ({ id, data, options }: Omit) const deleteMap = async (id: string) => { await getContentManagement().client.delete({ - contentTypeId: 'map', + contentTypeId, id, }); }; const search = async (query: SearchQuery = {}, options?: MapSearchOptions) => { return getContentManagement().client.search({ - contentTypeId: 'map', + contentTypeId, query, options, }); From ef07c0a5849e640614d5fb9b5e592465917b6b46 Mon Sep 17 00:00:00 2001 From: kibanamachine <42973632+kibanamachine@users.noreply.github.com> Date: Mon, 24 Apr 2023 20:49:07 +0000 Subject: [PATCH 22/27] [CI] Auto-commit changed files from 'node scripts/generate codeowners' --- .github/CODEOWNERS | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/CODEOWNERS b/.github/CODEOWNERS index afbb6c7c94064..2da78ed653a0d 100644 --- a/.github/CODEOWNERS +++ b/.github/CODEOWNERS @@ -88,7 +88,7 @@ packages/content-management/content_editor @elastic/appex-sharedux examples/content_management_examples @elastic/appex-sharedux src/plugins/content_management @elastic/appex-sharedux packages/content-management/table_list @elastic/appex-sharedux -packages/kbn-content-management-utils @elastic/appex-sharedux +packages/kbn-content-management-utils @elastic/kibana-data-discovery examples/controls_example @elastic/kibana-presentation src/plugins/controls @elastic/kibana-presentation src/core @elastic/kibana-core From 45642ea5853d403a0da63c53150d8d783dd3226a Mon Sep 17 00:00:00 2001 From: Matt Kime Date: Mon, 24 Apr 2023 16:35:10 -0500 Subject: [PATCH 23/27] cleanup --- packages/kbn-content-management-utils/src/schema.ts | 13 ------------- 1 file changed, 13 deletions(-) diff --git a/packages/kbn-content-management-utils/src/schema.ts b/packages/kbn-content-management-utils/src/schema.ts index 2a793f7c29538..639695ebec813 100644 --- a/packages/kbn-content-management-utils/src/schema.ts +++ b/packages/kbn-content-management-utils/src/schema.ts @@ -25,19 +25,6 @@ export const referenceSchema = schema.object( export const referencesSchema = schema.arrayOf(referenceSchema); -/* -export const mapAttributesSchema = schema.object( - { - title: schema.string(), - description: schema.maybe(schema.string()), - mapStateJSON: schema.maybe(schema.string()), - layerListJSON: schema.maybe(schema.string()), - uiStateJSON: schema.maybe(schema.string()), - }, - { unknowns: 'forbid' } -); -*/ - export const savedObjectSchema = (attributesSchema: ObjectType) => schema.object( { From 744c083c7d9e7fb92af63be9420ffd1d9627a67d Mon Sep 17 00:00:00 2001 From: Matt Kime Date: Mon, 24 Apr 2023 18:00:18 -0500 Subject: [PATCH 24/27] more complete schema --- .../src/schema.ts | 40 +++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/packages/kbn-content-management-utils/src/schema.ts b/packages/kbn-content-management-utils/src/schema.ts index 639695ebec813..a13a224f60a35 100644 --- a/packages/kbn-content-management-utils/src/schema.ts +++ b/packages/kbn-content-management-utils/src/schema.ts @@ -68,9 +68,49 @@ export const objectTypeToGetResultSchema = (soSchema: ObjectType) => ); export const createOptionsSchema = schema.object({ + id: schema.maybe(schema.string()), references: schema.maybe(referencesSchema), + overwrite: schema.maybe(schema.boolean()), + version: schema.maybe(schema.string()), + refresh: schema.maybe(schema.boolean()), + initialNamespaces: schema.maybe(schema.arrayOf(schema.string())), }); +export const schemaAndOr = schema.oneOf([schema.literal('AND'), schema.literal('OR')]); + +export const searchOptionsSchema = schema.object({ + page: schema.maybe(schema.number()), + perPage: schema.maybe(schema.number()), + sortField: schema.maybe(schema.string()), + sortOrder: schema.maybe(schema.oneOf([schema.literal('asc'), schema.literal('desc')])), + fields: schema.maybe(schema.arrayOf(schema.string())), + search: schema.maybe(schema.string()), + searchFields: schema.maybe(schema.oneOf([schema.string(), schema.arrayOf(schema.string())])), + rootSearchFields: schema.maybe(schema.arrayOf(schema.string())), + + hasReference: schema.maybe(schema.oneOf([referenceSchema, schema.arrayOf(referenceSchema)])), + hasReferenceOperator: schema.maybe(schemaAndOr), + hasNoReference: schema.maybe(schema.oneOf([referenceSchema, schema.arrayOf(referenceSchema)])), + hasNoReferenceOperator: schema.maybe(schemaAndOr), + defaultSearchOperator: schema.maybe(schemaAndOr), + namespaces: schema.maybe(schema.arrayOf(schema.string())), + type: schema.maybe(schema.string()), + + filter: schema.maybe(schema.string()), + pit: schema.maybe( + schema.object({ id: schema.string(), keepAlive: schema.maybe(schema.string()) }) + ), +}); + +export const updateOptionsSchema = (attributesSchema: ObjectType) => + schema.object({ + references: schema.maybe(referencesSchema), + version: schema.maybe(schema.string()), + refresh: schema.maybe(schema.oneOf([schema.boolean(), schema.literal('wait_for')])), + upsert: schema.maybe(savedObjectSchema(attributesSchema)), + retryOnConflict: schema.maybe(schema.number()), + }); + export const createResultSchema = (soSchema: ObjectType) => schema.object( { From 2a856883f6a3c2ccd973eff564c76636f873a24e Mon Sep 17 00:00:00 2001 From: Matt Kime Date: Mon, 24 Apr 2023 21:04:33 -0500 Subject: [PATCH 25/27] add notes --- packages/kbn-content-management-utils/src/schema.ts | 3 +++ 1 file changed, 3 insertions(+) diff --git a/packages/kbn-content-management-utils/src/schema.ts b/packages/kbn-content-management-utils/src/schema.ts index a13a224f60a35..18b373f405072 100644 --- a/packages/kbn-content-management-utils/src/schema.ts +++ b/packages/kbn-content-management-utils/src/schema.ts @@ -67,6 +67,7 @@ export const objectTypeToGetResultSchema = (soSchema: ObjectType) => { unknowns: 'forbid' } ); +// its recommended to create a subset of this schema for stricter validation export const createOptionsSchema = schema.object({ id: schema.maybe(schema.string()), references: schema.maybe(referencesSchema), @@ -78,6 +79,7 @@ export const createOptionsSchema = schema.object({ export const schemaAndOr = schema.oneOf([schema.literal('AND'), schema.literal('OR')]); +// its recommended to create a subset of this schema for stricter validation export const searchOptionsSchema = schema.object({ page: schema.maybe(schema.number()), perPage: schema.maybe(schema.number()), @@ -102,6 +104,7 @@ export const searchOptionsSchema = schema.object({ ), }); +// its recommended to create a subset of this schema for stricter validation export const updateOptionsSchema = (attributesSchema: ObjectType) => schema.object({ references: schema.maybe(referencesSchema), From 0d9d35df52f7b5a39f5b95b15d964c7f79c0da02 Mon Sep 17 00:00:00 2001 From: Matt Kime Date: Mon, 24 Apr 2023 21:10:45 -0500 Subject: [PATCH 26/27] remove mistaken duplicate types --- .../kbn-content-management-utils/types.ts | 283 ------------------ 1 file changed, 283 deletions(-) delete mode 100644 packages/kbn-content-management-utils/types.ts diff --git a/packages/kbn-content-management-utils/types.ts b/packages/kbn-content-management-utils/types.ts deleted file mode 100644 index 1f15c69947f63..0000000000000 --- a/packages/kbn-content-management-utils/types.ts +++ /dev/null @@ -1,283 +0,0 @@ -/* - * 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 { - GetIn, - GetResult, - CreateIn, - CreateResult, - SearchIn, - SearchResult, - UpdateIn, - UpdateResult, - DeleteIn, - DeleteResult, -} from '@kbn/content-management-plugin/common'; - -import type { - SortOrder, - AggregationsAggregationContainer, - SortResults, -} from '@elastic/elasticsearch/lib/api/typesWithBodyKey'; - -import { - MutatingOperationRefreshSetting, - SavedObjectsPitParams, - SavedObjectsFindOptionsReference, -} from '@kbn/core-saved-objects-api-server'; - -type KueryNode = any; - -export interface Reference { - type: string; - id: string; - name: string; -} - -/** Saved Object create options - Pick and Omit to customize */ -export interface SavedObjectCreateOptions { - /** (not recommended) Specify an id for the document */ - id?: string; - /** Overwrite existing documents (defaults to false) */ - overwrite?: boolean; - /** - * An opaque version number which changes on each successful write operation. - * Can be used in conjunction with `overwrite` for implementing optimistic concurrency control. - **/ - version?: string; - /** Array of referenced saved objects. */ - references?: Reference[]; - /** The Elasticsearch Refresh setting for this operation */ - refresh?: boolean; - /** - * Optional initial namespaces for the object to be created in. If this is defined, it will supersede the namespace ID that is in - * {@link SavedObjectsCreateOptions}. - * - * * For shareable object types (registered with `namespaceType: 'multiple'`): this option can be used to specify one or more spaces, - * including the "All spaces" identifier (`'*'`). - * * For isolated object types (registered with `namespaceType: 'single'` or `namespaceType: 'multiple-isolated'`): this option can only - * be used to specify a single space, and the "All spaces" identifier (`'*'`) is not allowed. - * * For global object types (registered with `namespaceType: 'agnostic'`): this option cannot be used. - */ - initialNamespaces?: string[]; -} - -/** Saved Object search options - Pick and Omit to customize */ -export interface SavedObjectSearchOptions { - /** the page of results to return */ - page?: number; - /** the number of objects per page */ - perPage?: number; - /** which field to sort by */ - sortField?: string; - /** sort order, ascending or descending */ - sortOrder?: SortOrder; - /** - * An array of fields to include in the results - * @example - * SavedObjects.find({type: 'dashboard', fields: ['attributes.name', 'attributes.location']}) - */ - fields?: string[]; - /** Search documents using the Elasticsearch Simple Query String syntax. See Elasticsearch Simple Query String `query` argument for more information */ - search?: string; - /** The fields to perform the parsed query against. See Elasticsearch Simple Query String `fields` argument for more information */ - searchFields?: string[]; - /** - * Use the sort values from the previous page to retrieve the next page of results. - */ - searchAfter?: SortResults; - /** - * The fields to perform the parsed query against. Unlike the `searchFields` argument, these are expected to be root fields and will not - * be modified. If used in conjunction with `searchFields`, both are concatenated together. - */ - rootSearchFields?: string[]; - /** - * Search for documents having a reference to the specified objects. - * Use `hasReferenceOperator` to specify the operator to use when searching for multiple references. - */ - hasReference?: SavedObjectsFindOptionsReference | SavedObjectsFindOptionsReference[]; - /** - * The operator to use when searching by multiple references using the `hasReference` option. Defaults to `OR` - */ - hasReferenceOperator?: 'AND' | 'OR'; - /** - * Search for documents *not* having a reference to the specified objects. - * Use `hasNoReferenceOperator` to specify the operator to use when searching for multiple references. - */ - hasNoReference?: SavedObjectsFindOptionsReference | SavedObjectsFindOptionsReference[]; - /** - * The operator to use when searching by multiple references using the `hasNoReference` option. Defaults to `OR` - */ - hasNoReferenceOperator?: 'AND' | 'OR'; - /** - * The search operator to use with the provided filter. Defaults to `OR` - */ - defaultSearchOperator?: 'AND' | 'OR'; - /** filter string for the search query */ - filter?: string | KueryNode; - /** - * A record of aggregations to perform. - * The API currently only supports a limited set of metrics and bucket aggregation types. - * Additional aggregation types can be contributed to Core. - * - * @example - * Aggregating on SO attribute field - * ```ts - * const aggs = { latest_version: { max: { field: 'dashboard.attributes.version' } } }; - * return client.find({ type: 'dashboard', aggs }) - * ``` - * - * @example - * Aggregating on SO root field - * ```ts - * const aggs = { latest_update: { max: { field: 'dashboard.updated_at' } } }; - * return client.find({ type: 'dashboard', aggs }) - * ``` - * - * @alpha - */ - aggs?: Record; - /** array of namespaces to search */ - namespaces?: string[]; - /** - * Search against a specific Point In Time (PIT) that you've opened with {@link SavedObjectsClient.openPointInTimeForType}. - */ - pit?: SavedObjectsPitParams; -} - -/** Saved Object update options - Pick and Omit to customize */ -export interface SavedObjectUpdateOptions { - /** Array of referenced saved objects. */ - references?: Reference[]; - version?: string; - /** The Elasticsearch Refresh setting for this operation */ - refresh?: MutatingOperationRefreshSetting; - /** If specified, will be used to perform an upsert if the object doesn't exist */ - upsert?: Attributes; - /** - * The Elasticsearch `retry_on_conflict` setting for this operation. - * Defaults to `0` when `version` is provided, `3` otherwise. - */ - retryOnConflict?: number; -} - -/** Return value for Saved Object get, T is item returned */ -export type GetResultSO = GetResult< - T, - { - outcome: 'exactMatch' | 'aliasMatch' | 'conflict'; - aliasTargetId?: string; - aliasPurpose?: 'savedObjectConversion' | 'savedObjectImport'; - } ->; - -/** - * Saved object with metadata - */ -export interface SOWithMetadata { - id: string; - type: string; - version?: string; - createdAt?: string; - updatedAt?: string; - error?: { - error: string; - message: string; - statusCode: number; - metadata?: Record; - }; - attributes: Attributes; - references: Reference[]; - namespaces?: string[]; - originId?: string; -} - -type PartialItem = Omit< - SOWithMetadata, - 'attributes' | 'references' -> & { - attributes: Partial; - references: Reference[] | undefined; -}; - -/** - * Types used by content management storage - * @argument ContentType - content management type. assumed to be the same as saved object type - * @argument Attributes - attributes of the saved object - */ -export interface ContentManagementCrudTypes< - ContentType extends string, - Attributes extends object, - CreateOptions extends object, - UpdateOptions extends object, - SearchOptions extends object -> { - /** - * Complete saved object - */ - Item: SOWithMetadata; - /** - * Partial saved object, used as output for update - */ - PartialItem: PartialItem; - /** - * Create options - */ - CreateOptions: CreateOptions; - /** - * Update options - */ - UpdateOptions: UpdateOptions; - /** - * Search options - */ - SearchOptions: SearchOptions; - /** - * Get item params - */ - GetIn: GetIn; - /** - * Get item result - */ - GetOut: GetResultSO>; - /** - * Create item params - */ - CreateIn: CreateIn; - /** - * Create item result - */ - CreateOut: CreateResult>; - - /** - * Search item params - */ - SearchIn: SearchIn; - /** - * Search item result - */ - SearchOut: SearchResult>; - - /** - * Update item params - */ - UpdateIn: UpdateIn; - /** - * Update item result - */ - UpdateOut: UpdateResult>; - - /** - * Delete item params - */ - DeleteIn: DeleteIn; - /** - * Delete item result - */ - DeleteOut: DeleteResult; -} From dda5f23796fd0867785b0aaed569ca18c4549e67 Mon Sep 17 00:00:00 2001 From: Matt Kime Date: Wed, 26 Apr 2023 21:51:05 -0500 Subject: [PATCH 27/27] better importing of partial schema --- .../src/schema.ts | 23 +++++++++---------- .../content_management/v1/cm_services.ts | 6 ++++- 2 files changed, 16 insertions(+), 13 deletions(-) diff --git a/packages/kbn-content-management-utils/src/schema.ts b/packages/kbn-content-management-utils/src/schema.ts index 18b373f405072..2e95624c36a22 100644 --- a/packages/kbn-content-management-utils/src/schema.ts +++ b/packages/kbn-content-management-utils/src/schema.ts @@ -68,19 +68,19 @@ export const objectTypeToGetResultSchema = (soSchema: ObjectType) => ); // its recommended to create a subset of this schema for stricter validation -export const createOptionsSchema = schema.object({ +export const createOptionsSchemas = { id: schema.maybe(schema.string()), references: schema.maybe(referencesSchema), overwrite: schema.maybe(schema.boolean()), version: schema.maybe(schema.string()), refresh: schema.maybe(schema.boolean()), initialNamespaces: schema.maybe(schema.arrayOf(schema.string())), -}); +}; export const schemaAndOr = schema.oneOf([schema.literal('AND'), schema.literal('OR')]); // its recommended to create a subset of this schema for stricter validation -export const searchOptionsSchema = schema.object({ +export const searchOptionsSchemas = { page: schema.maybe(schema.number()), perPage: schema.maybe(schema.number()), sortField: schema.maybe(schema.string()), @@ -102,17 +102,16 @@ export const searchOptionsSchema = schema.object({ pit: schema.maybe( schema.object({ id: schema.string(), keepAlive: schema.maybe(schema.string()) }) ), -}); +}; // its recommended to create a subset of this schema for stricter validation -export const updateOptionsSchema = (attributesSchema: ObjectType) => - schema.object({ - references: schema.maybe(referencesSchema), - version: schema.maybe(schema.string()), - refresh: schema.maybe(schema.oneOf([schema.boolean(), schema.literal('wait_for')])), - upsert: schema.maybe(savedObjectSchema(attributesSchema)), - retryOnConflict: schema.maybe(schema.number()), - }); +export const updateOptionsSchema = { + references: schema.maybe(referencesSchema), + version: schema.maybe(schema.string()), + refresh: schema.maybe(schema.oneOf([schema.boolean(), schema.literal('wait_for')])), + upsert: (attributesSchema: ObjectType) => schema.maybe(savedObjectSchema(attributesSchema)), + retryOnConflict: schema.maybe(schema.number()), +}; export const createResultSchema = (soSchema: ObjectType) => schema.object( diff --git a/x-pack/plugins/maps/common/content_management/v1/cm_services.ts b/x-pack/plugins/maps/common/content_management/v1/cm_services.ts index ce7ea87edab08..65d2e3082da7d 100644 --- a/x-pack/plugins/maps/common/content_management/v1/cm_services.ts +++ b/x-pack/plugins/maps/common/content_management/v1/cm_services.ts @@ -9,7 +9,7 @@ import type { ContentManagementServicesDefinition as ServicesDefinition } from ' import { savedObjectSchema, objectTypeToGetResultSchema, - createOptionsSchema, + createOptionsSchemas, createResultSchema, } from '@kbn/content-management-utils'; @@ -35,6 +35,10 @@ const searchOptionsSchema = schema.maybe( ) ); +const createOptionsSchema = schema.object({ + references: schema.maybe(createOptionsSchemas.references), +}); + // Content management service definition. // We need it for BWC support between different versions of the content export const serviceDefinition: ServicesDefinition = {