From fc65396fb1cc470c9c165bc563c769a5c73b7585 Mon Sep 17 00:00:00 2001 From: SrGooglo Date: Mon, 27 Feb 2023 09:15:21 +0000 Subject: [PATCH] use new api model --- .../app/constants/settings/about/index.jsx | 2 +- packages/app/src/App.jsx | 14 +- .../AdminTools/UserDataManager/index.jsx | 2 +- .../AdminTools/UserRolesManager/index.jsx | 2 +- .../app/src/components/CommentsCard/index.jsx | 9 +- .../src/components/ConnectedFriends/index.jsx | 4 +- .../FeaturedEventsAnnouncements/index.jsx | 2 +- .../src/components/ImageUploader/index.jsx | 2 +- .../app/src/components/PostCard/index.jsx | 6 +- .../app/src/components/PostCreator/index.jsx | 2 +- .../app/src/components/UploadButton/index.jsx | 2 +- .../app/src/components/UserRegister/index.jsx | 4 +- .../app/src/components/UserSelector/index.jsx | 2 +- packages/app/src/cores/api/index.js | 140 ++++++++++-------- packages/app/src/models/auth/index.js | 2 +- packages/app/src/models/feed/index.js | 8 +- packages/app/src/models/follows/index.js | 6 +- packages/app/src/models/livestream/index.js | 16 +- packages/app/src/models/music/index.js | 4 +- packages/app/src/models/playlists/index.js | 20 +-- packages/app/src/models/post/index.js | 24 +-- packages/app/src/models/session/index.js | 10 +- .../app/src/models/sync/cores/spotifyCore.js | 12 +- packages/app/src/models/sync/index.js | 2 +- packages/app/src/models/upload/index.js | 2 +- packages/app/src/models/user/index.js | 18 +-- packages/app/src/pages/account/index.jsx | 2 +- .../app/src/pages/featured-event/[id].jsx | 2 +- packages/app/src/pages/login/index.jsx | 2 +- .../pages/posts/components/explore/index.jsx | 4 +- .../src/pages/posts/components/feed/index.jsx | 4 +- 31 files changed, 166 insertions(+), 165 deletions(-) diff --git a/packages/app/constants/settings/about/index.jsx b/packages/app/constants/settings/about/index.jsx index be0c8dd1..e162b190 100644 --- a/packages/app/constants/settings/about/index.jsx +++ b/packages/app/constants/settings/about/index.jsx @@ -40,7 +40,7 @@ export default { const [serverManifest, setServerManifest] = React.useState(null) const checkServerVersion = async () => { - const serverManifest = await app.cores.api.customRequest("main") + const serverManifest = await app.cores.api.customRequest() setServerManifest(serverManifest.data) } diff --git a/packages/app/src/App.jsx b/packages/app/src/App.jsx index 5d5d28ce..95a42114 100755 --- a/packages/app/src/App.jsx +++ b/packages/app/src/App.jsx @@ -518,19 +518,7 @@ class ComtyApp extends React.Component { const initializationTasks = [ async () => { try { - // get remotes origins from config - const defaultRemotes = config.remotes - - // get storaged remotes origins - const storedRemotes = await app.cores.settings.get("remotes") ?? {} - - // mount main api bridge - await this.props.cores.ApiCore.attachBridge("main", { - origin: storedRemotes.mainApi ?? defaultRemotes.mainApi, - locked: true, - }) - - await this.props.cores.ApiCore.namespaces["main"].initialize() + await this.props.cores.ApiCore.attach() app.eventBus.emit("app.initialization.api_success") } catch (error) { diff --git a/packages/app/src/components/AdminTools/UserDataManager/index.jsx b/packages/app/src/components/AdminTools/UserDataManager/index.jsx index 102a9b80..cafb349f 100755 --- a/packages/app/src/components/AdminTools/UserDataManager/index.jsx +++ b/packages/app/src/components/AdminTools/UserDataManager/index.jsx @@ -71,7 +71,7 @@ export default class UserDataManager extends React.Component { loading: false, } - api = window.app.cores.api.withEndpoints("main") + api = window.app.cores.api.withEndpoints() componentDidMount = async () => { if (!this.props.user && this.props.userId) { diff --git a/packages/app/src/components/AdminTools/UserRolesManager/index.jsx b/packages/app/src/components/AdminTools/UserRolesManager/index.jsx index 72493e87..5410c263 100755 --- a/packages/app/src/components/AdminTools/UserRolesManager/index.jsx +++ b/packages/app/src/components/AdminTools/UserRolesManager/index.jsx @@ -12,7 +12,7 @@ export default class UserRolesManager extends React.Component { roles: null, } - api = window.app.cores.api.withEndpoints("main") + api = window.app.cores.api.withEndpoints() componentDidMount = async () => { await this.fetchRoles() diff --git a/packages/app/src/components/CommentsCard/index.jsx b/packages/app/src/components/CommentsCard/index.jsx index 42e00cab..57e638f2 100755 --- a/packages/app/src/components/CommentsCard/index.jsx +++ b/packages/app/src/components/CommentsCard/index.jsx @@ -104,12 +104,13 @@ export default (props) => { } const listenEvents = () => { - window.app.cores.api.namespaces["main"].listenEvent(`post.new.comment.${props.post_id}`, (comment) => { + app.cores.api.listenEvent(`post.new.comment.${props.post_id}`, (comment) => { setComments((comments) => { return [comment, ...comments] }) }) - window.app.cores.api.namespaces["main"].listenEvent(`post.delete.comment.${props.post_id}`, (comment_id) => { + + app.cores.api.listenEvent(`post.delete.comment.${props.post_id}`, (comment_id) => { setComments((comments) => { return comments.filter((comment) => comment._id !== comment_id) }) @@ -117,8 +118,8 @@ export default (props) => { } const unlistenEvents = () => { - window.app.cores.api.namespaces["main"].unlistenEvent(`post.new.comment.${props.post_id}`) - window.app.cores.api.namespaces["main"].unlistenEvent(`post.delete.comment.${props.post_id}`) + app.cores.api.unlistenEvent(`post.new.comment.${props.post_id}`) + app.cores.api.unlistenEvent(`post.delete.comment.${props.post_id}`) } React.useEffect(() => { diff --git a/packages/app/src/components/ConnectedFriends/index.jsx b/packages/app/src/components/ConnectedFriends/index.jsx index cfcf193d..d17c1838 100755 --- a/packages/app/src/components/ConnectedFriends/index.jsx +++ b/packages/app/src/components/ConnectedFriends/index.jsx @@ -35,12 +35,12 @@ export default (props) => { fetchConnectedFriends() for (const [event, callback] of Object.entries(wsEvents)) { - window.app.cores.api.namespaces["main"].listenEvent(event, callback) + app.cores.api.listenEvent(event, callback) } return () => { for (const [event, callback] of Object.entries(wsEvents)) { - window.app.cores.api.namespaces["main"].unlistenEvent(event, callback) + app.cores.api.unlistenEvent(event, callback) } } }, []) diff --git a/packages/app/src/components/FeaturedEventsAnnouncements/index.jsx b/packages/app/src/components/FeaturedEventsAnnouncements/index.jsx index c75edde9..dfcd3843 100755 --- a/packages/app/src/components/FeaturedEventsAnnouncements/index.jsx +++ b/packages/app/src/components/FeaturedEventsAnnouncements/index.jsx @@ -8,7 +8,7 @@ export default React.memo((props) => { const [featuredEvents, setFeaturedEvents] = React.useState([]) const fetchFeaturedEvents = React.useCallback(async () => { - let { data } = await app.cores.api.customRequest("main", { + let { data } = await app.cores.api.customRequest({ url: "/featured_events", method: "GET" }).catch((err) => { diff --git a/packages/app/src/components/ImageUploader/index.jsx b/packages/app/src/components/ImageUploader/index.jsx index 583b1840..b3a9e438 100755 --- a/packages/app/src/components/ImageUploader/index.jsx +++ b/packages/app/src/components/ImageUploader/index.jsx @@ -12,7 +12,7 @@ export default class ImageUploader extends React.Component { urlList: [], } - api = window.app.cores.api.withEndpoints("main") + api = window.app.cores.api.withEndpoints() handleChange = ({ fileList }) => { this.setState({ fileList }) diff --git a/packages/app/src/components/PostCard/index.jsx b/packages/app/src/components/PostCard/index.jsx index cc0a410b..30cf6fee 100755 --- a/packages/app/src/components/PostCard/index.jsx +++ b/packages/app/src/components/PostCard/index.jsx @@ -103,8 +103,7 @@ export default class PostCard extends React.Component { app.eventBus.on(`post.${this.state.data._id}.update`, this.onClickEdit) // first listen to post changes - //window.app.cores.api.namespaces["main"].listenEvent(`post.dataUpdate.${data._id}`, onDataUpdate) - window.app.cores.api.namespaces["main"].listenEvent(`post.${this.state.data._id}.likes.update`, this.onLikesUpdate) + app.cores.api.listenEvent(`post.${this.state.data._id}.likes.update`, this.onLikesUpdate) this.setState({ isSelf: app.cores.permissions.checkUserIdIsSelf(this.state.data.user_id), @@ -117,8 +116,7 @@ export default class PostCard extends React.Component { app.eventBus.off(`post.${this.state.data._id}.update`, this.onClickEdit) // remove the listener - //window.app.cores.api.namespaces["main"].unlistenEvent(`post.dataUpdate.${data._id}`, onDataUpdate) - window.app.cores.api.namespaces["main"].listenEvent(`post.${this.state.data._id}.likes.update`, this.onLikesUpdate) + app.cores.api.listenEvent(`post.${this.state.data._id}.likes.update`, this.onLikesUpdate) } componentDidCatch = (error, info) => { diff --git a/packages/app/src/components/PostCreator/index.jsx b/packages/app/src/components/PostCreator/index.jsx index 50d5b36d..79b3a465 100755 --- a/packages/app/src/components/PostCreator/index.jsx +++ b/packages/app/src/components/PostCreator/index.jsx @@ -18,7 +18,7 @@ const DEFAULT_POST_POLICY = { } export default (props) => { - const api = window.app.cores.api.withEndpoints("main") + const api = window.app.cores.api.withEndpoints() const creatorRef = React.useRef(null) diff --git a/packages/app/src/components/UploadButton/index.jsx b/packages/app/src/components/UploadButton/index.jsx index 27f3ad9a..6cff0574 100644 --- a/packages/app/src/components/UploadButton/index.jsx +++ b/packages/app/src/components/UploadButton/index.jsx @@ -13,7 +13,7 @@ export default (props) => { formData.append("files", req.file) - const response = await window.app.cores.api.customRequest("main", { + const response = await window.app.cores.api.customRequest( { url: "/upload", method: "POST", data: formData diff --git a/packages/app/src/components/UserRegister/index.jsx b/packages/app/src/components/UserRegister/index.jsx index 2da56771..b237bdd7 100755 --- a/packages/app/src/components/UserRegister/index.jsx +++ b/packages/app/src/components/UserRegister/index.jsx @@ -90,7 +90,7 @@ const steps = [ return } - const request = await app.cores.api.customRequest("main", { + const request = await app.cores.api.customRequest( { method: "GET", url: "/user/username-available", params: { @@ -308,7 +308,7 @@ const steps = [ const timer = setTimeout(async () => { if (!validFormat) return - const request = await app.cores.api.customRequest("main", { + const request = await app.cores.api.customRequest( { method: "GET", url: "/user/email-available", params: { diff --git a/packages/app/src/components/UserSelector/index.jsx b/packages/app/src/components/UserSelector/index.jsx index 917c2f73..b8db8bf3 100755 --- a/packages/app/src/components/UserSelector/index.jsx +++ b/packages/app/src/components/UserSelector/index.jsx @@ -14,7 +14,7 @@ export default class UserSelector extends React.Component { searchValue: null, } - api = window.app.cores.api.withEndpoints("main") + api = window.app.cores.api.withEndpoints() componentDidMount = async () => { this.toogleLoading(true) diff --git a/packages/app/src/cores/api/index.js b/packages/app/src/cores/api/index.js index 803a3622..78e06074 100755 --- a/packages/app/src/cores/api/index.js +++ b/packages/app/src/cores/api/index.js @@ -49,39 +49,54 @@ function generateWSFunctionHandler(socket, type = "listen") { export default class ApiCore extends Core { static namespace = "api" + static depends = ["settings"] excludedExpiredExceptionURL = ["/session/regenerate"] onExpiredExceptionEvent = false - namespaces = Object() + instance = null public = { - namespaces: this.namespaces, - customRequest: this.customRequest, - request: this.request, - withEndpoints: this.withEndpoints, - attachBridge: this.attachBridge, - detachBridge: this.detachBridge, - createBridge: this.createBridge, - autenticateWS: this.autenticateWS, + instance: function () { + return this.instance + }.bind(this), + customRequest: this.customRequest.bind(this), + request: this.request.bind(this), + withEndpoints: this.withEndpoints.bind(this), + attach: this.attach.bind(this), + createBridge: this.createBridge.bind(this), + autenticateWS: this.autenticateWS.bind(this), + listenEvent: this.listenEvent.bind(this), + unlistenEvent: this.unlistenEvent.bind(this), + } + + async attach() { + // get remotes origins from config + const defaultRemotes = config.remotes + + // get storaged remotes origins + const storedRemotes = await app.cores.settings.get("remotes") ?? {} + + const origin = storedRemotes.mainApi ?? defaultRemotes.mainApi + + this.instance = this.createBridge({ + origin, + }) + + await this.instance.initialize() + + console.debug(`[API] Attached to ${origin}`, this.instance) + + return this.instance } async customRequest( - namepace = undefined, payload = { method: "GET", }, ...args ) { - if (typeof namepace === "undefined") { - throw new Error("Namespace must be defined") - } - - if (typeof this.namespaces[namepace] === "undefined") { - throw new Error("Namespace not found") - } - if (typeof payload === "string") { payload = { url: payload, @@ -101,31 +116,15 @@ export default class ApiCore extends Core { console.warn("Making a request with no session token") } - return await this.namespaces[namepace].httpInterface(payload, ...args) + return await this.instance.httpInterface(payload, ...args) } - request(namespace = "main", method, endpoint, ...args) { - if (!this.namespaces[namespace]) { - throw new Error(`Namespace ${namespace} not found`) - } - - if (!this.namespaces[namespace].endpoints[method]) { - throw new Error(`Method ${method} not found`) - } - - if (!this.namespaces[namespace].endpoints[method][endpoint]) { - throw new Error(`Endpoint ${endpoint} not found`) - } - - return this.namespaces[namespace].endpoints[method][endpoint](...args) + request(method, endpoint, ...args) { + return this.instance.endpoints[method][endpoint](...args) } - withEndpoints(namespace = "main") { - if (!this.namespaces[namespace]) { - throw new Error(`Namespace ${namespace} not found`) - } - - return this.namespaces[namespace].endpoints + withEndpoints() { + return this.instance.endpoints } async handleBeforeRequest(request) { @@ -149,7 +148,7 @@ export default class ApiCore extends Core { const expiredToken = await SessionModel.token // send request to regenerate token - const response = await this.customRequest("main", { + const response = await this.customRequest({ method: "POST", url: "/session/regenerate", data: { @@ -176,14 +175,6 @@ export default class ApiCore extends Core { window.app.eventBus.emit("session.regenerated") } - attachBridge(key, params) { - return this.namespaces[key] = this.createBridge(params) - } - - detachBridge(key) { - return delete this.namespaces[key] - } - createBridge(params = {}) { const getSessionContext = async () => { const obj = {} @@ -236,51 +227,74 @@ export default class ApiCore extends Core { const bridge = new Bridge(bridgeOptions) // handle main ws onEvents - const mainWSSocket = bridge.wsInterface.sockets["main"] + const mainSocket = bridge.wsInterface.sockets["main"] - mainWSSocket.on("authenticated", () => { + mainSocket.on("authenticated", () => { console.debug("[WS] Authenticated") }) - mainWSSocket.on("authenticateFailed", (error) => { + mainSocket.on("authenticateFailed", (error) => { console.error("[WS] Authenticate Failed", error) }) - mainWSSocket.on("connect", () => { + mainSocket.on("connect", () => { if (this.ctx.eventBus) { this.ctx.eventBus.emit(`api.ws.main.connect`) } - this.autenticateWS(mainWSSocket) + console.debug("[WS] Connected, authenticating...") + + this.autenticateWS(mainSocket) }) - mainWSSocket.on("disconnect", (...context) => { + mainSocket.on("disconnect", (...context) => { if (this.ctx.eventBus) { this.ctx.eventBus.emit(`api.ws.main.disconnect`, ...context) } }) - mainWSSocket.on("connect_error", (...context) => { + mainSocket.on("connect_error", (...context) => { if (this.ctx.eventBus) { this.ctx.eventBus.emit(`api.ws.main.connect_error`, ...context) } }) - // generate functions - bridge.listenEvent = generateWSFunctionHandler(bridge.wsInterface, "listen") - bridge.unlistenEvent = generateWSFunctionHandler(bridge.wsInterface, "unlisten") + mainSocket.onAny((event, ...args) => { + console.debug(`[WS] Recived Event (${event})`, ...args) + }) + + // mainSocket.onAnyOutgoing((event, ...args) => { + // console.debug(`[WS] Sent Event (${event})`, ...args) + // }) - // return bridge return bridge } + listenEvent(event, callback) { + if (!this.instance.wsInterface) { + throw new Error("API is not attached") + } + + return this.instance.wsInterface.sockets["main"].on(event, callback) + } + + unlistenEvent(event, callback) { + if (!this.instance.wsInterface) { + throw new Error("API is not attached") + } + + return this.instance.wsInterface.sockets["main"].off(event, callback) + } + async autenticateWS(socket) { const token = await SessionModel.token - if (token) { - socket.emit("authenticate", { - token, - }) + if (!token) { + return console.error("Failed to authenticate WS, no token found") } + + socket.emit("authenticate", { + token, + }) } } \ No newline at end of file diff --git a/packages/app/src/models/auth/index.js b/packages/app/src/models/auth/index.js index 563a116e..bc5d9b99 100644 --- a/packages/app/src/models/auth/index.js +++ b/packages/app/src/models/auth/index.js @@ -2,7 +2,7 @@ import SessionModel from "../session" export default class AuthModel { static login = async (payload) => { - const response = await app.cores.api.customRequest("main", { + const response = await app.cores.api.customRequest( { method: "post", url: "/auth/login", data: { diff --git a/packages/app/src/models/feed/index.js b/packages/app/src/models/feed/index.js index b8b00292..aab7886c 100755 --- a/packages/app/src/models/feed/index.js +++ b/packages/app/src/models/feed/index.js @@ -1,6 +1,6 @@ export default class FeedModel { static get bridge() { - return window.app?.cores.api.withEndpoints("main") + return window.app?.cores.api.withEndpoints() } static async getPostsFeed({ trim, limit }) { @@ -8,7 +8,7 @@ export default class FeedModel { throw new Error("Bridge is not available") } - const { data } = await app.cores.api.customRequest("main", { + const { data } = await app.cores.api.customRequest( { method: "GET", url: `/feed/posts`, params: { @@ -25,7 +25,7 @@ export default class FeedModel { throw new Error("Bridge is not available") } - const { data } = await app.cores.api.customRequest("main", { + const { data } = await app.cores.api.customRequest( { method: "GET", url: `/feed/playlists`, params: { @@ -42,7 +42,7 @@ export default class FeedModel { throw new Error("Bridge is not available") } - const { data } = await app.cores.api.customRequest("main", { + const { data } = await app.cores.api.customRequest( { method: "GET", url: `/search`, params: { diff --git a/packages/app/src/models/follows/index.js b/packages/app/src/models/follows/index.js index f74a9307..c3336cea 100644 --- a/packages/app/src/models/follows/index.js +++ b/packages/app/src/models/follows/index.js @@ -6,7 +6,7 @@ export default class FollowsModel { throw new Error("user_id is required") } - const response = await app.cores.api.customRequest("main", { + const response = await app.cores.api.customRequest( { method: "GET", url: `/follow/user/${user_id}`, }) @@ -20,7 +20,7 @@ export default class FollowsModel { user_id = SessionModel.user_id } - const response = await app.cores.api.customRequest("main", { + const response = await app.cores.api.customRequest( { method: "GET", url: `/follow/user/${user_id}/followers`, }) @@ -33,7 +33,7 @@ export default class FollowsModel { throw new Error("user_id or username is required") } - const response = await app.cores.api.customRequest("main", { + const response = await app.cores.api.customRequest( { method: "POST", url: "/follow/user/toogle", data: { diff --git a/packages/app/src/models/livestream/index.js b/packages/app/src/models/livestream/index.js index 0272493a..3166790a 100755 --- a/packages/app/src/models/livestream/index.js +++ b/packages/app/src/models/livestream/index.js @@ -1,6 +1,6 @@ export default class Livestream { static async getStreamingKey() { - const request = await app.cores.api.customRequest("main", { + const request = await app.cores.api.customRequest( { method: "GET", url: `/tv/streaming/key`, }) @@ -9,7 +9,7 @@ export default class Livestream { } static async regenerateStreamingKey() { - const request = await app.cores.api.customRequest("main", { + const request = await app.cores.api.customRequest( { method: "POST", url: `/tv/streaming/key/regenerate`, @@ -19,7 +19,7 @@ export default class Livestream { } static async updateLivestreamInfo(payload) { - const request = await app.cores.api.customRequest("main", { + const request = await app.cores.api.customRequest( { method: "POST", url: `/tv/stream/info`, data: { @@ -31,7 +31,7 @@ export default class Livestream { } static async getCategories(key) { - const request = await app.cores.api.customRequest("main", { + const request = await app.cores.api.customRequest( { method: "GET", url: `/tv/streaming/categories`, params: { @@ -49,7 +49,7 @@ export default class Livestream { username = app.userData.username } - const request = await app.cores.api.customRequest("main", { + const request = await app.cores.api.customRequest( { method: "GET", url: `/tv/stream/info`, params: { @@ -65,7 +65,7 @@ export default class Livestream { throw new Error("Username is required") } - let request = await app.cores.api.customRequest("main", { + let request = await app.cores.api.customRequest( { method: "GET", url: `/tv/streams`, params: { @@ -77,7 +77,7 @@ export default class Livestream { } static async getAddresses() { - const request = await app.cores.api.customRequest("main", { + const request = await app.cores.api.customRequest( { method: "GET", url: `/tv/streaming/addresses`, }) @@ -86,7 +86,7 @@ export default class Livestream { } static async getLivestreams() { - const request = await app.cores.api.customRequest("main", { + const request = await app.cores.api.customRequest( { method: "GET", url: `/tv/streams`, }) diff --git a/packages/app/src/models/music/index.js b/packages/app/src/models/music/index.js index f7a27f76..b6f3abb8 100755 --- a/packages/app/src/models/music/index.js +++ b/packages/app/src/models/music/index.js @@ -1,10 +1,10 @@ export class MusicModel { static get bridge() { - return window.app?.cores.api.withEndpoints("main") + return window.app?.cores.api.withEndpoints() } static async createSpaceRoom() { - const { data } = await app.cores.api.customRequest("main", { + const { data } = await app.cores.api.customRequest( { method: "post", url: `/music/create_space_room`, }) diff --git a/packages/app/src/models/playlists/index.js b/packages/app/src/models/playlists/index.js index 0ac61b8a..221cf880 100755 --- a/packages/app/src/models/playlists/index.js +++ b/packages/app/src/models/playlists/index.js @@ -1,6 +1,6 @@ export default class PlaylistsModel { static get bridge() { - return window.app?.cores.api.withEndpoints("main") + return window.app?.cores.api.withEndpoints() } static async uploadTrack(file, payload) { @@ -22,7 +22,7 @@ export default class PlaylistsModel { formData.append("files", file) // send the request - const uploadRequest = await app.cores.api.customRequest("main", { + const uploadRequest = await app.cores.api.customRequest( { method: "POST", url: "/upload", data: formData, @@ -38,7 +38,7 @@ export default class PlaylistsModel { const source = uploadRequest.data.files[0].url // send the request for the track to be created - const trackRequest = await app.cores.api.customRequest("main", { + const trackRequest = await app.cores.api.customRequest( { method: "POST", url: "/tracks/publish", data: { @@ -51,7 +51,7 @@ export default class PlaylistsModel { } static async publishTrack(payload) { - const { data } = await app.cores.api.customRequest("main", { + const { data } = await app.cores.api.customRequest( { method: "POST", url: "/tracks/publish", data: payload, @@ -61,7 +61,7 @@ export default class PlaylistsModel { } static async publish(payload) { - const { data } = await app.cores.api.customRequest("main", { + const { data } = await app.cores.api.customRequest( { method: "POST", url: `/playlist/publish`, data: payload, @@ -71,7 +71,7 @@ export default class PlaylistsModel { } static async getPlaylist(id) { - const { data } = await app.cores.api.customRequest("main", { + const { data } = await app.cores.api.customRequest( { method: "GET", url: `/playlist/data/${id}`, }) @@ -80,7 +80,7 @@ export default class PlaylistsModel { } static async getMyReleases() { - const { data } = await app.cores.api.customRequest("main", { + const { data } = await app.cores.api.customRequest( { method: "GET", url: `/playlist/self`, }) @@ -93,7 +93,7 @@ export default class PlaylistsModel { throw new Error("Payload is required") } - const { data } = await app.cores.api.customRequest("main", { + const { data } = await app.cores.api.customRequest( { method: "PUT", url: `/tracks/${payload._id}`, data: { @@ -109,7 +109,7 @@ export default class PlaylistsModel { throw new Error("Payload is required") } - const { data } = await app.cores.api.customRequest("main", { + const { data } = await app.cores.api.customRequest( { method: "PUT", url: `/playlist/${payload._id}`, data: { @@ -125,7 +125,7 @@ export default class PlaylistsModel { throw new Error("ID is required") } - const { data } = await app.cores.api.customRequest("main", { + const { data } = await app.cores.api.customRequest( { method: "DELETE", url: `/playlist/${id}`, }) diff --git a/packages/app/src/models/post/index.js b/packages/app/src/models/post/index.js index c14b7a58..0ce8d10a 100755 --- a/packages/app/src/models/post/index.js +++ b/packages/app/src/models/post/index.js @@ -1,6 +1,6 @@ export default class Post { static get bridge() { - return window.app?.cores.api.withEndpoints("main") + return window.app?.cores.api.withEndpoints() } static get maxPostTextLength() { @@ -16,7 +16,7 @@ export default class Post { throw new Error("Post ID is required") } - const { data } = await app.cores.api.customRequest("main", { + const { data } = await app.cores.api.customRequest( { method: "GET", url: `/posts/post/${post_id}`, }) @@ -29,7 +29,7 @@ export default class Post { throw new Error("Post ID is required") } - const { data } = await app.cores.api.customRequest("main", { + const { data } = await app.cores.api.customRequest( { method: "GET", url: `/comments/post/${post_id}`, }) @@ -42,7 +42,7 @@ export default class Post { throw new Error("Post ID and/or comment are required") } - const request = await app.cores.apies.api.customRequest("main", { + const request = await app.cores.apies.api.customRequest( { method: "POST", url: `/comments/post/${post_id}`, data: { @@ -58,7 +58,7 @@ export default class Post { throw new Error("Post ID and/or comment ID are required") } - const request = await app.cores.apies.api.customRequest("main", { + const request = await app.cores.apies.api.customRequest( { method: "DELETE", url: `/comments/post/${post_id}/${comment_id}`, }) @@ -71,7 +71,7 @@ export default class Post { throw new Error("Bridge is not available") } - const { data } = await app.cores.api.customRequest("main", { + const { data } = await app.cores.api.customRequest( { method: "GET", url: `/posts/explore`, params: { @@ -88,7 +88,7 @@ export default class Post { throw new Error("Bridge is not available") } - const { data } = await app.cores.api.customRequest("main", { + const { data } = await app.cores.api.customRequest( { method: "GET", url: `/posts/saved`, params: { @@ -110,7 +110,7 @@ export default class Post { user_id = app.userData?._id } - const { data } = await app.cores.api.customRequest("main", { + const { data } = await app.cores.api.customRequest( { method: "GET", url: `/posts/user/${user_id}`, params: { @@ -131,7 +131,7 @@ export default class Post { throw new Error("Post ID is required") } - const { data } = await app.cores.api.customRequest("main", { + const { data } = await app.cores.api.customRequest( { method: "POST", url: `/posts/${post_id}/toogle_like`, }) @@ -148,7 +148,7 @@ export default class Post { throw new Error("Post ID is required") } - const { data } = await app.cores.api.customRequest("main", { + const { data } = await app.cores.api.customRequest( { method: "POST", url: `/posts/${post_id}/toogle_save`, }) @@ -161,7 +161,7 @@ export default class Post { throw new Error("Bridge is not available") } - const { data } = await app.cores.api.customRequest("main", { + const { data } = await app.cores.api.customRequest( { method: "POST", url: `/posts/new`, data: payload, @@ -179,7 +179,7 @@ export default class Post { throw new Error("Post ID is required") } - const { data } = await app.cores.api.customRequest("main", { + const { data } = await app.cores.api.customRequest( { method: "DELETE", url: `/posts/${post_id}`, }) diff --git a/packages/app/src/models/session/index.js b/packages/app/src/models/session/index.js index c24de045..93aeec32 100755 --- a/packages/app/src/models/session/index.js +++ b/packages/app/src/models/session/index.js @@ -28,7 +28,7 @@ export default class Session { } static async getAllSessions() { - const response = await app.cores.api.customRequest("main", { + const response = await app.cores.api.customRequest( { method: "get", url: "/session/all" }) @@ -37,7 +37,7 @@ export default class Session { } static async getCurrentSession() { - const response = await app.cores.api.customRequest("main", { + const response = await app.cores.api.customRequest( { method: "get", url: "/session/current" }) @@ -48,7 +48,7 @@ export default class Session { static async getTokenValidation() { const session = await Session.token - const response = await app.cores.api.customRequest("main", { + const response = await app.cores.api.customRequest( { method: "get", url: "/session/validate", data: { @@ -71,7 +71,7 @@ export default class Session { return false } - const response = await app.cores.api.customRequest("main", { + const response = await app.cores.api.customRequest( { method: "delete", url: "/session/current" }).catch((error) => { @@ -94,7 +94,7 @@ export default class Session { return false } - const response = await app.cores.api.customRequest("main", { + const response = await app.cores.api.customRequest( { method: "delete", url: "/session/all" }) diff --git a/packages/app/src/models/sync/cores/spotifyCore.js b/packages/app/src/models/sync/cores/spotifyCore.js index 6cf01c0a..82fb2d74 100755 --- a/packages/app/src/models/sync/cores/spotifyCore.js +++ b/packages/app/src/models/sync/cores/spotifyCore.js @@ -28,7 +28,7 @@ export default class SpotifySyncModel { } static async get_client_id() { - const { data } = await app.cores.api.customRequest("main", { + const { data } = await app.cores.api.customRequest( { method: "GET", url: `/sync/spotify/client_id`, }) @@ -37,7 +37,7 @@ export default class SpotifySyncModel { } static async syncAuthCode(code) { - const { data } = await app.cores.api.customRequest("main", { + const { data } = await app.cores.api.customRequest( { method: "POST", url: `/sync/spotify/auth`, data: { @@ -50,7 +50,7 @@ export default class SpotifySyncModel { } static async unlinkAccount() { - const { data } = await app.cores.api.customRequest("main", { + const { data } = await app.cores.api.customRequest( { method: "POST", url: `/sync/spotify/unlink`, }) @@ -59,7 +59,7 @@ export default class SpotifySyncModel { } static async isAuthorized() { - const { data } = await app.cores.api.customRequest("main", { + const { data } = await app.cores.api.customRequest( { method: "GET", url: `/sync/spotify/is_authorized`, }) @@ -68,7 +68,7 @@ export default class SpotifySyncModel { } static async getData() { - const { data } = await app.cores.api.customRequest("main", { + const { data } = await app.cores.api.customRequest( { method: "GET", url: `/sync/spotify/data`, }) @@ -77,7 +77,7 @@ export default class SpotifySyncModel { } static async getCurrentPlaying() { - const { data } = await app.cores.api.customRequest("main", { + const { data } = await app.cores.api.customRequest( { method: "GET", url: `/sync/spotify/currently_playing`, }) diff --git a/packages/app/src/models/sync/index.js b/packages/app/src/models/sync/index.js index d978e0b2..43bcbf0c 100755 --- a/packages/app/src/models/sync/index.js +++ b/packages/app/src/models/sync/index.js @@ -2,7 +2,7 @@ import SpotifySyncModel from "./cores/spotifyCore" export default class SyncModel { static get bridge() { - return window.app?.cores.api.withEndpoints("main") + return window.app?.cores.api.withEndpoints() } static get spotifyCore() { diff --git a/packages/app/src/models/upload/index.js b/packages/app/src/models/upload/index.js index 5ade7f5b..f54d6ce4 100644 --- a/packages/app/src/models/upload/index.js +++ b/packages/app/src/models/upload/index.js @@ -12,7 +12,7 @@ export default class UploadModel { formData.append("files", file) // send the request - const uploadRequest = await app.cores.api.customRequest("main", { + const uploadRequest = await app.cores.api.customRequest( { method: "POST", url: "/upload", data: formData, diff --git a/packages/app/src/models/user/index.js b/packages/app/src/models/user/index.js index a563bb26..6d95c4f5 100755 --- a/packages/app/src/models/user/index.js +++ b/packages/app/src/models/user/index.js @@ -13,7 +13,7 @@ export default class User { if (username && !user_id) { // resolve user_id from username - const resolveResponse = await app.cores.api.customRequest("main", { + const resolveResponse = await app.cores.api.customRequest( { method: "GET", url: `/user/user_id/${username}`, }) @@ -21,7 +21,7 @@ export default class User { user_id = resolveResponse.data.user_id } - const response = await app.cores.api.customRequest("main", { + const response = await app.cores.api.customRequest( { method: "GET", url: `/user/${user_id}/data`, }) @@ -30,7 +30,7 @@ export default class User { } static async updateData(payload) { - const response = await app.cores.api.customRequest("main", { + const response = await app.cores.api.customRequest( { method: "POST", url: "/user/self/update_data", data: { @@ -42,7 +42,7 @@ export default class User { } static async unsetFullName() { - const response = await app.cores.api.customRequest("main", { + const response = await app.cores.api.customRequest( { method: "DELETE", url: "/user/self/public_name", }) @@ -51,7 +51,7 @@ export default class User { } static async selfRoles() { - const response = await app.cores.api.customRequest("main", { + const response = await app.cores.api.customRequest( { method: "GET", url: "/roles/self", }) @@ -78,7 +78,7 @@ export default class User { user_id = SessionModel.user_id } - const { data } = await app.cores.api.customRequest("main", { + const { data } = await app.cores.api.customRequest( { method: "GET", url: `/badge/user/${user_id}`, }) @@ -89,7 +89,7 @@ export default class User { static async changePassword(payload) { const { currentPassword, newPassword } = payload - const { data } = await app.cores.api.customRequest("main", { + const { data } = await app.cores.api.customRequest( { method: "POST", url: "/self/update_password", data: { @@ -111,7 +111,7 @@ export default class User { user_id = SessionModel.user_id } - const { data } = await app.cores.api.customRequest("main", { + const { data } = await app.cores.api.customRequest( { method: "GET", url: `/user/${user_id}/followers`, params: { @@ -124,7 +124,7 @@ export default class User { } static async getConnectedUsersFollowing() { - const { data } = await app.cores.api.customRequest("main", { + const { data } = await app.cores.api.customRequest( { method: "GET", url: "/status/connected/following", }) diff --git a/packages/app/src/pages/account/index.jsx b/packages/app/src/pages/account/index.jsx index 546b4ad9..3a29dfc3 100755 --- a/packages/app/src/pages/account/index.jsx +++ b/packages/app/src/pages/account/index.jsx @@ -74,7 +74,7 @@ export default class Account extends React.Component { actionsRef = React.createRef() - api = window.app.cores.api.withEndpoints("main") + api = window.app.cores.api.withEndpoints() componentDidMount = async () => { const token = await SessionModel.getDecodedToken() diff --git a/packages/app/src/pages/featured-event/[id].jsx b/packages/app/src/pages/featured-event/[id].jsx index d5ec9821..603d1618 100755 --- a/packages/app/src/pages/featured-event/[id].jsx +++ b/packages/app/src/pages/featured-event/[id].jsx @@ -24,7 +24,7 @@ export default (props) => { const [eventData, setEventData] = React.useState(null) const fetchEventData = async () => { - const { data } = await app.cores.api.customRequest("main", { + const { data } = await app.cores.api.customRequest( { method: "GET", url: `/featured_event/${eventId}` }).catch((err) => { diff --git a/packages/app/src/pages/login/index.jsx b/packages/app/src/pages/login/index.jsx index 0f81db3f..d1ccea5a 100755 --- a/packages/app/src/pages/login/index.jsx +++ b/packages/app/src/pages/login/index.jsx @@ -33,7 +33,7 @@ export default (props) => { const [wallpaperData, setWallpaperData] = React.useState(null) const setRandomWallpaper = async () => { - const featuredWallpapers = await app.cores.api.request("main", "get", "featuredWallpapers").catch((err) => { + const featuredWallpapers = await app.cores.api.request("get", "featuredWallpapers").catch((err) => { console.error(err) return [] }) diff --git a/packages/app/src/pages/posts/components/explore/index.jsx b/packages/app/src/pages/posts/components/explore/index.jsx index 7729d466..54e1394e 100755 --- a/packages/app/src/pages/posts/components/explore/index.jsx +++ b/packages/app/src/pages/posts/components/explore/index.jsx @@ -92,13 +92,13 @@ export default class ExplorePosts extends React.Component { await this.loadPosts() Object.keys(this.wsEvents).forEach((event) => { - window.app.cores.api.namespaces["main"].listenEvent(event, this.wsEvents[event]) + app.cores.api.listenEvent(event, this.wsEvents[event]) }) } componentWillUnmount = async () => { Object.keys(this.wsEvents).forEach((event) => { - window.app.cores.api.namespaces["main"].unlistenEvent(event, this.wsEvents[event]) + app.cores.api.unlistenEvent(event, this.wsEvents[event]) }) } diff --git a/packages/app/src/pages/posts/components/feed/index.jsx b/packages/app/src/pages/posts/components/feed/index.jsx index f5e74ff9..1bc72bdf 100755 --- a/packages/app/src/pages/posts/components/feed/index.jsx +++ b/packages/app/src/pages/posts/components/feed/index.jsx @@ -90,13 +90,13 @@ export default class Feed extends React.Component { console.log(this.wsEvents) Object.keys(this.wsEvents).forEach((event) => { - window.app.cores.api.namespaces["main"].listenEvent(event, this.wsEvents[event]) + app.cores.api.listenEvent(event, this.wsEvents[event]) }) } componentWillUnmount = async () => { Object.keys(this.wsEvents).forEach((event) => { - window.app.cores.api.namespaces["main"].unlistenEvent(event, this.wsEvents[event]) + app.cores.api.unlistenEvent(event, this.wsEvents[event]) }) }