@squarecloud/api
Version:
A NodeJS wrapper for Square Cloud API
675 lines (661 loc) • 19.6 kB
JavaScript
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
var __export = (target, all) => {
for (var name in all)
__defProp(target, name, { get: all[name], enumerable: true });
};
var __copyProps = (to, from, except, desc) => {
if (from && typeof from === "object" || typeof from === "function") {
for (let key of __getOwnPropNames(from))
if (!__hasOwnProp.call(to, key) && key !== except)
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
}
return to;
};
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
// src/structures/application/base.ts
var base_exports = {};
__export(base_exports, {
BaseApplication: () => BaseApplication
});
module.exports = __toCommonJS(base_exports);
var import_promises2 = require("fs/promises");
// 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/services/cache/base.ts
var BaseCacheService = class {
constructor() {
__publicField(this, "cache");
}
set(key, value) {
Reflect.set(this.cache, key, value);
}
get(key) {
return this.cache[key];
}
remove(key) {
Reflect.set(this.cache, key, void 0);
}
};
// src/services/cache/application.ts
var ApplicationCacheService = class extends BaseCacheService {
constructor() {
super(...arguments);
__publicField(this, "cache", {
status: void 0,
backups: void 0,
logs: void 0
});
}
get status() {
return this.cache.status;
}
get backups() {
return this.cache.backups;
}
get logs() {
return this.cache.logs;
}
};
// 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/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);
}
};
// src/structures/deploy.ts
var Deployment = class {
/**
* Represents an application deployment
*
* @constructor
* @param application - The application from which you fetched the deployment
* @param data - The data from this deployment
*/
constructor(application, data) {
this.application = application;
/** The ID of the deploy. */
__publicField(this, "id");
/** The current state of the deploy. */
__publicField(this, "state");
/** The date the deploy was created. */
__publicField(this, "createdAt");
/** The date the deploy was created in millisseconds. */
__publicField(this, "createdTimestamp");
const { id, state, date } = data;
this.id = id;
this.state = state;
this.createdAt = new Date(date);
this.createdTimestamp = this.createdAt.getTime();
}
};
// src/modules/deploys.ts
var DeploysModule = class {
constructor(application) {
this.application = application;
}
/**
* Integrates Square Cloud with GitHub webhooks
*
* @param accessToken - The access token for your GitHub repository. You can find this in your [GitHub Tokens Classic](https://github.com/settings/tokens/new)
*/
async integrateGithubWebhook(accessToken) {
assertString(accessToken);
const data = await this.application.client.api.request(
Routes.apps.deployments.webhook(this.application.id),
{ method: "POST", body: { access_token: accessToken } }
);
return data.response.webhook;
}
/**
* Gets the last 10 deployments of an application from the last 24 hours
*/
async list() {
const data = await this.application.client.api.request(
Routes.apps.deployments.list(this.application.id)
);
return data.response.map(
(deployment) => new Deployment(this.application, deployment)
);
}
/**
* Gets the current webhook URL
*/
async webhookURL() {
const data = await this.application.client.api.request(
Routes.apps.deployments.current(this.application.id)
);
return data.response.webhook;
}
};
// src/modules/files.ts
var import_path = require("path");
var import_promises = require("fs/promises");
var FilesModule = class {
constructor(application) {
this.application = application;
}
/**
* Lists the files inside a directory
*
* @param path - The absolute directory path
*/
async list(path = "/") {
assertString(path, "LIST_FILES_PATH");
const { response } = await this.application.client.api.request(
Routes.apps.files.list(this.application.id),
{ query: { path } }
);
return response;
}
/**
* Reads the specified file content
*
* @param path - The absolute file path
*/
async read(path) {
assertString(path, "READ_FILE_PATH");
const { response } = await this.application.client.api.request(
Routes.apps.files.read(this.application.id),
{ query: { path } }
);
if (!response) {
return;
}
return Buffer.from(response.data);
}
/**
* Creates a new file
*
* @param file - The file content
* @param fileName - The file name with extension
* @param path - The absolute file path
*/
async create(file, fileName, path = "/") {
assertPathLike(file, "CREATE_FILE");
assertString(fileName, "CREATE_FILE_NAME");
assertString(path, "CREATE_FILE_PATH");
if (typeof file === "string") {
file = await (0, import_promises.readFile)(file);
}
path = (0, import_path.join)(path, fileName).replaceAll("\\", "/");
const { status } = await this.application.client.api.request(
Routes.apps.files.upsert(this.application.id),
{
method: "PUT",
body: { content: file.toString("utf8"), path }
}
);
return status === "success";
}
/**
* Edits an existing file (same as create)
*
* @param file - The file content
* @param path - The absolute file path
*/
async edit(file, path = "/") {
assertPathLike(file, "EDIT_FILE");
assertString(path, "EDIT_FILE_PATH");
return this.create(file, "", path);
}
/**
* Moves or renames a file
*
* @param path - The current absolute file path
* @param newPath - The new absolute file path
*/
async move(path, newPath) {
assertString(path, "MOVE_FILE_PATH");
assertString(newPath, "MOVE_FILE_NEW_PATH");
const { status } = await this.application.client.api.request(
Routes.apps.files.move(this.application.id),
{ method: "PATCH", body: { path, to: newPath } }
);
return status === "success";
}
/**
* Deletes the specified file or directory
*
* @param path - The absolute file or directory path
*/
async delete(path) {
assertString(path, "DELETE_FILE_PATH");
const { status } = await this.application.client.api.request(
Routes.apps.files.delete(this.application.id),
{ method: "DELETE", body: { path } }
);
return status === "success";
}
};
// 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/structures/status.ts
var ApplicationStatus = class {
/**
* Represents an application status
*
* @constructor
* @param client - The client for this status
* @param data - The data from this status
* @param applicationId - The application ID this status came from
*/
constructor(client, data, applicationId) {
this.client = client;
/** The application's ID this status came from */
__publicField(this, "applicationId");
/** Usage statuses for this application */
__publicField(this, "usage");
/** Whether the application is running or not */
__publicField(this, "running");
/**
* The status of the application
*
* - 'exited' (stopped)
* - 'created' (being created)
* - 'running'
* - 'starting'
* - 'restarting'
* - 'deleting'
*/
__publicField(this, "status");
/** For how long the app is running in millisseconds */
__publicField(this, "uptimeTimestamp");
/** For how long the app is running */
__publicField(this, "uptime");
const { cpu, ram, network, storage, running, status, uptime } = data;
this.applicationId = applicationId;
this.usage = { cpu, ram, network, storage };
this.running = running;
this.status = status;
this.uptime = uptime ? new Date(uptime) : void 0;
this.uptimeTimestamp = uptime ?? void 0;
}
};
// 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/application/base.ts
var BaseApplication = class {
/**
* Represents the base application from the user endpoint
*
* @constructor
* @param client - The client for this application
* @param data - The data from this application
*/
constructor(client, data) {
this.client = client;
/** The application ID */
__publicField(this, "id");
/** The application display name */
__publicField(this, "name");
/** The application description */
__publicField(this, "description");
/** The url to manage the application via web */
__publicField(this, "url");
/** The application total ram */
__publicField(this, "ram");
/** The application current cluster */
__publicField(this, "cluster");
/**
* The application programming language
*
* - `javascript`
* - `typescript`
* - `python`
* - `java`
* - `elixir`
* - `rust`
* - `go`
* - `php`
* - `dotnet`
* - `static`
*/
__publicField(this, "language");
/** Cache service for this application */
__publicField(this, "cache", new ApplicationCacheService());
/** Files module for this application */
__publicField(this, "files", new FilesModule(this));
/** Backup module for this application */
__publicField(this, "backups", new BackupsModule(this));
/** Deploys module for this application */
__publicField(this, "deploys", new DeploysModule(this));
const { id, name, desc, ram, lang, cluster } = data;
this.id = id;
this.name = name;
this.description = desc;
this.ram = ram;
this.language = lang;
this.cluster = cluster;
this.url = `https://squarecloud.app/dashboard/app/${id}`;
}
/** @deprecated Use `Application#backups` instead */
get backup() {
console.warn(
"[SquareCloudAPI] The 'backup' property is deprecated and will be removed in the the next major version. Use Application#backups instead."
);
return this.backups;
}
/**
* Fetches this application for full information
*/
async fetch() {
return this.client.applications.fetch(this.id);
}
/**
* Gets the application current status information
*/
async getStatus() {
const data = await this.client.api.request(Routes.apps.status(this.id));
const status = new ApplicationStatus(this.client, data.response, this.id);
this.client.emit("statusUpdate", this, this.cache.status, status);
this.cache.set("status", status);
return status;
}
/**
* Gets the application current logs
*/
async getLogs() {
const data = await this.client.api.request(Routes.apps.logs(this.id));
const { logs } = data.response;
this.client.emit("logsUpdate", this, this.cache.logs, logs);
this.cache.set("logs", logs);
return logs;
}
/**
* Starts up the application
* @returns `boolean` for success or fail
*/
async start() {
const data = await this.client.api.request(Routes.apps.start(this.id), {
method: "POST"
});
return data?.status === "success";
}
/**
* Stops the application
* @returns `boolean` for success or fail
*/
async stop() {
const data = await this.client.api.request(Routes.apps.stop(this.id), {
method: "POST"
});
return data?.status === "success";
}
/**
* Restarts the application
* @returns `boolean` for success or fail
*/
async restart() {
const data = await this.client.api.request(Routes.apps.restart(this.id), {
method: "POST"
});
return data?.status === "success";
}
/**
* Deletes your whole application
* - This action is irreversible.
*
* @returns `boolean` for success or fail
*/
async delete() {
const data = await this.client.api.request(Routes.apps.delete(this.id), {
method: "DELETE"
});
return data?.status === "success";
}
/**
* Commit files to your application folder
*
* - This action is irreversible.
*
* - Tip: use this to get an absolute path.
* ```ts
* require('path').join(__dirname, 'fileName')
* ```
* - Tip 2: use a zip file to commit more than one archive
*
* @param file - Buffer or absolute path to the file
* @param fileName - The file name (e.g.: "index.js")
* @param restart - Whether the application should be restarted after the commit
* @returns `true` for success or `false` for fail
*/
async commit(file, fileName) {
assertPathLike(file, "COMMIT_FILE");
if (fileName) {
assertString(fileName, "FILE_NAME");
}
if (typeof file === "string") {
file = await (0, import_promises2.readFile)(file);
}
const formData = new FormData();
const blob = new Blob([file]);
formData.append("file", blob, fileName || "commit.zip");
const data = await this.client.api.request(Routes.apps.commit(this.id), {
method: "POST",
body: formData
});
return data?.status === "success";
}
};
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
BaseApplication
});
//# sourceMappingURL=base.cjs.map