Skip to content

Commit

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

The promise wrapper introduced et al. in #38823 is generic enough for it to make sense to have it in `wpcom-proxy-request`. This commit moves it there. This follows [precedent](https://github.com/Automattic/wp-calypso/blob/bf345c5b040e75808fb2f92c9b9e34e6f126a235/packages/wpcom.js/src/lib/util/send-request.js#L78-L93) set by `wpcom.js`.

Note that this is a technically a breaking change, as noted in the package's `History.md`:

Co-authored-by: Jon Surrell <jon.surrell@automattic.com>

This reverts commit 7f5092a.
  • Loading branch information
ockham authored Mar 3, 2020
1 parent 98da469 commit 8ff15c6
Show file tree
Hide file tree
Showing 15 changed files with 91 additions and 69 deletions.
1 change: 0 additions & 1 deletion .dockerignore
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,3 @@ Dockerfile
/apps/*/dist/
/apps/*/types/
/packages/*/dist/
/packages/*/types/
3 changes: 0 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,3 @@ 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: 0 additions & 11 deletions packages/data-stores/global.d.ts

This file was deleted.

4 changes: 4 additions & 0 deletions packages/data-stores/src/shared-types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export interface WpcomClientCredentials {
client_id: string;
client_secret: string;
}
10 changes: 9 additions & 1 deletion packages/data-stores/src/site/controls.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,18 @@
/**
* 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 '../utils';
import { WpcomClientCredentials } from '../shared-types';

export * from './types';
export { State };
Expand Down
10 changes: 9 additions & 1 deletion packages/data-stores/src/user/controls.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,18 @@
/**
* 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 '../utils';
import { WpcomClientCredentials } from '../shared-types';

export * from './types';
export { State };
Expand Down
1 change: 0 additions & 1 deletion packages/data-stores/src/utils/index.ts

This file was deleted.

47 changes: 0 additions & 47 deletions packages/data-stores/src/utils/wpcom-wrapper.ts

This file was deleted.

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", "./global.d.ts" ],
"files": [ "./src/index.ts" ],
"exclude": [ "**/docs/*", "**/test/*" ]
}
4 changes: 4 additions & 0 deletions packages/wpcom-proxy-request/History.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# 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: 2 additions & 0 deletions packages/wpcom-proxy-request/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,11 @@
},
"files": [
"dist",
"types",
"History.md",
"README.md"
],
"types": "types",
"scripts": {
"clean": "npx rimraf dist",
"prepublish": "npm run clean",
Expand Down
36 changes: 35 additions & 1 deletion 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 request = ( originalParams, fn ) => {
const makeRequest = ( originalParams, fn ) => {
const params = Object.assign( {}, originalParams );

debug( 'request(%o)', params );
Expand Down Expand Up @@ -170,6 +170,40 @@ const request = ( 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: 25 additions & 0 deletions packages/wpcom-proxy-request/types/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/**
* TypeScript type definitions for wpcom-proxy-request
*
* @todo Migrate `src/index.js` to TypeScript, incorporate these type definitions.
* (Needs changes to the build chain, see other packages in the monorepo
* for inspiration, e.g. `@automattic/data-stores`.)
*/

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

export function reloadProxy(): void;

export function requestAllBlogsAccess(): ReturnType< typeof request >;

export default function request( params: WpcomRequestParams, callback: Function ): XMLHttpRequest;
export default function request< T >( params: WpcomRequestParams ): Promise< T >;

0 comments on commit 8ff15c6

Please sign in to comment.