Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions packages/discloud.app/src/managers/AppAptManager.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Routes, type APTString, type RESTDeleteApiAppAptResult, type RESTPutApiAppAptResult } from "@discloudapp/api-types/v2";
import z from "zod";
import type DiscloudApp from "../discloudApp/DiscloudApp";
import { validateNonEmptyString } from "../util/assertions";
import BaseManager from "./BaseManager";

/**
Expand All @@ -18,7 +18,7 @@ export default class AppAptManager extends BaseManager {
* @param apt - One or more APTs to install. See {@link AptString}.
*/
async install(appID: string, apt: APTString | APTString[]): Promise<RESTPutApiAppAptResult> {
z.string().parse(appID);
validateNonEmptyString(appID);

if (Array.isArray(apt)) apt = <APTString>apt.join();

Expand All @@ -34,7 +34,7 @@ export default class AppAptManager extends BaseManager {
* @param apt - One or more APTs to uninstall. See {@link AptString}.
*/
async uninstall(appID: string, apt: APTString | APTString[]): Promise<RESTDeleteApiAppAptResult> {
z.string().parse(appID);
validateNonEmptyString(appID);

if (Array.isArray(apt)) apt = <APTString>apt.join();

Expand Down
56 changes: 38 additions & 18 deletions packages/discloud.app/src/managers/AppManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ import { Routes, type ApiAppManagerRemovedAll, type ApiAppManagerRestartedAll, t
import { DiscloudAPIError } from "@discloudapp/rest";
import { resolveFile } from "@discloudapp/util";
import { constants } from "http2";
import z from "zod";
import { ProfileOptions, type CreateAppOptions, type UpdateAppOptions } from "../@types";
import type DiscloudApp from "../discloudApp/DiscloudApp";
import App from "../structures/App";
import AppBackup from "../structures/AppBackup";
import type AppStatus from "../structures/AppStatus";
import AppUploaded from "../structures/AppUploaded";
import { validateNonEmptyString, validateNumberType } from "../util/assertions";
import CachedManager from "./CachedManager";

/**
Expand All @@ -26,6 +26,8 @@ export default class AppManager extends CachedManager<typeof App> {
*/
async status(appID: string): Promise<AppStatus>
async status(appID: string) {
validateNonEmptyString(appID);

const data = await this.discloudApp.rest.get<RESTGetApiAppStatusResult>(Routes.appStatus(appID));

return this._add(data.apps).status;
Expand All @@ -38,6 +40,9 @@ export default class AppManager extends CachedManager<typeof App> {
* @param command - The command
*/
async console(appID: string, command: string): Promise<ApiConsoleAppShell> {
validateNonEmptyString(appID);
validateNonEmptyString(command);

const data = await this.discloudApp.rest.put<RESTPutApiAppConsoleResult>(Routes.appConsole(appID), {
body: { command },
});
Expand All @@ -51,8 +56,10 @@ export default class AppManager extends CachedManager<typeof App> {
* @param appID - Your app id
*/
async terminal(appID: string): Promise<ApiTerminal>
async terminal(appID?: "all"): Promise<Map<string, ApiTerminal>>
async terminal(appID = "all") {
async terminal(appID: "all"): Promise<Map<string, ApiTerminal>>
async terminal(appID: string) {
validateNonEmptyString(appID);

const data = await this.discloudApp.rest.get<
| RESTGetApiAppLogResult
| RESTGetApiAppAllLogResult
Expand All @@ -77,8 +84,10 @@ export default class AppManager extends CachedManager<typeof App> {
* @param appID - Your app id
*/
async backup(appID: string): Promise<AppBackup>
async backup(appID?: "all"): Promise<Map<string, AppBackup>>
async backup(appID = "all") {
async backup(appID: "all"): Promise<Map<string, AppBackup>>
async backup(appID: string) {
validateNonEmptyString(appID);

const data = await this.discloudApp.rest.get<
| RESTGetApiAppBackupResult
| RESTGetApiAppAllBackupResult
Expand All @@ -104,8 +113,8 @@ export default class AppManager extends CachedManager<typeof App> {
* @param quantity - Minimum values is `100` to `bot` or `512` for `site`
*/
async ram(appID: string, quantity: number) {
z.string().parse(appID);
z.number().min(100).parse(quantity);
validateNonEmptyString(appID);
validateNumberType(quantity);

const data = await this.discloudApp.rest.put<RESTPutApiAppRamResult>(Routes.appRam(appID), {
body: {
Expand Down Expand Up @@ -150,7 +159,7 @@ export default class AppManager extends CachedManager<typeof App> {
* @param options - Options to update your app.
*/
async update(appID: string, options: UpdateAppOptions) {
z.string().parse(appID);
validateNonEmptyString(appID);

options.file = await resolveFile(options.file);

Expand All @@ -167,8 +176,10 @@ export default class AppManager extends CachedManager<typeof App> {
* @param appID - Your app id
*/
async delete(appID: string): Promise<RESTDeleteApiAppDeleteResult>
async delete(appID?: "all"): Promise<ApiAppManagerRemovedAll>
async delete(appID = "all") {
async delete(appID: "all"): Promise<ApiAppManagerRemovedAll>
async delete(appID: string) {
validateNonEmptyString(appID);

const data = await this.discloudApp.rest.delete<
| RESTDeleteApiAppDeleteResult
| RESTDeleteApiAppAllDeleteResult
Expand All @@ -194,6 +205,7 @@ export default class AppManager extends CachedManager<typeof App> {
* @param options - Options to update your app.
*/
async profile(appID: string, options: ProfileOptions) {
validateNonEmptyString(appID);
ProfileOptions.parse(options);

const data = await this.discloudApp.rest.put<RESTApiBaseResult>(Routes.appProfile(appID), {
Expand All @@ -212,8 +224,10 @@ export default class AppManager extends CachedManager<typeof App> {
* @param appID - You app id
*/
async restart(appID: string): Promise<RESTPutApiAppRestartResult>
async restart(appID?: "all"): Promise<ApiAppManagerRestartedAll>
async restart(appID = "all") {
async restart(appID: "all"): Promise<ApiAppManagerRestartedAll>
async restart(appID: string) {
validateNonEmptyString(appID);

const data = await this.discloudApp.rest.put<
| RESTPutApiAppRestartResult
| RESTPutApiAppAllRestartResult
Expand Down Expand Up @@ -242,8 +256,10 @@ export default class AppManager extends CachedManager<typeof App> {
* @param appID - You app id
*/
async start(appID: string): Promise<RESTPutApiAppStartResult>
async start(appID?: "all"): Promise<ApiAppManagerStartedAll>
async start(appID = "all") {
async start(appID: "all"): Promise<ApiAppManagerStartedAll>
async start(appID: string) {
validateNonEmptyString(appID);

const data = await this.discloudApp.rest.put<
| RESTPutApiAppStartResult
| RESTPutApiAppAllStartResult
Expand Down Expand Up @@ -272,8 +288,10 @@ export default class AppManager extends CachedManager<typeof App> {
* @param appID - You app id
*/
async stop(appID: string): Promise<RESTPutApiAppStopResult>
async stop(appID?: "all"): Promise<ApiAppManagerStopedAll>
async stop(appID = "all") {
async stop(appID: "all"): Promise<ApiAppManagerStopedAll>
async stop(appID: string) {
validateNonEmptyString(appID);

const data = await this.discloudApp.rest.put<
| RESTPutApiAppStopResult
| RESTPutApiAppAllStopResult
Expand Down Expand Up @@ -302,8 +320,10 @@ export default class AppManager extends CachedManager<typeof App> {
* @param appID - You app id.
*/
async fetch(appID: string): Promise<App>
async fetch(appID?: "all"): Promise<Map<string, App>>
async fetch(appID = "all") {
async fetch(appID: "all"): Promise<Map<string, App>>
async fetch(appID: string) {
validateNonEmptyString(appID);

if (appID === "all") return this.#fetchMany();

try {
Expand Down
16 changes: 8 additions & 8 deletions packages/discloud.app/src/managers/AppTeamManager.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Routes, type ApiAppTeamManager, type RESTDeleteApiAppTeamResult, type RESTGetApiAppTeamResult, type RESTPostApiAppTeamResult, type RESTPutApiAppTeamResult } from "@discloudapp/api-types/v2";
import { ModPermissionsBF, type ModPermissionsResolvable } from "@discloudapp/util";
import z from "zod";
import type DiscloudApp from "../discloudApp/DiscloudApp";
import { validateNonEmptyString } from "../util/assertions";
import BaseManager from "./BaseManager";

/**
Expand All @@ -20,8 +20,8 @@ export default class AppTeamManager extends BaseManager {
* @param perms - The permissions for the mod. See {@link ModPermissionsResolvable}
*/
async create(appID: string, modID: string, perms: ModPermissionsResolvable): Promise<ApiAppTeamManager> {
z.string().parse(appID);
z.string().parse(modID);
validateNonEmptyString(appID);
validateNonEmptyString(modID);

const data = await this.discloudApp.rest.post<RESTPostApiAppTeamResult>(Routes.appTeam(appID), {
body: {
Expand All @@ -41,8 +41,8 @@ export default class AppTeamManager extends BaseManager {
* @param perms - The permissions for the mod. See {@link ModPermissionsResolvable}
*/
async edit(appID: string, modID: string, perms: ModPermissionsResolvable): Promise<ApiAppTeamManager> {
z.string().parse(appID);
z.string().parse(modID);
validateNonEmptyString(appID);
validateNonEmptyString(modID);

const data = await this.discloudApp.rest.put<RESTPutApiAppTeamResult>(Routes.appTeam(appID), {
body: {
Expand All @@ -62,8 +62,8 @@ export default class AppTeamManager extends BaseManager {
* @param modID - The mod id
*/
async delete(appID: string, modID: string): Promise<RESTDeleteApiAppTeamResult> {
z.string().parse(appID);
z.string().parse(modID);
validateNonEmptyString(appID);
validateNonEmptyString(modID);

const data = await this.discloudApp.rest.delete<
RESTDeleteApiAppTeamResult
Expand All @@ -76,7 +76,7 @@ export default class AppTeamManager extends BaseManager {
* Get mods information for your app on Discloud
*/
async fetch(appID: string) {
z.string().parse(appID);
validateNonEmptyString(appID);

const data = await this.discloudApp.rest.get<RESTGetApiAppTeamResult>(Routes.appTeam(appID));

Expand Down
40 changes: 26 additions & 14 deletions packages/discloud.app/src/managers/TeamAppManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ import { Routes, type ApiAppManagerRestartedAll, type ApiAppManagerStartedAll, t
import { DiscloudAPIError } from "@discloudapp/rest";
import { resolveFile } from "@discloudapp/util";
import { constants } from "http2";
import z from "zod";
import type { UpdateAppOptions } from "../@types";
import type DiscloudApp from "../discloudApp/DiscloudApp";
import AppBackup from "../structures/AppBackup";
import TeamApp from "../structures/TeamApp";
import type TeamAppStatus from "../structures/TeamAppStatus";
import { validateNumberType, validateNonEmptyString } from "../util/assertions";
import CachedManager from "./CachedManager";

/**
Expand All @@ -25,6 +25,8 @@ export default class TeamAppManager extends CachedManager<typeof TeamApp> {
*/
async status(appID: string): Promise<TeamAppStatus>
async status(appID: string) {
validateNonEmptyString(appID);

const data = await this.discloudApp.rest.get<RESTGetApiAppStatusResult>(Routes.teamStatus(appID));

return this._add(data.apps).status;
Expand All @@ -36,8 +38,10 @@ export default class TeamAppManager extends CachedManager<typeof TeamApp> {
* @param appID - Your team app id
*/
async terminal(appID: string): Promise<ApiTerminal>
async terminal(appID?: "all"): Promise<Map<string, ApiTerminal>>
async terminal(appID = "all") {
async terminal(appID: "all"): Promise<Map<string, ApiTerminal>>
async terminal(appID: string) {
validateNonEmptyString(appID);

const data = await this.discloudApp.rest.get<
| RESTGetApiAppLogResult
| RESTGetApiAppAllLogResult
Expand All @@ -61,8 +65,10 @@ export default class TeamAppManager extends CachedManager<typeof TeamApp> {
* @param appID - Your team app id
*/
async backup(appID: string): Promise<AppBackup>
async backup(appID?: "all"): Promise<Map<string, AppBackup>>
async backup(appID = "all") {
async backup(appID: "all"): Promise<Map<string, AppBackup>>
async backup(appID: string) {
validateNonEmptyString(appID);

const data = await this.discloudApp.rest.get<
| RESTGetApiAppBackupResult
| RESTGetApiAppAllBackupResult
Expand All @@ -88,8 +94,8 @@ export default class TeamAppManager extends CachedManager<typeof TeamApp> {
* @param quantity - Minimum values is `100` to `bot` or `512` for `site`
*/
async ram(appID: string, quantity: number) {
z.string().parse(appID);
z.number().parse(quantity);
validateNonEmptyString(appID);
validateNumberType(quantity);

const data = await this.discloudApp.rest.put<RESTPutApiAppRamResult>(Routes.teamRam(appID), {
body: {
Expand All @@ -113,7 +119,7 @@ export default class TeamAppManager extends CachedManager<typeof TeamApp> {
* @param options - Options to update your app.
*/
async update(appID: string, options: UpdateAppOptions) {
z.string().parse(appID);
validateNonEmptyString(appID);

options.file = await resolveFile(options.file);

Expand All @@ -132,8 +138,10 @@ export default class TeamAppManager extends CachedManager<typeof TeamApp> {
* @param appID - Your team app id
*/
async restart(appID: string): Promise<RESTPutApiAppRestartResult>
async restart(appID?: "all"): Promise<ApiAppManagerRestartedAll>
async restart(appID = "all") {
async restart(appID: "all"): Promise<ApiAppManagerRestartedAll>
async restart(appID: string) {
validateNonEmptyString(appID);

const data = await this.discloudApp.rest.put<
| RESTPutApiAppRestartResult
| RESTPutApiAppAllRestartResult
Expand Down Expand Up @@ -162,8 +170,10 @@ export default class TeamAppManager extends CachedManager<typeof TeamApp> {
* @param appID - Your team app id
*/
async start(appID: string): Promise<RESTPutApiAppStartResult>
async start(appID?: "all"): Promise<ApiAppManagerStartedAll>
async start(appID = "all") {
async start(appID: "all"): Promise<ApiAppManagerStartedAll>
async start(appID: string) {
validateNonEmptyString(appID);

const data = await this.discloudApp.rest.put<
| RESTPutApiAppStartResult
| RESTPutApiAppAllStartResult
Expand Down Expand Up @@ -192,8 +202,10 @@ export default class TeamAppManager extends CachedManager<typeof TeamApp> {
* @param appID - Your team app id
*/
async stop(appID: string): Promise<RESTPutApiAppStopResult>
async stop(appID?: "all"): Promise<ApiAppManagerStopedAll>
async stop(appID = "all") {
async stop(appID: "all"): Promise<ApiAppManagerStopedAll>
async stop(appID: string) {
validateNonEmptyString(appID);

const data = await this.discloudApp.rest.put<
| RESTPutApiAppStopResult
| RESTPutApiAppAllStopResult
Expand Down
20 changes: 20 additions & 0 deletions packages/discloud.app/src/util/assertions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import assert from "assert";

export function validateStringType(value: string): void
export function validateStringType(value: unknown): void
export function validateStringType(value: unknown) {
assert(typeof value === "string", new TypeError(`Expected string but received ${typeof value}`));
}

export function validateNonEmptyString(value: string): void
export function validateNonEmptyString(value: unknown): void
export function validateNonEmptyString(value: any) {
validateStringType(value);
assert(value.length, new RangeError("The string cannot be empty"));
}

export function validateNumberType(value: number): void
export function validateNumberType(value: unknown): void
export function validateNumberType(value: unknown) {
assert(typeof value === "number", new TypeError(`Expected number but received ${typeof value}`));
}
Loading