@sogni-ai/sogni-client 1.0.0-alpha.3 → 1.0.0
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/CHANGELOG.md +6 -0
- package/dist/Account/CurrentAccount.d.ts +5 -4
- package/dist/Account/CurrentAccount.js +29 -9
- package/dist/Account/CurrentAccount.js.map +1 -1
- package/dist/Account/index.d.ts +3 -5
- package/dist/Account/index.js +9 -20
- package/dist/Account/index.js.map +1 -1
- package/dist/Account/types.d.ts +0 -2
- package/dist/ApiClient/WebSocketClient/index.d.ts +4 -4
- package/dist/ApiClient/WebSocketClient/index.js +49 -33
- package/dist/ApiClient/WebSocketClient/index.js.map +1 -1
- package/dist/ApiClient/index.d.ts +10 -4
- package/dist/ApiClient/index.js +17 -26
- package/dist/ApiClient/index.js.map +1 -1
- package/dist/Projects/createJobRequestMessage.js +1 -1
- package/dist/Projects/createJobRequestMessage.js.map +1 -1
- package/dist/Projects/index.d.ts +2 -27
- package/dist/Projects/index.js +14 -94
- package/dist/Projects/index.js.map +1 -1
- package/dist/Projects/models.json +8906 -0
- package/dist/Projects/types/index.d.ts +0 -32
- package/dist/lib/RestClient.d.ts +7 -4
- package/dist/lib/RestClient.js +7 -7
- package/dist/lib/RestClient.js.map +1 -1
- package/dist/version.d.ts +1 -1
- package/dist/version.js +1 -1
- package/dist/version.js.map +1 -1
- package/package.json +6 -1
- package/src/Account/CurrentAccount.ts +33 -12
- package/src/Account/index.ts +8 -18
- package/src/Account/types.ts +0 -2
- package/src/ApiClient/WebSocketClient/index.ts +25 -16
- package/src/ApiClient/index.ts +27 -19
- package/src/Projects/createJobRequestMessage.ts +1 -2
- package/src/Projects/index.ts +8 -97
- package/src/Projects/models.json +8906 -0
- package/src/Projects/types/index.ts +0 -34
- package/src/lib/RestClient.ts +13 -8
- package/src/version.ts +1 -1
- package/dist/lib/AuthManager.d.ts +0 -51
- package/dist/lib/AuthManager.js +0 -157
- package/dist/lib/AuthManager.js.map +0 -1
- package/dist/lib/Cache.d.ts +0 -9
- package/dist/lib/Cache.js +0 -30
- package/dist/lib/Cache.js.map +0 -1
- package/dist/lib/utils.d.ts +0 -8
- package/dist/lib/utils.js +0 -20
- package/dist/lib/utils.js.map +0 -1
- package/src/lib/AuthManager.ts +0 -172
- package/src/lib/Cache.ts +0 -36
- package/src/lib/utils.ts +0 -17
|
@@ -1,22 +1,9 @@
|
|
|
1
1
|
import { SupernetType } from '../../ApiClient/WebSocketClient/types';
|
|
2
|
-
export interface SupportedModel {
|
|
3
|
-
id: string;
|
|
4
|
-
name: string;
|
|
5
|
-
SID: number;
|
|
6
|
-
}
|
|
7
2
|
export interface AvailableModel {
|
|
8
3
|
id: string;
|
|
9
4
|
name: string;
|
|
10
5
|
workerCount: number;
|
|
11
6
|
}
|
|
12
|
-
export interface SizePreset {
|
|
13
|
-
label: string;
|
|
14
|
-
id: string;
|
|
15
|
-
width: number;
|
|
16
|
-
height: number;
|
|
17
|
-
ratio: string;
|
|
18
|
-
aspect: string;
|
|
19
|
-
}
|
|
20
7
|
export interface AiModel {
|
|
21
8
|
isSD3: boolean;
|
|
22
9
|
modelShortName: string;
|
|
@@ -111,11 +98,6 @@ export interface ProjectParams {
|
|
|
111
98
|
* Time step spacing method
|
|
112
99
|
*/
|
|
113
100
|
timeStepSpacing?: TimeStepSpacing;
|
|
114
|
-
/**
|
|
115
|
-
* Size preset ID to use. You can query available size presets
|
|
116
|
-
* from `client.projects.sizePresets(network, modelId)`
|
|
117
|
-
*/
|
|
118
|
-
sizePreset?: string;
|
|
119
101
|
}
|
|
120
102
|
export type ImageUrlParams = {
|
|
121
103
|
imageId: string;
|
|
@@ -152,18 +134,4 @@ export interface EstimateRequest {
|
|
|
152
134
|
* How strong effect of starting image should be. From 0 to 1, default 0.5
|
|
153
135
|
*/
|
|
154
136
|
startingImageStrength?: number;
|
|
155
|
-
/**
|
|
156
|
-
* Size preset ID
|
|
157
|
-
*/
|
|
158
|
-
sizePreset?: string;
|
|
159
|
-
/**
|
|
160
|
-
* Size preset image width, if not using size preset
|
|
161
|
-
* @internal
|
|
162
|
-
*/
|
|
163
|
-
width?: number;
|
|
164
|
-
/**
|
|
165
|
-
* Size preset image height, if not using size preset
|
|
166
|
-
* @internal
|
|
167
|
-
*/
|
|
168
|
-
height?: number;
|
|
169
137
|
}
|
package/dist/lib/RestClient.d.ts
CHANGED
|
@@ -1,13 +1,16 @@
|
|
|
1
1
|
import TypedEventEmitter, { EventMap } from './TypedEventEmitter';
|
|
2
2
|
import { JSONValue } from '../types/json';
|
|
3
3
|
import { Logger } from './DefaultLogger';
|
|
4
|
-
|
|
4
|
+
export interface AuthData {
|
|
5
|
+
token: string;
|
|
6
|
+
}
|
|
5
7
|
declare class RestClient<E extends EventMap = never> extends TypedEventEmitter<E> {
|
|
6
8
|
readonly baseUrl: string;
|
|
7
|
-
protected _auth:
|
|
9
|
+
protected _auth: AuthData | null;
|
|
8
10
|
protected _logger: Logger;
|
|
9
|
-
constructor(baseUrl: string,
|
|
10
|
-
get auth():
|
|
11
|
+
constructor(baseUrl: string, logger: Logger);
|
|
12
|
+
get auth(): AuthData | null;
|
|
13
|
+
set auth(auth: AuthData | null);
|
|
11
14
|
private formatUrl;
|
|
12
15
|
private request;
|
|
13
16
|
private processResponse;
|
package/dist/lib/RestClient.js
CHANGED
|
@@ -15,15 +15,18 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
15
15
|
const ApiClient_1 = require("../ApiClient");
|
|
16
16
|
const TypedEventEmitter_1 = __importDefault(require("./TypedEventEmitter"));
|
|
17
17
|
class RestClient extends TypedEventEmitter_1.default {
|
|
18
|
-
constructor(baseUrl,
|
|
18
|
+
constructor(baseUrl, logger) {
|
|
19
19
|
super();
|
|
20
|
+
this._auth = null;
|
|
20
21
|
this.baseUrl = baseUrl;
|
|
21
|
-
this._auth = auth;
|
|
22
22
|
this._logger = logger;
|
|
23
23
|
}
|
|
24
24
|
get auth() {
|
|
25
25
|
return this._auth;
|
|
26
26
|
}
|
|
27
|
+
set auth(auth) {
|
|
28
|
+
this._auth = auth;
|
|
29
|
+
}
|
|
27
30
|
formatUrl(relativeUrl, query = {}) {
|
|
28
31
|
const url = new URL(relativeUrl, this.baseUrl);
|
|
29
32
|
Object.keys(query).forEach((key) => {
|
|
@@ -31,11 +34,8 @@ class RestClient extends TypedEventEmitter_1.default {
|
|
|
31
34
|
});
|
|
32
35
|
return url.toString();
|
|
33
36
|
}
|
|
34
|
-
request(
|
|
35
|
-
return
|
|
36
|
-
const token = yield this.auth.getToken();
|
|
37
|
-
return fetch(url, Object.assign(Object.assign({}, options), { headers: Object.assign(Object.assign({}, (options.headers || {})), (token ? { Authorization: token } : {})) })).then((r) => this.processResponse(r));
|
|
38
|
-
});
|
|
37
|
+
request(url, options = {}) {
|
|
38
|
+
return fetch(url, Object.assign(Object.assign({}, options), { headers: Object.assign(Object.assign({}, (options.headers || {})), (this.auth ? { Authorization: this.auth.token } : {})) })).then((r) => this.processResponse(r));
|
|
39
39
|
}
|
|
40
40
|
processResponse(response) {
|
|
41
41
|
return __awaiter(this, void 0, void 0, function* () {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"RestClient.js","sourceRoot":"","sources":["../../src/lib/RestClient.ts"],"names":[],"mappings":";;;;;;;;;;;;;;AAAA,4CAA0D;AAC1D,4EAAkE;
|
|
1
|
+
{"version":3,"file":"RestClient.js","sourceRoot":"","sources":["../../src/lib/RestClient.ts"],"names":[],"mappings":";;;;;;;;;;;;;;AAAA,4CAA0D;AAC1D,4EAAkE;AAQlE,MAAM,UAAuC,SAAQ,2BAAoB;IAKvE,YAAY,OAAe,EAAE,MAAc;QACzC,KAAK,EAAE,CAAC;QAJA,UAAK,GAAoB,IAAI,CAAC;QAKtC,IAAI,CAAC,OAAO,GAAG,OAAO,CAAC;QACvB,IAAI,CAAC,OAAO,GAAG,MAAM,CAAC;IACxB,CAAC;IAED,IAAI,IAAI;QACN,OAAO,IAAI,CAAC,KAAK,CAAC;IACpB,CAAC;IAED,IAAI,IAAI,CAAC,IAAqB;QAC5B,IAAI,CAAC,KAAK,GAAG,IAAI,CAAC;IACpB,CAAC;IAEO,SAAS,CAAC,WAAmB,EAAE,QAAgC,EAAE;QACvE,MAAM,GAAG,GAAG,IAAI,GAAG,CAAC,WAAW,EAAE,IAAI,CAAC,OAAO,CAAC,CAAC;QAC/C,MAAM,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC,OAAO,CAAC,CAAC,GAAG,EAAE,EAAE;YACjC,GAAG,CAAC,YAAY,CAAC,MAAM,CAAC,GAAG,EAAE,KAAK,CAAC,GAAG,CAAC,CAAC,CAAC;QAC3C,CAAC,CAAC,CAAC;QACH,OAAO,GAAG,CAAC,QAAQ,EAAE,CAAC;IACxB,CAAC;IAEO,OAAO,CAAgB,GAAW,EAAE,UAAuB,EAAE;QACnE,OAAO,KAAK,CAAC,GAAG,kCACX,OAAO,KACV,OAAO,kCACF,CAAC,OAAO,CAAC,OAAO,IAAI,EAAE,CAAC,GACvB,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC,CAAC,EAAE,aAAa,EAAE,IAAI,CAAC,IAAI,CAAC,KAAK,EAAE,CAAC,CAAC,CAAC,EAAE,CAAC,KAE1D,CAAC,IAAI,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,IAAI,CAAC,eAAe,CAAC,CAAC,CAAM,CAAC,CAAC;IAC/C,CAAC;IAEa,eAAe,CAAC,QAAkB;;YAC9C,IAAI,YAAY,CAAC;YACjB,IAAI,CAAC;gBACH,YAAY,GAAG,MAAM,QAAQ,CAAC,IAAI,EAAE,CAAC;YACvC,CAAC;YAAC,OAAO,CAAC,EAAE,CAAC;gBACX,IAAI,CAAC,OAAO,CAAC,KAAK,CAAC,2BAA2B,EAAE,CAAC,CAAC,CAAC;gBACnD,MAAM,IAAI,KAAK,CAAC,0BAA0B,CAAC,CAAC;YAC9C,CAAC;YACD,IAAI,CAAC,QAAQ,CAAC,EAAE,EAAE,CAAC;gBACjB,MAAM,IAAI,oBAAQ,CAAC,QAAQ,CAAC,MAAM,EAAE,YAAgC,CAAC,CAAC;YACxE,CAAC;YACD,OAAO,YAAyB,CAAC;QACnC,CAAC;KAAA;IAED,GAAG,CAAgB,IAAY,EAAE,QAA6B,EAAE;QAC9D,OAAO,IAAI,CAAC,OAAO,CAAI,IAAI,CAAC,SAAS,CAAC,IAAI,EAAE,KAAK,CAAC,EAAE,KAAK,CAAC,CAAC;IAC7D,CAAC;IAED,IAAI,CAAgB,IAAY,EAAE,OAAgC,EAAE;QAClE,OAAO,IAAI,CAAC,OAAO,CAAI,IAAI,CAAC,SAAS,CAAC,IAAI,CAAC,EAAE;YAC3C,MAAM,EAAE,MAAM;YACd,OAAO,EAAE;gBACP,cAAc,EAAE,kBAAkB;aACnC;YACD,IAAI,EAAE,IAAI,CAAC,SAAS,CAAC,IAAI,CAAC;SAC3B,CAAC,CAAC;IACL,CAAC;CACF;AAED,kBAAe,UAAU,CAAC"}
|
package/dist/version.d.ts
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export declare const LIB_VERSION = "1.0.0
|
|
1
|
+
export declare const LIB_VERSION = "1.0.0";
|
package/dist/version.js
CHANGED
package/dist/version.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"version.js","sourceRoot":"","sources":["../src/version.ts"],"names":[],"mappings":";;;AAAa,QAAA,WAAW,GAAG,
|
|
1
|
+
{"version":3,"file":"version.js","sourceRoot":"","sources":["../src/version.ts"],"names":[],"mappings":";;;AAAa,QAAA,WAAW,GAAG,OAAO,CAAC"}
|
package/package.json
CHANGED
|
@@ -3,11 +3,12 @@
|
|
|
3
3
|
"publishConfig": {
|
|
4
4
|
"access": "public"
|
|
5
5
|
},
|
|
6
|
-
"version": "1.0.0
|
|
6
|
+
"version": "1.0.0",
|
|
7
7
|
"description": "Sogni Supernet Client",
|
|
8
8
|
"main": "dist/index.js",
|
|
9
9
|
"types": "dist/index.d.ts",
|
|
10
10
|
"files": [
|
|
11
|
+
"CHANGELOG.md",
|
|
11
12
|
"README.md",
|
|
12
13
|
"dist/",
|
|
13
14
|
"src/"
|
|
@@ -21,6 +22,7 @@
|
|
|
21
22
|
"watch:esm": "npm run clean && tsc --watch --project tsconfig.esm.json",
|
|
22
23
|
"prettier": "prettier --check ./src",
|
|
23
24
|
"prettier:fix": "prettier --write ./src",
|
|
25
|
+
"release": "semantic-release",
|
|
24
26
|
"docs": "rimraf ./docs && npx typedoc",
|
|
25
27
|
"postdocs": "node -p \"'sdk-docs.sogni.ai'\" > docs/CNAME"
|
|
26
28
|
},
|
|
@@ -44,11 +46,14 @@
|
|
|
44
46
|
"ws": "^8.18.0"
|
|
45
47
|
},
|
|
46
48
|
"devDependencies": {
|
|
49
|
+
"@semantic-release/changelog": "^6.0.3",
|
|
50
|
+
"@semantic-release/git": "^10.0.1",
|
|
47
51
|
"@types/js-cookie": "^3.0.6",
|
|
48
52
|
"@types/lodash": "^4.17.13",
|
|
49
53
|
"@types/ws": "^8.5.13",
|
|
50
54
|
"prettier": "^3.3.3",
|
|
51
55
|
"rimraf": "^6.0.1",
|
|
56
|
+
"semantic-release": "^24.2.1",
|
|
52
57
|
"typedoc": "^0.27.3",
|
|
53
58
|
"typedoc-plugin-missing-exports": "^3.1.0",
|
|
54
59
|
"typescript": "^5.6.3"
|
|
@@ -1,10 +1,12 @@
|
|
|
1
1
|
import DataEntity from '../lib/DataEntity';
|
|
2
2
|
import { BalanceData } from './types';
|
|
3
|
+
import { jwtDecode } from 'jwt-decode';
|
|
3
4
|
import { SupernetType } from '../ApiClient/WebSocketClient/types';
|
|
4
5
|
/**
|
|
5
6
|
* @inline
|
|
6
7
|
*/
|
|
7
8
|
export interface AccountData {
|
|
9
|
+
token: string | null;
|
|
8
10
|
/**
|
|
9
11
|
* Current network status:\
|
|
10
12
|
* - `connected` - connected to the socket
|
|
@@ -19,13 +21,13 @@ export interface AccountData {
|
|
|
19
21
|
network: SupernetType | null;
|
|
20
22
|
balance: BalanceData;
|
|
21
23
|
walletAddress?: string;
|
|
24
|
+
expiresAt?: Date;
|
|
22
25
|
username?: string;
|
|
23
|
-
token?: string;
|
|
24
|
-
refreshToken?: string;
|
|
25
26
|
}
|
|
26
27
|
|
|
27
28
|
function getDefaults(): AccountData {
|
|
28
29
|
return {
|
|
30
|
+
token: null,
|
|
29
31
|
networkStatus: 'disconnected',
|
|
30
32
|
network: null,
|
|
31
33
|
balance: {
|
|
@@ -33,11 +35,15 @@ function getDefaults(): AccountData {
|
|
|
33
35
|
debit: '0',
|
|
34
36
|
net: '0',
|
|
35
37
|
settled: '0'
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
38
|
+
}
|
|
39
|
+
};
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
function decodeToken(token: string) {
|
|
43
|
+
const data = jwtDecode<{ addr: string; env: string; iat: number; exp: number }>(token);
|
|
44
|
+
return {
|
|
45
|
+
walletAddress: data.addr,
|
|
46
|
+
expiresAt: new Date(data.exp * 1000)
|
|
41
47
|
};
|
|
42
48
|
}
|
|
43
49
|
|
|
@@ -50,12 +56,27 @@ class CurrentAccount extends DataEntity<AccountData> {
|
|
|
50
56
|
super(data || getDefaults());
|
|
51
57
|
}
|
|
52
58
|
|
|
59
|
+
_update<K extends keyof AccountData>(delta: Partial<AccountData>) {
|
|
60
|
+
this.data = { ...this.data, ...(delta as Partial<AccountData>) };
|
|
61
|
+
const keys = Object.keys(delta);
|
|
62
|
+
if (delta.hasOwnProperty('token')) {
|
|
63
|
+
if (delta.token) {
|
|
64
|
+
Object.assign(this.data, decodeToken(delta.token));
|
|
65
|
+
} else {
|
|
66
|
+
delete this.data.walletAddress;
|
|
67
|
+
delete this.data.expiresAt;
|
|
68
|
+
}
|
|
69
|
+
keys.push('walletAddress', 'expiresAt');
|
|
70
|
+
}
|
|
71
|
+
this.emit('updated', keys);
|
|
72
|
+
}
|
|
73
|
+
|
|
53
74
|
_clear() {
|
|
54
75
|
this._update(getDefaults());
|
|
55
76
|
}
|
|
56
77
|
|
|
57
78
|
get isAuthenicated() {
|
|
58
|
-
return !!this.data.
|
|
79
|
+
return !!this.data.token && !!this.data.expiresAt && this.data.expiresAt > new Date();
|
|
59
80
|
}
|
|
60
81
|
|
|
61
82
|
get networkStatus() {
|
|
@@ -74,6 +95,10 @@ class CurrentAccount extends DataEntity<AccountData> {
|
|
|
74
95
|
return this.data.walletAddress;
|
|
75
96
|
}
|
|
76
97
|
|
|
98
|
+
get expiresAt() {
|
|
99
|
+
return this.data.expiresAt;
|
|
100
|
+
}
|
|
101
|
+
|
|
77
102
|
get username() {
|
|
78
103
|
return this.data.username;
|
|
79
104
|
}
|
|
@@ -81,10 +106,6 @@ class CurrentAccount extends DataEntity<AccountData> {
|
|
|
81
106
|
get token() {
|
|
82
107
|
return this.data.token;
|
|
83
108
|
}
|
|
84
|
-
|
|
85
|
-
get refreshToken() {
|
|
86
|
-
return this.data.refreshToken;
|
|
87
|
-
}
|
|
88
109
|
}
|
|
89
110
|
|
|
90
111
|
export default CurrentAccount;
|
package/src/Account/index.ts
CHANGED
|
@@ -14,7 +14,6 @@ import { Wallet, pbkdf2, toUtf8Bytes, Signature, parseEther } from 'ethers';
|
|
|
14
14
|
import { ApiError, ApiReponse } from '../ApiClient';
|
|
15
15
|
import CurrentAccount from './CurrentAccount';
|
|
16
16
|
import { SupernetType } from '../ApiClient/WebSocketClient/types';
|
|
17
|
-
import { AuthUpdatedEvent, Tokens } from '../lib/AuthManager';
|
|
18
17
|
|
|
19
18
|
/**
|
|
20
19
|
* Account API methods that let you interact with the user's account.
|
|
@@ -35,7 +34,6 @@ class AccountApi extends ApiGroup {
|
|
|
35
34
|
this.client.socket.on('balanceUpdate', this.handleBalanceUpdate.bind(this));
|
|
36
35
|
this.client.on('connected', this.handleServerConnected.bind(this));
|
|
37
36
|
this.client.on('disconnected', this.handleServerDisconnected.bind(this));
|
|
38
|
-
this.client.auth.on('updated', this.handleAuthUpdated.bind(this));
|
|
39
37
|
}
|
|
40
38
|
|
|
41
39
|
private handleBalanceUpdate(data: BalanceData) {
|
|
@@ -53,14 +51,6 @@ class AccountApi extends ApiGroup {
|
|
|
53
51
|
this.currentAccount._clear();
|
|
54
52
|
}
|
|
55
53
|
|
|
56
|
-
private handleAuthUpdated({ refreshToken, token, walletAddress }: AuthUpdatedEvent) {
|
|
57
|
-
if (!refreshToken) {
|
|
58
|
-
this.currentAccount._clear();
|
|
59
|
-
} else {
|
|
60
|
-
this.currentAccount._update({ walletAddress, token, refreshToken });
|
|
61
|
-
}
|
|
62
|
-
}
|
|
63
|
-
|
|
64
54
|
private async getNonce(walletAddress: string): Promise<string> {
|
|
65
55
|
const res = await this.client.rest.post<ApiReponse<Nonce>>('/v1/account/nonce', {
|
|
66
56
|
walletAddress
|
|
@@ -121,12 +111,12 @@ class AccountApi extends ApiGroup {
|
|
|
121
111
|
referralCode,
|
|
122
112
|
signature
|
|
123
113
|
});
|
|
124
|
-
|
|
114
|
+
this.setToken(username, res.data.token);
|
|
125
115
|
return res.data;
|
|
126
116
|
}
|
|
127
117
|
|
|
128
118
|
/**
|
|
129
|
-
* Restore session with username and
|
|
119
|
+
* Restore session with username and access token.
|
|
130
120
|
*
|
|
131
121
|
* You can save access token that you get from the login method and restore the session with this method.
|
|
132
122
|
*
|
|
@@ -148,13 +138,13 @@ class AccountApi extends ApiGroup {
|
|
|
148
138
|
* ```
|
|
149
139
|
*
|
|
150
140
|
* @param username
|
|
151
|
-
* @param
|
|
141
|
+
* @param token
|
|
152
142
|
*/
|
|
153
|
-
|
|
154
|
-
|
|
143
|
+
setToken(username: string, token: string): void {
|
|
144
|
+
this.client.authenticate(token);
|
|
155
145
|
this.currentAccount._update({
|
|
156
|
-
|
|
157
|
-
|
|
146
|
+
token,
|
|
147
|
+
username
|
|
158
148
|
});
|
|
159
149
|
}
|
|
160
150
|
|
|
@@ -181,7 +171,7 @@ class AccountApi extends ApiGroup {
|
|
|
181
171
|
walletAddress: wallet.address,
|
|
182
172
|
signature
|
|
183
173
|
});
|
|
184
|
-
|
|
174
|
+
this.setToken(username, res.data.token);
|
|
185
175
|
return res.data;
|
|
186
176
|
}
|
|
187
177
|
|
package/src/Account/types.ts
CHANGED
|
@@ -1,15 +1,13 @@
|
|
|
1
1
|
import { MessageType, SocketMessageMap } from './messages';
|
|
2
2
|
import { SocketEventMap } from './events';
|
|
3
|
-
import RestClient from '../../lib/RestClient';
|
|
3
|
+
import RestClient, { AuthData } from '../../lib/RestClient';
|
|
4
4
|
import { SupernetType } from './types';
|
|
5
5
|
import WebSocket, { CloseEvent, ErrorEvent, MessageEvent } from 'isomorphic-ws';
|
|
6
6
|
import { base64Decode, base64Encode } from '../../lib/base64';
|
|
7
7
|
import isNodejs from '../../lib/isNodejs';
|
|
8
|
+
import Cookie from 'js-cookie';
|
|
8
9
|
import { LIB_VERSION } from '../../version';
|
|
9
10
|
import { Logger } from '../../lib/DefaultLogger';
|
|
10
|
-
import AuthManager from '../../lib/AuthManager';
|
|
11
|
-
|
|
12
|
-
const PROTOCOL_VERSION = '0.4.3';
|
|
13
11
|
|
|
14
12
|
const PING_INTERVAL = 15000;
|
|
15
13
|
|
|
@@ -20,23 +18,34 @@ class WebSocketClient extends RestClient<SocketEventMap> {
|
|
|
20
18
|
private _supernetType: SupernetType;
|
|
21
19
|
private _pingInterval: NodeJS.Timeout | null = null;
|
|
22
20
|
|
|
23
|
-
constructor(
|
|
24
|
-
baseUrl: string,
|
|
25
|
-
auth: AuthManager,
|
|
26
|
-
appId: string,
|
|
27
|
-
supernetType: SupernetType,
|
|
28
|
-
logger: Logger
|
|
29
|
-
) {
|
|
21
|
+
constructor(baseUrl: string, appId: string, supernetType: SupernetType, logger: Logger) {
|
|
30
22
|
const _baseUrl = new URL(baseUrl);
|
|
31
23
|
if (_baseUrl.protocol === 'wss:') {
|
|
32
24
|
_baseUrl.protocol = 'https:';
|
|
33
25
|
}
|
|
34
|
-
super(_baseUrl.toString(),
|
|
26
|
+
super(_baseUrl.toString(), logger);
|
|
35
27
|
this.appId = appId;
|
|
36
28
|
this.baseUrl = _baseUrl.toString();
|
|
37
29
|
this._supernetType = supernetType;
|
|
38
30
|
}
|
|
39
31
|
|
|
32
|
+
set auth(auth: AuthData | null) {
|
|
33
|
+
//In browser, set the cookie
|
|
34
|
+
if (!isNodejs) {
|
|
35
|
+
if (auth) {
|
|
36
|
+
Cookie.set('authorization', auth.token, {
|
|
37
|
+
domain: '.sogni.ai',
|
|
38
|
+
expires: 1
|
|
39
|
+
});
|
|
40
|
+
} else {
|
|
41
|
+
Cookie.remove('authorization', {
|
|
42
|
+
domain: '.sogni.ai'
|
|
43
|
+
});
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
this._auth = auth;
|
|
47
|
+
}
|
|
48
|
+
|
|
40
49
|
get supernetType(): SupernetType {
|
|
41
50
|
return this._supernetType;
|
|
42
51
|
}
|
|
@@ -45,11 +54,11 @@ class WebSocketClient extends RestClient<SocketEventMap> {
|
|
|
45
54
|
return !!this.socket;
|
|
46
55
|
}
|
|
47
56
|
|
|
48
|
-
|
|
57
|
+
connect() {
|
|
49
58
|
if (this.socket) {
|
|
50
59
|
this.disconnect();
|
|
51
60
|
}
|
|
52
|
-
const userAgent = `Sogni/${
|
|
61
|
+
const userAgent = `Sogni/${LIB_VERSION} (sogni-client)`;
|
|
53
62
|
const url = new URL(this.baseUrl);
|
|
54
63
|
url.protocol = 'wss:';
|
|
55
64
|
url.searchParams.set('appId', this.appId);
|
|
@@ -62,7 +71,7 @@ class WebSocketClient extends RestClient<SocketEventMap> {
|
|
|
62
71
|
if (isNodejs) {
|
|
63
72
|
params = {
|
|
64
73
|
headers: {
|
|
65
|
-
Authorization:
|
|
74
|
+
Authorization: this._auth?.token,
|
|
66
75
|
'User-Agent': userAgent
|
|
67
76
|
}
|
|
68
77
|
};
|
|
@@ -105,7 +114,7 @@ class WebSocketClient extends RestClient<SocketEventMap> {
|
|
|
105
114
|
}
|
|
106
115
|
|
|
107
116
|
switchNetwork(supernetType: SupernetType): Promise<SupernetType> {
|
|
108
|
-
return new Promise<SupernetType>(async (resolve) => {
|
|
117
|
+
return new Promise<SupernetType>(async (resolve, reject) => {
|
|
109
118
|
this.once('changeNetwork', ({ network }) => {
|
|
110
119
|
this._supernetType = network;
|
|
111
120
|
resolve(network);
|
package/src/ApiClient/index.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import RestClient from '../lib/RestClient';
|
|
2
2
|
import WebSocketClient from './WebSocketClient';
|
|
3
|
+
import { jwtDecode } from 'jwt-decode';
|
|
3
4
|
import TypedEventEmitter from '../lib/TypedEventEmitter';
|
|
4
5
|
import { ApiClientEvents } from './events';
|
|
5
6
|
import { ServerConnectData, ServerDisconnectData } from './WebSocketClient/events';
|
|
@@ -7,7 +8,6 @@ import { isNotRecoverable } from './WebSocketClient/ErrorCode';
|
|
|
7
8
|
import { JSONValue } from '../types/json';
|
|
8
9
|
import { SupernetType } from './WebSocketClient/types';
|
|
9
10
|
import { Logger } from '../lib/DefaultLogger';
|
|
10
|
-
import AuthManager, { Tokens } from '../lib/AuthManager';
|
|
11
11
|
|
|
12
12
|
const WS_RECONNECT_ATTEMPTS = 5;
|
|
13
13
|
|
|
@@ -33,12 +33,21 @@ export class ApiError extends Error {
|
|
|
33
33
|
}
|
|
34
34
|
}
|
|
35
35
|
|
|
36
|
+
/**
|
|
37
|
+
* @inline
|
|
38
|
+
*/
|
|
39
|
+
interface AuthData {
|
|
40
|
+
token: string;
|
|
41
|
+
walletAddress: string;
|
|
42
|
+
expiresAt: Date;
|
|
43
|
+
}
|
|
44
|
+
|
|
36
45
|
class ApiClient extends TypedEventEmitter<ApiClientEvents> {
|
|
37
46
|
readonly appId: string;
|
|
38
47
|
readonly logger: Logger;
|
|
39
48
|
private _rest: RestClient;
|
|
40
49
|
private _socket: WebSocketClient;
|
|
41
|
-
private _auth:
|
|
50
|
+
private _auth: AuthData | null = null;
|
|
42
51
|
private _reconnectAttempts = WS_RECONNECT_ATTEMPTS;
|
|
43
52
|
|
|
44
53
|
constructor(
|
|
@@ -51,21 +60,18 @@ class ApiClient extends TypedEventEmitter<ApiClientEvents> {
|
|
|
51
60
|
super();
|
|
52
61
|
this.appId = appId;
|
|
53
62
|
this.logger = logger;
|
|
54
|
-
this.
|
|
55
|
-
this.
|
|
56
|
-
this._socket = new WebSocketClient(socketUrl, this._auth, appId, networkType, logger);
|
|
57
|
-
|
|
58
|
-
this._auth.on('refreshFailed', this.handleRefreshFailed.bind(this));
|
|
63
|
+
this._rest = new RestClient(baseUrl, logger);
|
|
64
|
+
this._socket = new WebSocketClient(socketUrl, appId, networkType, logger);
|
|
59
65
|
this._socket.on('connected', this.handleSocketConnect.bind(this));
|
|
60
66
|
this._socket.on('disconnected', this.handleSocketDisconnect.bind(this));
|
|
61
67
|
}
|
|
62
68
|
|
|
63
69
|
get isAuthenticated(): boolean {
|
|
64
|
-
return this.
|
|
70
|
+
return !!this._auth && this._auth.expiresAt > new Date();
|
|
65
71
|
}
|
|
66
72
|
|
|
67
|
-
get auth():
|
|
68
|
-
return this._auth;
|
|
73
|
+
get auth(): AuthData | null {
|
|
74
|
+
return this._auth && this._auth.expiresAt > new Date() ? this._auth : null;
|
|
69
75
|
}
|
|
70
76
|
|
|
71
77
|
get socket(): WebSocketClient {
|
|
@@ -76,13 +82,20 @@ class ApiClient extends TypedEventEmitter<ApiClientEvents> {
|
|
|
76
82
|
return this._rest;
|
|
77
83
|
}
|
|
78
84
|
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
85
|
+
authenticate(token: string) {
|
|
86
|
+
const decoded = jwtDecode<{ addr: string; env: string; iat: number; exp: number }>(token);
|
|
87
|
+
this._auth = {
|
|
88
|
+
token,
|
|
89
|
+
walletAddress: decoded.addr,
|
|
90
|
+
expiresAt: new Date(decoded.exp * 1000)
|
|
91
|
+
};
|
|
92
|
+
this.rest.auth = { token };
|
|
93
|
+
this.socket.auth = { token };
|
|
94
|
+
this.socket.connect();
|
|
82
95
|
}
|
|
83
96
|
|
|
84
97
|
removeAuth() {
|
|
85
|
-
this.
|
|
98
|
+
this._auth = null;
|
|
86
99
|
this.socket.disconnect();
|
|
87
100
|
}
|
|
88
101
|
|
|
@@ -99,7 +112,6 @@ class ApiClient extends TypedEventEmitter<ApiClientEvents> {
|
|
|
99
112
|
return;
|
|
100
113
|
}
|
|
101
114
|
if (this._reconnectAttempts <= 0) {
|
|
102
|
-
this.removeAuth();
|
|
103
115
|
this.emit('disconnected', data);
|
|
104
116
|
this._reconnectAttempts = WS_RECONNECT_ATTEMPTS;
|
|
105
117
|
return;
|
|
@@ -107,10 +119,6 @@ class ApiClient extends TypedEventEmitter<ApiClientEvents> {
|
|
|
107
119
|
this._reconnectAttempts--;
|
|
108
120
|
setTimeout(() => this.socket.connect(), 1000);
|
|
109
121
|
}
|
|
110
|
-
|
|
111
|
-
handleRefreshFailed() {
|
|
112
|
-
this.removeAuth();
|
|
113
|
-
}
|
|
114
122
|
}
|
|
115
123
|
|
|
116
124
|
export default ApiClient;
|
|
@@ -84,8 +84,7 @@ function createJobRequestMessage(id: string, params: ProjectParams) {
|
|
|
84
84
|
strengthIsEnabled: !!params.startingImage,
|
|
85
85
|
strength: !!params.startingImage
|
|
86
86
|
? 1 - (Number(params.startingImageStrength) || 0.5)
|
|
87
|
-
: undefined
|
|
88
|
-
sizePreset: params.sizePreset
|
|
87
|
+
: undefined
|
|
89
88
|
}
|
|
90
89
|
],
|
|
91
90
|
previews: params.numberOfPreviews || 0,
|