File size: 943 Bytes
1e92f2d |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
import { wpcomRequest } from '../wpcom-request-controls';
import { createActions } from './actions';
import type { CurrentUser } from './types';
declare global {
interface Window {
currentUser?: CurrentUser;
}
}
export function createResolvers() {
const { receiveCurrentUser, receiveCurrentUserFailed } = createActions();
function* getCurrentUser() {
// In environments where `wpcom-user-bootstrap` is set to true, the currentUser
// object will be server-side rendered to window.currentUser. In these cases,
// return that object instead of performing another API request to `/me`.
if ( window.currentUser ) {
return receiveCurrentUser( window.currentUser );
}
try {
const currentUser: CurrentUser = yield wpcomRequest( {
path: '/me',
apiVersion: '1.1',
} );
return receiveCurrentUser( currentUser );
} catch ( err ) {
return receiveCurrentUserFailed();
}
}
return {
getCurrentUser,
};
}
|