Skip to content

Commit ff4f4d8

Browse files
committed
Add backupActivityJSONAsFinished(…) to get a value of type FinishedBackupActivityJSON for finished backups.
1 parent 43ec4cd commit ff4f4d8

File tree

4 files changed

+52
-8
lines changed

4 files changed

+52
-8
lines changed

README.md

+13-4
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,8 @@ This package is published as TypeScript source files. You will need to use a com
99
## API
1010

1111
````ts
12-
interface BackupActivityJSON {
12+
interface CommonBackupActivityJSONFields {
1313
aborted: boolean;
14-
abortReason?: string;
1514
activityLogPath: string;
1615
backupPlanDbId: number;
1716
backupSetUUID: string;
@@ -23,12 +22,22 @@ interface BackupActivityJSON {
2322
dataVersion: number;
2423
errorCount: number;
2524
finishedTime: number;
26-
message: string;
2725
subType: string;
2826
type: string;
2927
updatedTime: number;
3028
uuid: string;
3129
}
30+
interface InProgressBackupActivityJSON extends CommonBackupActivityJSONFields {
31+
message: string;
32+
}
33+
interface FinishedBackupActivityJSON extends CommonBackupActivityJSONFields {
34+
message: "Idle";
35+
abortReason?: string;
36+
totalBytes: 0;
37+
totalFiles: 0;
38+
}
39+
type BackupActivityJSON = InProgressBackupActivityJSON & FinishedBackupActivityJSON;
40+
declare function backupActivityJSONAsFinished(backupActivityJSON: BackupActivityJSON): FinishedBackupActivityJSON | null;
3241
interface ArqBackupPlanConfig {
3342
backupSetUUID: string;
3443
name?: string;
@@ -52,5 +61,5 @@ declare function listBackupPlans(): Promise<ArqBackupPlan[]>;
5261

5362
declare function setArqcCommandPath(newPath: string): void;
5463

55-
export { ArqBackupPlan, type ArqBackupPlanConfig, type BackupActivityJSON, acceptLicenseAgreement, listBackupPlans, pauseBackups, resumeBackups, setArqcCommandPath };
64+
export { ArqBackupPlan, type ArqBackupPlanConfig, type BackupActivityJSON, type FinishedBackupActivityJSON, type InProgressBackupActivityJSON, acceptLicenseAgreement, backupActivityJSONAsFinished, listBackupPlans, pauseBackups, resumeBackups, setArqcCommandPath };
5665
````

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "arqc",
3-
"version": "0.3.2",
3+
"version": "0.3.3",
44
"license": "MIT",
55
"author": "Lucas Garron",
66
"description": "A wrapper for the `arqc` command.",

src/ArqBackupPlan.ts

+35-3
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
import { $, type ShellPromise } from "bun";
22
import { arqcCommand } from "./arqcCommand";
33

4-
export interface BackupActivityJSON {
4+
interface CommonBackupActivityJSONFields {
55
// Note: `"aborted"` may still be `false` for an aborted activity when the post-backup script is running.
66
aborted: boolean;
7-
abortReason?: string;
87
activityLogPath: string;
98
backupPlanDbId: number;
109
backupSetUUID: string;
@@ -16,12 +15,45 @@ export interface BackupActivityJSON {
1615
dataVersion: number;
1716
errorCount: number;
1817
finishedTime: number;
19-
message: string;
2018
subType: string;
2119
type: string;
2220
updatedTime: number;
2321
uuid: string;
2422
}
23+
export interface InProgressBackupActivityJSON
24+
extends CommonBackupActivityJSONFields {
25+
// We'd use `Exclude<string, "Idle">`, but that is currently treated as `string` in TypeScript
26+
message: string;
27+
}
28+
29+
export interface FinishedBackupActivityJSON
30+
extends CommonBackupActivityJSONFields {
31+
message: "Idle";
32+
abortReason?: string;
33+
totalBytes: 0;
34+
totalFiles: 0;
35+
}
36+
37+
// Note: this should be `|` rather than `&`, but this is `&` due to TypeScript
38+
// limitations.
39+
//
40+
// This means that `if (backupActivityJSON.message === "Idle")` unfortunately
41+
// cannot type narrow the type of backupActivityJSON from `BackupActivityJSON`
42+
// to `FinishedBackupActivityJSON` inside the `if` block where the condition is
43+
// true. We provide the `backupActivityJSONAsFinished(…)` function for this.
44+
export type BackupActivityJSON = InProgressBackupActivityJSON &
45+
FinishedBackupActivityJSON;
46+
47+
// Applies a heuristic to return `FinishedBackupActivityJSON` if and only if the backup has finished.
48+
// Current heuristic: check if the `"message"` field is `"Idle"`.
49+
export function backupActivityJSONAsFinished(
50+
backupActivityJSON: BackupActivityJSON,
51+
): FinishedBackupActivityJSON | null {
52+
if (backupActivityJSON.message === "Idle") {
53+
return backupActivityJSON as FinishedBackupActivityJSON;
54+
}
55+
return null;
56+
}
2557

2658
export interface ArqBackupPlanConfig {
2759
backupSetUUID: string;

src/index.ts

+3
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@ export {
22
type BackupActivityJSON,
33
type ArqBackupPlanConfig,
44
ArqBackupPlan,
5+
backupActivityJSONAsFinished,
6+
FinishedBackupActivityJSON,
7+
InProgressBackupActivityJSON,
58
} from "./ArqBackupPlan";
69
export {
710
acceptLicenseAgreement,

0 commit comments

Comments
 (0)