From 395f6d876564a1cb6fdaefeb6bbe03992f626b00 Mon Sep 17 00:00:00 2001 From: vadimkibana Date: Fri, 30 Jul 2021 22:27:24 +0200 Subject: [PATCH] improve saved object url storage --- .../common/url_service/short_urls/types.ts | 8 --- .../storage/saved_object_short_url_storage.ts | 62 ++++++++++++++----- 2 files changed, 45 insertions(+), 25 deletions(-) diff --git a/src/plugins/share/common/url_service/short_urls/types.ts b/src/plugins/share/common/url_service/short_urls/types.ts index 7e6986be8de9c..9ad4edac1adc7 100644 --- a/src/plugins/share/common/url_service/short_urls/types.ts +++ b/src/plugins/share/common/url_service/short_urls/types.ts @@ -102,14 +102,6 @@ export interface ShortUrlData; - - /** - * Legacy field - was used in old short URL versions. This field will - * be removed in a future by a migration. - * - * @deprecated - */ - readonly url: string; } /** diff --git a/src/plugins/share/server/url_service/short_urls/storage/saved_object_short_url_storage.ts b/src/plugins/share/server/url_service/short_urls/storage/saved_object_short_url_storage.ts index 9cf25704537b6..c2f4cef720b5b 100644 --- a/src/plugins/share/server/url_service/short_urls/storage/saved_object_short_url_storage.ts +++ b/src/plugins/share/server/url_service/short_urls/storage/saved_object_short_url_storage.ts @@ -13,15 +13,7 @@ import { ShortUrlStorage } from '../types'; export type ShortUrlSavedObject = SavedObject; -/** - * Fields that stored in the short url saved object. - */ -export interface ShortUrlSavedObjectAttributes { - /** - * The slug of the short URL, the part after the `/` in the URL. - */ - readonly slug: string; - +export interface ShortUrlSavedObjectAttributesBase { /** * Number of times the short URL has been resolved. */ @@ -36,33 +28,69 @@ export interface ShortUrlSavedObjectAttributes { * The timestamp when the short URL was created. */ readonly createDate: number; +} - /** - * Serialized locator state. - */ - readonly locatorJSON: string; - +export interface ShortUrlSavedObjectAttributesV1 extends ShortUrlSavedObjectAttributesBase { /** * Legacy field - was used in old short URL versions. This field will - * be removed in a future by a migration. + * be removed in the future by a migration. * * @deprecated */ readonly url: string; } +/** + * Fields that stored in the short url saved object. + */ +export interface ShortUrlSavedObjectAttributesV2 extends ShortUrlSavedObjectAttributesBase { + /** + * The slug of the short URL, the part after the `/` in the URL. + */ + readonly slug: string; + + /** + * Serialized locator state. + */ + readonly locatorJSON: string; +} + +export type ShortUrlSavedObjectAttributes = + | ShortUrlSavedObjectAttributesV1 + | ShortUrlSavedObjectAttributesV2; + +const isShortUrlV1 = (x: ShortUrlSavedObjectAttributes): x is ShortUrlSavedObjectAttributesV1 => + typeof (x as ShortUrlSavedObjectAttributesV1).url === 'string'; + const createShortUrlData =

( savedObject: ShortUrlSavedObject ): ShortUrlData

=> { const attributes = savedObject.attributes; + + if (isShortUrlV1(attributes)) { + const { url, ...rest } = attributes; + const state = ({ url } as unknown) as P; + + return { + id: savedObject.id, + slug: savedObject.id, + locator: { + id: 'FILL_THIS_IN', + version: 'FILL_THIS_IN', + state, + }, + ...rest, + } as ShortUrlData

; + } + const { locatorJSON, ...rest } = attributes; const locator = JSON.parse(locatorJSON) as ShortUrlData

['locator']; return { id: savedObject.id, - ...rest, locator, - }; + ...rest, + } as ShortUrlData

; }; const createAttributes =

(