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
This commit is contained in:
Rish 2020-04-28 00:52:21 +05:30
parent ead29b9e23
commit e8a05c252b

View File

@ -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;