-
Notifications
You must be signed in to change notification settings - Fork 4.3k
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
Changes from 3 commits
80702c8
9a2b944
4e16220
546a955
b794195
c92e484
9b9f5ea
2d0e19c
62ff7b9
2277dba
ff79125
40f5266
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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 ); | ||
} ); | ||
} ); |
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=' ) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It'd be nice if There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; |
There was a problem hiding this comment.
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