Skip to content
This repository was archived by the owner on Sep 17, 2024. It is now read-only.

Commit 035a138

Browse files
authored
chore: Convert old modules to camelCase and register RuntimeErrors (#52)
2 parents c898d1f + 116761f commit 035a138

File tree

8 files changed

+23
-18
lines changed

8 files changed

+23
-18
lines changed

modules/currency/scripts/set_balance.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ export async function run(
1414
ctx: ScriptContext,
1515
req: Request,
1616
): Promise<Response> {
17-
await ctx.call("rate_limit", "throttle", { requests: 25 });
17+
await ctx.modules.rateLimit.throttle({ requests: 25 });
1818

1919
if (req.balance < 0) throw new RuntimeError("INVALID_AMOUNT");
2020

modules/currency/tests/e2e.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ test(
1717

1818
assertEquals(updatedBalance, 100);
1919

20-
const { balance: initialBalance } = await ctx.modules.currency.get_balance({
20+
const { balance: initialBalance } = await ctx.modules.currency.getBalance({
2121
userId: user.id,
2222
});
2323

@@ -30,14 +30,14 @@ test(
3030

3131
assertEquals(withdraw, 50);
3232

33-
const { balance: postWithdrawGetBalance } = await ctx.modules.currency.get_balance({
33+
const { balance: postWithdrawGetBalance } = await ctx.modules.currency.getBalance({
3434
userId: user.id,
3535
});
3636

3737
assertEquals(postWithdrawGetBalance, 50);
3838

39-
const { balance } = await ctx.modules.currency.get_balance_by_token({
40-
userToken: token,
39+
const { balance } = await ctx.modules.currency.getBalanceByToken({
40+
userToken: token.token,
4141
}) ;
4242

4343
assertEquals(balance, 50);
@@ -46,19 +46,19 @@ test(
4646
assertEquals(postWithdrawGetBalance, balance);
4747

4848
// set bal to 0
49-
await ctx.modules.currency.set_balance({
49+
await ctx.modules.currency.setBalance({
5050
userId: user.id,
5151
balance: 0,
5252
});
5353

54-
const { balance: finalBalance } = await ctx.modules.currency.get_balance({
54+
const { balance: finalBalance } = await ctx.modules.currency.getBalance({
5555
userId: user.id,
5656
});
5757

5858
assertEquals(finalBalance, 0);
5959

60-
const { balance: finalBalanceByToken } = await ctx.modules.currency.get_balance_by_token({
61-
userToken: token,
60+
const { balance: finalBalanceByToken } = await ctx.modules.currency.getBalanceByToken({
61+
userToken: token.token,
6262
});
6363

6464
assertEquals(finalBalanceByToken, 0);

modules/currency/utils/set_balance.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { prisma } from "../_gen/mod.ts";
1+
import { RuntimeError, prisma } from "../_gen/mod.ts";
22

33
type LimittedDB = Omit<
44
prisma.Prisma.DefaultPrismaClient,
@@ -10,7 +10,7 @@ export const setBalance = async (
1010
userId: string,
1111
balance: number,
1212
) => {
13-
if (balance < 0) throw new RangeError("Invalid balance");
13+
if (balance < 0) throw new RuntimeError("INVALID_BALANCE");
1414

1515
await db.userWallet.upsert({
1616
where: {

modules/friends/module.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,4 @@ errors:
2323
NOT_FRIEND_REQUEST_RECIPIENT: {}
2424
FRIEND_REQUEST_ALREADY_ACCEPTED: {}
2525
FRIEND_REQUEST_ALREADY_DECLINED: {}
26+
CANNOT_SEND_TO_SELF: {}

modules/friends/scripts/send_request.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,10 @@ export async function run(
2121

2222
const { userId } = await ctx.call("users", "validate_token", {
2323
userToken: req.userToken,
24-
}) as any;
24+
});
2525

2626
if (userId === req.targetUserId) {
27-
throw new Error("You cannot send a friend request to yourself");
27+
throw new RuntimeError("CANNOT_SEND_TO_SELF");
2828
}
2929

3030
// Sort the user IDs to ensure consistency

modules/users/module.yaml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,6 @@ scripts:
77
register:
88
public: true
99
validate_token: {}
10-
errors: {}
10+
errors:
11+
TOKEN_NOT_USER_TOKEN: {}
12+
UNKNOWN_IDENTITY_TYPE: {}

modules/users/scripts/register.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { RuntimeError } from "../_gen/mod.ts";
12
import { ScriptContext } from "../_gen/scripts/get.ts";
23
import { User } from "../types/common.ts";
34

@@ -37,7 +38,7 @@ export async function run(
3738
},
3839
};
3940
} else {
40-
throw new Error("Unknown identity type");
41+
throw new RuntimeError("UNKNOWN_IDENTITY_TYPE");
4142
}
4243

4344
// Create user

modules/users/scripts/validate_token.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { RuntimeError } from "../_gen/mod.ts";
12
import { ScriptContext } from "../_gen/scripts/validate_token.ts";
23

34
export interface Request {
@@ -12,10 +13,10 @@ export async function run(
1213
ctx: ScriptContext,
1314
req: Request,
1415
): Promise<Response> {
15-
const { token } = await ctx.call("tokens", "validate", {
16+
const { token } = await ctx.modules.tokens.validate({
1617
token: req.userToken,
17-
}) as any;
18-
if (token.type !== "user") throw new Error("Token is not a user token");
18+
});
19+
if (token.type !== "user") throw new RuntimeError("TOKEN_NOT_USER_TOKEN");
1920
const userId = token.meta.userId;
2021

2122
return { userId };

0 commit comments

Comments
 (0)