From fe25c8a91f4462c9ac5a15594fa71155df48c1eb Mon Sep 17 00:00:00 2001 From: Lacey Pevey <7490308+pevey@users.noreply.github.com> Date: Wed, 28 Jun 2023 07:04:58 -0600 Subject: [PATCH] feat(medusa-react,medusa-js): Allow custom headers (#4409) * add support for passing custom headers to backend through medusa-js and medusa-react * switch order of override of headers * Create gentle-teachers-greet.md --------- Co-authored-by: Oliver Windall Juhl <59018053+olivermrbl@users.noreply.github.com> --- .changeset/gentle-teachers-greet.md | 6 ++++++ packages/medusa-js/src/request.ts | 4 ++++ packages/medusa-react/src/contexts/medusa.tsx | 3 +++ 3 files changed, 13 insertions(+) create mode 100644 .changeset/gentle-teachers-greet.md diff --git a/.changeset/gentle-teachers-greet.md b/.changeset/gentle-teachers-greet.md new file mode 100644 index 0000000000..0fe506d5f8 --- /dev/null +++ b/.changeset/gentle-teachers-greet.md @@ -0,0 +1,6 @@ +--- +"@medusajs/medusa-js": patch +"medusa-react": patch +--- + +feat(medusa-react,medusa-js): Allow custom headers diff --git a/packages/medusa-js/src/request.ts b/packages/medusa-js/src/request.ts index 20194fd5e4..6c2252bd4c 100644 --- a/packages/medusa-js/src/request.ts +++ b/packages/medusa-js/src/request.ts @@ -16,6 +16,7 @@ export interface Config { maxRetries: number apiKey?: string publishableApiKey?: string + customHeaders?: Record } export interface RequestOptions { @@ -199,6 +200,9 @@ class Client { options: RequestOptions = {}, customHeaders: Record = {} ): Promise { + + customHeaders = { ...this.config.customHeaders, ...customHeaders } + const reqOpts = { method, withCredentials: true, diff --git a/packages/medusa-react/src/contexts/medusa.tsx b/packages/medusa-react/src/contexts/medusa.tsx index a7ad14c776..0cec47ed6f 100644 --- a/packages/medusa-react/src/contexts/medusa.tsx +++ b/packages/medusa-react/src/contexts/medusa.tsx @@ -32,6 +32,7 @@ interface MedusaProviderProps { * available within the request */ publishableApiKey?: string + customHeaders?: Record } export const MedusaProvider = ({ @@ -39,6 +40,7 @@ export const MedusaProvider = ({ baseUrl, apiKey, publishableApiKey, + customHeaders, children, }: MedusaProviderProps) => { const medusaClient = new Medusa({ @@ -46,6 +48,7 @@ export const MedusaProvider = ({ maxRetries: 0, apiKey, publishableApiKey, + customHeaders }) return (