feat(snapshot): backend restore api

This commit is contained in:
Nicolas Meienberger
2025-10-31 21:15:43 +01:00
parent 5846c1ff86
commit c7db88fb56
7 changed files with 251 additions and 7 deletions

View File

@@ -24,6 +24,7 @@ import {
getRepository,
listSnapshots,
listSnapshotFiles,
restoreSnapshot,
listBackupSchedules,
createBackupSchedule,
deleteBackupSchedule,
@@ -69,6 +70,8 @@ import type {
GetRepositoryData,
ListSnapshotsData,
ListSnapshotFilesData,
RestoreSnapshotData,
RestoreSnapshotResponse,
ListBackupSchedulesData,
CreateBackupScheduleData,
CreateBackupScheduleResponse,
@@ -738,6 +741,46 @@ export const listSnapshotFilesOptions = (options: Options<ListSnapshotFilesData>
});
};
export const restoreSnapshotQueryKey = (options: Options<RestoreSnapshotData>) =>
createQueryKey("restoreSnapshot", options);
/**
* Restore a snapshot to a target path on the filesystem
*/
export const restoreSnapshotOptions = (options: Options<RestoreSnapshotData>) => {
return queryOptions({
queryFn: async ({ queryKey, signal }) => {
const { data } = await restoreSnapshot({
...options,
...queryKey[0],
signal,
throwOnError: true,
});
return data;
},
queryKey: restoreSnapshotQueryKey(options),
});
};
/**
* Restore a snapshot to a target path on the filesystem
*/
export const restoreSnapshotMutation = (
options?: Partial<Options<RestoreSnapshotData>>,
): UseMutationOptions<RestoreSnapshotResponse, DefaultError, Options<RestoreSnapshotData>> => {
const mutationOptions: UseMutationOptions<RestoreSnapshotResponse, DefaultError, Options<RestoreSnapshotData>> = {
mutationFn: async (localOptions) => {
const { data } = await restoreSnapshot({
...options,
...localOptions,
throwOnError: true,
});
return data;
},
};
return mutationOptions;
};
export const listBackupSchedulesQueryKey = (options?: Options<ListBackupSchedulesData>) =>
createQueryKey("listBackupSchedules", options);

View File

@@ -52,6 +52,8 @@ import type {
ListSnapshotsResponses,
ListSnapshotFilesData,
ListSnapshotFilesResponses,
RestoreSnapshotData,
RestoreSnapshotResponses,
ListBackupSchedulesData,
ListBackupSchedulesResponses,
CreateBackupScheduleData,
@@ -362,6 +364,22 @@ export const listSnapshotFiles = <ThrowOnError extends boolean = false>(
});
};
/**
* Restore a snapshot to a target path on the filesystem
*/
export const restoreSnapshot = <ThrowOnError extends boolean = false>(
options: Options<RestoreSnapshotData, ThrowOnError>,
) => {
return (options.client ?? _heyApiClient).post<RestoreSnapshotResponses, unknown, ThrowOnError>({
url: "/api/v1/repositories/{name}/restore",
...options,
headers: {
"Content-Type": "application/json",
...options.headers,
},
});
};
/**
* List all backup schedules
*/

View File

@@ -851,6 +851,36 @@ export type ListSnapshotFilesResponses = {
export type ListSnapshotFilesResponse = ListSnapshotFilesResponses[keyof ListSnapshotFilesResponses];
export type RestoreSnapshotData = {
body?: {
snapshotId: string;
targetPath: string;
exclude?: Array<string>;
include?: Array<string>;
path?: string;
};
path: {
name: string;
};
query?: never;
url: "/api/v1/repositories/{name}/restore";
};
export type RestoreSnapshotResponses = {
/**
* Snapshot restored successfully
*/
200: {
filesRestored: number;
filesUpdated: number;
message: string;
success: boolean;
totalBytes: number;
};
};
export type RestoreSnapshotResponse = RestoreSnapshotResponses[keyof RestoreSnapshotResponses];
export type ListBackupSchedulesData = {
body?: never;
path?: never;