chore(): Update locale header usage (#14318)
* chore(): Update locale header usage * Create three-kiwis-shave.md * chore(): Update locale header usage
This commit is contained in:
committed by
GitHub
parent
e94e1a4676
commit
c8a7122ba9
@@ -36,10 +36,10 @@ describe("applyLocale", () => {
|
||||
expect(nextFunction).toHaveBeenCalledTimes(1)
|
||||
})
|
||||
|
||||
it("should set locale from Content-Language header when query param is not present", async () => {
|
||||
it("should set locale from x-medusa-locale header when query param is not present", async () => {
|
||||
mockRequest.query = {}
|
||||
;(mockRequest.get as jest.Mock).mockImplementation((header: string) => {
|
||||
if (header === "content-language") {
|
||||
if (header === "x-medusa-locale") {
|
||||
return "fr-FR"
|
||||
}
|
||||
return undefined
|
||||
@@ -51,10 +51,10 @@ describe("applyLocale", () => {
|
||||
expect(nextFunction).toHaveBeenCalledTimes(1)
|
||||
})
|
||||
|
||||
it("should prioritize query parameter over Content-Language header", async () => {
|
||||
it("should prioritize query parameter over x-medusa-locale header", async () => {
|
||||
mockRequest.query = { locale: "de-DE" }
|
||||
;(mockRequest.get as jest.Mock).mockImplementation((header: string) => {
|
||||
if (header === "content-language") {
|
||||
if (header === "x-medusa-locale") {
|
||||
return "fr-FR"
|
||||
}
|
||||
return undefined
|
||||
@@ -80,7 +80,7 @@ describe("applyLocale", () => {
|
||||
it("should handle empty string in query parameter", async () => {
|
||||
mockRequest.query = { locale: "" }
|
||||
;(mockRequest.get as jest.Mock).mockImplementation((header: string) => {
|
||||
if (header === "content-language") {
|
||||
if (header === "x-medusa-locale") {
|
||||
return "es-ES"
|
||||
}
|
||||
return undefined
|
||||
|
||||
@@ -5,14 +5,14 @@ import type {
|
||||
MedusaResponse,
|
||||
} from "../types"
|
||||
|
||||
const CONTENT_LANGUAGE_HEADER = "content-language"
|
||||
const CONTENT_LANGUAGE_HEADER = "x-medusa-locale"
|
||||
|
||||
/**
|
||||
* Middleware that resolves the locale for the current request.
|
||||
*
|
||||
* Resolution order:
|
||||
* 1. Query parameter `?locale=en-US`
|
||||
* 2. Content-Language header
|
||||
* 2. x-medusa-locale header
|
||||
*
|
||||
* The resolved locale is set on `req.locale`.
|
||||
*/
|
||||
@@ -29,7 +29,7 @@ export async function applyLocale(
|
||||
return next()
|
||||
}
|
||||
|
||||
// 2. Check Content-Language header
|
||||
// 2. Check x-medusa-locale header
|
||||
const headerLocale = req.get(CONTENT_LANGUAGE_HEADER)
|
||||
if (headerLocale) {
|
||||
req.locale = normalizeLocale(headerLocale)
|
||||
|
||||
@@ -187,7 +187,7 @@ export interface MedusaRequest<
|
||||
/**
|
||||
* The locale for the current request, resolved from:
|
||||
* 1. Query parameter `?locale=`
|
||||
* 2. Content-Language header
|
||||
* 2. x-medusa-locale header
|
||||
* 3. Store's default locale
|
||||
*/
|
||||
locale?: string
|
||||
|
||||
@@ -250,7 +250,7 @@ export class Client {
|
||||
// We always want to fetch the up-to-date JWT token before firing off a request.
|
||||
const headers = new Headers(defaultHeaders)
|
||||
const customHeaders = {
|
||||
"content-language": this.locale,
|
||||
"x-medusa-locale": this.locale,
|
||||
...this.config.globalHeaders,
|
||||
...(await this.getJwtHeader_()),
|
||||
...init?.headers,
|
||||
|
||||
Reference in New Issue
Block a user