comty.js 0.46.0 → 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
@@ -5,7 +5,7 @@ var _socketioclient = require('socket.io-client');
5
5
 
6
6
  var _remotes = require('./remotes'); var _remotes2 = _interopRequireDefault(_remotes);
7
7
 
8
-
8
+ //import request from "./handlers/request"
9
9
  var _withStorage = require('./helpers/withStorage'); var _withStorage2 = _interopRequireDefault(_withStorage);
10
10
 
11
11
  var _session = require('./models/session'); var _session2 = _interopRequireDefault(_session);
@@ -24,11 +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
- wsEvents = Object(),
29
- useWs = false,
30
136
  accessKey = null,
31
137
  privateKey = null,
138
+ enableWs = false,
32
139
  } = {}) {
33
140
  const sharedState = globalThis.__comty_shared_state = {
34
141
  onExpiredExceptionEvent: false,
@@ -52,52 +159,31 @@ if (globalThis.isServerMode) {
52
159
  for (const [key, remote] of Object.entries(_remotes2.default)) {
53
160
  sharedState.instances[key] = _axios2.default.create({
54
161
  baseURL: remote.origin,
55
- })
56
-
57
- if (useWs && remote.hasWebsocket) {
58
- sharedState.wsInstances[key] = _socketioclient.io.call(void 0, _nullishCoalesce(remote.wsOrigin, () => ( remote.origin)), {
59
- transports: ["websocket"],
60
- autoConnect: true,
61
- ..._nullishCoalesce(remote.wsParams, () => ( {})),
62
- })
63
- }
64
- }
65
-
66
- // register ws events
67
- Object.keys(sharedState.wsInstances).forEach((key) => {
68
- const ws = sharedState.wsInstances[key]
69
-
70
- ws.on("connect", () => {
71
- console.log(`[WS-API][${key}] Connected`)
72
-
73
- if (_remotes2.default[key].needsAuth) {
74
- // try to auth
75
- ws.emit("authenticate", {
76
- token: _session2.default.token,
77
- })
162
+ headers: {
163
+ "Content-Type": "application/json",
78
164
  }
79
165
  })
80
166
 
81
- ws.on("disconnect", () => {
82
- console.log(`[WS-API][${key}] Disconnected`)
83
- })
84
-
85
- ws.on("error", (error) => {
86
- console.error(`[WS-API][${key}] Error`, error)
87
- })
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
172
+
173
+ if (sessionToken) {
174
+ config.headers["Authorization"] = `${globalThis.isServerMode ? "Server" : "Bearer"} ${sessionToken}`
175
+ } else {
176
+ console.warn("Making a request with no session token")
177
+ }
178
+ }
88
179
 
89
- ws.onAny((event, ...args) => {
90
- console.log(`[WS-API][${key}] Event recived`, event, ...args)
180
+ return config
91
181
  })
182
+ }
92
183
 
93
- const customEvents = wsEvents[key]
94
-
95
- if (customEvents) {
96
- for (const [eventName, eventHandler] of Object.entries(customEvents)) {
97
- ws.on(eventName, eventHandler)
98
- }
99
- }
100
- })
184
+ if (enableWs) {
185
+ createWebsockets()
186
+ }
101
187
 
102
188
  return sharedState
103
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;
@@ -50,7 +50,7 @@ var _withSettings = require('../../helpers/withSettings'); var _withSettings2 =
50
50
  throw new Error("Post ID and/or comment are required")
51
51
  }
52
52
 
53
- const request = await request({
53
+ const { data } = await _request2.default.call(void 0, {
54
54
  method: "POST",
55
55
  url: `/comments/post/${post_id}`,
56
56
  data: {
@@ -58,7 +58,7 @@ var _withSettings = require('../../helpers/withSettings'); var _withSettings2 =
58
58
  },
59
59
  })
60
60
 
61
- return request
61
+ return data
62
62
  }}
63
63
 
64
64
  static __initStatic5() {this.deleteComment = async ({ post_id, comment_id }) => {
@@ -66,12 +66,12 @@ var _withSettings = require('../../helpers/withSettings'); var _withSettings2 =
66
66
  throw new Error("Post ID and/or comment ID are required")
67
67
  }
68
68
 
69
- const request = await request({
69
+ const { data } = await _request2.default.call(void 0, {
70
70
  method: "DELETE",
71
71
  url: `/comments/post/${post_id}/${comment_id}`,
72
72
  })
73
73
 
74
- return request
74
+ return data
75
75
  }}
76
76
 
77
77
  static __initStatic6() {this.getExplorePosts = async ({ trim, limit }) => {
@@ -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;
@@ -106,9 +106,14 @@ var _withStorage = require('../../helpers/withStorage'); var _withStorage2 = _in
106
106
  return response.data
107
107
  }}
108
108
 
109
+ // alias for validateToken method
109
110
  static __initStatic8() {this.validSession = async (token) => {
111
+ return await Session.validateToken(token)
112
+ }}
113
+
114
+ static __initStatic9() {this.validateToken = async (token) => {
110
115
  const response = await _request2.default.call(void 0, {
111
- method: "POST",
116
+ method: "post",
112
117
  url: "/session/validate",
113
118
  data: {
114
119
  token: token
@@ -118,9 +123,9 @@ var _withStorage = require('../../helpers/withStorage'); var _withStorage2 = _in
118
123
  return response.data
119
124
  }}
120
125
 
121
- static __initStatic9() {this.isCurrentTokenValid = async () => {
126
+ static __initStatic10() {this.isCurrentTokenValid = async () => {
122
127
  const health = await Session.getTokenValidation()
123
128
 
124
129
  return health.valid
125
130
  }}
126
- } Session.__initStatic(); Session.__initStatic2(); Session.__initStatic3(); Session.__initStatic4(); Session.__initStatic5(); Session.__initStatic6(); Session.__initStatic7(); Session.__initStatic8(); Session.__initStatic9(); exports.default = Session;
131
+ } Session.__initStatic(); Session.__initStatic2(); Session.__initStatic3(); Session.__initStatic4(); Session.__initStatic5(); Session.__initStatic6(); Session.__initStatic7(); Session.__initStatic8(); Session.__initStatic9(); Session.__initStatic10(); exports.default = Session;
package/dist/remotes.js CHANGED
@@ -13,15 +13,17 @@ function getCurrentHostname() {
13
13
  const envOrigins = {
14
14
  "development": {
15
15
  default: `http://${getCurrentHostname()}:3010`,
16
- messaging: `http://${getCurrentHostname()}:3020`,
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
- messaging: `https://messaging_api.comty.app`,
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
 
@@ -29,12 +31,16 @@ exports. default = {
29
31
  default: {
30
32
  origin: composeRemote("default"),
31
33
  hasWebsocket: true,
32
- needsAuth: true,
34
+ useClassicAuth: true,
35
+ autoconnect: true,
33
36
  },
34
- messaging: {
35
- origin: composeRemote("messaging"),
37
+ chat: {
38
+ origin: composeRemote("chat"),
39
+ hasWebsocket: true,
40
+ },
41
+ music: {
42
+ origin: composeRemote("music"),
36
43
  hasWebsocket: true,
37
- needsAuth: true,
38
44
  },
39
45
  livestreaming: {
40
46
  origin: composeRemote("livestreaming"),
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "comty.js",
3
- "version": "0.46.0",
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"