|
1 | | -import FormData from 'form-data'; |
2 | | -import { readFile } from 'fs/promises'; |
3 | | -import { ApiManager, SquareCloudAPIError } from './ApiManager'; |
4 | | -import { validatePathLike, validateString } from './Assertions'; |
5 | | -import { Application } from './structures/Application'; |
6 | | -import { FullUser, User } from './structures/User'; |
7 | | -import { Options } from './typings'; |
8 | | - |
9 | | -class SquareCloudAPI { |
| 1 | +import { validateString } from './assertions'; |
| 2 | +import APIManager from './managers/api'; |
| 3 | +import ApplicationManager from './managers/application'; |
| 4 | +import ExperimentalManager from './managers/experimental'; |
| 5 | +import UserManager from './managers/user'; |
| 6 | +import { APIOptions } from './types'; |
| 7 | + |
| 8 | +export class SquareCloudAPI { |
10 | 9 | static apiInfo = { |
11 | | - version: 'v1', |
| 10 | + latestVersion: 'v2', |
12 | 11 | baseUrl: 'https://api.squarecloud.app/', |
13 | 12 | }; |
14 | 13 |
|
15 | | - private apiManager: ApiManager; |
16 | | - private experimental?: boolean; |
| 14 | + private apiManager: APIManager; |
| 15 | + |
| 16 | + /** Use experimental features */ |
| 17 | + public experimental?: ExperimentalManager; |
| 18 | + /** The applications manager */ |
| 19 | + public applications: ApplicationManager; |
| 20 | + /** The users manager */ |
| 21 | + public users: UserManager; |
17 | 22 |
|
18 | 23 | /** |
19 | 24 | * Creates an API instance |
20 | 25 | * |
21 | 26 | * @param apiKey - Your API Token (generate at [Square Cloud Dashboard](https://squarecloud.app/dashboard)) |
22 | | - * @param options.customApiManager - Custom API manager. Just use if you know what you are doing! |
23 | 27 | * @param options.experimental - Whether to enable experimental features |
24 | 28 | */ |
25 | | - constructor(apiKey: string, options?: Options) { |
| 29 | + constructor(apiKey: string, options?: APIOptions) { |
26 | 30 | validateString(apiKey, 'API_KEY'); |
27 | 31 |
|
28 | | - this.apiManager = new ApiManager(apiKey); |
29 | | - this.experimental = Boolean(options?.experimental); |
30 | | - } |
31 | | - |
32 | | - /** |
33 | | - * Gets a user's informations |
34 | | - * |
35 | | - * @param userId - The user ID, if not provided it will get your own information |
36 | | - */ |
37 | | - async getUser(): Promise<FullUser>; |
38 | | - async getUser(userId: string): Promise<User>; |
39 | | - async getUser(userId?: string): Promise<User> { |
40 | | - if (userId) { |
41 | | - validateString(userId, 'USER_ID'); |
42 | | - } |
43 | | - |
44 | | - const data = await this.apiManager.user(userId); |
45 | | - const hasAccess = data.user.email && data.user.email !== 'Access denied'; |
46 | | - |
47 | | - return new (hasAccess ? FullUser : User)(this.apiManager, data); |
48 | | - } |
49 | | - |
50 | | - /** |
51 | | - * Returns an application that you can manage or get information |
52 | | - * |
53 | | - * @param appId - The application ID, you must own the application |
54 | | - */ |
55 | | - async getApplication(appId: string): Promise<Application> { |
56 | | - validateString(appId, 'APP_ID'); |
57 | | - |
58 | | - const data = await this.apiManager.user(); |
59 | | - const applications = data.applications || []; |
60 | | - const applicaton = applications.find((app) => app.id === appId); |
61 | | - |
62 | | - if (!applicaton) { |
63 | | - throw new SquareCloudAPIError('APP_NOT_FOUND'); |
64 | | - } |
65 | | - |
66 | | - return new Application(this.apiManager, applicaton); |
67 | | - } |
68 | | - |
69 | | - /** |
70 | | - * Upload a new application to Square Cloud |
71 | | - * |
72 | | - * - Don't forget the [configuration file](https://config.squarecloud.app/). |
73 | | - * - This only accepts .zip files. |
74 | | - * |
75 | | - * - Tip: use this to get an absolute path. |
76 | | - * ```ts |
77 | | - * require('path').join(__dirname, 'fileName') |
78 | | - * ``` |
79 | | - * |
80 | | - * @param file - Buffer or absolute path to the file |
81 | | - * |
82 | | - * @returns The uploaded application ID |
83 | | - */ |
84 | | - async uploadApplication(file: string | Buffer) { |
85 | | - validatePathLike(file, 'UPLOAD_DATA'); |
86 | | - |
87 | | - if (typeof file === 'string') { |
88 | | - file = await readFile(file); |
89 | | - } |
90 | | - |
91 | | - const formData = new FormData(); |
92 | | - formData.append('file', file, { filename: 'app.zip' }); |
93 | | - |
94 | | - const data = await this.apiManager.fetch('upload', { |
95 | | - method: 'POST', |
96 | | - body: formData.getBuffer(), |
97 | | - headers: formData.getHeaders(), |
98 | | - }); |
99 | | - |
100 | | - return <string>data?.app?.id; |
101 | | - } |
102 | | - |
103 | | - /** |
104 | | - * @experimental |
105 | | - * Use the new Square Cloud experimental AI feature. |
106 | | - * **May have bugs.** |
107 | | - * |
108 | | - * @param question - The question you want to be answered :) |
109 | | - * @param prompt - Optional context or previous messages |
110 | | - */ |
111 | | - async askAi(question: string, prompt?: string): Promise<string | undefined> { |
112 | | - if (!this.experimental) { |
113 | | - return; |
114 | | - } |
115 | | - |
116 | | - const data = await this.apiManager.fetch( |
117 | | - 'ai', |
118 | | - { |
119 | | - method: 'POST', |
120 | | - body: JSON.stringify({ question, prompt }), |
121 | | - }, |
122 | | - 'experimental' |
123 | | - ); |
124 | | - |
125 | | - return data?.response; |
| 32 | + this.apiManager = new APIManager(apiKey); |
| 33 | + this.experimental = options?.experimental |
| 34 | + ? new ExperimentalManager(this.apiManager) |
| 35 | + : undefined; |
| 36 | + this.applications = new ApplicationManager(this.apiManager); |
| 37 | + this.users = new UserManager(this.apiManager); |
126 | 38 | } |
127 | 39 | } |
128 | | - |
129 | | -module.exports = Object.assign(SquareCloudAPI, { |
130 | | - default: SquareCloudAPI, |
131 | | - ApiManager, |
132 | | - Application, |
133 | | - FullUser, |
134 | | - User, |
135 | | -}); |
136 | | - |
137 | | -export default SquareCloudAPI; |
138 | | -export * from './typings'; |
139 | | -export { ApiManager, Application, FullUser, User }; |
140 | | - |
0 commit comments