UNPKG

@squarecloud/api

Version:
237 lines (231 loc) 6.49 kB
var __defProp = Object.defineProperty; var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value; var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value); // src/lib/routes.ts var Route = (route) => route; var Routes = { user: () => { return Route("users/me"); }, service: { status: () => { return Route("service/status"); } }, apps: { upload: () => { return Route("apps"); }, statusAll: () => { return Route("apps/status"); }, info: (appId) => { return Route(`apps/${appId}`); }, status: (appId) => { return Route(`apps/${appId}/status`); }, logs: (appId) => { return Route(`apps/${appId}/logs`); }, delete: (appId) => { return Route(`apps/${appId}`); }, commit: (appId) => { return Route(`apps/${appId}/commit`); }, snapshots: (appId) => { return Route(`apps/${appId}/snapshots`); }, generateSnapshot: (appId) => { return Route(`apps/${appId}/snapshots`); }, start: (appId) => { return Route(`apps/${appId}/start`); }, restart: (appId) => { return Route(`apps/${appId}/restart`); }, stop: (appId) => { return Route(`apps/${appId}/stop`); }, files: { read: (appId) => { return Route(`apps/${appId}/files/content`); }, list: (appId) => { return Route(`apps/${appId}/files`); }, upsert: (appId) => { return Route(`apps/${appId}/files`); }, move: (appId) => { return Route(`apps/${appId}/files`); }, delete: (appId) => { return Route(`apps/${appId}/files`); } }, deployments: { list: (appId) => { return Route(`apps/${appId}/deployments`); }, current: (appId) => { return Route( `apps/${appId}/deployments/current` ); }, webhook: (appId) => { return Route( `apps/${appId}/deploy/webhook` ); } }, network: { dns: (appId) => { return Route(`apps/${appId}/network/dns`); }, custom: (appId) => { return Route(`apps/${appId}/network/custom`); }, analytics: (appId) => { return Route( `apps/${appId}/network/analytics` ); } } } }; // src/assertions/common.ts function assert({ validate, value, expect, name }) { if (!validate(value)) { const code = name ? `INVALID_${name}` : "VALIDATION_ERROR"; const message = `Expected ${expect}, got ${typeof value}`; throw new SquareCloudAPIError(code, message); } } function makeAssertion(expect, validate) { return (value, name) => { assert({ validate, value, expect, name }); }; } // src/assertions/literal.ts var assertString = makeAssertion( "string", (value) => typeof value === "string" ); var assertBoolean = makeAssertion( "boolean", (value) => typeof value === "boolean" ); var assertPathLike = makeAssertion( "string or Buffer", (value) => typeof value === "string" || value instanceof Buffer ); // src/structures/backup.ts var Backup = class { /** * Represents an application backup (snapshot) * * @constructor * @param application - The application from which you fetched the backups * @param data - The data from this backup */ constructor(application, data) { this.application = application; /** Size of the backup in bytes. */ __publicField(this, "size"); /** Date of the last modification of the backup. */ __publicField(this, "modifiedAt"); /** Date of the last modification of the backup in millisseconds. */ __publicField(this, "modifiedTimestamp"); /** AWS access key for the backup. */ __publicField(this, "key"); /** The URL for downloading this backup */ __publicField(this, "url"); const { name, size, modified, key } = data; const { userId } = application.client.api; this.size = size; this.modifiedAt = new Date(modified); this.modifiedTimestamp = this.modifiedAt.getTime(); this.key = key; this.url = `https://snapshots.squarecloud.app/applications/${userId}/${name}.zip?${key}`; } /** * Downloads this backup * @returns The downloaded backup bufer */ async download() { const res = await fetch(this.url).then((res2) => res2.arrayBuffer()).catch(() => void 0); if (!res) { throw new Error("BACKUP_DOWNLOAD_FAILED"); } return Buffer.from(res); } }; // src/structures/error.ts var SquareCloudAPIError = class extends TypeError { constructor(code, message, options) { super(code); this.name = "SquareCloudAPIError"; this.message = (code?.replaceAll("_", " ").toLowerCase().replace(/(^|\s)\S/g, (L) => L.toUpperCase()) || "UNKNOWN_CODE") + (message ? `: ${message}` : ""); if (options?.stack) { this.stack = options.stack; } if (options?.cause) { this.cause = options.cause; } } }; // src/modules/backups.ts var BackupsModule = class { constructor(application) { this.application = application; } /** * Gets the list of generated backups (snapshots) for this application */ async list() { const data = await this.application.client.api.request( Routes.apps.snapshots(this.application.id) ); const backups = data.response.map( (backup) => new Backup(this.application, backup) ); this.application.client.emit( "backupsUpdate", this.application, this.application.cache.backups, backups ); this.application.cache.set("backups", backups); return backups; } /** * Generates a new backup * @returns The generated backup URL and key */ async create() { const data = await this.application.client.api.request( Routes.apps.generateSnapshot(this.application.id), { method: "POST" } ); return data.response; } /** * Generates a new backup and downloads it * @returns The downloaded backup bufer */ async download() { const backup = await this.create(); const res = await fetch(backup.url).then((res2) => res2.arrayBuffer()).catch(() => void 0); if (!res) { throw new SquareCloudAPIError("BACKUP_DOWNLOAD_FAILED"); } return Buffer.from(res); } }; export { BackupsModule }; //# sourceMappingURL=backups.js.map