Skip to content

Commit

Permalink
Revert "wpcom-proxy-request: Return Promise if no callback specified …
Browse files Browse the repository at this point in the history
…(Take Two) (#39722)"

This reverts commit 8ff15c6.
  • Loading branch information
ockham authored Mar 3, 2020
1 parent 8ff15c6 commit be5e1a3
Show file tree
Hide file tree
Showing 15 changed files with 69 additions and 91 deletions.
1 change: 1 addition & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,4 @@ Dockerfile
/apps/*/dist/
/apps/*/types/
/packages/*/dist/
/packages/*/types/
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -64,3 +64,6 @@ cached-requests.json
/apps/*/dist/
/apps/*/types/
/packages/*/dist/
# Redundant after https://github.com/Automattic/wp-calypso/pull/39173
# Safe to remove after some time has passed
/packages/*/types/
11 changes: 11 additions & 0 deletions packages/data-stores/global.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/**
* External dependencies
*/
declare module 'wpcom-proxy-request' {
type WpcomRequestParams = import('./src/utils/wpcom-wrapper').WpcomRequestParams;
export function reloadProxy(): void;
export default function wpcomProxyRequest(
params: WpcomRequestParams,
callback: Function
): XMLHttpRequest;
}
4 changes: 0 additions & 4 deletions packages/data-stores/src/shared-types.ts

This file was deleted.

10 changes: 1 addition & 9 deletions packages/data-stores/src/site/controls.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,10 @@
/**
* External dependencies
*/
import wpcomRequest, { requestAllBlogsAccess } from 'wpcom-proxy-request';

/**
* Internal dependencies
*/
import { wpcomRequest, WpcomClientCredentials } from '../utils';
import { CreateSiteAction } from './types';
import { WpcomClientCredentials } from '../shared-types';

export default function createControls( clientCreds: WpcomClientCredentials ) {
requestAllBlogsAccess().catch( () => {
throw new Error( 'Could not get all blog access.' );
} );
return {
CREATE_SITE: async ( action: CreateSiteAction ) => {
const { authToken, ...providedParams } = action.params;
Expand Down
2 changes: 1 addition & 1 deletion packages/data-stores/src/site/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import * as actions from './actions';
import * as selectors from './selectors';
import createControls from './controls';
import { DispatchFromMap, SelectFromMap } from '../mapped-types';
import { WpcomClientCredentials } from '../shared-types';
import { WpcomClientCredentials } from '../utils';

export * from './types';
export { State };
Expand Down
10 changes: 1 addition & 9 deletions packages/data-stores/src/user/controls.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,10 @@
/**
* External dependencies
*/
import wpcomRequest, { requestAllBlogsAccess } from 'wpcom-proxy-request';

/**
* Internal dependencies
*/
import { wpcomRequest, WpcomClientCredentials } from '../utils';
import { CreateAccountAction } from './types';
import { WpcomClientCredentials } from '../shared-types';

export default function createControls( clientCreds: WpcomClientCredentials ) {
requestAllBlogsAccess().catch( () => {
throw new Error( 'Could not get all blog access.' );
} );
return {
CREATE_ACCOUNT: async ( action: CreateAccountAction ) => {
const defaultParams = {
Expand Down
2 changes: 1 addition & 1 deletion packages/data-stores/src/user/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import * as resolvers from './resolvers';
import * as selectors from './selectors';
import createControls from './controls';
import { DispatchFromMap, SelectFromMap } from '../mapped-types';
import { WpcomClientCredentials } from '../shared-types';
import { WpcomClientCredentials } from '../utils';

export * from './types';
export { State };
Expand Down
1 change: 1 addition & 0 deletions packages/data-stores/src/utils/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './wpcom-wrapper';
47 changes: 47 additions & 0 deletions packages/data-stores/src/utils/wpcom-wrapper.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/**
* External dependencies
*/
import wpcomProxyRequest, { reloadProxy } from 'wpcom-proxy-request';
import debugFactory from 'debug';

const debug = debugFactory( 'data-stores:utils:wpcom-wrapper' );

export interface WpcomClientCredentials {
client_id: string;
client_secret: string;
}

export interface WpcomRequestParams {
path?: string;
method?: string;
apiVersion?: string;
body?: object;
token?: string;
metaAPI?: {
accessAllUsersBlogs?: boolean;
};
}

export function wpcomRequest< T >( params: WpcomRequestParams ): Promise< T > {
return new Promise( ( resolve, reject ) => {
wpcomProxyRequest( params, ( err: Error, res: T ) => {
debug( res );
err ? reject( err ) : resolve( res );
} );
} );
}
/*
* Reloading the proxy ensures that the proxy iframe has set the correct API cookie.
* This is particularly useful for making authenticated API requests
* *after* the user has logged in or signed up without the need for a hard browser refresh.
*/
export function reloadWpcomProxy(): void {
reloadProxy();
}

wpcomProxyRequest( { metaAPI: { accessAllUsersBlogs: true } }, ( error: Error ) => {
if ( error ) {
throw error;
}
debug( 'Proxy now running in "access all user\'s blogs" mode' );
} );
2 changes: 1 addition & 1 deletion packages/data-stores/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,6 @@
"incremental": true,
"tsBuildInfoFile": "../../.tsc-cache/data-stores"
},
"files": [ "./src/index.ts" ],
"files": [ "./src/index.ts", "./global.d.ts" ],
"exclude": [ "**/docs/*", "**/test/*" ]
}
4 changes: 0 additions & 4 deletions packages/wpcom-proxy-request/History.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
# 6.0.0 / TBD

- Breaking: Return Promise (rather than `XMLHttpRequest` instance) if no callback argument is provided.
- In practice, most people have probably been using the callback rather than the returned `XMLHttpRequest` instance, so this shouldn't be a breaking change for most.
- Add `requestAllBlogsAccess()`.
- Add a few type definitions.
- Move the published `build/` folder to `dist/` to align with other Calypso packages
- Upgrade dependency 'debug' to 4.1.1
- Remove `component-event` dependency, use `addEventListener`/`removeEventListener` directly
Expand Down
2 changes: 0 additions & 2 deletions packages/wpcom-proxy-request/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,9 @@
},
"files": [
"dist",
"types",
"History.md",
"README.md"
],
"types": "types",
"scripts": {
"clean": "npx rimraf dist",
"prepublish": "npm run clean",
Expand Down
36 changes: 1 addition & 35 deletions packages/wpcom-proxy-request/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ debug( 'using "origin": %o', origin );
* @param {Function} [fn] - callback response
* @returns {window.XMLHttpRequest} XMLHttpRequest instance
*/
const makeRequest = ( originalParams, fn ) => {
const request = ( originalParams, fn ) => {
const params = Object.assign( {}, originalParams );

debug( 'request(%o)', params );
Expand Down Expand Up @@ -170,40 +170,6 @@ const makeRequest = ( originalParams, fn ) => {
return xhr;
};

/**
* Performs a "proxied REST API request". This happens by calling
* `iframe.postMessage()` on the proxy iframe instance, which from there
* takes care of WordPress.com user authentication (via the currently
* logged-in user's cookies).
*
* If no function is specified as second parameter, a promise is returned.
*
* @param {object} originalParams - request parameters
* @param {Function} [fn] - callback response
* @returns {window.XMLHttpRequest|Promise} XMLHttpRequest instance or Promise
*/
const request = ( originalParams, fn ) => {
// if callback is provided, behave traditionally
if ( 'function' === typeof fn ) {
// request method
return makeRequest( originalParams, fn );
}

// but if not, return a Promise
return new Promise( ( res, rej ) => {
makeRequest( originalParams, ( err, response ) => {
err ? rej( err ) : res( response );
} );
} );
};

/**
* Set proxy to "access all users' blogs" mode.
*/
export function requestAllBlogsAccess() {
return request( { metaAPI: { accessAllUsersBlogs: true } } );
}

/**
* Calls the `postMessage()` function on the <iframe>.
*
Expand Down
25 changes: 0 additions & 25 deletions packages/wpcom-proxy-request/types/index.d.ts

This file was deleted.

0 comments on commit be5e1a3

Please sign in to comment.