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
6
.changeset/three-kiwis-shave.md
Normal file
6
.changeset/three-kiwis-shave.md
Normal file
@@ -0,0 +1,6 @@
|
|||||||
|
---
|
||||||
|
"@medusajs/framework": patch
|
||||||
|
"@medusajs/js-sdk": patch
|
||||||
|
---
|
||||||
|
|
||||||
|
chore(): Update locale header usage
|
||||||
@@ -36,10 +36,10 @@ describe("applyLocale", () => {
|
|||||||
expect(nextFunction).toHaveBeenCalledTimes(1)
|
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.query = {}
|
||||||
;(mockRequest.get as jest.Mock).mockImplementation((header: string) => {
|
;(mockRequest.get as jest.Mock).mockImplementation((header: string) => {
|
||||||
if (header === "content-language") {
|
if (header === "x-medusa-locale") {
|
||||||
return "fr-FR"
|
return "fr-FR"
|
||||||
}
|
}
|
||||||
return undefined
|
return undefined
|
||||||
@@ -51,10 +51,10 @@ describe("applyLocale", () => {
|
|||||||
expect(nextFunction).toHaveBeenCalledTimes(1)
|
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.query = { locale: "de-DE" }
|
||||||
;(mockRequest.get as jest.Mock).mockImplementation((header: string) => {
|
;(mockRequest.get as jest.Mock).mockImplementation((header: string) => {
|
||||||
if (header === "content-language") {
|
if (header === "x-medusa-locale") {
|
||||||
return "fr-FR"
|
return "fr-FR"
|
||||||
}
|
}
|
||||||
return undefined
|
return undefined
|
||||||
@@ -80,7 +80,7 @@ describe("applyLocale", () => {
|
|||||||
it("should handle empty string in query parameter", async () => {
|
it("should handle empty string in query parameter", async () => {
|
||||||
mockRequest.query = { locale: "" }
|
mockRequest.query = { locale: "" }
|
||||||
;(mockRequest.get as jest.Mock).mockImplementation((header: string) => {
|
;(mockRequest.get as jest.Mock).mockImplementation((header: string) => {
|
||||||
if (header === "content-language") {
|
if (header === "x-medusa-locale") {
|
||||||
return "es-ES"
|
return "es-ES"
|
||||||
}
|
}
|
||||||
return undefined
|
return undefined
|
||||||
|
|||||||
@@ -5,14 +5,14 @@ import type {
|
|||||||
MedusaResponse,
|
MedusaResponse,
|
||||||
} from "../types"
|
} from "../types"
|
||||||
|
|
||||||
const CONTENT_LANGUAGE_HEADER = "content-language"
|
const CONTENT_LANGUAGE_HEADER = "x-medusa-locale"
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Middleware that resolves the locale for the current request.
|
* Middleware that resolves the locale for the current request.
|
||||||
*
|
*
|
||||||
* Resolution order:
|
* Resolution order:
|
||||||
* 1. Query parameter `?locale=en-US`
|
* 1. Query parameter `?locale=en-US`
|
||||||
* 2. Content-Language header
|
* 2. x-medusa-locale header
|
||||||
*
|
*
|
||||||
* The resolved locale is set on `req.locale`.
|
* The resolved locale is set on `req.locale`.
|
||||||
*/
|
*/
|
||||||
@@ -29,7 +29,7 @@ export async function applyLocale(
|
|||||||
return next()
|
return next()
|
||||||
}
|
}
|
||||||
|
|
||||||
// 2. Check Content-Language header
|
// 2. Check x-medusa-locale header
|
||||||
const headerLocale = req.get(CONTENT_LANGUAGE_HEADER)
|
const headerLocale = req.get(CONTENT_LANGUAGE_HEADER)
|
||||||
if (headerLocale) {
|
if (headerLocale) {
|
||||||
req.locale = normalizeLocale(headerLocale)
|
req.locale = normalizeLocale(headerLocale)
|
||||||
|
|||||||
@@ -187,7 +187,7 @@ export interface MedusaRequest<
|
|||||||
/**
|
/**
|
||||||
* The locale for the current request, resolved from:
|
* The locale for the current request, resolved from:
|
||||||
* 1. Query parameter `?locale=`
|
* 1. Query parameter `?locale=`
|
||||||
* 2. Content-Language header
|
* 2. x-medusa-locale header
|
||||||
* 3. Store's default locale
|
* 3. Store's default locale
|
||||||
*/
|
*/
|
||||||
locale?: string
|
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.
|
// 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,
|
"x-medusa-locale": this.locale,
|
||||||
...this.config.globalHeaders,
|
...this.config.globalHeaders,
|
||||||
...(await this.getJwtHeader_()),
|
...(await this.getJwtHeader_()),
|
||||||
...init?.headers,
|
...init?.headers,
|
||||||
|
|||||||
Reference in New Issue
Block a user