Skip to content

Commit

Permalink
feat: optimize code
Browse files Browse the repository at this point in the history
Signed-off-by: SuZhou-Joe <[email protected]>
  • Loading branch information
SuZhou-Joe committed Mar 9, 2024
1 parent e202085 commit 9685b71
Showing 1 changed file with 14 additions and 22 deletions.
36 changes: 14 additions & 22 deletions src/core/public/saved_objects/saved_objects_client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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')) {
Expand All @@ -206,7 +206,13 @@ export class SavedObjectsClient {
finalWorkspaces = [currentWorkspaceId];
}

return finalWorkspaces;
if (finalWorkspaces) {
return {
workspaces: finalWorkspaces,
};
}

return {};
}

/**
Expand Down Expand Up @@ -278,20 +284,14 @@ export class SavedObjectsClient {
overwrite: options.overwrite,
};

const finalWorkspaces = this.formatWorkspacesParams(options);

const createRequest: Promise<SavedObject<T>> = this.savedObjectsFetch(path, {
method: 'POST',
query,
body: JSON.stringify({
attributes,
migrationVersion: options.migrationVersion,
references: options.references,
...(finalWorkspaces
? {
workspaces: finalWorkspaces,
}
: {}),
...this.formatWorkspacesParams(options),
}),
});

Expand All @@ -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<SavedObjectsApi['bulkCreate']> = this.savedObjectsFetch(path, {
method: 'POST',
query,
query: {
...query,
...this.formatWorkspacesParams(options),
},
body: JSON.stringify(objects),
});
return request.then((resp) => {
Expand Down Expand Up @@ -389,15 +387,9 @@ export class SavedObjectsClient {
workspaces: 'workspaces',
};

const finalWorkspaces = this.formatWorkspacesParams(options);

const renamedQuery = renameKeys<SavedObjectsFindOptions, any>(renameMap, {
...options,
...(finalWorkspaces
? {
workspaces: finalWorkspaces,
}
: {}),
...this.formatWorkspacesParams(options),
});
const query = pick.apply(null, [renamedQuery, ...Object.values<string>(renameMap)]) as Partial<
Record<string, any>
Expand Down

0 comments on commit 9685b71

Please sign in to comment.