feat(): Add support for locale to the js sdk (#14306)
* feat(): Add support for locale to the js sdk * Create great-icons-thank.md
This commit is contained in:
@@ -0,0 +1,5 @@
|
|||||||
|
---
|
||||||
|
"@medusajs/js-sdk": patch
|
||||||
|
---
|
||||||
|
|
||||||
|
feat(): Add support for locale to the js sdk
|
||||||
@@ -10,6 +10,7 @@ import {
|
|||||||
} from "./types"
|
} from "./types"
|
||||||
|
|
||||||
export const PUBLISHABLE_KEY_HEADER = "x-publishable-api-key"
|
export const PUBLISHABLE_KEY_HEADER = "x-publishable-api-key"
|
||||||
|
export const LOCALE_STORAGE_KEY = "medusa_locale"
|
||||||
|
|
||||||
// We want to explicitly retrieve the base URL instead of relying on relative paths that differ in behavior between browsers.
|
// We want to explicitly retrieve the base URL instead of relying on relative paths that differ in behavior between browsers.
|
||||||
const getBaseUrl = (passedBaseUrl: string) => {
|
const getBaseUrl = (passedBaseUrl: string) => {
|
||||||
@@ -112,6 +113,18 @@ export class Client {
|
|||||||
private DEFAULT_JWT_STORAGE_KEY = "medusa_auth_token"
|
private DEFAULT_JWT_STORAGE_KEY = "medusa_auth_token"
|
||||||
private token = ""
|
private token = ""
|
||||||
|
|
||||||
|
private locale_ = ""
|
||||||
|
|
||||||
|
get locale() {
|
||||||
|
if (hasStorage("localStorage")) {
|
||||||
|
const storedLocale = window.localStorage.getItem(LOCALE_STORAGE_KEY)
|
||||||
|
if (storedLocale) {
|
||||||
|
return storedLocale
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return this.locale_
|
||||||
|
}
|
||||||
|
|
||||||
constructor(config: Config) {
|
constructor(config: Config) {
|
||||||
this.config = { ...config, baseUrl: getBaseUrl(config.baseUrl) }
|
this.config = { ...config, baseUrl: getBaseUrl(config.baseUrl) }
|
||||||
const logger = config.logger || {
|
const logger = config.logger || {
|
||||||
@@ -126,9 +139,20 @@ export class Client {
|
|||||||
debug: config.debug ? logger.debug : () => {},
|
debug: config.debug ? logger.debug : () => {},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (hasStorage("localStorage")) {
|
||||||
|
this.locale_ = window.localStorage.getItem(LOCALE_STORAGE_KEY) || ""
|
||||||
|
}
|
||||||
|
|
||||||
this.fetch_ = this.initClient()
|
this.fetch_ = this.initClient()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
setLocale(locale: string) {
|
||||||
|
if (hasStorage("localStorage")) {
|
||||||
|
window.localStorage.setItem(LOCALE_STORAGE_KEY, locale)
|
||||||
|
}
|
||||||
|
this.locale_ = locale
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* `fetch` closely follows (and uses under the hood) the native `fetch` API. There are, however, few key differences:
|
* `fetch` closely follows (and uses under the hood) the native `fetch` API. There are, however, few key differences:
|
||||||
* - Non 2xx statuses throw a `FetchError` with the status code as the `status` property, rather than resolving the promise
|
* - Non 2xx statuses throw a `FetchError` with the status code as the `status` property, rather than resolving the promise
|
||||||
@@ -226,10 +250,12 @@ export class Client {
|
|||||||
// We always want to fetch the up-to-date JWT token before firing off a request.
|
// We always want to fetch the up-to-date JWT token before firing off a request.
|
||||||
const headers = new Headers(defaultHeaders)
|
const headers = new Headers(defaultHeaders)
|
||||||
const customHeaders = {
|
const customHeaders = {
|
||||||
|
"content-language": this.locale,
|
||||||
...this.config.globalHeaders,
|
...this.config.globalHeaders,
|
||||||
...(await this.getJwtHeader_()),
|
...(await this.getJwtHeader_()),
|
||||||
...init?.headers,
|
...init?.headers,
|
||||||
}
|
}
|
||||||
|
|
||||||
// We use `headers.set` in order to ensure headers are overwritten in a case-insensitive manner.
|
// We use `headers.set` in order to ensure headers are overwritten in a case-insensitive manner.
|
||||||
Object.entries(customHeaders).forEach(([key, value]) => {
|
Object.entries(customHeaders).forEach(([key, value]) => {
|
||||||
if (value === null) {
|
if (value === null) {
|
||||||
|
|||||||
@@ -18,6 +18,14 @@ class Medusa {
|
|||||||
this.store = new Store(this.client)
|
this.store = new Store(this.client)
|
||||||
this.auth = new Auth(this.client, config)
|
this.auth = new Auth(this.client, config)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
setLocale(locale: string) {
|
||||||
|
this.client.setLocale(locale)
|
||||||
|
}
|
||||||
|
|
||||||
|
getLocale() {
|
||||||
|
return this.client.locale
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export default Medusa
|
export default Medusa
|
||||||
|
|||||||
Reference in New Issue
Block a user