Skip to content

Commit 08f91ee

Browse files
committed
add user assertion and update User constructor
1 parent 302c8dc commit 08f91ee

File tree

2 files changed

+57
-8
lines changed

2 files changed

+57
-8
lines changed

src/assertions/user.ts

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import { ApplicationLanguage, UserPlanName } from "@squarecloud/api-types/v2";
2+
import * as z from "zod";
3+
import { SquareCloudAPIError } from "..";
4+
5+
const userSchema = z.object({
6+
id: z.string(),
7+
tag: z.string(),
8+
locale: z.string(),
9+
email: z.string(),
10+
plan: z.object({
11+
name: z.nativeEnum(UserPlanName),
12+
memory: z.object({
13+
limit: z.number(),
14+
available: z.number(),
15+
used: z.number(),
16+
}),
17+
duration: z.number().nullable(),
18+
}),
19+
});
20+
21+
const userApplicationSchema = z.object({
22+
id: z.string(),
23+
tag: z.string(),
24+
desc: z.string().optional(),
25+
ram: z.number(),
26+
lang: z.nativeEnum(ApplicationLanguage),
27+
cluster: z.string(),
28+
isWebsite: z.boolean(),
29+
});
30+
31+
const userInfoSchema = z.object({
32+
user: userSchema,
33+
applications: z.array(userApplicationSchema),
34+
});
35+
36+
export function assertUserInfo(value: unknown): asserts value is z.infer<typeof userInfoSchema> {
37+
try {
38+
userInfoSchema.parse(value);
39+
} catch {
40+
throw new SquareCloudAPIError("INVALID_API_USER_INFO", "Invalid user info object received from API /user");
41+
}
42+
}

src/structures/user.ts

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { assertUserInfo } from "@/assertions/user";
12
import { UserPlan } from "@/types/user";
23
import { APIUserInfo } from "@squarecloud/api-types/v2";
34
import { BaseApplication, Collection, SquareCloudAPI } from "..";
@@ -24,15 +25,21 @@ export class User {
2425
applications: Collection<string, BaseApplication>;
2526

2627
constructor(client: SquareCloudAPI, data: APIUserInfo) {
27-
this.id = data.user.id;
28-
this.tag = data.user.tag;
29-
this.locale = data.user.locale;
28+
assertUserInfo(data);
29+
30+
const { user, applications } = data;
31+
const { id, tag, locale, plan, email } = user;
32+
const { duration } = plan;
33+
34+
this.id = id;
35+
this.tag = tag;
36+
this.locale = locale;
37+
this.email = email;
3038
this.plan = {
31-
...data.user.plan,
32-
expiresInTimestamp: data.user.plan.duration ?? undefined,
33-
expiresIn: data.user.plan.duration ? new Date(data.user.plan.duration) : undefined,
39+
...plan,
40+
expiresInTimestamp: duration ?? undefined,
41+
expiresIn: duration ? new Date(duration) : undefined,
3442
};
35-
this.email = data.user.email;
36-
this.applications = new Collection(data.applications.map((app) => [app.id, new BaseApplication(client, app)]));
43+
this.applications = new Collection(applications.map((app) => [app.id, new BaseApplication(client, app)]));
3744
}
3845
}

0 commit comments

Comments
 (0)