Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add optional pre-request hook to Axios fetcher #341

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/Client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ class Client {
...customOptions
}

const fetcherOptions: CreateFetcherConfig = { host: options.host }
const fetcherOptions: CreateFetcherConfig = { host: options.host, beforeRequestFunction: options?.beforeRequestFunction }

this.fetcher = options.createFetcher(fetcherOptions)

Expand Down
7 changes: 4 additions & 3 deletions src/Http.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,13 +69,14 @@ export default class Http {
}

protected processError(error: Error): SpreeSDKError {
if (error instanceof FetchError) {
if (error.response) {
if ((error as FetchError)?.response) {
const fetchError = (error as FetchError)
if (fetchError.response) {
// Error from Spree outside HTTP 2xx codes
return this.processSpreeError(error)
}

if (error.request) {
if (fetchError.request) {
// No response received from Spree
return new NoResponseError()
}
Expand Down
3 changes: 3 additions & 0 deletions src/fetchers/createAxiosFetcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ const createAxiosFetcher: CreateFetcher = (fetcherOptions) => {
headers: { 'Content-Type': 'application/json' },
paramsSerializer: (params) => objectToQuerystring(params)
})
if (fetcherOptions.beforeRequestFunction){
fetcherOptions?.beforeRequestFunction(axios)
}

return {
fetch: async (fetchOptions) => {
Expand Down
4 changes: 4 additions & 0 deletions src/fetchers/createFetchFetcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ const createCustomizedFetchFetcher: CreateCustomizedFetchFetcher = (fetcherOptio

const { host, fetch, requestConstructor } = fetcherOptions

if (fetcherOptions.beforeRequestFunction){
fetcherOptions?.beforeRequestFunction()
}

return {
fetch: async (fetchOptions) => {
try {
Expand Down
3 changes: 3 additions & 0 deletions src/interfaces/ClientConfig.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type { FetchConfig } from './FetchConfig'
import { AxiosInstance } from 'axios'

export type Fetcher = {
fetch: (options: FetchConfig) => Promise<{ data: any }>
Expand All @@ -8,10 +9,12 @@ export type CreateFetcher = (options: CreateFetcherConfig) => Fetcher

export type CreateFetcherConfig = {
host: string
beforeRequestFunction?(axios?: AxiosInstance): any
}

export type FetcherConfig = {
createFetcher: CreateFetcher
}


export type IClientConfig = CreateFetcherConfig & FetcherConfig