-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(commons): Add withEventConsistencyHandling interceptor to config…
…ure automatic header handling for a client intstance
- Loading branch information
Lukas Fritze
committed
Mar 20, 2024
1 parent
c23e0af
commit 9fdbd96
Showing
5 changed files
with
65 additions
and
5 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,39 @@ | ||
import { AxiosResponseHeaders, AxiosInstance, AxiosRequestConfig } from "axios"; | ||
import ApiClientBase from "../core/ApiClientBase.js"; | ||
|
||
function isMutatingRequest(request: AxiosRequestConfig): boolean { | ||
return ["post", "put", "delete", "patch"].includes( | ||
request.method?.toLowerCase() ?? "", | ||
); | ||
} | ||
|
||
function configureConsistencyHandlingInterceptor(axios: AxiosInstance): void { | ||
let lastEventId: string | undefined = undefined; | ||
|
||
axios.interceptors.request.use((config) => { | ||
if (lastEventId !== undefined && !isMutatingRequest(config)) { | ||
config.headers["if-event-reached"] = lastEventId; | ||
} | ||
return config; | ||
}); | ||
|
||
axios.interceptors.response.use((response) => { | ||
const headers = response.headers as AxiosResponseHeaders; | ||
|
||
if (headers.has("etag") && isMutatingRequest(response.config)) { | ||
lastEventId = headers.get("etag") as string; | ||
} | ||
return response; | ||
}); | ||
} | ||
|
||
export function withEventConsistencyHandling< | ||
T extends ApiClientBase | AxiosInstance, | ||
>(clientOrAxiosInstance: T): T { | ||
const axiosInstance: AxiosInstance = | ||
clientOrAxiosInstance instanceof ApiClientBase | ||
? clientOrAxiosInstance.axios | ||
: clientOrAxiosInstance; | ||
configureConsistencyHandlingInterceptor(axiosInstance); | ||
return clientOrAxiosInstance; | ||
} |
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 |
---|---|---|
@@ -1 +1,2 @@ | ||
export * from "./accessToken.js"; | ||
export * from "./consistencyHandling.js"; |
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 |
---|---|---|
@@ -1,13 +1,14 @@ | ||
{ | ||
"compilerOptions": { | ||
"esModuleInterop": true, | ||
"stripInternal": true, | ||
"target": "ES2022", | ||
"declaration": true, | ||
"esModuleInterop": true, | ||
"forceConsistentCasingInFileNames": true, | ||
"lib": ["ES2022", "dom"], | ||
"module": "NodeNext", | ||
"moduleResolution": "NodeNext", | ||
"forceConsistentCasingInFileNames": true, | ||
"skipLibCheck": true, | ||
"strict": true, | ||
"skipLibCheck": true | ||
"stripInternal": true, | ||
"target": "ES2022" | ||
} | ||
} |