@pear-protocol/core-sdk 0.0.1 → 0.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/dist/auth/index.d.ts +5 -0
- package/dist/index.js +38 -0
- package/dist/info/index.d.ts +18 -11
- package/dist/info/index.js +6 -0
- package/dist/trade/index.d.ts +6 -0
- package/dist/types.d.ts +7 -1
- package/dist/utils.js +3 -0
- package/package.json +3 -3
package/dist/auth/index.d.ts
CHANGED
|
@@ -8,11 +8,13 @@ declare class Auth {
|
|
|
8
8
|
me(headers?: ClientHeaders): Promise<{
|
|
9
9
|
id: string;
|
|
10
10
|
role: "basic" | "pro";
|
|
11
|
+
address?: string | undefined;
|
|
11
12
|
}>;
|
|
12
13
|
login(body: HttpTypes.LoginRequest, headers?: ClientHeaders): Promise<{
|
|
13
14
|
user: {
|
|
14
15
|
id: string;
|
|
15
16
|
role: "basic" | "pro";
|
|
17
|
+
address?: string | undefined;
|
|
16
18
|
};
|
|
17
19
|
}>;
|
|
18
20
|
refreshSession(headers?: ClientHeaders): Promise<void>;
|
|
@@ -31,18 +33,21 @@ declare class Auth {
|
|
|
31
33
|
user: {
|
|
32
34
|
id: string;
|
|
33
35
|
role: "basic" | "pro";
|
|
36
|
+
address?: string | undefined;
|
|
34
37
|
};
|
|
35
38
|
}>;
|
|
36
39
|
linkEmail(body: HttpTypes.LinkEmailRequest, headers?: ClientHeaders): Promise<{
|
|
37
40
|
user: {
|
|
38
41
|
id: string;
|
|
39
42
|
role: "basic" | "pro";
|
|
43
|
+
address?: string | undefined;
|
|
40
44
|
};
|
|
41
45
|
}>;
|
|
42
46
|
linkWallet(body: HttpTypes.LinkWalletRequest, headers?: ClientHeaders): Promise<{
|
|
43
47
|
user: {
|
|
44
48
|
id: string;
|
|
45
49
|
role: "basic" | "pro";
|
|
50
|
+
address?: string | undefined;
|
|
46
51
|
};
|
|
47
52
|
}>;
|
|
48
53
|
}
|
package/dist/index.js
CHANGED
|
@@ -11,11 +11,49 @@ class PearSDK {
|
|
|
11
11
|
info;
|
|
12
12
|
constructor(config) {
|
|
13
13
|
const authMode = config.auth?.type ?? "session";
|
|
14
|
+
const logger = config.logger ?? {
|
|
15
|
+
error: console.error,
|
|
16
|
+
warn: console.warn,
|
|
17
|
+
info: console.info,
|
|
18
|
+
debug: config.debug ? console.debug : () => {
|
|
19
|
+
}
|
|
20
|
+
};
|
|
14
21
|
const apiClient = new HttpClient(config.apiUrl, authMode, config.logger, config.debug);
|
|
15
22
|
const authClient = new HttpClient(config.authUrl, authMode, config.logger, config.debug);
|
|
16
23
|
this.auth = new Auth(authClient);
|
|
17
24
|
this.trade = new Trade(apiClient);
|
|
18
25
|
this.info = new Info(apiClient);
|
|
26
|
+
if (authMode === "session") {
|
|
27
|
+
let refreshInFlight = null;
|
|
28
|
+
const refreshSession = async () => {
|
|
29
|
+
if (!refreshInFlight) {
|
|
30
|
+
logger.info("[session-refresh] Refreshing session...");
|
|
31
|
+
refreshInFlight = this.auth.refreshSession().then(() => logger.info("[session-refresh] Session refreshed successfully")).catch((err) => {
|
|
32
|
+
logger.error("[session-refresh] Refresh failed:", String(err));
|
|
33
|
+
throw err;
|
|
34
|
+
}).finally(() => {
|
|
35
|
+
refreshInFlight = null;
|
|
36
|
+
});
|
|
37
|
+
} else {
|
|
38
|
+
logger.debug("[session-refresh] Refresh already in flight, waiting...");
|
|
39
|
+
}
|
|
40
|
+
return refreshInFlight;
|
|
41
|
+
};
|
|
42
|
+
const sessionRefreshMiddleware = async (response, retry, request) => {
|
|
43
|
+
if (response.status !== 401) return response;
|
|
44
|
+
logger.warn(`[session-refresh] 401 received for ${request.method} ${request.url}`);
|
|
45
|
+
try {
|
|
46
|
+
await refreshSession();
|
|
47
|
+
logger.info(`[session-refresh] Retrying ${request.method} ${request.url}`);
|
|
48
|
+
return retry();
|
|
49
|
+
} catch {
|
|
50
|
+
logger.error("[session-refresh] Session expired, cannot recover");
|
|
51
|
+
config.onSessionExpired?.();
|
|
52
|
+
throw new FetchError("Session expired. Please log in again.", "Unauthorized", 401);
|
|
53
|
+
}
|
|
54
|
+
};
|
|
55
|
+
apiClient.useResponse(sessionRefreshMiddleware);
|
|
56
|
+
}
|
|
19
57
|
if (authMode === "bearer" && config.auth?.token) {
|
|
20
58
|
const getToken = config.auth.token;
|
|
21
59
|
apiClient.useRequest(async (request, next) => {
|
package/dist/info/index.d.ts
CHANGED
|
@@ -11,38 +11,41 @@ declare class Info {
|
|
|
11
11
|
id: string;
|
|
12
12
|
alias: string;
|
|
13
13
|
connector: "hyperliquid" | "binanceusdm" | "bybit" | "okx";
|
|
14
|
+
exchangeIdentifier: string;
|
|
14
15
|
createdAt: string;
|
|
15
16
|
updatedAt: string;
|
|
16
|
-
metadata?:
|
|
17
|
-
|
|
17
|
+
metadata?: {
|
|
18
|
+
agentWalletAddress: string;
|
|
18
19
|
isSubaccount: boolean;
|
|
19
|
-
} |
|
|
20
|
+
} | undefined;
|
|
20
21
|
}[];
|
|
21
22
|
}>;
|
|
22
|
-
create: (body: HttpTypes.
|
|
23
|
+
create: (body: HttpTypes.CreateTradeAccountInput, headers?: ClientHeaders) => Promise<{
|
|
23
24
|
account: {
|
|
24
25
|
id: string;
|
|
25
26
|
alias: string;
|
|
26
27
|
connector: "hyperliquid" | "binanceusdm" | "bybit" | "okx";
|
|
28
|
+
exchangeIdentifier: string;
|
|
27
29
|
createdAt: string;
|
|
28
30
|
updatedAt: string;
|
|
29
|
-
metadata?:
|
|
30
|
-
|
|
31
|
+
metadata?: {
|
|
32
|
+
agentWalletAddress: string;
|
|
31
33
|
isSubaccount: boolean;
|
|
32
|
-
} |
|
|
34
|
+
} | undefined;
|
|
33
35
|
};
|
|
34
36
|
}>;
|
|
35
|
-
update: (id: string, body: HttpTypes.
|
|
37
|
+
update: (id: string, body: HttpTypes.UpdateTradeAccountInput, headers?: ClientHeaders) => Promise<{
|
|
36
38
|
account: {
|
|
37
39
|
id: string;
|
|
38
40
|
alias: string;
|
|
39
41
|
connector: "hyperliquid" | "binanceusdm" | "bybit" | "okx";
|
|
42
|
+
exchangeIdentifier: string;
|
|
40
43
|
createdAt: string;
|
|
41
44
|
updatedAt: string;
|
|
42
|
-
metadata?:
|
|
43
|
-
|
|
45
|
+
metadata?: {
|
|
46
|
+
agentWalletAddress: string;
|
|
44
47
|
isSubaccount: boolean;
|
|
45
|
-
} |
|
|
48
|
+
} | undefined;
|
|
46
49
|
};
|
|
47
50
|
}>;
|
|
48
51
|
delete: (id: string, headers?: ClientHeaders) => Promise<void>;
|
|
@@ -71,6 +74,7 @@ declare class Info {
|
|
|
71
74
|
api_pass: string;
|
|
72
75
|
};
|
|
73
76
|
}>;
|
|
77
|
+
restore: (id: string, headers?: ClientHeaders) => Promise<void>;
|
|
74
78
|
};
|
|
75
79
|
users: {
|
|
76
80
|
profile: (headers?: ClientHeaders) => Promise<{
|
|
@@ -260,6 +264,7 @@ declare class Info {
|
|
|
260
264
|
cloid?: string | undefined;
|
|
261
265
|
fee?: string | undefined;
|
|
262
266
|
}[] | undefined;
|
|
267
|
+
bracketType?: "STOP_LOSS" | "TAKE_PROFIT" | undefined;
|
|
263
268
|
} | {
|
|
264
269
|
id: string;
|
|
265
270
|
tradeAccountId: string;
|
|
@@ -369,6 +374,7 @@ declare class Info {
|
|
|
369
374
|
cloid?: string | undefined;
|
|
370
375
|
fee?: string | undefined;
|
|
371
376
|
}[] | undefined;
|
|
377
|
+
bracketType?: "STOP_LOSS" | "TAKE_PROFIT" | undefined;
|
|
372
378
|
} | {
|
|
373
379
|
id: string;
|
|
374
380
|
tradeAccountId: string;
|
|
@@ -465,6 +471,7 @@ declare class Info {
|
|
|
465
471
|
cloid?: string | undefined;
|
|
466
472
|
fee?: string | undefined;
|
|
467
473
|
}[] | undefined;
|
|
474
|
+
bracketType?: "STOP_LOSS" | "TAKE_PROFIT" | undefined;
|
|
468
475
|
})[];
|
|
469
476
|
}>;
|
|
470
477
|
};
|
package/dist/info/index.js
CHANGED
package/dist/trade/index.d.ts
CHANGED
|
@@ -636,6 +636,7 @@ declare class Trade {
|
|
|
636
636
|
cloid?: string | undefined;
|
|
637
637
|
fee?: string | undefined;
|
|
638
638
|
}[] | undefined;
|
|
639
|
+
bracketType?: "STOP_LOSS" | "TAKE_PROFIT" | undefined;
|
|
639
640
|
} | {
|
|
640
641
|
id: string;
|
|
641
642
|
tradeAccountId: string;
|
|
@@ -745,6 +746,7 @@ declare class Trade {
|
|
|
745
746
|
cloid?: string | undefined;
|
|
746
747
|
fee?: string | undefined;
|
|
747
748
|
}[] | undefined;
|
|
749
|
+
bracketType?: "STOP_LOSS" | "TAKE_PROFIT" | undefined;
|
|
748
750
|
} | {
|
|
749
751
|
id: string;
|
|
750
752
|
tradeAccountId: string;
|
|
@@ -841,6 +843,7 @@ declare class Trade {
|
|
|
841
843
|
cloid?: string | undefined;
|
|
842
844
|
fee?: string | undefined;
|
|
843
845
|
}[] | undefined;
|
|
846
|
+
bracketType?: "STOP_LOSS" | "TAKE_PROFIT" | undefined;
|
|
844
847
|
};
|
|
845
848
|
}>;
|
|
846
849
|
close: (body: HttpTypes.CreateTriggerClose, headers?: ClientHeaders) => Promise<{
|
|
@@ -926,6 +929,7 @@ declare class Trade {
|
|
|
926
929
|
cloid?: string | undefined;
|
|
927
930
|
fee?: string | undefined;
|
|
928
931
|
}[] | undefined;
|
|
932
|
+
bracketType?: "STOP_LOSS" | "TAKE_PROFIT" | undefined;
|
|
929
933
|
} | {
|
|
930
934
|
id: string;
|
|
931
935
|
tradeAccountId: string;
|
|
@@ -1035,6 +1039,7 @@ declare class Trade {
|
|
|
1035
1039
|
cloid?: string | undefined;
|
|
1036
1040
|
fee?: string | undefined;
|
|
1037
1041
|
}[] | undefined;
|
|
1042
|
+
bracketType?: "STOP_LOSS" | "TAKE_PROFIT" | undefined;
|
|
1038
1043
|
} | {
|
|
1039
1044
|
id: string;
|
|
1040
1045
|
tradeAccountId: string;
|
|
@@ -1131,6 +1136,7 @@ declare class Trade {
|
|
|
1131
1136
|
cloid?: string | undefined;
|
|
1132
1137
|
fee?: string | undefined;
|
|
1133
1138
|
}[] | undefined;
|
|
1139
|
+
bracketType?: "STOP_LOSS" | "TAKE_PROFIT" | undefined;
|
|
1134
1140
|
};
|
|
1135
1141
|
}>;
|
|
1136
1142
|
cancel: (triggerId: string, headers?: ClientHeaders) => Promise<{
|
package/dist/types.d.ts
CHANGED
|
@@ -41,7 +41,7 @@ type SDKConfig = {
|
|
|
41
41
|
*/
|
|
42
42
|
getTradeAccountId?: () => string | null;
|
|
43
43
|
/**
|
|
44
|
-
* Called when server returns 401 (bearer mode only)
|
|
44
|
+
* Called when server returns 401 (bearer mode only).
|
|
45
45
|
*
|
|
46
46
|
* Example: () => {
|
|
47
47
|
* console.error('Unauthorized. Token may be expired or revoked.');
|
|
@@ -49,6 +49,12 @@ type SDKConfig = {
|
|
|
49
49
|
* }
|
|
50
50
|
*/
|
|
51
51
|
onAuthError?: () => void;
|
|
52
|
+
/**
|
|
53
|
+
* Called when the session cannot be recovered (session mode only).
|
|
54
|
+
* Fires after a 401 when the subsequent `POST /auth/refresh` also fails.
|
|
55
|
+
* Typical use: clear UI state and redirect to login.
|
|
56
|
+
*/
|
|
57
|
+
onSessionExpired?: () => void;
|
|
52
58
|
/**
|
|
53
59
|
* Logger for debugging
|
|
54
60
|
*
|
package/dist/utils.js
CHANGED
|
@@ -29,6 +29,9 @@ async function normalizeResponseData(resp, reqHeaders) {
|
|
|
29
29
|
const jsonError = await resp.json().catch(() => ({}));
|
|
30
30
|
throw new FetchError(jsonError.message ?? resp.statusText, resp.statusText, resp.status);
|
|
31
31
|
}
|
|
32
|
+
if (resp.status === 204 || resp.status === 205) {
|
|
33
|
+
return null;
|
|
34
|
+
}
|
|
32
35
|
const isJsonRequest = reqHeaders.get("accept")?.includes("application/json");
|
|
33
36
|
return isJsonRequest ? await resp.json() : resp;
|
|
34
37
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@pear-protocol/core-sdk",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.3",
|
|
4
4
|
"description": "Pear Protocol Core SDK",
|
|
5
5
|
"private": false,
|
|
6
6
|
"type": "module",
|
|
@@ -25,8 +25,8 @@
|
|
|
25
25
|
"typecheck": "tsc --noEmit"
|
|
26
26
|
},
|
|
27
27
|
"dependencies": {
|
|
28
|
-
"@pear-protocol/types": "0.0.
|
|
29
|
-
"@pear-protocol/utils": "0.0.
|
|
28
|
+
"@pear-protocol/types": "0.0.3",
|
|
29
|
+
"@pear-protocol/utils": "0.0.3",
|
|
30
30
|
"qs": "^6.14.0"
|
|
31
31
|
},
|
|
32
32
|
"devDependencies": {
|