-
Notifications
You must be signed in to change notification settings - Fork 8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Demo][Dashboard] Public create
API
#181950
Draft
jloleysens
wants to merge
7
commits into
elastic:main
Choose a base branch
from
jloleysens:dashboard/public-create-endpoint
base: main
Could not load branches
Branch not found: {{ refName }}
Could not load tags
Nothing to show
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+185
−1
Draft
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
633e058
wip
jloleysens e5ec87c
more wip
jloleysens f4cceca
register endpoint
jloleysens 92df82a
simplify
jloleysens 7f2c07b
update demo based on Antons feedback
jloleysens 4bf0113
remove unneeded changes
jloleysens 774e872
pass req handler ctx
jloleysens File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
/* | ||
* 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, type TypeOf } from '@kbn/config-schema'; | ||
|
||
const controlGroupInputSchema = schema | ||
.object({ | ||
panelsJSON: schema.maybe(schema.string({ meta: { description: 'Lorem ipsum' } })), | ||
controlStyle: schema.maybe(schema.string({ meta: { description: 'Lorem ipsum' } })), | ||
chainingSystem: schema.maybe(schema.string({ meta: { description: 'Lorem ipsum' } })), | ||
ignoreParentSettingsJSON: schema.maybe(schema.string({ meta: { description: 'Lorem ipsum' } })), | ||
}) | ||
.extends({}, { unknowns: 'ignore' }); | ||
|
||
const baseDashboard = schema.object({ | ||
// General | ||
title: schema.string({ meta: { description: 'A human-readable title for the dashboard' } }), | ||
description: schema.string({ defaultValue: '', meta: { description: 'A short description.' } }), | ||
|
||
// Search | ||
kibanaSavedObjectMeta: schema.object( | ||
{ | ||
searchSourceJSON: schema.maybe( | ||
schema.string({ | ||
meta: { | ||
description: 'A string containing search source', | ||
}, | ||
}) | ||
), | ||
}, | ||
{ | ||
meta: { | ||
description: 'A container for various metadata', | ||
}, | ||
} | ||
), | ||
|
||
// Time | ||
timeRestore: schema.maybe( | ||
schema.boolean({ meta: { description: 'Whether to restore time upon viewing this dashboard' } }) | ||
), | ||
timeFrom: schema.maybe( | ||
schema.string({ meta: { description: 'An ISO string indicating when to restore time from' } }) | ||
), | ||
timeTo: schema.maybe( | ||
schema.string({ meta: { description: 'An ISO string indicating when to restore time from' } }) | ||
), | ||
refreshInterval: schema.maybe( | ||
schema.object( | ||
{ | ||
pause: schema.boolean({ | ||
meta: { | ||
description: | ||
'Whether the refresh interval is set to be paused while viewing the dashboard.', | ||
}, | ||
}), | ||
value: schema.number({ | ||
meta: { | ||
description: | ||
'A numeric value indicating refresh frequency expressed as, for example, 30s.', | ||
}, | ||
}), | ||
display: schema.maybe( | ||
schema.string({ | ||
meta: { | ||
description: 'Lorem ipsum', | ||
}, | ||
}) | ||
), | ||
section: schema.maybe( | ||
schema.number({ | ||
meta: { | ||
description: 'Lorem ipsum', | ||
}, | ||
}) | ||
), | ||
}, | ||
{ | ||
meta: { | ||
description: 'A container for various refresh interval settings', | ||
}, | ||
} | ||
) | ||
), | ||
|
||
// Dashboard Content | ||
controlGroupInput: schema.maybe(controlGroupInputSchema), | ||
panelsJSON: schema.string({ defaultValue: '[]', meta: { description: 'Lorem ipsum' } }), | ||
optionsJSON: schema.string({ defaultValue: '{}', meta: { description: 'Lorem ipsum' } }), | ||
|
||
// Legacy | ||
// hits: schema.maybe(schema.number()), | ||
// version: schema.maybe(schema.number()), | ||
}); | ||
|
||
export const dashboardCreate = baseDashboard.extends( | ||
{ | ||
id: schema.maybe(schema.string({ meta: { description: 'The ID of the dashboard' } })), | ||
}, | ||
{ | ||
meta: { id: 'dashboard.create' }, | ||
} | ||
); | ||
|
||
export const dashboard = baseDashboard.extends( | ||
{ | ||
id: schema.string({ meta: { description: 'The ID of the dashboard' } }), | ||
}, | ||
{ | ||
meta: { id: 'dashboard' }, | ||
} | ||
); | ||
|
||
export type Dashboard = TypeOf<typeof dashboard>; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 * as v2023_10_31 from './2023_10_31'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
/* | ||
* 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 { RequestHandlerContext } from '@kbn/core-http-request-handler-context-server'; | ||
import type { VersionedRouter } from '@kbn/core-http-server'; | ||
import type { ContentManagementServerSetup } from '@kbn/content-management-plugin/server'; | ||
import { CONTENT_ID } from '@kbn/visualizations-plugin/common/content_management'; | ||
import type { Dashboard } from '../../common/api/2023_10_31'; | ||
import { v2023_10_31 } from '../../common/api'; | ||
|
||
export function registerCreate( | ||
router: VersionedRouter<RequestHandlerContext>, | ||
cm: ContentManagementServerSetup | ||
) { | ||
router | ||
.post({ | ||
path: '/api/dashboard', | ||
access: 'public', | ||
description: 'Create a dashboard', | ||
}) | ||
.addVersion( | ||
{ | ||
version: '2023-10-31', | ||
validate: { | ||
request: { | ||
body: v2023_10_31.dashboardCreate, | ||
}, | ||
response: { | ||
200: { body: v2023_10_31.dashboard }, | ||
}, | ||
}, | ||
}, | ||
async (ctx, req, res) => { | ||
const client = cm.contentClient | ||
.getForRequest({ request: req, requestHandlerContext: ctx }) | ||
.for<Dashboard>(CONTENT_ID); | ||
let result: Dashboard; | ||
try { | ||
const cmResult = await client.create(req.body); | ||
result = cmResult.result as unknown as Dashboard; | ||
} catch (e) { | ||
// do some handling; | ||
throw e; | ||
} | ||
return res.ok({ body: result }); | ||
} | ||
); | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Seems like there is a gap in the
Dashboard
type being passed through so I was gettingobject
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
cc @vadimkibana @sebelga
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To be investigated, in the code it looks like we are forwarding the type in
CreateItemResponse<T>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think the problem is
data: object
does not let the return type infer the type of the generic to something other thanobject
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
But here we are talking about the response right? You might be right but it is strange the the input type affects the output type. Although I've seen weirder things with TS 😄