comty.js 0.46.1 → 0.47.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.
@@ -1,6 +1,5 @@
1
1
  "use strict";Object.defineProperty(exports, "__esModule", {value: true}); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } function _nullishCoalesce(lhs, rhsFn) { if (lhs != null) { return lhs; } else { return rhsFn(); } }var _handleBeforeRequest = require('../helpers/handleBeforeRequest'); var _handleBeforeRequest2 = _interopRequireDefault(_handleBeforeRequest);
2
2
  var _handleAfterRequest = require('../helpers/handleAfterRequest'); var _handleAfterRequest2 = _interopRequireDefault(_handleAfterRequest);
3
- var _session = require('../models/session'); var _session2 = _interopRequireDefault(_session);
4
3
 
5
4
  exports. default = async (
6
5
  request = {
@@ -30,14 +29,6 @@ exports. default = async (
30
29
  let result = null
31
30
 
32
31
  const makeRequest = async () => {
33
- const sessionToken = await _session2.default.token
34
-
35
- if (sessionToken) {
36
- request.headers["Authorization"] = `${globalThis.isServerMode ? "Server" : "Bearer"} ${sessionToken}`
37
- } else {
38
- console.warn("Making a request with no session token")
39
- }
40
-
41
32
  const _result = await instance(request, ...args)
42
33
  .catch((error) => {
43
34
  return error
@@ -1,7 +1,8 @@
1
1
  "use strict";Object.defineProperty(exports, "__esModule", {value: true}); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } function _optionalChain(ops) { let lastAccessLHS = undefined; let value = ops[0]; let i = 1; while (i < ops.length) { const op = ops[i]; const fn = ops[i + 1]; i += 2; if ((op === 'optionalAccess' || op === 'optionalCall') && value == null) { return undefined; } if (op === 'access' || op === 'optionalAccess') { lastAccessLHS = value; value = fn(value); } else if (op === 'call' || op === 'optionalCall') { value = fn((...args) => value.call(lastAccessLHS, ...args)); lastAccessLHS = undefined; } } return value; }var _session = require('../models/session'); var _session2 = _interopRequireDefault(_session);
2
2
  var _request = require('../handlers/request'); var _request2 = _interopRequireDefault(_request);
3
+ var _ = require('../');
3
4
 
4
- exports. default = async (refreshToken) =>{
5
+ exports. default = async (refreshToken) => {
5
6
  __comty_shared_state.eventBus.emit("session.expiredExceptionEvent", refreshToken)
6
7
 
7
8
  __comty_shared_state.onExpiredExceptionEvent = true
@@ -25,7 +26,7 @@ exports. default = async (refreshToken) =>{
25
26
  return __comty_shared_state.eventBus.emit("session.invalid", "Failed to regenerate token")
26
27
  }
27
28
 
28
- if (!_optionalChain([response, 'access', _ => _.data, 'optionalAccess', _2 => _2.token])) {
29
+ if (!_optionalChain([response, 'access', _3 => _3.data, 'optionalAccess', _4 => _4.token])) {
29
30
  return __comty_shared_state.eventBus.emit("session.invalid", "Failed to regenerate token, invalid server response.")
30
31
  }
31
32
 
@@ -36,4 +37,7 @@ exports. default = async (refreshToken) =>{
36
37
 
37
38
  // emit event
38
39
  __comty_shared_state.eventBus.emit("session.regenerated")
40
+
41
+ // reconnect websockets
42
+ _.reconnectWebsockets.call(void 0, )
39
43
  }
package/dist/index.js CHANGED
@@ -24,12 +24,118 @@ if (globalThis.isServerMode) {
24
24
  }
25
25
  }
26
26
 
27
+ async function createWebsockets() {
28
+ const instances = globalThis.__comty_shared_state.wsInstances
29
+
30
+ for (let [key, instance] of Object.entries(instances)) {
31
+ if (instance.connected) {
32
+ // disconnect first
33
+ instance.disconnect()
34
+ }
35
+
36
+ // remove current listeners
37
+ instance.removeAllListeners()
38
+
39
+ delete globalThis.__comty_shared_state.wsInstances[key]
40
+ }
41
+
42
+ for (let [key, remote] of Object.entries(_remotes2.default)) {
43
+ if (!remote.hasWebsocket) {
44
+ continue
45
+ }
46
+
47
+ let opts = {
48
+ transports: ["websocket"],
49
+ autoConnect: _nullishCoalesce(remote.autoConnect, () => ( true)),
50
+ ..._nullishCoalesce(remote.wsParams, () => ( {})),
51
+ }
52
+
53
+ if (remote.noAuth !== true) {
54
+ opts.auth = {
55
+ token: _session2.default.token,
56
+ }
57
+ }
58
+
59
+ globalThis.__comty_shared_state.wsInstances[key] = _socketioclient.io.call(void 0, _nullishCoalesce(remote.wsOrigin, () => ( remote.origin)), opts)
60
+ }
61
+
62
+ // regsister events
63
+ for (let [key, instance] of Object.entries(instances)) {
64
+ instance.on("connect", () => {
65
+ console.debug(`[WS-API][${key}] Connected`)
66
+
67
+ if (_remotes2.default[key].useClassicAuth && _remotes2.default[key].noAuth !== true) {
68
+ // try to auth
69
+ instance.emit("authenticate", {
70
+ token: _session2.default.token,
71
+ })
72
+ }
73
+
74
+ globalThis.__comty_shared_state.eventBus.emit(`${key}:connected`)
75
+ })
76
+
77
+ instance.on("disconnect", () => {
78
+ console.debug(`[WS-API][${key}] Disconnected`)
79
+
80
+ globalThis.__comty_shared_state.eventBus.emit(`${key}:disconnected`)
81
+ })
82
+
83
+ instance.on("error", (error) => {
84
+ console.error(`[WS-API][${key}] Error`, error)
85
+
86
+ globalThis.__comty_shared_state.eventBus.emit(`${key}:error`, error)
87
+ })
88
+
89
+ instance.onAny((event, ...args) => {
90
+ console.debug(`[WS-API][${key}] Event (${event})`, ...args)
91
+
92
+ globalThis.__comty_shared_state.eventBus.emit(`${key}:${event}`, ...args)
93
+ })
94
+ }
95
+ } exports.createWebsockets = createWebsockets;
96
+
97
+ async function reconnectWebsockets({ force = false } = {}) {
98
+ const instances = globalThis.__comty_shared_state.wsInstances
99
+
100
+ for (let [key, instance] of Object.entries(instances)) {
101
+ if (instance.connected) {
102
+ if (!instance.auth) {
103
+ instance.disconnect()
104
+
105
+ instance.auth = {
106
+ token: _session2.default.token,
107
+ }
108
+
109
+ instance.connect()
110
+ continue
111
+ }
112
+
113
+ if (!force) {
114
+ instance.emit("reauthenticate", {
115
+ token: _session2.default.token,
116
+ })
117
+
118
+ continue
119
+ }
120
+
121
+ // disconnect first
122
+ instance.disconnect()
123
+ }
124
+
125
+ if (_remotes2.default[key].noAuth !== true) {
126
+ instance.auth = {
127
+ token: _session2.default.token,
128
+ }
129
+ }
130
+
131
+ instance.connect()
132
+ }
133
+ } exports.reconnectWebsockets = reconnectWebsockets;
134
+
27
135
  function createClient({
28
136
  accessKey = null,
29
137
  privateKey = null,
30
138
  enableWs = false,
31
- wsEvents = Object(),
32
- wsParams = Object(),
33
139
  } = {}) {
34
140
  const sharedState = globalThis.__comty_shared_state = {
35
141
  onExpiredExceptionEvent: false,
@@ -53,65 +159,31 @@ if (globalThis.isServerMode) {
53
159
  for (const [key, remote] of Object.entries(_remotes2.default)) {
54
160
  sharedState.instances[key] = _axios2.default.create({
55
161
  baseURL: remote.origin,
162
+ headers: {
163
+ "Content-Type": "application/json",
164
+ }
56
165
  })
57
166
 
58
- if (enableWs && remote.hasWebsocket) {
59
- let opts = {
60
- transports: ["websocket"],
61
- autoConnect: _nullishCoalesce(remote.autoConnect, () => ( true)),
62
- ..._nullishCoalesce(remote.wsParams, () => ( {})),
63
- }
167
+ // create a interceptor to attach the token every request
168
+ sharedState.instances[key].interceptors.request.use((config) => {
169
+ // check if current request has no Authorization header, if so, attach the token
170
+ if (!config.headers["Authorization"]) {
171
+ const sessionToken = _session2.default.token
64
172
 
65
- if (wsParams[key]) {
66
- if (typeof wsParams[key] === "function") {
67
- opts = wsParams[key](opts)
173
+ if (sessionToken) {
174
+ config.headers["Authorization"] = `${globalThis.isServerMode ? "Server" : "Bearer"} ${sessionToken}`
68
175
  } else {
69
- opts = {
70
- ...opts,
71
- ...wsParams[key],
72
- }
176
+ console.warn("Making a request with no session token")
73
177
  }
74
178
  }
75
179
 
76
- sharedState.wsInstances[key] = _socketioclient.io.call(void 0, _nullishCoalesce(remote.wsOrigin, () => ( remote.origin)), opts)
77
- }
78
- }
79
-
80
- // register ws events
81
- Object.keys(sharedState.wsInstances).forEach((key) => {
82
- const ws = sharedState.wsInstances[key]
83
-
84
- ws.on("connect", () => {
85
- console.debug(`[WS-API][${key}] Connected`)
86
-
87
- if (_remotes2.default[key].useClassicAuth) {
88
- // try to auth
89
- ws.emit("authenticate", {
90
- token: _session2.default.token,
91
- })
92
- }
180
+ return config
93
181
  })
182
+ }
94
183
 
95
- ws.on("disconnect", () => {
96
- console.log(`[WS-API][${key}] Disconnected`)
97
- })
98
-
99
- ws.on("error", (error) => {
100
- console.error(`[WS-API][${key}] Error`, error)
101
- })
102
-
103
- ws.onAny((event, ...args) => {
104
- console.debug(`[WS-API][${key}] Event recived`, event, ...args)
105
- })
106
-
107
- const customEvents = wsEvents[key]
108
-
109
- if (customEvents) {
110
- for (const [eventName, eventHandler] of Object.entries(customEvents)) {
111
- ws.on(eventName, eventHandler)
112
- }
113
- }
114
- })
184
+ if (enableWs) {
185
+ createWebsockets()
186
+ }
115
187
 
116
188
  return sharedState
117
189
  } exports.default = createClient;
@@ -66,17 +66,4 @@ var _withSettings = require('../../helpers/withSettings'); var _withSettings2 =
66
66
 
67
67
  return data
68
68
  }}
69
-
70
- static __initStatic6() {this.search = async (keywords, params = {}) => {
71
- const { data } = await _request2.default.call(void 0, {
72
- method: "GET",
73
- url: `/search`,
74
- params: {
75
- keywords: keywords,
76
- params: params
77
- }
78
- })
79
-
80
- return data
81
- }}
82
- } FeedModel.__initStatic(); FeedModel.__initStatic2(); FeedModel.__initStatic3(); FeedModel.__initStatic4(); FeedModel.__initStatic5(); FeedModel.__initStatic6(); exports.default = FeedModel;
69
+ } FeedModel.__initStatic(); FeedModel.__initStatic2(); FeedModel.__initStatic3(); FeedModel.__initStatic4(); FeedModel.__initStatic5(); exports.default = FeedModel;
@@ -1,48 +1,95 @@
1
1
  "use strict";Object.defineProperty(exports, "__esModule", {value: true}); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }var _request = require('../../handlers/request'); var _request2 = _interopRequireDefault(_request);
2
2
 
3
3
  class PlaylistsModel {
4
+ static get api_instance() {
5
+ return globalThis.__comty_shared_state.instances["music"]
6
+ }
7
+
4
8
  static __initStatic() {this.putPlaylist = async (payload) => {
5
9
  if (!payload) {
6
10
  throw new Error("Payload is required")
7
11
  }
8
12
 
9
13
  const { data } = await _request2.default.call(void 0, {
14
+ instance: PlaylistsModel.api_instance,
10
15
  method: "PUT",
11
- url: `/playlist`,
16
+ url: `/playlists/playlist`,
12
17
  data: payload,
13
18
  })
14
19
 
15
20
  return data
16
21
  }}
17
22
 
18
- static __initStatic2() {this.getPlaylist = async (id) => {
23
+ static __initStatic2() {this.deletePlaylist = async (id) => {
24
+ if (!id) {
25
+ throw new Error("ID is required")
26
+ }
27
+
28
+ const { data } = await _request2.default.call(void 0, {
29
+ instance: PlaylistsModel.api_instance,
30
+ method: "DELETE",
31
+ url: `/playlist/${id}`,
32
+ })
33
+
34
+ return data
35
+ }}
36
+
37
+ static __initStatic3() {this.getTrack = async (id) => {
19
38
  const { data } = await _request2.default.call(void 0, {
39
+ instance: PlaylistsModel.api_instance,
20
40
  method: "GET",
21
- url: `/playlist/data/${id}`,
41
+ url: `/tracks/${id}/data`,
22
42
  })
23
43
 
24
44
  return data
25
45
  }}
26
46
 
27
- static __initStatic3() {this.getMyReleases = async () => {
47
+ static __initStatic4() {this.getTracks = async (ids) => {
28
48
  const { data } = await _request2.default.call(void 0, {
49
+ instance: PlaylistsModel.api_instance,
29
50
  method: "GET",
30
- url: `/playlist/self`,
51
+ url: `/tracks/many`,
52
+ params: {
53
+ ids,
54
+ }
31
55
  })
32
56
 
33
57
  return data
34
58
  }}
35
59
 
36
- static __initStatic4() {this.deletePlaylist = async (id) => {
37
- if (!id) {
38
- throw new Error("ID is required")
39
- }
60
+ static __initStatic5() {this.getPlaylist = async (id) => {
61
+ const { data } = await _request2.default.call(void 0, {
62
+ instance: PlaylistsModel.api_instance,
63
+ method: "GET",
64
+ url: `/playlists/${id}/data`,
65
+ })
66
+
67
+ return data
68
+ }}
40
69
 
70
+ static __initStatic6() {this.search = async (keywords) => {
41
71
  const { data } = await _request2.default.call(void 0, {
42
- method: "DELETE",
43
- url: `/playlist/${id}`,
72
+ instance: PlaylistsModel.api_instance,
73
+ method: "GET",
74
+ url: `/playlists/search`,
75
+ params: {
76
+ keywords,
77
+ }
78
+ })
79
+
80
+ return data
81
+ }}
82
+
83
+ static __initStatic7() {this.getMyReleases = async (keywords) => {
84
+ const { data } = await _request2.default.call(void 0, {
85
+ instance: PlaylistsModel.api_instance,
86
+ method: "GET",
87
+ url: `/playlists/self`,
88
+ params: {
89
+ keywords,
90
+ }
44
91
  })
45
92
 
46
93
  return data
47
94
  }}
48
- } PlaylistsModel.__initStatic(); PlaylistsModel.__initStatic2(); PlaylistsModel.__initStatic3(); PlaylistsModel.__initStatic4(); exports.default = PlaylistsModel;
95
+ } PlaylistsModel.__initStatic(); PlaylistsModel.__initStatic2(); PlaylistsModel.__initStatic3(); PlaylistsModel.__initStatic4(); PlaylistsModel.__initStatic5(); PlaylistsModel.__initStatic6(); PlaylistsModel.__initStatic7(); exports.default = PlaylistsModel;
@@ -0,0 +1,26 @@
1
+ "use strict";Object.defineProperty(exports, "__esModule", {value: true}); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }var _request = require('../../handlers/request'); var _request2 = _interopRequireDefault(_request);
2
+
3
+ class Search {
4
+ static __initStatic() {this.search = async (keywords, params = {}) => {
5
+ const { data } = await _request2.default.call(void 0, {
6
+ method: "GET",
7
+ url: `/search`,
8
+ params: {
9
+ keywords: keywords,
10
+ params: params
11
+ }
12
+ })
13
+
14
+ return data
15
+ }}
16
+
17
+ static async quickSearch(params) {
18
+ const response = await _request2.default.call(void 0, {
19
+ method: "GET",
20
+ url: "/search/quick",
21
+ params: params
22
+ })
23
+
24
+ return response.data
25
+ }
26
+ } Search.__initStatic(); exports.default = Search;
package/dist/remotes.js CHANGED
@@ -16,12 +16,14 @@ const envOrigins = {
16
16
  chat: `http://${getCurrentHostname()}:3020`,
17
17
  livestreaming: `http://${getCurrentHostname()}:3030`,
18
18
  marketplace: `http://${getCurrentHostname()}:3040`,
19
+ music: `http://${getCurrentHostname()}:3050`,
19
20
  },
20
21
  "production": {
21
22
  default: "https://api.comty.app",
22
23
  chat: `https://chat_api.comty.app`,
23
24
  livestreaming: `https://livestreaming_api.comty.app`,
24
25
  marketplace: `https://marketplace_api.comty.app`,
26
+ music: `https://music_api.comty.app`,
25
27
  }
26
28
  }
27
29
 
@@ -36,6 +38,10 @@ exports. default = {
36
38
  origin: composeRemote("chat"),
37
39
  hasWebsocket: true,
38
40
  },
41
+ music: {
42
+ origin: composeRemote("music"),
43
+ hasWebsocket: true,
44
+ },
39
45
  livestreaming: {
40
46
  origin: composeRemote("livestreaming"),
41
47
  },
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "comty.js",
3
- "version": "0.46.1",
3
+ "version": "0.47.0",
4
4
  "main": "./dist/index.js",
5
5
  "author": "RageStudio <support@ragestudio.net>",
6
6
  "scripts": {
@@ -16,7 +16,7 @@
16
16
  "js-cookie": "^3.0.5",
17
17
  "jsonwebtoken": "^9.0.0",
18
18
  "jwt-decode": "^3.1.2",
19
- "linebridge": "^0.15.12"
19
+ "socket.io-client": "^4.6.1"
20
20
  },
21
21
  "devDependencies": {
22
22
  "corenode": "^0.28.26"