Skip to content

Commit 3298be1

Browse files
committed
refactor: improve api internal typings and helpers
1 parent a74ca01 commit 3298be1

File tree

3 files changed

+59
-46
lines changed

3 files changed

+59
-46
lines changed

src/lib/routes.ts

Lines changed: 38 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,100 +1,103 @@
11
import type { APIEndpoint } from "@/types";
22

33
export type Route<T extends APIEndpoint> = string & { __route: T };
4-
export const Route = <T extends APIEndpoint>(route: string) => {
5-
return route as Route<T>;
6-
};
4+
export const Route = <T extends APIEndpoint>(route: string) =>
5+
route as Route<T>;
6+
7+
interface IRoutes {
8+
[k: string]: ((...args: string[]) => Route<APIEndpoint>) | IRoutes;
9+
}
710

811
export const Routes = {
9-
user() {
12+
user: () => {
1013
return Route<"user">("/v2/users/me");
1114
},
1215
service: {
13-
status() {
16+
status: () => {
1417
return Route<"service/status">("/v2/service/status");
1518
},
1619
},
1720
apps: {
18-
upload() {
21+
upload: () => {
1922
return Route<"apps/upload">("/v2/apps");
2023
},
21-
statusAll() {
24+
statusAll: () => {
2225
return Route<"apps/status-all">("/v2/apps/status");
2326
},
24-
info(appId: string) {
27+
info: (appId: string) => {
2528
return Route<"apps/info">(`/v2/apps/${appId}`);
2629
},
27-
status(appId: string) {
30+
status: (appId: string) => {
2831
return Route<"apps/status">(`/v2/apps/${appId}/status`);
2932
},
30-
logs(appId: string) {
33+
logs: (appId: string) => {
3134
return Route<"apps/logs">(`/v2/apps/${appId}/logs`);
3235
},
33-
backups(appId: string) {
36+
delete: (appId: string) => {
37+
return Route<"apps/delete">(`/v2/apps/${appId}`);
38+
},
39+
commit: (appId: string) => {
40+
return Route<"apps/commit">(`/v2/apps/${appId}/commit`);
41+
},
42+
backups: (appId: string) => {
3443
return Route<"apps/backups">(`/v2/apps/${appId}/backups`);
3544
},
36-
generateBackup(appId: string) {
45+
generateBackup: (appId: string) => {
3746
return Route<"apps/generate-backup">(`/v2/apps/${appId}/backups`);
3847
},
39-
start(appId: string) {
48+
start: (appId: string) => {
4049
return Route<"apps/start">(`/v2/apps/${appId}/start`);
4150
},
42-
restart(appId: string) {
51+
restart: (appId: string) => {
4352
return Route<"apps/restart">(`/v2/apps/${appId}/restart`);
4453
},
45-
stop(appId: string) {
54+
stop: (appId: string) => {
4655
return Route<"apps/stop">(`/v2/apps/${appId}/stop`);
4756
},
48-
delete(appId: string) {
49-
return Route<"apps/delete">(`/v2/apps/${appId}`);
50-
},
51-
commit(appId: string) {
52-
return Route<"apps/commit">(`/v2/apps/${appId}/commit`);
53-
},
5457
files: {
55-
read(appId: string) {
58+
read: (appId: string) => {
5659
return Route<"apps/files/read">(`/v2/apps/${appId}/files/content`);
5760
},
58-
list(appId: string) {
61+
list: (appId: string) => {
5962
return Route<"apps/files/list">(`/v2/apps/${appId}/files`);
6063
},
61-
upsert(appId: string) {
64+
upsert: (appId: string) => {
6265
return Route<"apps/files/upsert">(`/v2/apps/${appId}/files`);
6366
},
64-
move(appId: string) {
67+
move: (appId: string) => {
6568
return Route<"apps/files/move">(`/v2/apps/${appId}/files`);
6669
},
67-
delete(appId: string) {
70+
delete: (appId: string) => {
6871
return Route<"apps/files/delete">(`/v2/apps/${appId}/files`);
6972
},
7073
},
7174
deployments: {
72-
list(appId: string) {
75+
list: (appId: string) => {
7376
return Route<"apps/deployments/list">(`/v2/apps/${appId}/deployments`);
7477
},
75-
current(appId: string) {
78+
current: (appId: string) => {
7679
return Route<"apps/deployments/current">(
7780
`/v2/apps/${appId}/deployments/current`,
7881
);
7982
},
80-
webhook(appId: string) {
83+
webhook: (appId: string) => {
8184
return Route<"apps/deployments/webhook">(
8285
`/v2/apps/${appId}/deploy/webhook`,
8386
);
8487
},
8588
},
8689
network: {
87-
dns(appId: string) {
90+
dns: (appId: string) => {
8891
return Route<"apps/network/dns">(`/v2/apps/${appId}/network/dns`);
8992
},
90-
analytics(appId: string) {
93+
custom: (appId: string) => {
94+
return Route<"apps/network/custom">(`/v2/apps/${appId}/network/custom`);
95+
},
96+
analytics: (appId: string) => {
9197
return Route<"apps/network/analytics">(
9298
`/v2/apps/${appId}/network/analytics`,
9399
);
94100
},
95-
custom(appId: string) {
96-
return Route<"apps/network/custom">(`/v2/apps/${appId}/network/custom`);
97-
},
98101
},
99102
},
100-
};
103+
} satisfies IRoutes;

src/managers/api.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import type { Route } from "@/lib/routes";
2-
import type { APIPayload, APIVersion } from "@squarecloud/api-types/v2";
2+
import type { APIVersion } from "@squarecloud/api-types/v2";
33
import {
44
type APIEndpoint,
55
type APIRequestOptions,
@@ -11,11 +11,11 @@ export class APIManager {
1111
public readonly baseUrl = "https://api.squarecloud.app";
1212
public readonly version: APIVersion<1 | 2> = "v2";
1313

14-
constructor(readonly apiKey: string) {}
14+
constructor(protected readonly apiKey: string) {}
1515

16-
async request<T extends APIEndpoint>(
16+
async request<T extends APIEndpoint, U extends APIRequestOptions<T>>(
1717
path: Route<T>,
18-
options?: APIRequestOptions<T>,
18+
options?: U,
1919
): Promise<APIResponse<T>> {
2020
const { url, init } = this.parseRequestOptions(path, options);
2121

@@ -35,7 +35,7 @@ export class APIManager {
3535
path: string,
3636
options?: APIRequestOptions<APIEndpoint>,
3737
) {
38-
const init = options || ({} as RequestInit);
38+
const init: RequestInit = options || {};
3939

4040
init.method = init.method || "GET";
4141
init.headers = {

src/types/api.ts

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ export interface APIEndpoints {
4949
response: APIServiceStatus;
5050
};
5151
"apps/upload": {
52+
method: "POST";
5253
body: Buffer;
5354
response: RESTPostAPIApplicationUploadResult;
5455
};
@@ -68,21 +69,27 @@ export interface APIEndpoints {
6869
response: APIApplicationBackup[];
6970
};
7071
"apps/generate-backup": {
72+
method: "POST";
7173
response: RESTPostAPIApplicationBackupResult;
7274
};
7375
"apps/start": {
76+
method: "POST";
7477
response: undefined;
7578
};
7679
"apps/restart": {
80+
method: "POST";
7781
response: undefined;
7882
};
7983
"apps/stop": {
84+
method: "POST";
8085
response: undefined;
8186
};
8287
"apps/delete": {
88+
method: "DELETE";
8389
response: undefined;
8490
};
8591
"apps/commit": {
92+
method: "POST";
8693
query: RESTPostAPIApplicationCommitQuery;
8794
body: Buffer;
8895
response: undefined;
@@ -96,14 +103,17 @@ export interface APIEndpoints {
96103
response: APIListedFile[];
97104
};
98105
"apps/files/upsert": {
106+
method: "PUT";
99107
body: RESTPutAPIFileUpsertJSONBody;
100108
response: undefined;
101109
};
102110
"apps/files/move": {
111+
method: "PATCH";
103112
body: RESTPatchAPIFileMoveJSONBody;
104113
response: undefined;
105114
};
106115
"apps/files/delete": {
116+
method: "DELETE";
107117
query: RESTDeleteAPIFileDeleteQuery;
108118
response: undefined;
109119
};
@@ -114,6 +124,7 @@ export interface APIEndpoints {
114124
response: APIDeploymentCurrent;
115125
};
116126
"apps/deployments/webhook": {
127+
method: "POST";
117128
body: RESTPostAPIGithubWebhookJSONBody;
118129
response: RESTPostAPIGithubWebhookResult;
119130
};
@@ -124,20 +135,19 @@ export interface APIEndpoints {
124135
response: APINetworkAnalytics;
125136
};
126137
"apps/network/custom": {
138+
method: "POST";
127139
body: RESTPostAPINetworkCustomDomainJSONBody;
128140
response: undefined;
129141
};
130142
}
131143

132144
export type APIEndpoint = keyof APIEndpoints;
133145

134-
export type APIRequestOptions<
135-
T extends APIEndpoint,
136-
U extends APIEndpoints[T] = APIEndpoints[T],
137-
> = Omit<U, "response"> & {
138-
method?: "GET" | "POST" | "PATCH" | "PUT" | "DELETE";
146+
export type APIMethod = "GET" | "POST" | "PATCH" | "PUT" | "DELETE";
147+
148+
export type APIRequestOptions<T extends APIEndpoint> = {
139149
headers?: HeadersInit;
140-
};
150+
} & Omit<APIEndpoints[T], "response">;
141151

142152
export type APIResponse<T extends APIEndpoint> = APIPayload<
143153
APIEndpoints[T]["response"]

0 commit comments

Comments
 (0)