Skip to content

Commit

Permalink
Add user locale api-fetch middleware
Browse files Browse the repository at this point in the history
See #10811
  • Loading branch information
swissspidy committed Oct 21, 2018
1 parent dee3dcf commit 80702c8
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 0 deletions.
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
66 changes: 66 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,66 @@
/**
* 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( '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 );
} );
} );
21 changes: 21 additions & 0 deletions packages/api-fetch/src/middlewares/user-locale.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
function userLocaleMiddleware( options, next ) {
if ( typeof options.url === 'string' ) {
if ( -1 !== options.url.indexOf( '?' ) ) {
options.url += '&_locale=user';
} else {
options.url += '?_locale=user';
}
}

if ( typeof options.path === 'string' ) {
if ( -1 !== options.path.indexOf( '?' ) ) {
options.path += '&_locale=user';
} else {
options.path += '?_locale=user';
}
}

return next( options, next );
}

export default userLocaleMiddleware;

0 comments on commit 80702c8

Please sign in to comment.