Skip to content

Commit 838c145

Browse files
committed
Fixed API Manager
1 parent cfaed19 commit 838c145

File tree

2 files changed

+19
-20
lines changed

2 files changed

+19
-20
lines changed

src/APIManager.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import { APIResponse, RawUserData } from './typings';
2-
import axios, { AxiosRequestConfig } from 'axios';
32

43
export class SquareCloudAPIError extends Error {
54
constructor(code: string, message?: string) {
@@ -25,22 +24,23 @@ export class APIManager {
2524
try {
2625
await this.fetch('user');
2726
} catch {
28-
throw new SquareCloudAPIError('INVALID_API_KEY')
27+
throw new SquareCloudAPIError('INVALID_API_KEY');
2928
}
3029
}
3130

32-
private async fetch(path: string, options: AxiosRequestConfig = {}) {
31+
private async fetch(path: string, options: RequestInit = {}) {
3332
options.headers = {
3433
...(options.headers || {}),
3534
Authorization: this.apiKey,
3635
};
3736

3837
options.method = options.method || 'GET';
3938

40-
const { data } = await axios(
39+
const data = await fetch(
4140
'https://api.squarecloud.app/v1/public/' + path,
4241
options
43-
).catch((r) => r.response);
42+
)
43+
.then((r) => r.json());
4444

4545
if (data.status === 'error') {
4646
throw new SquareCloudAPIError(data.code);
@@ -49,7 +49,7 @@ export class APIManager {
4949
return data;
5050
}
5151

52-
user(id?: string, options: AxiosRequestConfig = {}): Promise<RawUserData> {
52+
user(id?: string, options: RequestInit = {}): Promise<RawUserData> {
5353
return this.fetch('user' + (id ? `/${id}` : ''), options).then(
5454
(e) => e.response
5555
);
@@ -58,7 +58,7 @@ export class APIManager {
5858
application(
5959
path: string,
6060
id: string,
61-
options: AxiosRequestConfig | boolean = {}
61+
options: RequestInit | boolean = {}
6262
): Promise<APIResponse> {
6363
return this.fetch(
6464
`${path}/${id}`,

src/structures/Application.ts

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,11 @@ export class Application {
134134
* - This action is irreversible.
135135
*/
136136
async delete(): Promise<boolean> {
137-
const { code } = await this.#apiManager.application('delete', this.id, true);
137+
const { code } = await this.#apiManager.application(
138+
'delete',
139+
this.id,
140+
true
141+
);
138142

139143
return code === 'APP_DELETED';
140144
}
@@ -149,28 +153,23 @@ export class Application {
149153
* ```
150154
* - Tip2: use zip file to commit more than one file
151155
*
152-
* @param file - The absolute file path or a ReadStream
156+
* @param file - The absolute file path, a Buffer or a ReadStream
157+
* @param fileName - If a Buffer is provided you must provide the file name and extension too
153158
*/
154159
async commit(file: string | ReadStream): Promise<boolean>;
155-
async commit(
156-
file: Buffer,
157-
fileName: string,
158-
fileExtension: `.${string}`
159-
): Promise<boolean>;
160+
async commit(file: Buffer, fileName: string): Promise<boolean>;
160161
async commit(
161162
file: string | ReadStream | Buffer,
162-
fileName?: string,
163-
fileExtension?: `.${string}`
163+
fileName?: string
164164
): Promise<boolean> {
165165
validateCommitLike(file, 'COMMIT_DATA');
166166

167167
const formData = new FormData();
168168

169169
if (file instanceof Buffer) {
170170
validateString(fileName, 'FILE_NAME');
171-
validateString(fileExtension, 'FILE_EXTENSION');
172171

173-
formData.append('file', file, { filename: fileName + fileExtension });
172+
formData.append('file', file, { filename: fileName });
174173
} else {
175174
formData.append(
176175
'file',
@@ -180,8 +179,8 @@ export class Application {
180179

181180
const { code } = await this.#apiManager.application('commit', this.id, {
182181
method: 'POST',
183-
data: formData,
184-
headers: { ...formData.getHeaders() },
182+
body: formData.getBuffer(),
183+
headers: formData.getHeaders(),
185184
});
186185

187186
return code === 'SUCCESS';

0 commit comments

Comments
 (0)