From e8a05c252bccaff4c85c5d5d4d4e4054eec395fa Mon Sep 17 00:00:00 2001 From: Rish Date: Tue, 28 Apr 2020 00:52:21 +0530 Subject: [PATCH] Added init method to api refs https://github.com/TryGhost/members.js/issues/5 - renamed default function name - Adds `init` method to api for fetching site data and member session data together - Update site url from site data to remove window.location.origin dependency in case of iframe --- ghost/portal/src/utils/api.js | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/ghost/portal/src/utils/api.js b/ghost/portal/src/utils/api.js index 6bb881c1f2..c4f1aa2ade 100644 --- a/ghost/portal/src/utils/api.js +++ b/ghost/portal/src/utils/api.js @@ -1,9 +1,9 @@ -function MembersAPI({adminUrl}) { +function setupGhostApi({adminUrl}) { const ghostPath = 'ghost'; const ssrPath = 'members/ssr'; const version = 'v3'; - - const siteUrl = window.location.origin; + adminUrl = adminUrl.replace(/\/$/, ''); + let siteUrl = window.location.origin; function endpointFor({type, resource}) { if (type === 'members') { @@ -39,7 +39,7 @@ function MembersAPI({adminUrl}) { if (res.ok) { return res.json(); } else { - return 'Failed to fetch site data'; + throw new Error('Failed to fetch site data'); } }); } @@ -145,7 +145,15 @@ function MembersAPI({adminUrl}) { } }; + api.init = async () => { + const {site} = await api.site.read(); + // Update site url from site data instead of default window.location.origin + siteUrl = site.url.replace(/\/$/, ''); + const member = await api.member.sessionData(); + return {site, member}; + }; + return api; } -export default MembersAPI; +export default setupGhostApi;