-
-
Notifications
You must be signed in to change notification settings - Fork 45
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(plugin): add a plugin mecanism to zodios
- Loading branch information
Showing
6 changed files
with
163 additions
and
113 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import { AxiosRequestConfig } from "axios"; | ||
import { Zodios } from "../zodios"; | ||
import { ZodiosEnpointDescriptions } from "../zodios.types"; | ||
|
||
function createRequestInterceptor() { | ||
return async (config: AxiosRequestConfig) => { | ||
config.withCredentials = true; | ||
// istanbul ignore next | ||
if (!config.headers) { | ||
config.headers = {}; | ||
} | ||
if (config.method !== "get") { | ||
config.headers = { | ||
...config.headers, | ||
"Content-Type": "application/json", | ||
Accept: "application/json", | ||
}; | ||
} else { | ||
config.headers = { | ||
...config.headers, | ||
Accept: "application/json", | ||
}; | ||
} | ||
return config; | ||
}; | ||
} | ||
|
||
/** | ||
* plugin that add application/json header to all requests | ||
* @param zodios | ||
*/ | ||
export function pluginApi<Api extends ZodiosEnpointDescriptions>() { | ||
return (zodios: Zodios<Api>) => { | ||
zodios.axios.interceptors.request.use(createRequestInterceptor()); | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
import axios, { AxiosInstance, AxiosRequestConfig } from "axios"; | ||
import { ReadonlyDeep } from "../utils.types"; | ||
import { Zodios } from "../zodios"; | ||
import { | ||
AxiosRetryRequestConfig, | ||
TokenProvider, | ||
ZodiosEndpointDescription, | ||
ZodiosEnpointDescriptions, | ||
} from "../zodios.types"; | ||
|
||
function createRequestInterceptor(provider: TokenProvider) { | ||
return async (config: AxiosRequestConfig) => { | ||
const token = await provider.getToken(); | ||
if (token) { | ||
config.headers = { | ||
...config.headers, | ||
Authorization: `Bearer ${token}`, | ||
}; | ||
} | ||
return config; | ||
}; | ||
} | ||
|
||
function createResponseInterceptor( | ||
instance: AxiosInstance, | ||
provider: TokenProvider | ||
) { | ||
return async (error: Error) => { | ||
if (axios.isAxiosError(error) && provider.renewToken) { | ||
const retryConfig = error.config as AxiosRetryRequestConfig; | ||
if (error.response?.status === 401 && !retryConfig.retried) { | ||
retryConfig.retried = true; | ||
provider.renewToken(); | ||
return instance.request(retryConfig); | ||
} | ||
} | ||
throw error; | ||
}; | ||
} | ||
|
||
export function pluginToken<Api extends ZodiosEnpointDescriptions>( | ||
provider: TokenProvider | ||
) { | ||
return (zodios: Zodios<Api>) => { | ||
zodios.axios.interceptors.request.use(createRequestInterceptor(provider)); | ||
if (provider?.renewToken) { | ||
zodios.axios.interceptors.response.use( | ||
undefined, | ||
createResponseInterceptor(zodios.axios, provider) | ||
); | ||
} | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters