@squarecloud/api
Version:
A NodeJS wrapper for Square Cloud API
1,302 lines (1,274 loc) • 37.8 kB
JavaScript
var __create = Object.create;
var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __getProtoOf = Object.getPrototypeOf;
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 __commonJS = (cb, mod) => function __require() {
return mod || (0, cb[__getOwnPropNames(cb)[0]])((mod = { exports: {} }).exports, mod), mod.exports;
};
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 __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
// If the importer is in node compatibility mode or this is not an ESM
// file that has been converted to a CommonJS file using a Babel-
// compatible transform (i.e. "__esModule" has not been set), then set
// "default" to the CommonJS "module.exports" for node compatibility.
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
mod
));
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value);
// package.json
var require_package = __commonJS({
"package.json"(exports2, module2) {
module2.exports = {
name: "@squarecloud/api",
version: "3.7.9",
description: "A NodeJS wrapper for Square Cloud API",
exports: {
".": {
import: {
types: "./lib/index.d.ts",
default: "./lib/index.js"
},
require: {
types: "./lib/index.d.cts",
default: "./lib/index.cjs"
},
default: "./lib/index.js"
}
},
packageManager: "pnpm@10.11.1",
type: "module",
scripts: {
release: "pnpm build && changeset publish",
build: "tsup ./src",
"check-types": "tsc --noEmit",
lint: "biome check --write .",
"lint:ci": "biome check .",
test: "node --test test/*.test.js"
},
engines: {
node: ">=18.0.0"
},
devDependencies: {
"@biomejs/biome": "^1.9.4",
"@changesets/cli": "^2.29.4",
"@squarecloud/api-types": "^0.5.0",
"@types/node": "^22.15.29",
"ts-node": "^10.9.2",
"ts-node-dev": "^2.0.0",
"tsc-alias": "^1.8.16",
tsup: "^8.5.0",
typescript: "^5.8.3"
},
keywords: [
"wrapper",
"square",
"squarecloud",
"api",
"typescript",
"app",
"bot",
"website",
"host"
],
author: {
name: "joaotonaco",
url: "https://github.com/joaotonaco"
},
repository: {
type: "git",
url: "git+https://github.com/squarecloudofc/sdk-api-js.git"
},
bugs: {
url: "https://github.com/squarecloudofc/sdk-api-js/issues"
},
homepage: "https://docs.squarecloud.app/sdks/js/client",
license: "MIT"
};
}
});
// src/index.ts
var index_exports = {};
__export(index_exports, {
Application: () => Application,
ApplicationStatus: () => ApplicationStatus,
Backup: () => Backup,
BaseApplication: () => BaseApplication,
Collection: () => Collection,
Deployment: () => Deployment,
SimpleApplicationStatus: () => SimpleApplicationStatus,
SquareCloudAPI: () => SquareCloudAPI,
SquareCloudAPIError: () => SquareCloudAPIError,
TypedEventEmitter: () => TypedEventEmitter,
User: () => User,
WebsiteApplication: () => WebsiteApplication
});
module.exports = __toCommonJS(index_exports);
// src/structures/application/base.ts
var import_promises3 = 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/modules/applications.ts
var import_promises2 = require("fs/promises");
// 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/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/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 (0, import_promises2.readFile)(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);
}
};
// src/modules/user.ts
var UserModule = class {
constructor(client) {
this.client = client;
}
/**
* Gets the authenticated user information
*/
async get() {
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);
return user;
}
};
// src/services/api.ts
var APIService = class {
constructor(apiKey) {
this.apiKey = apiKey;
__publicField(this, "baseUrl", "https://api.squarecloud.app");
__publicField(this, "version", "v2");
__publicField(this, "sdkVersion", require_package().version);
__publicField(this, "userId");
this.userId = apiKey.split("-")[0];
}
async request(...[path, options]) {
const { url, init } = this.parseRequestOptions(path, options);
const response = await fetch(url, init).catch((err) => {
throw new SquareCloudAPIError(err.code, err.message);
});
if (response.status === 413) {
throw new SquareCloudAPIError("PAYLOAD_TOO_LARGE");
}
if (response.status === 429) {
throw new SquareCloudAPIError("RATE_LIMIT_EXCEEDED", "Try again later");
}
if (response.status === 502 || response.status === 504) {
throw new SquareCloudAPIError("SERVER_UNAVAILABLE", "Try again later");
}
const data = await response.json().catch(() => {
throw new SquareCloudAPIError(
"CANNOT_PARSE_RESPONSE",
`Failed with status ${response.status}`
);
});
if (!data || data.status === "error" || !response.ok) {
throw new SquareCloudAPIError(data?.code || "COMMON_ERROR");
}
return data;
}
parseRequestOptions(path, options) {
const init = options || {};
init.method = init.method || "GET";
init.headers = {
Accept: "application/json",
...init.headers || {},
Authorization: this.apiKey,
"User-Agent": `squarecloud-sdk-js/${this.sdkVersion}`
};
const url = new URL(path, `${this.baseUrl}/${this.version}/`);
if ("query" in init && init.query) {
const query = new URLSearchParams(init.query);
url.search = query.toString();
init.query = void 0;
}
if ("body" in init && init.body && !(init.body instanceof FormData)) {
init.body = JSON.stringify(init.body);
init.headers = {
...init.headers,
"Content-Type": "application/json"
};
}
return { url, init };
}
};
// src/services/cache/global.ts
var GlobalCacheService = class extends BaseCacheService {
constructor() {
super(...arguments);
__publicField(this, "cache", {
user: void 0
});
}
get user() {
return this.cache.user;
}
};
// 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_promises3.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";
}
};
// 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/application/website.ts
var WebsiteApplication = class extends Application {
/**
* 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);
this.client = client;
/** The application default domain (e.g. example.squareweb.app) */
__publicField(this, "domain");
/** The custom configured domain (e.g. yoursite.com) */
__publicField(this, "custom");
/** Network module for this application */
__publicField(this, "network", new NetworkModule(this));
const { domain, custom } = data;
this.domain = domain;
this.custom = custom || void 0;
}
};
// 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/types/client.ts
var import_events = __toESM(require("events"), 1);
var TypedEventEmitter = class {
constructor() {
__publicField(this, "emitter", new import_events.default());
}
emit(eventName, ...eventArg) {
this.emitter.emit(eventName, ...eventArg);
}
on(eventName, handler) {
this.emitter.on(eventName, handler);
}
off(eventName, handler) {
this.emitter.off(eventName, handler);
}
};
// src/index.ts
var SquareCloudAPI = class extends TypedEventEmitter {
/**
* Creates an API instance
*
* @param apiKey - Your API Token (request at [Square Cloud Dashboard](https://squarecloud.app/dashboard))
*/
constructor(apiKey) {
super();
/** The API service */
__publicField(this, "api");
/** The applications module */
__publicField(this, "applications", new ApplicationsModule(this));
/** The user module */
__publicField(this, "user", new UserModule(this));
/** The global cache service */
__publicField(this, "cache", new GlobalCacheService());
assertString(apiKey, "API_KEY");
this.api = new APIService(apiKey);
}
/** @deprecated Use SquareCloudAPI#user instead. */
get users() {
console.warn(
"[SquareCloudAPI] The 'users' property is deprecated and will be removed in the next major version. Use SquareCloudAPI#user instead."
);
return this.user;
}
};
__publicField(SquareCloudAPI, "apiInfo", {
baseUrl: "https://api.squarecloud.app",
version: "v2"
});
// Annotate the CommonJS export names for ESM import in node:
0 && (module.exports = {
Application,
ApplicationStatus,
Backup,
BaseApplication,
Collection,
Deployment,
SimpleApplicationStatus,
SquareCloudAPI,
SquareCloudAPIError,
TypedEventEmitter,
User,
WebsiteApplication
});
//# sourceMappingURL=index.cjs.map