@squarecloud/api
Version:
A NodeJS wrapper for Square Cloud API
1,011 lines (991 loc) • 28.6 kB
JavaScript
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/modules/applications.ts
import { readFile as readFile3 } from "fs/promises";
// src/structures/application/base.ts
import { readFile as readFile2 } from "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/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
import { join } from "path";
import { readFile } from "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 readFile(file);
}
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/modules/network.ts
var NetworkModule = class {
constructor(application) {
this.application = application;
}
/**
* Integrates your website with a custom domain
* - Requires [Senior plan](https://squarecloud.app/plans) or higher
*
* @param custom - The custom domain you want to use (e.g. yoursite.com)
*/
async setCustomDomain(custom) {
assertString(custom, "CUSTOM_DOMAIN");
const data = await this.application.client.api.request(
Routes.apps.network.custom(this.application.id),
{ method: "POST", body: { custom } }
);
return data.status === "success";
}
/**
* Gets analytics for a custom domain
* - Requires [Senior plan](https://squarecloud.app/plans) or higher
* - Requires the application to have an integrated custom domain
*/
async analytics() {
const data = await this.application.client.api.request(
Routes.apps.network.analytics(this.application.id)
);
return data?.response;
}
/**
* Get the DNS records for your custom domain.
*/
async dns() {
const data = await this.application.client.api.request(
Routes.apps.network.dns(this.application.id)
);
return data?.response;
}
};
// 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/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 readFile2(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";
}
};
// src/structures/application/application.ts
var Application = class extends BaseApplication {
/**
* Represents a Square Cloud application
*
* @constructor
* @param client - The client for this application
* @param data - The data from this application
*/
constructor(client, data) {
super(client, { ...data, lang: data.language });
this.client = client;
}
isWebsite() {
const domain = Reflect.get(this, "domain");
return Boolean(domain);
}
};
// src/structures/collection.ts
var Collection = class extends Map {
first(amount) {
if (typeof amount === "undefined") {
return this.values().next().value;
}
if (amount < 0) {
return this.last(amount * -1);
}
amount = Math.min(this.size, amount);
return Array.from({ length: amount }, () => this.values().next().value);
}
last(amount) {
const arr = [...this.values()];
if (typeof amount === "undefined") return arr[arr.length - 1];
if (amount < 0) return this.first(amount * -1);
if (!amount) return [];
return arr.slice(-amount);
}
/**
* Identical to {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/reverse Array.reverse()}
* but returns a Collection instead of an Array.
*/
reverse() {
const entries = [...this.entries()].reverse();
this.clear();
for (const [key, value] of entries) {
this.set(key, value);
}
return this;
}
find(fn, thisArg) {
if (typeof fn !== "function") {
throw new TypeError(`${fn} is not a function`);
}
if (typeof thisArg !== "undefined") {
fn = fn.bind(thisArg);
}
for (const [key, val] of this) {
if (fn(val, key, this)) return val;
}
}
filter(fn, thisArg) {
if (typeof fn !== "function") {
throw new TypeError(`${fn} is not a function`);
}
if (typeof thisArg !== "undefined") {
fn = fn.bind(thisArg);
}
const results = new this.constructor[Symbol.species]();
for (const [key, val] of this) {
if (fn(val, key, this)) results.set(key, val);
}
return results;
}
map(fn, thisArg) {
if (typeof fn !== "function") {
throw new TypeError(`${fn} is not a function`);
}
if (typeof thisArg !== "undefined") {
fn = fn.bind(thisArg);
}
return Array.from({ length: this.size }, () => {
const [key, value] = this.entries().next().value;
return fn(value, key, this);
});
}
some(fn, thisArg) {
if (typeof fn !== "function") {
throw new TypeError(`${fn} is not a function`);
}
if (typeof thisArg !== "undefined") {
fn = fn.bind(thisArg);
}
for (const [key, val] of this) {
if (fn(val, key, this)) return true;
}
return false;
}
every(fn, thisArg) {
if (typeof fn !== "function") {
throw new TypeError(`${fn} is not a function`);
}
if (typeof thisArg !== "undefined") {
fn = fn.bind(thisArg);
}
for (const [key, val] of this) {
if (!fn(val, key, this)) return false;
}
return true;
}
/**
* Applies a function to produce a single value. Identical in behavior to
* {@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/reduce Array.reduce()}.
*
* @param fn - Function used to reduce, taking four arguments; `accumulator`, `currentValue`, `currentKey`,
* and `collection`
* @param initialValue - Starting value for the accumulator
* @example
* ```ts
* collection.reduce((acc, guild) => acc + guild.memberCount, 0);
* ```
*/
reduce(fn, initialValue) {
if (typeof fn !== "function") {
throw new TypeError(`${fn} is not a function`);
}
let accumulator;
if (typeof initialValue !== "undefined") {
accumulator = initialValue;
for (const [key, val] of this) {
accumulator = fn(accumulator, val, key, this);
}
return accumulator;
}
let first = true;
for (const [key, val] of this) {
if (first) {
accumulator = val;
first = false;
continue;
}
accumulator = fn(accumulator, val, key, this);
}
if (first) {
throw new TypeError("Reduce of empty collection with no initial value");
}
return accumulator;
}
each(fn, thisArg) {
if (typeof fn !== "function") {
throw new TypeError(`${fn} is not a function`);
}
this.forEach(fn, thisArg);
return this;
}
/**
* Creates an identical shallow copy of this collection.
*
* @example
* ```ts
* const newColl = someColl.clone();
* ```
*/
clone() {
return new this.constructor[Symbol.species](this);
}
toJSON() {
return [...this.values()];
}
};
// 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 SimpleApplicationStatus = class {
/**
* Represents an application status fetched from status all endpoint
*
* @constructor
* @param client - The client for this status
* @param data - The data from this status
*/
constructor(client, data) {
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");
const { id, running } = data;
this.applicationId = id;
this.running = running;
if (running) {
const { cpu, ram } = data;
this.usage = { cpu, ram };
}
}
/**
* Fetches the full application status
*/
async fetch() {
const data = await this.client.api.request(
Routes.apps.status(this.applicationId)
);
return new ApplicationStatus(
this.client,
data.response,
this.applicationId
);
}
};
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/structures/user.ts
var User = class {
/**
* Represents a Square Cloud user
*
* @constructor
* @param client - The client for this user
* @param data - The data from this user
*/
constructor(client, data) {
/** The user's id */
__publicField(this, "id");
/** The user's display name */
__publicField(this, "name");
/** The user's current plan */
__publicField(this, "plan");
/** The user's registered email */
__publicField(this, "email");
/** The user's registered applications Collection */
__publicField(this, "applications");
const { user, applications } = data;
const { id, name, plan, email } = user;
const { duration } = plan;
this.id = id;
this.name = name;
this.email = email;
this.plan = {
...plan,
expiresInTimestamp: duration ?? void 0,
expiresIn: duration ? new Date(duration) : void 0
};
this.applications = new Collection(
applications.map((app) => [app.id, new BaseApplication(client, app)])
);
}
};
// 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/modules/applications.ts
var ApplicationsModule = class {
constructor(client) {
this.client = client;
}
async get(applicationId) {
const { response } = await this.client.api.request(Routes.user());
const user = new User(this.client, response);
this.client.emit("userUpdate", this.client.cache.user, user);
this.client.cache.set("user", user);
if (applicationId) {
assertString(applicationId, "APP_ID");
const application = user.applications.get(applicationId);
if (!application) {
throw new SquareCloudAPIError("APP_NOT_FOUND");
}
return application;
}
return user.applications;
}
/**
* Uploads an application
*
* @param file - The zip file path or Buffer
*
* @returns The uploaded application data
*/
async create(file) {
assertPathLike(file, "UPLOAD_FILE");
if (typeof file === "string") {
file = await readFile3(file);
}
const formData = new FormData();
const blob = new Blob([file]);
formData.append("file", blob, "app.zip");
const data = await this.client.api.request(Routes.apps.upload(), {
method: "POST",
body: formData
});
return data.response;
}
/**
* Gets the summary status for all your applications
*/
async statusAll() {
const data = await this.client.api.request(Routes.apps.statusAll());
return data.response.map(
(status) => new SimpleApplicationStatus(this.client, status)
);
}
/**
* Returns an application that you can manage or get information
*
* @param applicationId - The application ID, you must own the application
*/
async fetch(applicationId) {
const { response } = await this.client.api.request(
Routes.apps.info(applicationId)
);
return new Application(this.client, response);
}
};
export {
ApplicationCacheService,
ApplicationsModule,
BackupsModule,
DeploysModule,
FilesModule,
NetworkModule
};
//# sourceMappingURL=applications.js.map