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
…39683)"

This reverts commit 3a001be.
  • Loading branch information
ockham authored Feb 27, 2020
1 parent ef4e5e9 commit 96c4a75
Show file tree
Hide file tree
Showing 14 changed files with 179 additions and 180 deletions.
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/*" ]
}
204 changes: 111 additions & 93 deletions packages/wpcom-proxy-request/History.md
Original file line number Diff line number Diff line change
@@ -1,136 +1,154 @@
# 6.0.0 / TBD
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
* 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 <iframe> "src" URL
- examples: fix <script> tag src location
* index: update <iframe> "src" URL
* examples: fix <script> tag src location

# 0.1.1 / 2014-05-12
0.1.1 / 2014-05-12
==================

- examples: add `upload.html` example
- index: rename `res` variable to `body`
- index: bind to iframe "load" event before setting `.src`
* examples: add `upload.html` example
* index: rename `res` variable to `body`
* index: bind to iframe "load" event before setting `.src`

# 0.1.0 / 2014-04-22
0.1.0 / 2014-04-22
==================

- initial release
* initial release
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
Loading

0 comments on commit 96c4a75

Please sign in to comment.