Skip to content

Commit 0c39dc8

Browse files
committed
New error handling & SquareCloudAPI returns apiManager
1 parent acf9fef commit 0c39dc8

File tree

4 files changed

+66
-28
lines changed

4 files changed

+66
-28
lines changed

src/APIManager.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,17 @@ import { APIResponse, RawUserData } from './typings';
22
import axios, { AxiosRequestConfig } from 'axios';
33

44
export class SquareCloudAPIError extends Error {
5-
constructor(code: string) {
5+
constructor(code: string, message?: string) {
66
super();
77

88
this.name = 'SquareCloudAPIError';
99

10-
this.message = code
11-
.replaceAll('_', ' ')
12-
.toLowerCase()
13-
.replace(/(^|\s)\S/g, (L) => L.toUpperCase());
10+
this.message =
11+
code
12+
.replaceAll('_', ' ')
13+
.toLowerCase()
14+
.replace(/(^|\s)\S/g, (L) => L.toUpperCase()) +
15+
(message ? `: ${message}` : '');
1416
}
1517
}
1618

src/Assertions.ts

Lines changed: 48 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,58 @@
1+
import { SquareCloudAPIError } from './APIManager';
12
import { ReadStream } from 'fs';
23
import z from 'zod';
34

4-
export function validateString(value: any): asserts value is string {
5-
z.string().parse(value);
5+
export function validateString(
6+
value: any,
7+
code?: string,
8+
starts: string = ''
9+
): asserts value is string {
10+
if (starts) validateString(starts);
11+
12+
handleParser(
13+
() => z.string().parse(value),
14+
'Expect string, got ' + typeof value,
15+
code
16+
);
617
}
718

8-
export function validateBoolean(value: any): asserts value is boolean {
9-
z.boolean().parse(value);
19+
export function validateBoolean(
20+
value: any,
21+
code?: string
22+
): asserts value is boolean {
23+
handleParser(
24+
() => z.boolean().parse(value),
25+
'Expect boolean, got ' + typeof value,
26+
code
27+
);
1028
}
1129

1230
export function validateCommitLike(
13-
value: any
14-
): asserts value is string | ReadStream {
15-
z.string()
16-
.or(
31+
value: any,
32+
code?: string
33+
): asserts value is string | ReadStream | Buffer {
34+
handleParser(
35+
() =>
1736
z
18-
.custom((value) => value instanceof ReadStream)
19-
.or(z.custom((value) => value instanceof Buffer))
20-
)
21-
.parse(value);
37+
.string()
38+
.or(
39+
z
40+
.custom((value) => value instanceof ReadStream)
41+
.or(z.custom((value) => value instanceof Buffer))
42+
)
43+
.parse(value),
44+
'Expect string, ReadStream or Buffer, got ' + typeof value,
45+
code
46+
);
47+
}
48+
49+
function handleParser(func: any, message: string, code?: string) {
50+
try {
51+
func();
52+
} catch {
53+
throw new SquareCloudAPIError(
54+
code ? `INVALID_${code}` : 'VALIDATION_ERROR',
55+
message
56+
);
57+
}
2258
}

src/index.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,15 @@ export class SquareCloudAPI {
99
baseUrl: 'https://api.squarecloud.app/v1/public/',
1010
};
1111

12-
private apiManager: APIManager;
12+
public readonly apiManager: APIManager;
1313

1414
/**
1515
* Creates an API instance
1616
*
1717
* @param apiKey - Your API Token (you can get it at [SquareCloud Dashboard](https://squarecloud.app/dashboard))
1818
*/
1919
constructor(apiKey: string) {
20-
validateString(apiKey);
20+
validateString(apiKey, 'API_KEY');
2121

2222
this.apiManager = new APIManager(apiKey);
2323
}
@@ -30,7 +30,7 @@ export class SquareCloudAPI {
3030
async getUser(): Promise<FullUser>;
3131
async getUser(userId: string): Promise<User>;
3232
async getUser(userId?: string): Promise<User> {
33-
if (userId) validateString(userId);
33+
if (userId) validateString(userId, 'USER_ID');
3434

3535
const userData = await this.apiManager.user(userId);
3636
const hasAccess = userData.user.email !== 'Access denied';
@@ -44,7 +44,7 @@ export class SquareCloudAPI {
4444
* @param appId - The application id, you must own the application
4545
*/
4646
async getApplication(appId: string): Promise<Application> {
47-
validateString(appId);
47+
validateString(appId, 'APP_ID');
4848

4949
const { applications } = await this.apiManager.user();
5050
const appData = applications.find((app) => app.id === appId);

src/structures/Application.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ export class Application {
8585
* @param full - Whether you want the complete logs (true) or the recent ones (false)
8686
*/
8787
async getLogs(full: boolean = false) {
88-
validateBoolean(full);
88+
validateBoolean(full, '[LOGS_FULL]');
8989

9090
return (
9191
await this.apiManager.application(`${full ? 'full-' : ''}logs`, this.id)
@@ -138,7 +138,7 @@ export class Application {
138138
* Commit changes to a specific file inside your application folder
139139
*
140140
* - This action is irreversible.
141-
* - Tip: use this to get an absolute path.
141+
* - Tip: use this to get an absolute path.
142142
* ```ts
143143
* require('path').join(__dirname, 'fileName')
144144
* ```
@@ -150,20 +150,20 @@ export class Application {
150150
async commit(
151151
file: Buffer,
152152
fileName: string,
153-
fileExtension: string
153+
fileExtension: `.${string}`
154154
): Promise<boolean>;
155155
async commit(
156156
file: string | ReadStream | Buffer,
157157
fileName?: string,
158-
fileExtension?: string
158+
fileExtension?: `.${string}`
159159
): Promise<boolean> {
160-
validateCommitLike(file);
160+
validateCommitLike(file, 'COMMIT_DATA');
161161

162162
const formData = new FormData();
163163

164164
if (file instanceof Buffer) {
165-
validateString(fileName);
166-
validateString(fileExtension);
165+
validateString(fileName, 'FILE_NAME');
166+
validateString(fileExtension, 'FILE_EXTENSION');
167167

168168
formData.append('file', file, { filename: fileName + fileExtension });
169169
} else {

0 commit comments

Comments
 (0)