-
Notifications
You must be signed in to change notification settings - Fork 334
implement resources API part 2 #17749
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -23,6 +23,25 @@ import { | |
| */ | ||
| export type ResourceType = K8SResourceType | string; | ||
|
|
||
| /** | ||
| * @interface | ||
| * Data object for creating a new resource. Must include a `type` property. | ||
| * | ||
| * @example | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. probably worth creating an instance of CreateResourceData first and then using it for it's indented purpose. this makes it nice and clear |
||
| * ```ts | ||
| * import { useResources, K8S } from '@shell/apis'; | ||
| * | ||
| * const resources = useResources(); | ||
| * | ||
| * await resources.cluster.create({ | ||
| * type: K8S.CONFIG_MAP, | ||
| * metadata: { name: 'my-config', namespace: 'default' }, | ||
| * data: { key: 'value' } | ||
| * }); | ||
| * ``` | ||
| */ | ||
| export type CreateResourceData = { type: ResourceType } & Record<string, any>; | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this might be more of a preference thing, but why not just... ? |
||
|
|
||
| /** | ||
| * @interface | ||
| * Resources API "findAll" options | ||
|
|
||
| Original file line number | Diff line number | Diff line change | ||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,96 @@ | ||||||||||||
| import { SteveGetResponse } from '@shell/types/rancher/steve.api'; | ||||||||||||
|
|
||||||||||||
| /** | ||||||||||||
| * Instance-level operations available on resources returned by the Resources API. | ||||||||||||
| * | ||||||||||||
| * These methods operate on a specific resource that has already been fetched. | ||||||||||||
| */ | ||||||||||||
| export interface ResourceInstanceApi { | ||||||||||||
| /** | ||||||||||||
| * Applies a partial update to a resource using HTTP PATCH (merge-patch). | ||||||||||||
| * | ||||||||||||
| * Only the fields provided in `data` are sent to the server — the rest of the resource | ||||||||||||
| * remains unchanged. The server response is merged back into this instance. | ||||||||||||
| * | ||||||||||||
| * Requires edit permissions (`canEdit`). | ||||||||||||
| * | ||||||||||||
| * @param data - An object containing only the fields to update. | ||||||||||||
| * @returns a resource instance, updated with the server response. | ||||||||||||
| * | ||||||||||||
| * @example | ||||||||||||
| * ```ts | ||||||||||||
| * import { useResources, K8S } from '@shell/apis'; | ||||||||||||
| * | ||||||||||||
| * const resources = useResources(); | ||||||||||||
| * const configMap = await resources.cluster.find(K8S.CONFIG_MAP, 'default/my-config'); | ||||||||||||
| * | ||||||||||||
| * await configMap.patch({ newKey: 'newValue' }); | ||||||||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||
| * ``` | ||||||||||||
| */ | ||||||||||||
| patch(data: Record<string, any>): Promise<ResourceInstanceApi>; | ||||||||||||
|
|
||||||||||||
| /** | ||||||||||||
| * Performs a full replacement update of a resource using HTTP PUT. | ||||||||||||
| * | ||||||||||||
| * Sends the entire current state of the resource to the server. | ||||||||||||
| * | ||||||||||||
| * Requires edit permissions (`canEdit`). | ||||||||||||
| * | ||||||||||||
| * @returns a resource instance, updated with the server response. | ||||||||||||
| * | ||||||||||||
| * @example | ||||||||||||
| * ```ts | ||||||||||||
| * import { useResources, K8S } from '@shell/apis'; | ||||||||||||
| * | ||||||||||||
| * const resources = useResources(); | ||||||||||||
| * const configMap = await resources.cluster.find(K8S.CONFIG_MAP, 'default/my-config'); | ||||||||||||
| * | ||||||||||||
| * configMap.data.myKey = 'updatedValue'; | ||||||||||||
| * await configMap.update(); | ||||||||||||
| * ``` | ||||||||||||
| */ | ||||||||||||
| update(): Promise<ResourceInstanceApi>; | ||||||||||||
|
|
||||||||||||
| /** | ||||||||||||
| * Deletes a resource instance. | ||||||||||||
| * | ||||||||||||
| * Requires delete permissions (`canDelete`). | ||||||||||||
| * | ||||||||||||
| * @returns A promise that resolves when the resource has been deleted. | ||||||||||||
| * | ||||||||||||
| * @example | ||||||||||||
| * ```ts | ||||||||||||
| * import { useResources, K8S } from '@shell/apis'; | ||||||||||||
| * | ||||||||||||
| * const resources = useResources(); | ||||||||||||
| * const pod = await resources.cluster.find(K8S.POD, 'default/my-pod-123'); | ||||||||||||
| * | ||||||||||||
| * await pod.delete(); | ||||||||||||
| * ``` | ||||||||||||
| */ | ||||||||||||
| delete(): Promise<void>; | ||||||||||||
| } | ||||||||||||
|
|
||||||||||||
| /** | ||||||||||||
| * Represents a single resource instance returned from the Resources API. | ||||||||||||
| * | ||||||||||||
| * Provides instance-level operations such as deleting or updating a resource instance. | ||||||||||||
| * The resource data (metadata, spec, status, etc.) is accessible directly on the instance. | ||||||||||||
| * | ||||||||||||
| * @template T - The shape of the underlying resource data (defaults to SteveGetResponse) | ||||||||||||
| * | ||||||||||||
| * @example | ||||||||||||
| * ```ts | ||||||||||||
| * import { useResources, K8S } from '@shell/apis'; | ||||||||||||
| * | ||||||||||||
| * const resources = useResources(); | ||||||||||||
| * const pod = await resources.cluster.find(K8S.POD, 'my-pod-123'); | ||||||||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||
| * | ||||||||||||
| * // Access resource data directly | ||||||||||||
| * console.log(pod.metadata.name); | ||||||||||||
| * | ||||||||||||
| * // Use instance operations | ||||||||||||
| * await pod.delete(); | ||||||||||||
| * ``` | ||||||||||||
| */ | ||||||||||||
| export type ResourceInstance<T = SteveGetResponse> = T & ResourceInstanceApi; | ||||||||||||
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 don't think we need to surface this as a major api, it's more a facet of the above two