Skip to content

Commit

Permalink
feat(renterd-libs): add patch methods
Browse files Browse the repository at this point in the history
  • Loading branch information
alexfreska committed Nov 19, 2024
1 parent 85902f1 commit 3e2e32e
Show file tree
Hide file tree
Showing 7 changed files with 152 additions and 2 deletions.
7 changes: 7 additions & 0 deletions .changeset/moody-rocks-flow.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
'@siafoundation/renterd-js': minor
'@siafoundation/renterd-react': minor
'@siafoundation/renterd-types': minor
---

Add settings gouging, pinned, s3, and upload patch APIs.
7 changes: 7 additions & 0 deletions .changeset/tiny-islands-fry.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
'@siafoundation/renterd-js': minor
'@siafoundation/renterd-react': minor
'@siafoundation/renterd-types': minor
---

Add autopilot config patch API.
30 changes: 28 additions & 2 deletions libs/renterd-react/src/autopilot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
HookArgsWithPayloadSwr,
delay,
usePostFunc,
usePatchFunc,
} from '@siafoundation/react-core'
import {
AutopilotConfigParams,
Expand All @@ -25,6 +26,9 @@ import {
autopilotConfigRoute,
autopilotStateRoute,
autopilotTriggerRoute,
AutopilotConfigPatchParams,
AutopilotConfigPatchPayload,
AutopilotConfigPatchResponse,
} from '@siafoundation/renterd-types'

export function useAutopilotState(
Expand Down Expand Up @@ -56,8 +60,30 @@ export function useAutopilotConfigUpdate(
{ ...args, route: autopilotConfigRoute },
async (mutate) => {
mutate((key) => key === autopilotConfigRoute)
// might need a delay before revalidating status which returns whether
// or not autopilot is configured
// Might need a delay before revalidating status which returns whether
// or not autopilot is configured.
const func = async () => {
await delay(1000)
mutate((key) => key === autopilotStateRoute)
}
func()
}
)
}

export function useAutopilotConfigPatch(
args?: HookArgsCallback<
AutopilotConfigPatchParams,
AutopilotConfigPatchPayload,
AutopilotConfigPatchResponse
>
) {
return usePatchFunc(
{ ...args, route: autopilotConfigRoute },
async (mutate) => {
mutate((key) => key === autopilotConfigRoute)
// Might need a delay before revalidating status which returns whether
// or not autopilot is configured.
const func = async () => {
await delay(1000)
mutate((key) => key === autopilotStateRoute)
Expand Down
85 changes: 85 additions & 0 deletions libs/renterd-react/src/bus.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
HookArgsCallback,
HookArgsWithPayloadSwr,
delay,
usePatchFunc,
} from '@siafoundation/react-core'
import {
getMainnetBlockHeight,
Expand Down Expand Up @@ -239,6 +240,18 @@ import {
SettingsUploadUpdateParams,
SettingsUploadUpdatePayload,
SettingsUploadUpdateResponse,
SettingsGougingPatchResponse,
SettingsGougingPatchPayload,
SettingsGougingPatchParams,
SettingsPinnedPatchParams,
SettingsPinnedPatchPayload,
SettingsPinnedPatchResponse,
SettingsS3PatchParams,
SettingsS3PatchPayload,
SettingsS3PatchResponse,
SettingsUploadPatchParams,
SettingsUploadPatchPayload,
SettingsUploadPatchResponse,
SettingsPinnedResponse,
busObjectsRemoveRoute,
ConsensusNetworkParams,
Expand Down Expand Up @@ -833,6 +846,78 @@ export function useSettingsUploadUpdate(
)
}

export function useSettingsGougingPatch(
args?: HookArgsCallback<
SettingsGougingPatchParams,
SettingsGougingPatchPayload,
SettingsGougingPatchResponse
>
) {
return usePatchFunc(
{
...args,
route: busSettingsGougingRoute,
},
async (mutate) => {
mutate((key) => key.startsWith(busSettingsGougingRoute))
}
)
}

export function useSettingsPinnedPatch(
args?: HookArgsCallback<
SettingsPinnedPatchParams,
SettingsPinnedPatchPayload,
SettingsPinnedPatchResponse
>
) {
return usePatchFunc(
{
...args,
route: busSettingsPinnedRoute,
},
async (mutate) => {
mutate((key) => key.startsWith(busSettingsPinnedRoute))
}
)
}

export function useSettingsS3Patch(
args?: HookArgsCallback<
SettingsS3PatchParams,
SettingsS3PatchPayload,
SettingsS3PatchResponse
>
) {
return usePatchFunc(
{
...args,
route: busSettingsS3Route,
},
async (mutate) => {
mutate((key) => key.startsWith(busSettingsS3Route))
}
)
}

export function useSettingsUploadPatch(
args?: HookArgsCallback<
SettingsUploadPatchParams,
SettingsUploadPatchPayload,
SettingsUploadPatchResponse
>
) {
return usePatchFunc(
{
...args,
route: busSettingsUploadRoute,
},
async (mutate) => {
mutate((key) => key.startsWith(busSettingsUploadRoute))
}
)
}

// params are required because omitting them returns a deprecated response structure
export function useAlerts(args?: HookArgsSwr<AlertsParams, AlertsResponse>) {
return useGetSwr({ ...args, route: busAlertsRoute })
Expand Down
5 changes: 5 additions & 0 deletions libs/renterd-types/src/autopilot.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { AutopilotConfig, SettingsGouging, SettingsRedundancy } from './types'
import { BusStateResponse } from './bus'
import { DeepPartial } from './utils'

export const autopilotStateRoute = '/autopilot/state'
export const autopilotConfigRoute = '/autopilot/config'
Expand Down Expand Up @@ -30,6 +31,10 @@ export type AutopilotConfigUpdateParams = void
export type AutopilotConfigUpdatePayload = AutopilotConfig
export type AutopilotConfigUpdateResponse = void

export type AutopilotConfigPatchParams = void
export type AutopilotConfigPatchPayload = DeepPartial<AutopilotConfig>
export type AutopilotConfigPatchResponse = AutopilotConfig

export type ConfigRecommendation = {
gougingSettings: SettingsGouging
}
Expand Down
17 changes: 17 additions & 0 deletions libs/renterd-types/src/bus.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import {
SettingsUpload,
SlabSlice,
} from './types'
import { DeepPartial } from './utils'

export const busStateRoute = '/bus/state'
export const busAutopilotsRoute = '/bus/autopilots'
Expand Down Expand Up @@ -685,3 +686,19 @@ export type SettingsS3UpdateResponse = void
export type SettingsUploadUpdateParams = void
export type SettingsUploadUpdatePayload = SettingsUpload
export type SettingsUploadUpdateResponse = void

export type SettingsGougingPatchParams = void
export type SettingsGougingPatchPayload = DeepPartial<SettingsGouging>
export type SettingsGougingPatchResponse = SettingsGouging

export type SettingsPinnedPatchParams = void
export type SettingsPinnedPatchPayload = DeepPartial<SettingsPinned>
export type SettingsPinnedPatchResponse = SettingsPinned

export type SettingsS3PatchParams = void
export type SettingsS3PatchPayload = DeepPartial<SettingsS3>
export type SettingsS3PatchResponse = SettingsS3

export type SettingsUploadPatchParams = void
export type SettingsUploadPatchPayload = DeepPartial<SettingsUpload>
export type SettingsUploadPatchResponse = SettingsUpload
3 changes: 3 additions & 0 deletions libs/renterd-types/src/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export type DeepPartial<T> = {
[P in keyof T]?: T[P] extends object | undefined ? DeepPartial<T[P]> : T[P]
}

0 comments on commit 3e2e32e

Please sign in to comment.