Skip to content

Commit 855cb9f

Browse files
authored
chore(build): upgrade msw (#553)
1 parent b7027b4 commit 855cb9f

File tree

9 files changed

+273
-309
lines changed

9 files changed

+273
-309
lines changed

pnpm-lock.yaml

Lines changed: 159 additions & 168 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,39 @@
1-
import { rest } from "msw";
1+
import { http, HttpResponse } from "msw";
22

33
import { MOCK_VALUES } from "./constants";
44

55
export const balanceHandlers = [
6-
rest.get("/v2/balances*", (req, res, ctx) => {
6+
http.get("/v2/balances*", () => {
77
const response = {
88
balance: {
99
bbn: MOCK_VALUES.BBN_BALANCE,
1010
stakable_btc: MOCK_VALUES.STAKABLE_BTC,
1111
},
1212
};
13-
return res(ctx.json(response));
13+
return HttpResponse.json(response);
1414
}),
1515

16-
rest.get("/v2/staked*", (req, res, ctx) => {
16+
http.get("/v2/staked*", () => {
1717
const response = {
1818
staked: {
1919
btc: MOCK_VALUES.STAKED_BTC,
2020
delegated_btc: MOCK_VALUES.STAKED_BTC,
2121
},
2222
};
23-
return res(ctx.json(response));
23+
return HttpResponse.json(response);
2424
}),
2525

26-
rest.get("/v2/stakable-btc*", (req, res, ctx) => {
26+
http.get("/v2/stakable-btc*", () => {
2727
const response = {
2828
balance: MOCK_VALUES.STAKABLE_BTC,
2929
};
30-
return res(ctx.json(response));
30+
return HttpResponse.json(response);
3131
}),
3232

33-
rest.get("/v2/rewards*", (req, res, ctx) => {
33+
http.get("/v2/rewards*", () => {
3434
const response = {
3535
rewards: MOCK_VALUES.REWARDS,
3636
};
37-
return res(ctx.json(response));
37+
return HttpResponse.json(response);
3838
}),
3939
];
Lines changed: 42 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,17 @@
11
import { incentivequery } from "@babylonlabs-io/babylon-proto-ts";
22
import { QueryBalanceResponse } from "cosmjs-types/cosmos/bank/v1beta1/query.js";
3-
import {
4-
type ResponseComposition,
5-
type RestContext,
6-
type RestRequest,
7-
rest,
8-
} from "msw";
3+
import { http, HttpResponse, type HttpHandler } from "msw";
94

105
import { MOCK_VALUES } from "./constants";
116

12-
type QueryHandler = (
13-
req: RestRequest,
14-
res: ResponseComposition,
15-
ctx: RestContext,
16-
) => any;
7+
type QueryHandler = ({ request }: { request: Request }) => Response;
178

189
interface QueryStrategy {
1910
pattern: RegExp | string;
2011
handler: QueryHandler;
2112
}
2213

23-
const handleRewardGauges: QueryHandler = (req, res, ctx) => {
14+
const handleRewardGauges: QueryHandler = ({ request }) => {
2415
try {
2516
const mockResponse = incentivequery.QueryRewardGaugesResponse.fromPartial({
2617
rewardGauges: {
@@ -36,32 +27,30 @@ const handleRewardGauges: QueryHandler = (req, res, ctx) => {
3627

3728
const base64Value = Buffer.from(encoded).toString("base64");
3829

39-
return res(
40-
ctx.json({
41-
jsonrpc: "2.0",
42-
id: -1,
43-
result: {
44-
response: {
45-
code: 0,
46-
log: "",
47-
info: "",
48-
index: "0",
49-
key: null,
50-
value: base64Value,
51-
proof_ops: null,
52-
height: "0",
53-
codespace: "",
54-
},
30+
return HttpResponse.json({
31+
jsonrpc: "2.0",
32+
id: -1,
33+
result: {
34+
response: {
35+
code: 0,
36+
log: "",
37+
info: "",
38+
index: "0",
39+
key: null,
40+
value: base64Value,
41+
proof_ops: null,
42+
height: "0",
43+
codespace: "",
5544
},
56-
}),
57-
);
45+
},
46+
});
5847
} catch (error) {
5948
console.error("Failed to build mock RewardGauges response", error);
60-
return req.passthrough();
49+
return fetch(request);
6150
}
6251
};
6352

64-
const handleBankBalance: QueryHandler = (req, res, ctx) => {
53+
const handleBankBalance: QueryHandler = ({ request }) => {
6554
try {
6655
const mockResp = QueryBalanceResponse.fromPartial({
6756
balance: { denom: "ubbn", amount: MOCK_VALUES.BBN_BALANCE },
@@ -70,28 +59,26 @@ const handleBankBalance: QueryHandler = (req, res, ctx) => {
7059
const encoded = QueryBalanceResponse.encode(mockResp).finish();
7160
const base64Value = Buffer.from(encoded).toString("base64");
7261

73-
return res(
74-
ctx.json({
75-
jsonrpc: "2.0",
76-
id: -1,
77-
result: {
78-
response: {
79-
code: 0,
80-
log: "",
81-
info: "",
82-
index: "0",
83-
key: null,
84-
value: base64Value,
85-
proof_ops: null,
86-
height: "0",
87-
codespace: "",
88-
},
62+
return HttpResponse.json({
63+
jsonrpc: "2.0",
64+
id: -1,
65+
result: {
66+
response: {
67+
code: 0,
68+
log: "",
69+
info: "",
70+
index: "0",
71+
key: null,
72+
value: base64Value,
73+
proof_ops: null,
74+
height: "0",
75+
codespace: "",
8976
},
90-
}),
91-
);
77+
},
78+
});
9279
} catch (error) {
9380
console.error("Failed to build mock Bank Balance response", error);
94-
return req.passthrough();
81+
return fetch(request);
9582
}
9683
};
9784

@@ -108,8 +95,8 @@ const queryStrategies: QueryStrategy[] = [
10895
];
10996

11097
export const blockchainHandlers = [
111-
rest.get(/.*\/abci_query$/, (req, res, ctx) => {
112-
const url = new URL(req.url.href);
98+
http.get(/.*\/abci_query$/, ({ request }) => {
99+
const url = new URL(request.url);
113100
let pathParam = url.searchParams.get("path");
114101

115102
if (pathParam) {
@@ -128,11 +115,11 @@ export const blockchainHandlers = [
128115
? pathParam.includes(strategy.pattern)
129116
: strategy.pattern.test(pathParam)
130117
) {
131-
return strategy.handler(req, res, ctx);
118+
return strategy.handler({ request });
132119
}
133120
}
134121
}
135122

136-
return req.passthrough();
123+
return fetch(request);
137124
}),
138125
];
Lines changed: 15 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
import { rest } from "msw";
1+
import { http, HttpResponse } from "msw";
22

33
import { MOCK_VALUES } from "./constants";
44

55
export const btcHandlers = [
66
// Mempool API handlers
7-
rest.get("*/api/address/*/utxo", (req, res, ctx) => {
7+
http.get("*/api/address/*/utxo", () => {
88
const response = [
99
{
1010
txid: "txid1",
@@ -15,26 +15,22 @@ export const btcHandlers = [
1515
},
1616
},
1717
];
18-
return res(ctx.json(response));
18+
return HttpResponse.json(response);
1919
}),
2020

21-
rest.get("*/api/v1/validate-address/*", (req, res, ctx) => {
22-
return res(
23-
ctx.json({
24-
isvalid: true,
25-
scriptPubKey: "0014abcdef1234567890abcdef1234567890abcdef12",
26-
}),
27-
);
21+
http.get("*/api/v1/validate-address/*", () => {
22+
return HttpResponse.json({
23+
isvalid: true,
24+
scriptPubKey: "0014abcdef1234567890abcdef1234567890abcdef12",
25+
});
2826
}),
2927

30-
rest.get("*/api/address/*", (req, res, ctx) => {
31-
return res(
32-
ctx.json({
33-
chain_stats: {
34-
funded_txo_sum: Number.parseInt(MOCK_VALUES.STAKABLE_BTC),
35-
spent_txo_sum: 0,
36-
},
37-
}),
38-
);
28+
http.get("*/api/address/*", () => {
29+
return HttpResponse.json({
30+
chain_stats: {
31+
funded_txo_sum: Number.parseInt(MOCK_VALUES.STAKABLE_BTC),
32+
spent_txo_sum: 0,
33+
},
34+
});
3935
}),
4036
];
Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,23 @@
1-
import { rest } from "msw";
1+
import { http, HttpResponse } from "msw";
22

33
import { mockDelegation, mockNetworkInfo } from "./constants";
44

55
export const delegationHandlers = [
6-
rest.get("/v1/staker/delegations*", (req, res, ctx) => {
7-
return res(
8-
ctx.json({
9-
data: [],
10-
pagination: { next_key: null, total: "0" },
11-
}),
12-
);
6+
http.get("/v1/staker/delegations*", () => {
7+
return HttpResponse.json({
8+
data: [],
9+
pagination: { next_key: null, total: "0" },
10+
});
1311
}),
1412

15-
rest.get("/v2/delegations*", (req, res, ctx) => {
16-
return res(
17-
ctx.json({
18-
data: [mockDelegation],
19-
pagination: { next_key: "", total: "1" },
20-
}),
21-
);
13+
http.get("/v2/delegations*", () => {
14+
return HttpResponse.json({
15+
data: [mockDelegation],
16+
pagination: { next_key: "", total: "1" },
17+
});
2218
}),
2319

24-
rest.get("/v2/network-info*", (req, res, ctx) => {
25-
return res(ctx.json(mockNetworkInfo));
20+
http.get("/v2/network-info*", () => {
21+
return HttpResponse.json(mockNetworkInfo);
2622
}),
2723
];

services/simple-staking/e2e/mocks/handlers/stats-handlers.ts

Lines changed: 15 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { rest } from "msw";
1+
import { http, HttpResponse } from "msw";
22

33
const mockStatsV2 = {
44
active_tvl: 3238664649941,
@@ -107,29 +107,25 @@ const mockFinalityProviders = [
107107
];
108108

109109
export const statsHandlers = [
110-
rest.get("/v2/stats*", (req, res, ctx) => {
111-
return res(ctx.json({ data: mockStatsV2 }));
110+
http.get("/v2/stats*", () => {
111+
return HttpResponse.json({ data: mockStatsV2 });
112112
}),
113113

114-
rest.get("/v2/prices*", (req, res, ctx) => {
115-
return res(ctx.json({ data: mockPrices }));
114+
http.get("/v2/prices*", () => {
115+
return HttpResponse.json({ data: mockPrices });
116116
}),
117117

118-
rest.get("/v1/finality-providers*", (req, res, ctx) => {
119-
return res(
120-
ctx.json({
121-
data: mockFinalityProviders,
122-
pagination: { next_key: "", total: `${mockFinalityProviders.length}` },
123-
}),
124-
);
118+
http.get("/v1/finality-providers*", () => {
119+
return HttpResponse.json({
120+
data: mockFinalityProviders,
121+
pagination: { next_key: "", total: `${mockFinalityProviders.length}` },
122+
});
125123
}),
126124

127-
rest.get("/v2/finality-providers*", (req, res, ctx) => {
128-
return res(
129-
ctx.json({
130-
data: mockFinalityProviders,
131-
pagination: { next_key: "", total: `${mockFinalityProviders.length}` },
132-
}),
133-
);
125+
http.get("/v2/finality-providers*", () => {
126+
return HttpResponse.json({
127+
data: mockFinalityProviders,
128+
pagination: { next_key: "", total: `${mockFinalityProviders.length}` },
129+
});
134130
}),
135131
];
Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,13 @@
1-
import { rest } from "msw";
1+
import { http, HttpResponse } from "msw";
22

33
export const utilityHandlers = [
4-
rest.get("/address/screening*", (req, res, ctx) => {
5-
return res(
6-
ctx.json({
7-
data: {
8-
btc_address: {
9-
risk: "low",
10-
},
4+
http.get("/address/screening*", () => {
5+
return HttpResponse.json({
6+
data: {
7+
btc_address: {
8+
risk: "low",
119
},
12-
}),
13-
);
10+
},
11+
});
1412
}),
1513
];

0 commit comments

Comments
 (0)