Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add user locale api-fetch middleware #10862

Merged
merged 12 commits into from
Oct 23, 2018
1 change: 1 addition & 0 deletions packages/api-fetch/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
## 2.1.0 (Unreleased)

- Support `per_page=-1` paginated requests.
- Added new middleware to append `?_locale=user` to API requests ([#10862](https://github.com/WordPress/gutenberg/pull/10862)).
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's not documented anywhere the name of said middleware or how to use it.

Since we already have a section in the README, seems prudent to include there:

https://github.com/WordPress/gutenberg/tree/4e1622029e12a59639580e75c634c320a0904891/packages/api-fetch#built-in-middlewares


## 2.0.0 (2018-09-05)

Expand Down
2 changes: 2 additions & 0 deletions packages/api-fetch/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import createPreloadingMiddleware from './middlewares/preloading';
import fetchAllMiddleware from './middlewares/fetch-all-middleware';
import namespaceEndpointMiddleware from './middlewares/namespace-endpoint';
import httpV1Middleware from './middlewares/http-v1';
import userLocaleMiddleware from './middlewares/user-locale';

const middlewares = [];

Expand Down Expand Up @@ -109,6 +110,7 @@ function apiFetch( options ) {
fetchAllMiddleware,
httpV1Middleware,
namespaceEndpointMiddleware,
userLocaleMiddleware,
...middlewares,
].reverse();

Expand Down
126 changes: 126 additions & 0 deletions packages/api-fetch/src/middlewares/test/user-locale.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
/**
* Internal dependencies
*/
import userLocaleMiddleware from '../user-locale';

describe( 'User locale middleware', () => {
it( 'should append the _locale parameter to the path', () => {
expect.hasAssertions();

const requestOptions = {
method: 'GET',
path: '/wp/v2/posts',
};

const callback = ( options ) => {
expect( options.path ).toBe( '/wp/v2/posts?_locale=user' );
};

userLocaleMiddleware( requestOptions, callback );
} );

it( 'should append the _locale parameter to path with existing query argument', () => {
expect.hasAssertions();

const requestOptions = {
method: 'GET',
path: '/wp/v2/posts?foo=bar',
};

const callback = ( options ) => {
expect( options.path ).toBe( '/wp/v2/posts?foo=bar&_locale=user' );
};

userLocaleMiddleware( requestOptions, callback );
} );

it( 'does not modify existing single _locale parameter in path', () => {
expect.hasAssertions();

const requestOptions = {
method: 'GET',
path: '/wp/v2/posts?_locale=foo',
};

const callback = ( options ) => {
expect( options.path ).toBe( '/wp/v2/posts?_locale=foo' );
};

userLocaleMiddleware( requestOptions, callback );
} );

it( 'does not modify existing _locale parameter in path', () => {
expect.hasAssertions();

const requestOptions = {
method: 'GET',
path: '/wp/v2/posts?foo=bar&_locale=foo',
};

const callback = ( options ) => {
expect( options.path ).toBe( '/wp/v2/posts?foo=bar&_locale=foo' );
};

userLocaleMiddleware( requestOptions, callback );
} );

it( 'should append the _locale parameter to the url', () => {
expect.hasAssertions();

const requestOptions = {
method: 'GET',
url: 'http://wp.org/wp-json/wp/v2/posts',
};

const callback = ( options ) => {
expect( options.url ).toBe( 'http://wp.org/wp-json/wp/v2/posts?_locale=user' );
};

userLocaleMiddleware( requestOptions, callback );
} );

it( 'should append the _locale parameter to url with existing query argument', () => {
expect.hasAssertions();

const requestOptions = {
method: 'GET',
url: 'http://wp.org/wp-json/wp/v2/posts?foo=bar',
};

const callback = ( options ) => {
expect( options.url ).toBe( 'http://wp.org/wp-json/wp/v2/posts?foo=bar&_locale=user' );
};

userLocaleMiddleware( requestOptions, callback );
} );

it( 'does not modify existing single _locale parameter in url', () => {
expect.hasAssertions();

const requestOptions = {
method: 'GET',
url: 'http://wp.org/wp-json/wp/v2/posts?_locale=foo',
};

const callback = ( options ) => {
expect( options.url ).toBe( 'http://wp.org/wp-json/wp/v2/posts?_locale=foo' );
};

userLocaleMiddleware( requestOptions, callback );
} );

it( 'does not modify existing _locale parameter in url', () => {
expect.hasAssertions();

const requestOptions = {
method: 'GET',
url: 'http://wp.org/wp-json/wp/v2/posts?foo=bar&_locale=foo',
};

const callback = ( options ) => {
expect( options.url ).toBe( 'http://wp.org/wp-json/wp/v2/posts?foo=bar&_locale=foo' );
};

userLocaleMiddleware( requestOptions, callback );
} );
} );
26 changes: 26 additions & 0 deletions packages/api-fetch/src/middlewares/user-locale.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/**
* WordPress dependencies
*/
import { addQueryArgs } from '@wordpress/url';

function userLocaleMiddleware( options, next ) {
swissspidy marked this conversation as resolved.
Show resolved Hide resolved
if (
typeof options.url === 'string' &&
-1 === options.url.indexOf( '?_locale=' ) &&
-1 === options.url.indexOf( '&_locale=' )
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It'd be nice if @wordpress/url had a hasQueryArg helper... Not critical for this PR though.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@danielbachhuber Ha :-) I thought the same and created #10879 because of this.

) {
options.url = addQueryArgs( options.url, { _locale: 'user' } );
}

if (
typeof options.path === 'string' &&
-1 === options.path.indexOf( '?_locale=' ) &&
-1 === options.path.indexOf( '&_locale=' )
) {
options.path = addQueryArgs( options.path, { _locale: 'user' } );
}

return next( options, next );
}

export default userLocaleMiddleware;