Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Enable axios fetcher to get custom function
  • Loading branch information
rafalkosla101 committed Aug 3, 2022
commit 456da650b4cb1a17d5c15b7ccb4c202ab8724d48
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
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
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