odin-connect 1.0.2 → 1.0.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.github/workflows/pr.yml +24 -0
- package/demo/package-lock.json +6 -2
- package/dist/index.d.ts +2 -2
- package/dist/index.d.ts.map +1 -1
- package/dist/models/achievement.d.ts +20 -0
- package/dist/models/achievement.d.ts.map +1 -0
- package/dist/models/achievement.js +1 -0
- package/dist/models/token.d.ts +5 -0
- package/dist/models/token.d.ts.map +1 -1
- package/dist/services/api.d.ts +26 -0
- package/dist/services/api.d.ts.map +1 -1
- package/dist/services/api.js +93 -0
- package/dist/services/api.test.d.ts +2 -0
- package/dist/services/api.test.d.ts.map +1 -0
- package/dist/services/api.test.js +70 -0
- package/dist/services/connect.d.ts +15 -1
- package/dist/services/connect.d.ts.map +1 -1
- package/dist/services/connect.js +71 -18
- package/dist/services/connect.test.d.ts +2 -0
- package/dist/services/connect.test.d.ts.map +1 -0
- package/dist/services/connect.test.js +306 -0
- package/dist/services/http.d.ts +6 -2
- package/dist/services/http.d.ts.map +1 -1
- package/dist/services/http.js +30 -0
- package/dist/services/http.test.d.ts +2 -0
- package/dist/services/http.test.d.ts.map +1 -0
- package/dist/services/http.test.js +37 -0
- package/dist/utils/conver-to-odin-amount.test.d.ts +2 -0
- package/dist/utils/conver-to-odin-amount.test.d.ts.map +1 -0
- package/dist/utils/conver-to-odin-amount.test.js +36 -0
- package/dist/utils/index.d.ts +4 -1
- package/dist/utils/index.d.ts.map +1 -1
- package/dist/utils/index.js +108 -0
- package/dist/utils/token-field-validators.test.d.ts +2 -0
- package/dist/utils/token-field-validators.test.d.ts.map +1 -0
- package/dist/utils/token-field-validators.test.js +31 -0
- package/package.json +7 -2
- package/readme.md +26 -3
- package/vitest.config.ts +7 -0
|
@@ -0,0 +1,306 @@
|
|
|
1
|
+
import { describe, it, expect, beforeAll, vi } from "vitest";
|
|
2
|
+
import { Connect } from "./connect";
|
|
3
|
+
const mockTokens = [
|
|
4
|
+
{
|
|
5
|
+
id: "2jjj",
|
|
6
|
+
ticker: "TEST",
|
|
7
|
+
},
|
|
8
|
+
];
|
|
9
|
+
describe("Connect", () => {
|
|
10
|
+
let connect;
|
|
11
|
+
beforeAll(() => {
|
|
12
|
+
connect = new Connect({ name: "TestApp", env: "local" });
|
|
13
|
+
connect["_api"].getUser = vi.fn().mockResolvedValue({
|
|
14
|
+
principal: "user-principal",
|
|
15
|
+
username: "username",
|
|
16
|
+
});
|
|
17
|
+
});
|
|
18
|
+
it("should initialize Connect instance", () => {
|
|
19
|
+
expect(connect).toBeDefined();
|
|
20
|
+
expect(connect).toBeInstanceOf(Connect);
|
|
21
|
+
expect(connect["_appInfo"]?.name).toBe("TestApp");
|
|
22
|
+
expect(connect["origin"])?.toBe("http://localhost:5173");
|
|
23
|
+
});
|
|
24
|
+
it("should say hello", () => {
|
|
25
|
+
const consoleLogSpy = vi.spyOn(console, "log").mockImplementation(() => { });
|
|
26
|
+
connect.hello();
|
|
27
|
+
expect(consoleLogSpy).toHaveBeenCalledWith("Hello from Odin Connect!");
|
|
28
|
+
consoleLogSpy.mockRestore();
|
|
29
|
+
});
|
|
30
|
+
it("should create authorization URL", () => {
|
|
31
|
+
const url = connect["createUrl"]("authorize/test");
|
|
32
|
+
expect(url).toBeInstanceOf(URL);
|
|
33
|
+
expect(url.pathname).toBe("/authorize/test");
|
|
34
|
+
expect(url.searchParams.get("app_name")).toBe("TestApp");
|
|
35
|
+
expect(url.searchParams.get("referrer")).toBeDefined();
|
|
36
|
+
});
|
|
37
|
+
it("should open the connect window", () => {
|
|
38
|
+
const openSpy = vi.spyOn(window, "open").mockImplementation(() => null);
|
|
39
|
+
const getUserSpy = vi.spyOn(connect["_api"], "getUser");
|
|
40
|
+
const connectPromise = connect.connect({ requires_api: false });
|
|
41
|
+
connectPromise.then((res) => {
|
|
42
|
+
expect(res).toBeDefined();
|
|
43
|
+
expect(res.username).toBe("username");
|
|
44
|
+
expect(res.principal).toBe("user-principal");
|
|
45
|
+
expect(connect.apiClient.apiKey).toBeNull();
|
|
46
|
+
}, (e) => {
|
|
47
|
+
expect(true).toBe(false);
|
|
48
|
+
});
|
|
49
|
+
const url = openSpy.mock.calls[0][0];
|
|
50
|
+
expect(url).toBeInstanceOf(URL);
|
|
51
|
+
expect(url?.toString()).toContain("http://localhost:5173/authorize/connect?");
|
|
52
|
+
const params = new URLSearchParams(url?.search);
|
|
53
|
+
expect(params.get("app_name")).toBe("TestApp");
|
|
54
|
+
expect(params.get("referrer")).toBeDefined();
|
|
55
|
+
expect(params.get("requires_api")).toBe("0");
|
|
56
|
+
simulateMessage("authorize/connect", "user-principal");
|
|
57
|
+
expect(getUserSpy).toHaveBeenCalledWith("user-principal");
|
|
58
|
+
});
|
|
59
|
+
it("should open connect window with API requirement", () => {
|
|
60
|
+
const openSpy = vi.spyOn(window, "open").mockImplementation(() => null);
|
|
61
|
+
const connectPromise = connect.connect({ requires_api: true });
|
|
62
|
+
connectPromise.then((res) => {
|
|
63
|
+
expect(res).toBeDefined();
|
|
64
|
+
expect(res.username).toBe("username");
|
|
65
|
+
expect(res.principal).toBe("user-principal");
|
|
66
|
+
expect(connect.apiClient.apiKey).toBe("test-jwt");
|
|
67
|
+
}, () => {
|
|
68
|
+
expect(true).toBe(false);
|
|
69
|
+
});
|
|
70
|
+
const url = openSpy.mock.calls[0][0];
|
|
71
|
+
expect(url).toBeInstanceOf(URL);
|
|
72
|
+
expect(url?.toString()).toContain("http://localhost:5173/authorize/connect?");
|
|
73
|
+
const params = new URLSearchParams(url?.search);
|
|
74
|
+
expect(params.get("app_name")).toBe("TestApp");
|
|
75
|
+
expect(params.get("referrer")).toBeDefined();
|
|
76
|
+
expect(params.get("requires_api")).toBe("1");
|
|
77
|
+
simulateMessage("authorize/connect", "user-principal::test-jwt");
|
|
78
|
+
});
|
|
79
|
+
it("should handle rejected connection", () => {
|
|
80
|
+
const connectPromise = connect.connect({ requires_api: false });
|
|
81
|
+
connectPromise.then(() => expect(false).toBe(true), (e) => {
|
|
82
|
+
expect(e).toBeInstanceOf(Error);
|
|
83
|
+
expect(e.message).toContain("rejected");
|
|
84
|
+
});
|
|
85
|
+
simulateRejectMessage("authorize/connect");
|
|
86
|
+
});
|
|
87
|
+
it("should open the buy authorization window", () => {
|
|
88
|
+
const openSpy = vi.spyOn(window, "open").mockImplementation(() => null);
|
|
89
|
+
const result = connect.buy({
|
|
90
|
+
token: "2jjj",
|
|
91
|
+
btcAmount: 20001n,
|
|
92
|
+
principal: "test-principal",
|
|
93
|
+
});
|
|
94
|
+
result.catch((e) => {
|
|
95
|
+
console.log(e);
|
|
96
|
+
expect(e).toBeInstanceOf(Error);
|
|
97
|
+
});
|
|
98
|
+
expect(openSpy).toHaveBeenCalled();
|
|
99
|
+
const url = openSpy.mock.calls[0][0];
|
|
100
|
+
expect(url).toBeInstanceOf(URL);
|
|
101
|
+
expect(url?.toString()).toContain("http://localhost:5173/authorize/buy?");
|
|
102
|
+
const params = new URLSearchParams(url?.search);
|
|
103
|
+
expect(params.get("app_name")).toBe("TestApp");
|
|
104
|
+
expect(params.get("token")).toBe("2jjj");
|
|
105
|
+
expect(params.get("amount")).toBe("20001");
|
|
106
|
+
expect(params.get("principal")).toBe("test-principal");
|
|
107
|
+
});
|
|
108
|
+
it("should open the sell authorization window", () => {
|
|
109
|
+
const openSpy = vi.spyOn(window, "open").mockImplementation(() => null);
|
|
110
|
+
const sellPromise = connect.sell({
|
|
111
|
+
token: "2jjj",
|
|
112
|
+
tokenAmount: 32000n,
|
|
113
|
+
principal: "test-principal",
|
|
114
|
+
});
|
|
115
|
+
sellPromise.then((result) => {
|
|
116
|
+
expect(result).toBe(true);
|
|
117
|
+
});
|
|
118
|
+
expect(openSpy).toHaveBeenCalled();
|
|
119
|
+
const url = openSpy.mock.calls[0][0];
|
|
120
|
+
expect(url).toBeInstanceOf(URL);
|
|
121
|
+
expect(url?.toString()).toContain("http://localhost:5173/authorize/sell?");
|
|
122
|
+
const params = new URLSearchParams(url?.search);
|
|
123
|
+
expect(params.get("app_name")).toBe("TestApp");
|
|
124
|
+
expect(params.get("token")).toBe("2jjj");
|
|
125
|
+
expect(params.get("amount")).toBe("32000");
|
|
126
|
+
expect(params.get("principal")).toBe("test-principal");
|
|
127
|
+
simulateMessage("authorize/sell", "sold");
|
|
128
|
+
});
|
|
129
|
+
it("should handle rejected sell", () => {
|
|
130
|
+
const openSpy = vi.spyOn(window, "open").mockImplementation(() => null);
|
|
131
|
+
const sellPromise = connect.sell({
|
|
132
|
+
token: "2jjj",
|
|
133
|
+
tokenAmount: 32000n,
|
|
134
|
+
principal: "test-principal",
|
|
135
|
+
});
|
|
136
|
+
sellPromise.then(() => {
|
|
137
|
+
expect(true).toBe(false); // should not be called
|
|
138
|
+
}, (e) => {
|
|
139
|
+
expect(e).toBeInstanceOf(Error);
|
|
140
|
+
expect(e.message).toContain("failed");
|
|
141
|
+
});
|
|
142
|
+
expect(openSpy).toHaveBeenCalled();
|
|
143
|
+
simulateRejectMessage("authorize/sell");
|
|
144
|
+
});
|
|
145
|
+
it("should open the transfer authorization window", () => {
|
|
146
|
+
const openSpy = vi.spyOn(window, "open").mockImplementation(() => null);
|
|
147
|
+
const result = connect.transfer({
|
|
148
|
+
token: "2jjj",
|
|
149
|
+
amount: 45000n,
|
|
150
|
+
destination: "to-principal",
|
|
151
|
+
principal: "from-principal",
|
|
152
|
+
});
|
|
153
|
+
result.catch((e) => {
|
|
154
|
+
expect(e).toBeInstanceOf(Error);
|
|
155
|
+
});
|
|
156
|
+
expect(openSpy).toHaveBeenCalled();
|
|
157
|
+
const url = openSpy.mock.calls[0][0];
|
|
158
|
+
expect(url).toBeInstanceOf(URL);
|
|
159
|
+
expect(url?.toString()).toContain("http://localhost:5173/authorize/transfer?");
|
|
160
|
+
const params = new URLSearchParams(url?.search);
|
|
161
|
+
expect(params.get("app_name")).toBe("TestApp");
|
|
162
|
+
expect(params.get("token")).toBe("2jjj");
|
|
163
|
+
expect(params.get("amount")).toBe("45000");
|
|
164
|
+
expect(params.get("destination")).toBe("to-principal");
|
|
165
|
+
expect(params.get("principal")).toBe("from-principal");
|
|
166
|
+
});
|
|
167
|
+
it("should handle rejected transfer", () => {
|
|
168
|
+
const openSpy = vi.spyOn(window, "open").mockImplementation(() => null);
|
|
169
|
+
const transferPromise = connect.transfer({
|
|
170
|
+
token: "2jjj",
|
|
171
|
+
amount: 45000n,
|
|
172
|
+
destination: "to-principal",
|
|
173
|
+
principal: "from-principal",
|
|
174
|
+
});
|
|
175
|
+
transferPromise.then(() => expect(true).toBe(false), // should not be called
|
|
176
|
+
(e) => {
|
|
177
|
+
expect(e).toBeInstanceOf(Error);
|
|
178
|
+
expect(e.message).toContain("failed");
|
|
179
|
+
});
|
|
180
|
+
expect(openSpy).toHaveBeenCalled();
|
|
181
|
+
simulateRejectMessage("authorize/transfer");
|
|
182
|
+
});
|
|
183
|
+
it("should open the add liquidity authorization window", () => {
|
|
184
|
+
const openSpy = vi.spyOn(window, "open").mockImplementation(() => null);
|
|
185
|
+
const addLiquidityPromise = connect.addLiquidity({
|
|
186
|
+
token: "2jjj",
|
|
187
|
+
btcAmount: 10000n,
|
|
188
|
+
principal: "test-principal",
|
|
189
|
+
});
|
|
190
|
+
addLiquidityPromise.then((result) => expect(result).toBe(true), () => expect(true).toBe(false) // should not be called
|
|
191
|
+
);
|
|
192
|
+
expect(openSpy).toHaveBeenCalled();
|
|
193
|
+
const url = openSpy.mock.calls[0][0];
|
|
194
|
+
expect(url).toBeInstanceOf(URL);
|
|
195
|
+
expect(url?.toString()).toContain("http://localhost:5173/authorize/add_liquidity?");
|
|
196
|
+
const params = new URLSearchParams(url?.search);
|
|
197
|
+
expect(params.get("app_name")).toBe("TestApp");
|
|
198
|
+
expect(params.get("token")).toBe("2jjj");
|
|
199
|
+
expect(params.get("amount")).toBe("10000");
|
|
200
|
+
expect(params.get("principal")).toBe("test-principal");
|
|
201
|
+
simulateMessage("authorize/add_liquidity", "addedLiquidity");
|
|
202
|
+
});
|
|
203
|
+
it("should handle rejected add liquidity", () => {
|
|
204
|
+
const openSpy = vi.spyOn(window, "open").mockImplementation(() => null);
|
|
205
|
+
const addLiquidityPromise = connect.addLiquidity({
|
|
206
|
+
token: "2jjj",
|
|
207
|
+
btcAmount: 10000n,
|
|
208
|
+
principal: "test-principal",
|
|
209
|
+
});
|
|
210
|
+
addLiquidityPromise.then(() => expect(true).toBe(false), // should not be called
|
|
211
|
+
(e) => {
|
|
212
|
+
expect(e).toBeInstanceOf(Error);
|
|
213
|
+
expect(e.message).toContain("failed");
|
|
214
|
+
});
|
|
215
|
+
expect(openSpy).toHaveBeenCalled();
|
|
216
|
+
simulateRejectMessage("authorize/add_liquidity");
|
|
217
|
+
});
|
|
218
|
+
it("should open the remove liquidity authorization window", () => {
|
|
219
|
+
const openSpy = vi.spyOn(window, "open").mockImplementation(() => null);
|
|
220
|
+
const removeLiquidityPromise = connect.removeLiquidity({
|
|
221
|
+
token: "2jjj",
|
|
222
|
+
lpAmount: 15000n,
|
|
223
|
+
principal: "test-principal",
|
|
224
|
+
});
|
|
225
|
+
removeLiquidityPromise.then((result) => expect(result).toBe(true), () => expect(true).toBe(false) // should not be called
|
|
226
|
+
);
|
|
227
|
+
expect(openSpy).toHaveBeenCalled();
|
|
228
|
+
const url = openSpy.mock.calls[0][0];
|
|
229
|
+
expect(url).toBeInstanceOf(URL);
|
|
230
|
+
expect(url?.toString()).toContain("http://localhost:5173/authorize/remove_liquidity?");
|
|
231
|
+
const params = new URLSearchParams(url?.search);
|
|
232
|
+
expect(params.get("app_name")).toBe("TestApp");
|
|
233
|
+
expect(params.get("token")).toBe("2jjj");
|
|
234
|
+
expect(params.get("amount")).toBe("15000");
|
|
235
|
+
expect(params.get("principal")).toBe("test-principal");
|
|
236
|
+
simulateMessage("authorize/remove_liquidity", "removedLiquidity");
|
|
237
|
+
});
|
|
238
|
+
it("should handle rejected remove liquidity", () => {
|
|
239
|
+
const openSpy = vi.spyOn(window, "open").mockImplementation(() => null);
|
|
240
|
+
const removeLiquidityPromise = connect.removeLiquidity({
|
|
241
|
+
token: "2jjj",
|
|
242
|
+
lpAmount: 15000n,
|
|
243
|
+
principal: "test-principal",
|
|
244
|
+
});
|
|
245
|
+
removeLiquidityPromise.then(() => expect(true).toBe(false), // should not be called
|
|
246
|
+
(e) => {
|
|
247
|
+
expect(e).toBeInstanceOf(Error);
|
|
248
|
+
expect(e.message).toContain("failed");
|
|
249
|
+
});
|
|
250
|
+
expect(openSpy).toHaveBeenCalled();
|
|
251
|
+
simulateRejectMessage("authorize/remove_liquidity");
|
|
252
|
+
});
|
|
253
|
+
it("should create a base action", async () => {
|
|
254
|
+
const openSpy = vi.spyOn(window, "open").mockImplementation(() => null);
|
|
255
|
+
const action = () => connect["baseAction"]({
|
|
256
|
+
params: { test: "param" },
|
|
257
|
+
odinPath: "authorize/test_action",
|
|
258
|
+
receivedMessageFromOrigin(message) {
|
|
259
|
+
return message === "OK";
|
|
260
|
+
},
|
|
261
|
+
resolve: {
|
|
262
|
+
success: (message) => {
|
|
263
|
+
return message + "!";
|
|
264
|
+
},
|
|
265
|
+
failure: "test failed",
|
|
266
|
+
close: "rejected",
|
|
267
|
+
},
|
|
268
|
+
});
|
|
269
|
+
action().then((res) => {
|
|
270
|
+
expect(res).toBe("OK!");
|
|
271
|
+
});
|
|
272
|
+
expect(openSpy).toHaveBeenCalled();
|
|
273
|
+
const url = openSpy.mock.calls[0][0];
|
|
274
|
+
expect(url).toBeInstanceOf(URL);
|
|
275
|
+
expect(url?.toString()).toContain("http://localhost:5173/authorize/test_action?");
|
|
276
|
+
const params = new URLSearchParams(url?.search);
|
|
277
|
+
expect(params.get("app_name")).toBe("TestApp");
|
|
278
|
+
expect(params.get("test")).toBe("param");
|
|
279
|
+
simulateMessage("authorize/test_action", "OK");
|
|
280
|
+
action().then(() => expect(true).toBe(false), (e) => {
|
|
281
|
+
expect(e).toBeInstanceOf(Error);
|
|
282
|
+
expect(e.message).toBe("test failed");
|
|
283
|
+
});
|
|
284
|
+
simulateRejectMessage("authorize/test_action");
|
|
285
|
+
});
|
|
286
|
+
});
|
|
287
|
+
function simulateRejectMessage(path) {
|
|
288
|
+
const messageEvent = new MessageEvent("message", {
|
|
289
|
+
data: {
|
|
290
|
+
path: "/" + path,
|
|
291
|
+
message: "rejected",
|
|
292
|
+
},
|
|
293
|
+
origin: "http://localhost:5173",
|
|
294
|
+
});
|
|
295
|
+
window.dispatchEvent(messageEvent);
|
|
296
|
+
}
|
|
297
|
+
function simulateMessage(path, data) {
|
|
298
|
+
const messageEvent = new MessageEvent("message", {
|
|
299
|
+
data: {
|
|
300
|
+
path: "/" + path,
|
|
301
|
+
message: data,
|
|
302
|
+
},
|
|
303
|
+
origin: "http://localhost:5173",
|
|
304
|
+
});
|
|
305
|
+
window.dispatchEvent(messageEvent);
|
|
306
|
+
}
|
package/dist/services/http.d.ts
CHANGED
|
@@ -1,6 +1,10 @@
|
|
|
1
1
|
import { AxiosRequestConfig } from "axios";
|
|
2
|
+
export declare const bigIntTransformer: (data: string) => any;
|
|
2
3
|
export declare class HttpClient {
|
|
3
|
-
get<
|
|
4
|
-
fetch<
|
|
4
|
+
get<ResponseType>(url: string, options?: AxiosRequestConfig): Promise<ResponseType>;
|
|
5
|
+
fetch<ResponseType>(url: string, options?: AxiosRequestConfig): Promise<ResponseType>;
|
|
6
|
+
post<ResponseType, DataType = string>(url: string, data?: DataType, options?: AxiosRequestConfig): Promise<ResponseType>;
|
|
7
|
+
put<DataType, ResponseType>(url: string, data?: DataType, options?: AxiosRequestConfig): Promise<ResponseType>;
|
|
8
|
+
delete<ResponseType>(url: string, options?: AxiosRequestConfig): Promise<ResponseType>;
|
|
5
9
|
}
|
|
6
10
|
//# sourceMappingURL=http.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"http.d.ts","sourceRoot":"","sources":["../../src/services/http.ts"],"names":[],"mappings":"AAAA,OAAc,EAAE,kBAAkB,
|
|
1
|
+
{"version":3,"file":"http.d.ts","sourceRoot":"","sources":["../../src/services/http.ts"],"names":[],"mappings":"AAAA,OAAc,EAAE,kBAAkB,EAA4B,MAAM,OAAO,CAAC;AAG5E,eAAO,MAAM,iBAAiB,GACzB,MAAK,MAAM,QAOX,CAAA;AACL,qBAAa,UAAU;IAEf,GAAG,CAAC,YAAY,EAAE,GAAG,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,kBAAkB;IAI3D,KAAK,CAAC,YAAY,EAAE,GAAG,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,kBAAkB;IAW7D,IAAI,CAAC,YAAY,EAAE,QAAQ,GAAG,MAAM,EACxC,GAAG,EAAE,MAAM,EACX,IAAI,CAAC,EAAE,QAAQ,EACf,OAAO,CAAC,EAAE,kBAAkB;IASxB,GAAG,CAAC,QAAQ,EAAE,YAAY,EAC9B,GAAG,EAAE,MAAM,EACX,IAAI,CAAC,EAAE,QAAQ,EACf,OAAO,CAAC,EAAE,kBAAkB;IASxB,MAAM,CAAC,YAAY,EAAE,GAAG,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,kBAAkB;CAGrE"}
|
package/dist/services/http.js
CHANGED
|
@@ -1,13 +1,43 @@
|
|
|
1
1
|
import axios from "axios";
|
|
2
|
+
import JSONBigInt from "@apimatic/json-bigint";
|
|
3
|
+
export const bigIntTransformer = (data) => {
|
|
4
|
+
try {
|
|
5
|
+
return JSONBigInt.parse(data);
|
|
6
|
+
}
|
|
7
|
+
catch (e) {
|
|
8
|
+
console.error("Error parsing response:", e);
|
|
9
|
+
return data;
|
|
10
|
+
}
|
|
11
|
+
};
|
|
2
12
|
export class HttpClient {
|
|
3
13
|
async get(url, options) {
|
|
4
14
|
return this.fetch(url, { method: "GET", ...options });
|
|
5
15
|
}
|
|
6
16
|
async fetch(url, options) {
|
|
17
|
+
// for any custom headers or other axios options in the future
|
|
18
|
+
// should we add Auth bearer token here if available?
|
|
7
19
|
const overrides = {
|
|
20
|
+
transformResponse: [bigIntTransformer],
|
|
8
21
|
...options,
|
|
9
22
|
};
|
|
10
23
|
const response = await axios(url, overrides);
|
|
11
24
|
return response.data;
|
|
12
25
|
}
|
|
26
|
+
async post(url, data, options) {
|
|
27
|
+
return this.fetch(url, {
|
|
28
|
+
method: "POST",
|
|
29
|
+
data,
|
|
30
|
+
...options,
|
|
31
|
+
});
|
|
32
|
+
}
|
|
33
|
+
async put(url, data, options) {
|
|
34
|
+
return this.fetch(url, {
|
|
35
|
+
method: "PUT",
|
|
36
|
+
data,
|
|
37
|
+
...options,
|
|
38
|
+
});
|
|
39
|
+
}
|
|
40
|
+
async delete(url, options) {
|
|
41
|
+
return this.fetch(url, { method: "DELETE", ...options });
|
|
42
|
+
}
|
|
13
43
|
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"http.test.d.ts","sourceRoot":"","sources":["../../src/services/http.test.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import { it, beforeAll, describe, expect, vi } from "vitest";
|
|
2
|
+
import { bigIntTransformer, HttpClient } from "./http";
|
|
3
|
+
import axios from "axios";
|
|
4
|
+
import { afterEach } from "node:test";
|
|
5
|
+
describe("HttpClient", () => {
|
|
6
|
+
let http;
|
|
7
|
+
let mockedAxios = axios;
|
|
8
|
+
vi.mock("axios");
|
|
9
|
+
beforeAll(() => {
|
|
10
|
+
http = new HttpClient();
|
|
11
|
+
});
|
|
12
|
+
afterEach(() => {
|
|
13
|
+
mockedAxios.mockClear();
|
|
14
|
+
});
|
|
15
|
+
it("should create http instance", () => {
|
|
16
|
+
expect(http).toBeDefined();
|
|
17
|
+
expect(http).toBeInstanceOf(HttpClient);
|
|
18
|
+
});
|
|
19
|
+
it("should send GET request", async () => {
|
|
20
|
+
const url = "http://localhost:3000";
|
|
21
|
+
mockedAxios.mockResolvedValue({ data: true });
|
|
22
|
+
const data = await http.get(url);
|
|
23
|
+
expect(mockedAxios).toHaveBeenCalledWith(url, expect.objectContaining({ method: "GET" }));
|
|
24
|
+
expect(data).toBe(true);
|
|
25
|
+
});
|
|
26
|
+
it("should send POST request", async () => {
|
|
27
|
+
const url = "http://localhost:3000";
|
|
28
|
+
mockedAxios.mockResolvedValue({ data: "success" });
|
|
29
|
+
const data = await http.post(url);
|
|
30
|
+
expect(mockedAxios).toHaveBeenCalledWith(url, expect.objectContaining({ method: "POST" }));
|
|
31
|
+
expect(data).toBe("success");
|
|
32
|
+
});
|
|
33
|
+
it("should transform", () => {
|
|
34
|
+
expect(bigIntTransformer(`{"bigInt":200000000000000023}`)["bigInt"]).toBeTypeOf("bigint");
|
|
35
|
+
expect(bigIntTransformer(`{"num":200}`)["num"]).toBeTypeOf("number");
|
|
36
|
+
});
|
|
37
|
+
});
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"conver-to-odin-amount.test.d.ts","sourceRoot":"","sources":["../../src/utils/conver-to-odin-amount.test.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import { describe, expect, it } from "vitest";
|
|
2
|
+
import { convertToOdinAmount } from ".";
|
|
3
|
+
describe("convertToOdinAmount", () => {
|
|
4
|
+
it("should convert to Odin amount correctly", () => {
|
|
5
|
+
// divisibility of 8 i.e. ODIN tokens, BTC
|
|
6
|
+
expect(convertToOdinAmount("0.1", { decimals: 3, divisibility: 8 })).toBe(10000000000n);
|
|
7
|
+
expect(convertToOdinAmount("0.00000001", { decimals: 3, divisibility: 8 })).toBe(1000n);
|
|
8
|
+
expect(convertToOdinAmount("0.00000000000000001", { decimals: 3, divisibility: 8 })).toBe(0n);
|
|
9
|
+
// divisibility of 16 i.e. PUPS
|
|
10
|
+
expect(convertToOdinAmount("0.0000000000000005", { decimals: 3, divisibility: 16 })).toBe(5000n);
|
|
11
|
+
expect(convertToOdinAmount("2.00000000002", { decimals: 3, divisibility: 16 })).toBe(20000000000200000000n);
|
|
12
|
+
// divisibility of 0 i.e. whole tokens only
|
|
13
|
+
expect(convertToOdinAmount("120", { decimals: 0, divisibility: 0 })).toBe(120n);
|
|
14
|
+
});
|
|
15
|
+
it("should handle invalid inputs gracefully", () => {
|
|
16
|
+
expect(convertToOdinAmount("abc")).toBe(0n);
|
|
17
|
+
expect(convertToOdinAmount("")).toBe(0n);
|
|
18
|
+
expect(convertToOdinAmount(" ")).toBe(0n);
|
|
19
|
+
expect(convertToOdinAmount(NaN)).toBe(0n);
|
|
20
|
+
});
|
|
21
|
+
});
|
|
22
|
+
describe("convertToOdinAmount edge cases", () => {
|
|
23
|
+
it("should handle very large numbers", () => {
|
|
24
|
+
expect(convertToOdinAmount("12345678901234567890.123456789", { decimals: 3, divisibility: 8 })).toBe(1234567890123456789012345678900n);
|
|
25
|
+
});
|
|
26
|
+
it("should handle numbers with excessive decimal places", () => {
|
|
27
|
+
expect(convertToOdinAmount("1.1234567890123456789012345", {
|
|
28
|
+
decimals: 3,
|
|
29
|
+
divisibility: 8,
|
|
30
|
+
})).toBe(112345678901n); // truncated to 11 decimal places
|
|
31
|
+
});
|
|
32
|
+
it("should handle negative numbers", () => {
|
|
33
|
+
expect(convertToOdinAmount("-0.1", { decimals: 3, divisibility: 8 })).toBe(-10000000000n);
|
|
34
|
+
expect(convertToOdinAmount("-123.456", { decimals: 3, divisibility: 8 })).toBe(-12345600000000n);
|
|
35
|
+
});
|
|
36
|
+
});
|
package/dist/utils/index.d.ts
CHANGED
|
@@ -1,3 +1,6 @@
|
|
|
1
|
-
import { BaseToken } from "../models/token";
|
|
1
|
+
import { BaseToken, Token } from "../models/token";
|
|
2
2
|
export declare function convertToOdinAmount(numberStr: string | number, token?: Pick<BaseToken, "decimals" | "divisibility">): bigint;
|
|
3
|
+
type TokenValue = string | File | null | bigint;
|
|
4
|
+
export declare const createTokenValidators: Partial<Record<keyof Token, (value: TokenValue) => string | undefined>>;
|
|
5
|
+
export {};
|
|
3
6
|
//# sourceMappingURL=index.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/utils/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,iBAAiB,CAAC;
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/utils/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,KAAK,EAAE,MAAM,iBAAiB,CAAC;AAEnD,wBAAgB,mBAAmB,CACjC,SAAS,EAAE,MAAM,GAAG,MAAM,EAC1B,KAAK,GAAE,IAAI,CAAC,SAAS,EAAE,UAAU,GAAG,cAAc,CAGjD,UA6BF;AAED,KAAK,UAAU,GAAG,MAAM,GAAG,IAAI,GAAG,IAAI,GAAG,MAAM,CAAC;AAEhD,eAAO,MAAM,qBAAqB,EAAE,OAAO,CACzC,MAAM,CAAC,MAAM,KAAK,EAAE,CAAC,KAAK,EAAE,UAAU,KAAK,MAAM,GAAG,SAAS,CAAC,CAS/D,CAAC"}
|
package/dist/utils/index.js
CHANGED
|
@@ -24,3 +24,111 @@ export function convertToOdinAmount(numberStr, token = {
|
|
|
24
24
|
const result = BigInt(integerPart + decimalPart);
|
|
25
25
|
return result;
|
|
26
26
|
}
|
|
27
|
+
export const createTokenValidators = {
|
|
28
|
+
name: validateName,
|
|
29
|
+
image: validateImage,
|
|
30
|
+
ticker: validateTicker,
|
|
31
|
+
description: validateDescription,
|
|
32
|
+
twitter: validateTwitter,
|
|
33
|
+
website: validateWebsite,
|
|
34
|
+
telegram: validateTelegram,
|
|
35
|
+
};
|
|
36
|
+
function validateName(name) {
|
|
37
|
+
if (typeof name !== "string") {
|
|
38
|
+
return "Name must be a string.";
|
|
39
|
+
}
|
|
40
|
+
if (!name || name.trim() === "") {
|
|
41
|
+
return "Name is required.";
|
|
42
|
+
}
|
|
43
|
+
if (name.length < 3 || name.length > 30) {
|
|
44
|
+
return "Name must be between 3 and 30 characters.";
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
function validateDescription(description) {
|
|
48
|
+
if (!description) {
|
|
49
|
+
return;
|
|
50
|
+
}
|
|
51
|
+
if (typeof description !== "string") {
|
|
52
|
+
return "Description must be a string.";
|
|
53
|
+
}
|
|
54
|
+
if (description && description.length > 100) {
|
|
55
|
+
return "Description must not exceed 100 characters.";
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
function validateTicker(ticker) {
|
|
59
|
+
if (typeof ticker !== "string") {
|
|
60
|
+
return "Ticker must be a string.";
|
|
61
|
+
}
|
|
62
|
+
if (!ticker || ticker.trim() === "") {
|
|
63
|
+
return "Ticker is required.";
|
|
64
|
+
}
|
|
65
|
+
if (ticker.length < 3 || ticker.length > 10) {
|
|
66
|
+
return "Ticker must be between 3 and 10 characters.";
|
|
67
|
+
}
|
|
68
|
+
if (!/^[A-Z0-9]+$/.test(ticker)) {
|
|
69
|
+
return "Ticker must be alphanumeric characters in uppercase only.";
|
|
70
|
+
}
|
|
71
|
+
if (!/(?=(.*[A-Z].*[A-Z]))/.test(ticker)) {
|
|
72
|
+
return "Ticker must have at least 2 alpha characters.";
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
function validateImage(file) {
|
|
76
|
+
if (!(file instanceof File)) {
|
|
77
|
+
return "Image must be a file.";
|
|
78
|
+
}
|
|
79
|
+
const allowedFileTypes = [
|
|
80
|
+
"image/png",
|
|
81
|
+
"image/jpeg",
|
|
82
|
+
"image/webp",
|
|
83
|
+
"image/svg",
|
|
84
|
+
"image/gif",
|
|
85
|
+
"image/avif",
|
|
86
|
+
];
|
|
87
|
+
const maxFileSize = 200 * 1024; // 200KB
|
|
88
|
+
if (!file) {
|
|
89
|
+
return "Image file is required.";
|
|
90
|
+
}
|
|
91
|
+
if (!allowedFileTypes.includes(file.type)) {
|
|
92
|
+
return `Invalid file type. Allowed types are: ${allowedFileTypes.join(", ")}`;
|
|
93
|
+
}
|
|
94
|
+
if (file.size > maxFileSize) {
|
|
95
|
+
return `File size exceeds the maximum limit of ${maxFileSize / 1024}KB.`;
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
function validateTwitter(value) {
|
|
99
|
+
// it's optional
|
|
100
|
+
if (!value) {
|
|
101
|
+
return;
|
|
102
|
+
}
|
|
103
|
+
if (typeof value !== "string") {
|
|
104
|
+
return "Twitter handle must be a string.";
|
|
105
|
+
}
|
|
106
|
+
if (value &&
|
|
107
|
+
!/^https?:\/\/(www\.)?(twitter\.com|x\.com)\/[A-Za-z0-9_]{1,15}\/?$/.test(value)) {
|
|
108
|
+
return "Twitter must be a valid twitter URL.";
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
function validateWebsite(value) {
|
|
112
|
+
if (!value) {
|
|
113
|
+
return;
|
|
114
|
+
}
|
|
115
|
+
if (typeof value !== "string") {
|
|
116
|
+
return "Website must be a string.";
|
|
117
|
+
}
|
|
118
|
+
if (value &&
|
|
119
|
+
!/^(https?:\/\/)?([\w-]+(\.[\w-]+)+)(\/[\w-./?%&=]*)?$/.test(value)) {
|
|
120
|
+
return "Website must be a valid URL.";
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
function validateTelegram(value) {
|
|
124
|
+
if (!value) {
|
|
125
|
+
return;
|
|
126
|
+
}
|
|
127
|
+
if (typeof value !== "string") {
|
|
128
|
+
return "Telegram must be a string.";
|
|
129
|
+
}
|
|
130
|
+
if (value &&
|
|
131
|
+
!/^(https?:\/\/)?(t\.me|telegram\.me|telegram\.org)\/[A-Za-z0-9_]{5,32}\/?$/.test(value)) {
|
|
132
|
+
return "Telegram must be a valid telegram URL.";
|
|
133
|
+
}
|
|
134
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"token-field-validators.test.d.ts","sourceRoot":"","sources":["../../src/utils/token-field-validators.test.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { describe, expect, it } from "vitest";
|
|
2
|
+
import { createTokenValidators } from ".";
|
|
3
|
+
describe("token-field-validators", () => {
|
|
4
|
+
it("should validate ticker correctly", () => {
|
|
5
|
+
const validator = createTokenValidators.ticker;
|
|
6
|
+
if (validator) {
|
|
7
|
+
expect(validator("TEST")).toBeUndefined();
|
|
8
|
+
expect(validator("T3ST")).toBeUndefined();
|
|
9
|
+
expect(validator("TICKER1")).toBeUndefined();
|
|
10
|
+
expect(validator("tax")).toBe("Ticker must be alphanumeric characters in uppercase only.");
|
|
11
|
+
expect(validator("AB")).toBe("Ticker must be between 3 and 10 characters.");
|
|
12
|
+
expect(validator("A1")).toBe("Ticker must be between 3 and 10 characters.");
|
|
13
|
+
expect(validator("123456")).toBe("Ticker must have at least 2 alpha characters.");
|
|
14
|
+
}
|
|
15
|
+
});
|
|
16
|
+
it("should validate name correctly", () => {
|
|
17
|
+
const validator = createTokenValidators.name;
|
|
18
|
+
if (validator) {
|
|
19
|
+
expect(validator("My Token")).toBeUndefined();
|
|
20
|
+
expect(validator("")).toBe("Name is required.");
|
|
21
|
+
expect(validator("A".repeat(51))).toBe("Name must be between 3 and 30 characters.");
|
|
22
|
+
}
|
|
23
|
+
});
|
|
24
|
+
it("should validate description correctly", () => {
|
|
25
|
+
const validator = createTokenValidators.description;
|
|
26
|
+
if (validator) {
|
|
27
|
+
expect(validator("This is a token description.")).toBeUndefined();
|
|
28
|
+
expect(validator("A".repeat(201))).toBe("Description must not exceed 100 characters.");
|
|
29
|
+
}
|
|
30
|
+
});
|
|
31
|
+
});
|
package/package.json
CHANGED
|
@@ -1,12 +1,13 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "odin-connect",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.3",
|
|
4
4
|
"description": "",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"types": "dist/index.d.ts",
|
|
7
7
|
"scripts": {
|
|
8
8
|
"build": "tsc",
|
|
9
9
|
"prepublishOnly": "npm run build",
|
|
10
|
+
"test": "vitest",
|
|
10
11
|
"demo:update": "npm run build && cd demo && npm install ../ && cd ..",
|
|
11
12
|
"demo:start": "cd demo && npm run dev",
|
|
12
13
|
"demo": "npm run demo:update && npm run demo:start",
|
|
@@ -26,10 +27,14 @@
|
|
|
26
27
|
"homepage": "https://github.com/Toniq-Labs/odin-connect#readme",
|
|
27
28
|
"devDependencies": {
|
|
28
29
|
"@types/node": "^24.3.1",
|
|
30
|
+
"@vitest/ui": "^3.2.4",
|
|
31
|
+
"jsdom": "^27.0.0",
|
|
29
32
|
"release-it": "^19.0.5",
|
|
30
|
-
"typescript": "^5.9.2"
|
|
33
|
+
"typescript": "^5.9.2",
|
|
34
|
+
"vitest": "^3.2.4"
|
|
31
35
|
},
|
|
32
36
|
"dependencies": {
|
|
37
|
+
"@apimatic/json-bigint": "^1.2.0",
|
|
33
38
|
"axios": "^1.12.0"
|
|
34
39
|
},
|
|
35
40
|
"release-it": {
|