From 74918eeb31f851f5a6a06b92314d62b31b097551 Mon Sep 17 00:00:00 2001 From: Bernie Reiter Date: Thu, 27 Feb 2020 15:01:23 +0600 Subject: [PATCH] Revert "Revert "wpcom-proxy-request: Return Promise if no callback specified (#39683)" (#39721)" This reverts commit 7f5092ab351a9b5a1b94e6b0aaae11542e9f6eb6. --- .gitignore | 3 - packages/data-stores/global.d.ts | 11 - packages/data-stores/src/shared-types.ts | 4 + packages/data-stores/src/site/controls.ts | 10 +- packages/data-stores/src/site/index.ts | 2 +- packages/data-stores/src/user/controls.ts | 10 +- packages/data-stores/src/user/index.ts | 2 +- packages/data-stores/src/utils/index.ts | 1 - .../data-stores/src/utils/wpcom-wrapper.ts | 47 ---- packages/data-stores/tsconfig.json | 2 +- packages/wpcom-proxy-request/History.md | 204 ++++++++---------- packages/wpcom-proxy-request/package.json | 2 + packages/wpcom-proxy-request/src/index.js | 36 +++- packages/wpcom-proxy-request/types/index.d.ts | 25 +++ 14 files changed, 180 insertions(+), 179 deletions(-) delete mode 100644 packages/data-stores/global.d.ts create mode 100644 packages/data-stores/src/shared-types.ts delete mode 100644 packages/data-stores/src/utils/index.ts delete mode 100644 packages/data-stores/src/utils/wpcom-wrapper.ts create mode 100644 packages/wpcom-proxy-request/types/index.d.ts diff --git a/.gitignore b/.gitignore index 7d8c1858a4436..737e0c3416325 100644 --- a/.gitignore +++ b/.gitignore @@ -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/ diff --git a/packages/data-stores/global.d.ts b/packages/data-stores/global.d.ts deleted file mode 100644 index 48339e4e8e18b..0000000000000 --- a/packages/data-stores/global.d.ts +++ /dev/null @@ -1,11 +0,0 @@ -/** - * 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; -} diff --git a/packages/data-stores/src/shared-types.ts b/packages/data-stores/src/shared-types.ts new file mode 100644 index 0000000000000..f42c43565f86c --- /dev/null +++ b/packages/data-stores/src/shared-types.ts @@ -0,0 +1,4 @@ +export interface WpcomClientCredentials { + client_id: string; + client_secret: string; +} diff --git a/packages/data-stores/src/site/controls.ts b/packages/data-stores/src/site/controls.ts index 615c712b43fef..963eba87cf7ba 100644 --- a/packages/data-stores/src/site/controls.ts +++ b/packages/data-stores/src/site/controls.ts @@ -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; diff --git a/packages/data-stores/src/site/index.ts b/packages/data-stores/src/site/index.ts index 872ae11b3a8e1..0b6e705e92593 100644 --- a/packages/data-stores/src/site/index.ts +++ b/packages/data-stores/src/site/index.ts @@ -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 }; diff --git a/packages/data-stores/src/user/controls.ts b/packages/data-stores/src/user/controls.ts index 2d096fc040b45..e63d1720d3f1c 100644 --- a/packages/data-stores/src/user/controls.ts +++ b/packages/data-stores/src/user/controls.ts @@ -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 = { diff --git a/packages/data-stores/src/user/index.ts b/packages/data-stores/src/user/index.ts index eef475ffcca60..e2804a557d45f 100644 --- a/packages/data-stores/src/user/index.ts +++ b/packages/data-stores/src/user/index.ts @@ -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 }; diff --git a/packages/data-stores/src/utils/index.ts b/packages/data-stores/src/utils/index.ts deleted file mode 100644 index 350c0bac17baa..0000000000000 --- a/packages/data-stores/src/utils/index.ts +++ /dev/null @@ -1 +0,0 @@ -export * from './wpcom-wrapper'; diff --git a/packages/data-stores/src/utils/wpcom-wrapper.ts b/packages/data-stores/src/utils/wpcom-wrapper.ts deleted file mode 100644 index b09f1ebfed692..0000000000000 --- a/packages/data-stores/src/utils/wpcom-wrapper.ts +++ /dev/null @@ -1,47 +0,0 @@ -/** - * 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' ); -} ); diff --git a/packages/data-stores/tsconfig.json b/packages/data-stores/tsconfig.json index 0a8be6b76037f..bef7dbde805be 100644 --- a/packages/data-stores/tsconfig.json +++ b/packages/data-stores/tsconfig.json @@ -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/*" ] } diff --git a/packages/wpcom-proxy-request/History.md b/packages/wpcom-proxy-request/History.md index 14486b1c1e56b..325e2ea90a357 100644 --- a/packages/wpcom-proxy-request/History.md +++ b/packages/wpcom-proxy-request/History.md @@ -1,154 +1,136 @@ -6.0.0 / TBD -================== +# 6.0.0 / TBD - * Move the published `build/` folder to `dist/` to align with other Calypso packages - * Upgrade dependency 'debug' to 4.1.1 +- 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 -5.0.2 / 2018-10-30 -================== +# 5.0.2 / 2018-10-30 - * Fix the Chrome site isolation workaround to not break file uploads on Safari 10 +- Fix the Chrome site isolation workaround to not break file uploads on Safari 10 -5.0.1 / 2018-10-29 -================== +# 5.0.1 / 2018-10-29 - * Work around a Chrome site isolation bug when uploading files +- Work around a Chrome site isolation bug when uploading files -3.0.0 / 2016-07-27 -================== +# 3.0.0 / 2016-07-27 - * examples: add wp-api example - * client-test-app: add index.html - * Add test stuff - Add tests - Add rules to compile the browser application - * core: improve WP-API integration - detect response error in envelope mode - remove _headers from body and add a thrid response parameter in the callback - add status to headers parameter - * examples: eslint - * examples: update bundle path and global var - * create bundle file only for testing purpose - * pkg: make build in prepublish hook - * pkg: publish only useful files in npm package - * rewrite using ES6 - * Add eslint rules - * change compiling process - Use n8-make to make pre-compilation - Make bundle file using Webpack`` - * index: opt-in to `supports_error_obj` better Errors - * Use `wp-error` module for common Error handling logic - * index: add missing `uninstall()` function - * Fix TypeError on string body 'error' from rest-proxy - * Add pinghub example - * Add support for persistent connections +- examples: add wp-api example +- client-test-app: add index.html +- Add test stuff - Add tests - Add rules to compile the browser application +- core: improve WP-API integration - detect response error in envelope mode - remove \_headers from body and add a thrid response parameter in the callback - add status to headers parameter +- examples: eslint +- examples: update bundle path and global var +- create bundle file only for testing purpose +- pkg: make build in prepublish hook +- pkg: publish only useful files in npm package +- rewrite using ES6 +- Add eslint rules +- change compiling process - Use n8-make to make pre-compilation - Make bundle file using Webpack`` +- index: opt-in to `supports_error_obj` better Errors +- Use `wp-error` module for common Error handling logic +- index: add missing `uninstall()` function +- Fix TypeError on string body 'error' from rest-proxy +- Add pinghub example +- Add support for persistent connections -2.0.0 / 2016-03-11 -================== +# 2.0.0 / 2016-03-11 - * index: opt-in to `supports_error_obj` better Errors +- index: opt-in to `supports_error_obj` better Errors -1.2.0 / 2016-03-09 -================== +# 1.2.0 / 2016-03-09 - * dist: recompile - * Use `wp-error` module for common Error handling logic - * add missing LICENSE stuff - * index: add missing `uninstall()` function +- dist: recompile +- Use `wp-error` module for common Error handling logic +- add missing LICENSE stuff +- index: add missing `uninstall()` function -1.1.1 / 2016-03-08 -================== +# 1.1.1 / 2016-03-08 - * fix TypeError on string body 'error' from rest-proxy +- fix TypeError on string body 'error' from rest-proxy -1.1.0 / 2016-02-24 -================== +# 1.1.0 / 2016-02-24 - * support persistent connections e.g. websockets - * add example for connecting to Pinghub via websocket +- support persistent connections e.g. websockets +- add example for connecting to Pinghub via websocket -1.0.5 / 2015-11-22 -================== +# 1.0.5 / 2015-11-22 - * package: update "debug" to v2.2.0 +- package: update "debug" to v2.2.0 -1.0.4 / 2015-02-27 -================== +# 1.0.4 / 2015-02-27 - * dist: recompile - * wrapping try/catch on an IIFE. - * optimization for short-circuit evaluation. - * detecting support for the structured clone algorithm. - * forcing JSON string for postMessage/onmessage to circumvent IE9 limitations. +- dist: recompile +- wrapping try/catch on an IIFE. +- optimization for short-circuit evaluation. +- detecting support for the structured clone algorithm. +- forcing JSON string for postMessage/onmessage to circumvent IE9 limitations. -1.0.3 / 2015-02-09 -================== +# 1.0.3 / 2015-02-09 - * index: don't throw in the case that there's no `buffered` Array - * examples: better me.html example output +- index: don't throw in the case that there's no `buffered` Array +- examples: better me.html example output -1.0.2 / 2014-10-21 -================== +# 1.0.2 / 2014-10-21 - * Republish since npm messed up v1.0.1 +- Republish since npm messed up v1.0.1 -1.0.1 / 2014-10-21 -================== +# 1.0.1 / 2014-10-21 - * index: bail if no matching XHR instance was found - * index: use `event` module to listen for XHR events +- index: bail if no matching XHR instance was found +- index: use `event` module to listen for XHR events -1.0.0 / 2014-10-20 -================== +# 1.0.0 / 2014-10-20 - * index: refactor to not use Promise anymore - * examples: tweak "me.html" example since it no longer returns a Promise - * examples: add "progress" listeners to upload example - * examples: multiply percent complete by 100 - * examples: make "upload.html" example use user's primary blog - * package: update "debug" to v2.1.0 - * package: update "browserify" to v6.1.0 - * package: remove unused "promise" dependency +- index: refactor to not use Promise anymore +- examples: tweak "me.html" example since it no longer returns a Promise +- examples: add "progress" listeners to upload example +- examples: multiply percent complete by 100 +- examples: make "upload.html" example use user's primary blog +- package: update "debug" to v2.1.0 +- package: update "browserify" to v6.1.0 +- package: remove unused "promise" dependency -0.2.5 / 2014-06-26 -================== +# 0.2.5 / 2014-06-26 - * dist: recompile - * index: honor ports on the host page (#3, @rralian) +- dist: recompile +- index: honor ports on the host page (#3, @rralian) -0.2.4 / 2014-06-24 -================== +# 0.2.4 / 2014-06-24 - * dist: recompile - * package: update all dependencies +- dist: recompile +- package: update all dependencies -0.2.3 / 2014-06-24 -================== +# 0.2.3 / 2014-06-24 - * dist: recompile - * index: don't bother doing a debug() call for the metaAPI calls - * index: use %o debug formatter when it makes sense - * index: implement File -> ArrayBuffer manual conversion for Firefox +- dist: recompile +- index: don't bother doing a debug() call for the metaAPI calls +- index: use %o debug formatter when it makes sense +- index: implement File -> ArrayBuffer manual conversion for Firefox -0.2.2 / 2014-06-10 -================== +# 0.2.2 / 2014-06-10 - * dist: recompile - * examples: add `freshly-pressed.html` example - * package: be loose with the `debug` version +- dist: recompile +- examples: add `freshly-pressed.html` example +- package: be loose with the `debug` version -0.2.1 / 2014-06-05 -================== +# 0.2.1 / 2014-06-05 - * package: update "debug" to v1.0.0 +- package: update "debug" to v1.0.0 -0.2.0 / 2014-05-27 -================== +# 0.2.0 / 2014-05-27 - * index: update