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
This commit is contained in:
1 parent
7b30512a65
commit
bf1125209a
1 file changed
+11
-4
@@ -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 * as rax from "retry-axios"
|
||||||
import { v4 as uuidv4 } from "uuid"
|
import { v4 as uuidv4 } from "uuid"
|
||||||
|
|
||||||
@@ -18,11 +23,12 @@ export interface Config {
|
|||||||
apiKey?: string
|
apiKey?: string
|
||||||
publishableApiKey?: string
|
publishableApiKey?: string
|
||||||
customHeaders?: Record<string, any>
|
customHeaders?: Record<string, any>
|
||||||
|
axiosAdapter?: AxiosAdapter
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @interface
|
* @interface
|
||||||
*
|
*
|
||||||
* Options to pass to requests sent to custom API Routes
|
* Options to pass to requests sent to custom API Routes
|
||||||
*/
|
*/
|
||||||
export interface RequestOptions {
|
export interface RequestOptions {
|
||||||
@@ -180,6 +186,7 @@ class Client {
|
|||||||
createClient(config: Config): AxiosInstance {
|
createClient(config: Config): AxiosInstance {
|
||||||
const client = axios.create({
|
const client = axios.create({
|
||||||
baseURL: config.baseUrl,
|
baseURL: config.baseUrl,
|
||||||
|
adapter: config.axiosAdapter,
|
||||||
})
|
})
|
||||||
|
|
||||||
rax.attach(client)
|
rax.attach(client)
|
||||||
@@ -221,8 +228,8 @@ class Client {
|
|||||||
options: RequestOptions = {},
|
options: RequestOptions = {},
|
||||||
customHeaders: Record<string, any> = {}
|
customHeaders: Record<string, any> = {}
|
||||||
): Promise<any> {
|
): Promise<any> {
|
||||||
|
|
||||||
customHeaders = { ...this.config.customHeaders, ...customHeaders }
|
customHeaders = { ...this.config.customHeaders, ...customHeaders }
|
||||||
|
|
||||||
const reqOpts = {
|
const reqOpts = {
|
||||||
method,
|
method,
|
||||||
|
|||||||
Reference in new issue
Block a user