From 9685b7111afc0544991a506f75526dc6e422b9d8 Mon Sep 17 00:00:00 2001 From: SuZhou-Joe Date: Fri, 8 Mar 2024 15:58:57 +0800 Subject: [PATCH] feat: optimize code Signed-off-by: SuZhou-Joe --- .../saved_objects/saved_objects_client.ts | 36 ++++++++----------- 1 file changed, 14 insertions(+), 22 deletions(-) diff --git a/src/core/public/saved_objects/saved_objects_client.ts b/src/core/public/saved_objects/saved_objects_client.ts index 4fa6d1f87d0a..f07d3890b7fa 100644 --- a/src/core/public/saved_objects/saved_objects_client.ts +++ b/src/core/public/saved_objects/saved_objects_client.ts @@ -197,7 +197,7 @@ export class SavedObjectsClient { */ private formatWorkspacesParams(options: { workspaces?: SavedObjectsCreateOptions['workspaces']; - }) { + }): { workspaces: string[] } | {} { const currentWorkspaceId = this.currentWorkspaceId; let finalWorkspaces; if (options.hasOwnProperty('workspaces')) { @@ -206,7 +206,13 @@ export class SavedObjectsClient { finalWorkspaces = [currentWorkspaceId]; } - return finalWorkspaces; + if (finalWorkspaces) { + return { + workspaces: finalWorkspaces, + }; + } + + return {}; } /** @@ -278,8 +284,6 @@ export class SavedObjectsClient { overwrite: options.overwrite, }; - const finalWorkspaces = this.formatWorkspacesParams(options); - const createRequest: Promise> = this.savedObjectsFetch(path, { method: 'POST', query, @@ -287,11 +291,7 @@ export class SavedObjectsClient { attributes, migrationVersion: options.migrationVersion, references: options.references, - ...(finalWorkspaces - ? { - workspaces: finalWorkspaces, - } - : {}), + ...this.formatWorkspacesParams(options), }), }); @@ -312,15 +312,13 @@ export class SavedObjectsClient { ) => { const path = this.getPath(['_bulk_create']); const query: HttpFetchOptions['query'] = { overwrite: options.overwrite }; - const finalWorkspaces = this.formatWorkspacesParams(options); - - if (finalWorkspaces) { - query.workspaces = finalWorkspaces; - } const request: ReturnType = this.savedObjectsFetch(path, { method: 'POST', - query, + query: { + ...query, + ...this.formatWorkspacesParams(options), + }, body: JSON.stringify(objects), }); return request.then((resp) => { @@ -389,15 +387,9 @@ export class SavedObjectsClient { workspaces: 'workspaces', }; - const finalWorkspaces = this.formatWorkspacesParams(options); - const renamedQuery = renameKeys(renameMap, { ...options, - ...(finalWorkspaces - ? { - workspaces: finalWorkspaces, - } - : {}), + ...this.formatWorkspacesParams(options), }); const query = pick.apply(null, [renamedQuery, ...Object.values(renameMap)]) as Partial< Record