@polymarket/relayer-client 0.0.6 → 0.0.7
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/handler.d.ts +4 -3
- package/dist/auth/handler.js +19 -29
- package/dist/client.d.ts +2 -3
- package/dist/client.js +6 -14
- package/dist/http-helpers/index.js +0 -1
- package/dist/types.d.ts +1 -2
- package/package.json +1 -1
package/dist/auth/handler.d.ts
CHANGED
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
import { AxiosRequestHeaders } from "axios";
|
|
2
1
|
import { HttpClient } from "../http-helpers";
|
|
3
2
|
import { AuthArgs } from "../types";
|
|
4
3
|
export declare class AuthHandler {
|
|
@@ -7,8 +6,10 @@ export declare class AuthHandler {
|
|
|
7
6
|
readonly token?: string;
|
|
8
7
|
private cookie?;
|
|
9
8
|
constructor(httpClient: HttpClient, args: AuthArgs);
|
|
9
|
+
/**
|
|
10
|
+
* Logs in to the auth url
|
|
11
|
+
* Extracts polymarket session cookies from the auth response header and cache it locally.
|
|
12
|
+
*/
|
|
10
13
|
login(): Promise<void>;
|
|
11
|
-
isLoggedIn(): boolean;
|
|
12
|
-
getAuthHeader(): Promise<AxiosRequestHeaders>;
|
|
13
14
|
getPolymarketCookies(): Promise<string>;
|
|
14
15
|
}
|
package/dist/auth/handler.js
CHANGED
|
@@ -6,49 +6,39 @@ const http_helpers_1 = require("../http-helpers");
|
|
|
6
6
|
const POLYMARKET_COOKIE_NAME = "polymarket";
|
|
7
7
|
class AuthHandler {
|
|
8
8
|
constructor(httpClient, args) {
|
|
9
|
-
if (args.
|
|
9
|
+
if (args.authUrl == undefined || args.authToken == undefined) {
|
|
10
10
|
throw new Error("invalid authorization arguments");
|
|
11
11
|
}
|
|
12
12
|
this.url = args.authUrl.endsWith("/") ? args.authUrl.slice(0, -1) : args.authUrl;
|
|
13
|
-
;
|
|
13
|
+
this.token = args.authToken;
|
|
14
14
|
this.httpClient = httpClient;
|
|
15
|
-
if (args.token !== undefined) {
|
|
16
|
-
this.token = args.token;
|
|
17
|
-
}
|
|
18
|
-
if (args.cookie !== undefined) {
|
|
19
|
-
this.cookie = args.cookie;
|
|
20
|
-
}
|
|
21
15
|
}
|
|
16
|
+
/**
|
|
17
|
+
* Logs in to the auth url
|
|
18
|
+
* Extracts polymarket session cookies from the auth response header and cache it locally.
|
|
19
|
+
*/
|
|
22
20
|
login() {
|
|
23
21
|
return tslib_1.__awaiter(this, void 0, void 0, function* () {
|
|
24
|
-
if (this.
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
22
|
+
if (this.cookie == undefined) {
|
|
23
|
+
// Logging in will set the session cookies on the httpClient
|
|
24
|
+
const resp = yield this.httpClient.send(`${this.url}/login`, http_helpers_1.GET, { Authorization: `Bearer ${this.token}` });
|
|
25
|
+
const cookies = resp.headers['set-cookie'];
|
|
26
|
+
let aggregatedCookie = "";
|
|
27
|
+
for (const cookie of cookies) {
|
|
28
|
+
if (cookie.includes(POLYMARKET_COOKIE_NAME)) {
|
|
29
|
+
aggregatedCookie += `${cookie}; `;
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
if (aggregatedCookie != "") {
|
|
33
|
+
this.cookie = aggregatedCookie;
|
|
33
34
|
}
|
|
34
|
-
}
|
|
35
|
-
if (aggregatedCookie != "") {
|
|
36
|
-
this.cookie = aggregatedCookie;
|
|
37
35
|
console.log(`Successfully logged in!`);
|
|
38
36
|
}
|
|
39
37
|
});
|
|
40
38
|
}
|
|
41
|
-
isLoggedIn() {
|
|
42
|
-
return this.cookie != undefined;
|
|
43
|
-
}
|
|
44
|
-
getAuthHeader() {
|
|
45
|
-
return tslib_1.__awaiter(this, void 0, void 0, function* () {
|
|
46
|
-
return { "Cookie": yield this.getPolymarketCookies() };
|
|
47
|
-
});
|
|
48
|
-
}
|
|
49
39
|
getPolymarketCookies() {
|
|
50
40
|
return tslib_1.__awaiter(this, void 0, void 0, function* () {
|
|
51
|
-
if (
|
|
41
|
+
if (this.cookie == undefined) {
|
|
52
42
|
console.log("Authorization not set, logging in...");
|
|
53
43
|
yield this.login();
|
|
54
44
|
}
|
package/dist/client.d.ts
CHANGED
|
@@ -6,10 +6,9 @@ export declare class RelayClient {
|
|
|
6
6
|
readonly relayerUrl: string;
|
|
7
7
|
readonly chainId: number;
|
|
8
8
|
readonly httpClient: HttpClient;
|
|
9
|
+
private authHandler;
|
|
9
10
|
readonly signer?: Wallet | JsonRpcSigner;
|
|
10
|
-
|
|
11
|
-
constructor(relayerUrl: string, chainId: number, signer?: Wallet | JsonRpcSigner, authArgs?: AuthArgs);
|
|
12
|
-
getOk(): Promise<any>;
|
|
11
|
+
constructor(relayerUrl: string, chainId: number, authArgs: AuthArgs, signer?: Wallet | JsonRpcSigner);
|
|
13
12
|
getRelayAddress(): Promise<AddressPayload>;
|
|
14
13
|
getNonce(signerAddress: string, signerType: string): Promise<NoncePayload>;
|
|
15
14
|
getRelayPayload(signerAddress: string, signerType: string): Promise<RelayPayload>;
|
package/dist/client.js
CHANGED
|
@@ -3,6 +3,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
exports.RelayClient = void 0;
|
|
4
4
|
const tslib_1 = require("tslib");
|
|
5
5
|
const ethers_1 = require("ethers");
|
|
6
|
+
const browser_or_node_1 = require("browser-or-node");
|
|
6
7
|
const http_helpers_1 = require("./http-helpers");
|
|
7
8
|
const types_1 = require("./types");
|
|
8
9
|
const endpoints_1 = require("./endpoints");
|
|
@@ -10,26 +11,18 @@ const builder_1 = require("./builder");
|
|
|
10
11
|
const encode_1 = require("./encode");
|
|
11
12
|
const builder_2 = require("./builder");
|
|
12
13
|
const auth_1 = require("./auth");
|
|
13
|
-
const browser_or_node_1 = require("browser-or-node");
|
|
14
14
|
class RelayClient {
|
|
15
|
-
constructor(relayerUrl, chainId,
|
|
15
|
+
constructor(relayerUrl, chainId, authArgs, signer) {
|
|
16
16
|
this.relayerUrl = relayerUrl.endsWith("/") ? relayerUrl.slice(0, -1) : relayerUrl;
|
|
17
17
|
this.chainId = chainId;
|
|
18
18
|
this.httpClient = new http_helpers_1.HttpClient();
|
|
19
|
+
this.authHandler = new auth_1.AuthHandler(this.httpClient, authArgs);
|
|
19
20
|
if (signer !== undefined) {
|
|
20
21
|
this.signer = signer;
|
|
21
22
|
if (signer.provider == undefined) {
|
|
22
23
|
throw new Error("signer must have provider attached");
|
|
23
24
|
}
|
|
24
25
|
}
|
|
25
|
-
if (authArgs !== undefined) {
|
|
26
|
-
this.authHandler = new auth_1.AuthHandler(this.httpClient, authArgs);
|
|
27
|
-
}
|
|
28
|
-
}
|
|
29
|
-
getOk() {
|
|
30
|
-
return tslib_1.__awaiter(this, void 0, void 0, function* () {
|
|
31
|
-
return this.send(`${this.relayerUrl}/`, http_helpers_1.GET);
|
|
32
|
-
});
|
|
33
26
|
}
|
|
34
27
|
getRelayAddress() {
|
|
35
28
|
return tslib_1.__awaiter(this, void 0, void 0, function* () {
|
|
@@ -123,9 +116,10 @@ class RelayClient {
|
|
|
123
116
|
}
|
|
124
117
|
send(endpoint, method, headers, data, params) {
|
|
125
118
|
return tslib_1.__awaiter(this, void 0, void 0, function* () {
|
|
126
|
-
|
|
119
|
+
yield this.authHandler.login();
|
|
120
|
+
// If client is instantiated locally/not on the browser, explicitly inject the polymarket session cookies
|
|
127
121
|
// this is to ensure examples still run locally
|
|
128
|
-
if (!browser_or_node_1.isBrowser
|
|
122
|
+
if (!browser_or_node_1.isBrowser) {
|
|
129
123
|
const cookie = yield this.authHandler.getPolymarketCookies();
|
|
130
124
|
if (headers != undefined) {
|
|
131
125
|
headers["Cookie"] = cookie;
|
|
@@ -133,8 +127,6 @@ class RelayClient {
|
|
|
133
127
|
else {
|
|
134
128
|
headers = { "Cookie": cookie };
|
|
135
129
|
}
|
|
136
|
-
const resp = yield this.httpClient.send(endpoint, method, headers, data, params);
|
|
137
|
-
return resp.data;
|
|
138
130
|
}
|
|
139
131
|
// If the client is instantiated on the browser, do not inject cookies
|
|
140
132
|
const resp = yield this.httpClient.send(endpoint, method, headers, data, params);
|
|
@@ -14,7 +14,6 @@ class HttpClient {
|
|
|
14
14
|
send(endpoint, method, headers, data, params) {
|
|
15
15
|
var _a, _b, _c;
|
|
16
16
|
return tslib_1.__awaiter(this, void 0, void 0, function* () {
|
|
17
|
-
console.log(`In HttpClient send`);
|
|
18
17
|
if (headers != undefined) {
|
|
19
18
|
headers["Access-Control-Allow-Credentials"] = true;
|
|
20
19
|
}
|
package/dist/types.d.ts
CHANGED