From bf1125209ad90f38d05fdac05f0f75152229b68d Mon Sep 17 00:00:00 2001 From: Rodney <34220863+SirRodney@users.noreply.github.com> Date: Thu, 25 Jan 2024 09:15:22 -0500 Subject: [PATCH] feat(medusa-js): add axios adapter to config (#6214) **What** - Adds the ability to pass an Axios adapter through the client's config. **Why** - When using NextJS with the edge runtime for deployment on Cloudflare Pages, for example, Axios does not work. Therefore, it is necessary to pass an adapter like [@vespaiach/axios-fetch-adapter](https://github.com/vespaiach/axios-fetch-adapter). **How** - Receives the **axiosAdapter** in the config and implements it when creating the **axiosClient**. Closes #6133 --- packages/medusa-js/src/request.ts | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/packages/medusa-js/src/request.ts b/packages/medusa-js/src/request.ts index 2f98064308..d9f928aef8 100644 --- a/packages/medusa-js/src/request.ts +++ b/packages/medusa-js/src/request.ts @@ -1,4 +1,9 @@ -import axios, { AxiosError, AxiosInstance, AxiosRequestHeaders } from "axios" +import axios, { + AxiosAdapter, + AxiosError, + AxiosInstance, + AxiosRequestHeaders, +} from "axios" import * as rax from "retry-axios" import { v4 as uuidv4 } from "uuid" @@ -18,11 +23,12 @@ export interface Config { apiKey?: string publishableApiKey?: string customHeaders?: Record + axiosAdapter?: AxiosAdapter } /** * @interface - * + * * Options to pass to requests sent to custom API Routes */ export interface RequestOptions { @@ -180,6 +186,7 @@ class Client { createClient(config: Config): AxiosInstance { const client = axios.create({ baseURL: config.baseUrl, + adapter: config.axiosAdapter, }) rax.attach(client) @@ -221,8 +228,8 @@ class Client { options: RequestOptions = {}, customHeaders: Record = {} ): Promise { - - customHeaders = { ...this.config.customHeaders, ...customHeaders } + + customHeaders = { ...this.config.customHeaders, ...customHeaders } const reqOpts = { method,