Skip to content

Commit 7a806b0

Browse files
committed
feat: create structure for backups list
1 parent 0cc6f6c commit 7a806b0

File tree

5 files changed

+89
-8
lines changed

5 files changed

+89
-8
lines changed

.changeset/many-ducks-thank.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@squarecloud/api": patch
3+
---
4+
5+
New structure for backups with download method. It's used at `backups.list()` method.

src/assertions/backup.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import { z } from "zod";
2+
3+
import { assertAPIObject } from "./common";
4+
5+
const BackupSchema = z.object({
6+
name: z.string(),
7+
size: z.number(),
8+
modified: z.coerce.date(),
9+
key: z.string(),
10+
});
11+
12+
export function assertBackup(
13+
value: unknown,
14+
): asserts value is z.infer<typeof BackupSchema> {
15+
assertAPIObject({
16+
schema: BackupSchema,
17+
value,
18+
code: "BACKUP",
19+
route: "/apps/{app_id}/backups",
20+
});
21+
}

src/modules/backups.ts

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
1-
import type {
2-
APIApplicationBackup,
3-
RESTPostAPIApplicationBackupResult,
4-
} from "@squarecloud/api-types/v2";
1+
import type { RESTPostAPIApplicationBackupResult } from "@squarecloud/api-types/v2";
52

63
import { Routes } from "@/lib/routes";
74
import { type BaseApplication, SquareCloudAPIError } from "@/structures";
5+
import { Backup } from "@/structures/backup";
86

97
export class BackupsModule {
108
constructor(public readonly application: BaseApplication) {}
119

12-
async list(): Promise<APIApplicationBackup[]> {
10+
/** @returns The list of backups (snapshots) */
11+
async list(): Promise<Backup[]> {
1312
const data = await this.application.client.api.request(
1413
Routes.apps.backups(this.application.id),
1514
);
@@ -24,10 +23,10 @@ export class BackupsModule {
2423
);
2524
this.application.cache.set("backups", backups);
2625

27-
return backups;
26+
return backups.map((backup) => new Backup(this.application, backup));
2827
}
2928

30-
/** @returns The generated backup URL */
29+
/** @returns The generated backup URL and key */
3130
async create(): Promise<RESTPostAPIApplicationBackupResult> {
3231
const data = await this.application.client.api.request(
3332
Routes.apps.generateBackup(this.application.id),

src/services/api.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,11 @@ import type {
1111
export class APIService {
1212
public readonly baseUrl = "https://api.squarecloud.app";
1313
public readonly version: APIVersion<1 | 2> = "v2";
14+
public readonly userId: string;
1415

15-
constructor(protected readonly apiKey: string) {}
16+
constructor(protected readonly apiKey: string) {
17+
this.userId = apiKey.split("-")[0];
18+
}
1619

1720
async request<T extends APIEndpoint>(
1821
...[path, options]: APIRequestArgs<T>

src/structures/backup.ts

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
import type { APIApplicationBackup } from "@squarecloud/api-types/v2";
2+
3+
import { assertBackup } from "@/assertions/backup";
4+
import type { BaseApplication } from "./application/base";
5+
6+
export class Backup {
7+
/** The ID of the application from which you fetched the backups. */
8+
applicationId: string;
9+
10+
/** Size of the backup in bytes. */
11+
size: number;
12+
13+
/** Date of the last modification of the backup. */
14+
modifiedAt: Date;
15+
16+
/** Date of the last modification of the backup in millisseconds. */
17+
modifiedTimestamp: number;
18+
19+
/** AWS access key for the backup. */
20+
key: string;
21+
22+
/** The URL for downloading this backup */
23+
url: string;
24+
25+
constructor(
26+
public readonly application: BaseApplication,
27+
data: APIApplicationBackup,
28+
) {
29+
assertBackup(data);
30+
const { name, size, modified, key } = data;
31+
const { userId } = application.client.api;
32+
33+
this.applicationId = name;
34+
this.size = size;
35+
this.modifiedAt = new Date(modified);
36+
this.modifiedTimestamp = this.modifiedAt.getTime();
37+
this.key = key;
38+
this.url = `https://backups.squarecloud.app/${userId}_${name}.zip?${key}`;
39+
}
40+
41+
/** @returns The downloaded backup buffer */
42+
async download(): Promise<Buffer> {
43+
const res = await fetch(this.url)
44+
.then((res) => res.arrayBuffer())
45+
.catch(() => undefined);
46+
47+
if (!res) {
48+
throw new Error("BACKUP_DOWNLOAD_FAILED");
49+
}
50+
51+
return Buffer.from(res);
52+
}
53+
}

0 commit comments

Comments
 (0)