@@ -7,6 +7,7 @@ export default class Client {
77 baseUrl ;
88 fetchClient ;
99 fetchOptions ;
10+ locale ;
1011 onError ;
1112 onResponse ;
1213 removeEmbedded ;
@@ -18,6 +19,7 @@ export default class Client {
1819 * @param {string } [options.baseUrl=window.location.origin] - The base URL for the API.
1920 * @param {function } [options.fetchClient=fetch.bind(window)] - The fetch client to use.
2021 * @param {Object } [options.fetchOptions={}] - The fetch client options.
22+ * @param {string } [options.locale=''] - The locale for every request.
2123 * @param {function } [options.onError=() => {}] - The function to call on error.
2224 * @param {function } [options.onResponse=(r) => r] - The function to call on response.
2325 * @param {boolean } [options.removeEmbedded=false] - Whether to remove the _embedded layer from the response if present.
@@ -26,13 +28,15 @@ export default class Client {
2628 baseUrl = window . location . origin ,
2729 fetchClient = fetch . bind ( window ) ,
2830 fetchOptions = { } ,
31+ locale = '' ,
2932 onError = ( ) => { } ,
3033 onResponse = ( r ) => r ,
3134 removeEmbedded = false ,
3235 } ) {
3336 this . baseUrl = baseUrl ;
3437 this . fetchClient = fetchClient ;
3538 this . fetchOptions = fetchOptions ;
39+ this . locale = locale ;
3640 this . onError = onError ;
3741 this . onResponse = onResponse ;
3842 this . removeEmbedded = removeEmbedded ;
@@ -43,10 +47,11 @@ export default class Client {
4347 *
4448 * @param {string } path - The path for the URL.
4549 * @param {Object } [params={}] - The query parameters for the URL.
50+ * @param {boolean } [withLocale=true] - Whether to prepend locale to path.
4651 * @returns {URL } The built URL.
4752 */
48- buildUrl ( path , params = { } ) {
49- const url = new URL ( path , this . baseUrl ) ;
53+ buildUrl ( path , params = { } , withLocale = true ) {
54+ const url = new URL ( ( this . locale && withLocale ? `/ ${ this . locale } ` : '' ) + path , this . baseUrl ) ;
5055 url . search = new URLSearchParams ( params ) ;
5156
5257 return url ;
@@ -56,15 +61,16 @@ export default class Client {
5661 * Sends a request to the API with the given path and query parameters.
5762 *
5863 * @param {string } path - The path for the request.
59- * @param {Object } [params={}] - The query parameters for the request.
64+ * @param {Object } [params] - The query parameters for the request.
65+ * @param {boolean } [withLocale] - Whether to build a localized URL.
6066 * @returns {Promise<Object> } A Promise that resolves to the request's JSON.
6167 */
62- async request ( path , params = { } ) {
68+ async request ( path , params , withLocale ) {
6369 let response = null ;
6470
6571 try {
6672 response = await this . fetchClient (
67- this . buildUrl ( path , params ) . toString ( ) ,
73+ this . buildUrl ( path , params , withLocale ) . toString ( ) ,
6874 this . fetchOptions
6975 ) ;
7076 } catch ( error ) {
@@ -98,7 +104,7 @@ export default class Client {
98104 * @returns {Promise<Object> } A Promise that resolves to the page data.
99105 */
100106 getPageByPath ( path ) {
101- return this . request ( `${ path } .json` ) ;
107+ return this . request ( `${ path } .json` , null , false ) ;
102108 }
103109
104110 /**
0 commit comments