-
Notifications
You must be signed in to change notification settings - Fork 20
/
proxy.js
49 lines (43 loc) · 1.08 KB
/
proxy.js
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
37
38
39
40
41
42
43
44
45
46
47
48
49
import proxy from 'wpcom-proxy-request';
export const boot = () =>
new Promise( ( resolve, reject ) => {
proxy( { metaAPI: { accessAllUsersBlogs: true } }, err => {
if ( err !== null ) {
reject( err );
throw err;
}
const timer = setTimeout( () =>
reject()
, 3000 );
proxy( { path: '/me' }, ( err, response ) => {
clearTimeout( timer );
if ( err ) {
reject();
return;
}
if ( response.avatar_URL ) {
response.avatarUrl = response.avatar_URL;
delete response.avatar_URL;
}
resolve( response );
} );
} );
} )
;
export const request = req =>
new Promise( resolve => {
proxy( req, ( err, body, xhr ) => {
resolve( {
status: xhr.status === undefined ? 200 : xhr.status,
body,
error: err,
} );
} );
} )
;
export const login = () => {
window.location = `https://wordpress.com/wp-login.php?redirect_to=${ encodeURI( window.location.href ) }`;
};
export const logout = () => {
window.location = `https://wordpress.com/wp-login.php?action=logout&redirect_to=${ encodeURI( window.location.href ) }`;
};