Add onMigrationDetected function

This commit is contained in:
Adria Navarro 2023-12-12 13:42:01 +01:00
parent 005dd27663
commit 50da799216
1 changed files with 18 additions and 1 deletions

View File

@ -63,6 +63,11 @@ const defaultAPIClientConfig = {
* invoked before the actual JS error is thrown up the stack. * invoked before the actual JS error is thrown up the stack.
*/ */
onError: null, onError: null,
/**
* A function can be passed to be called when an API call returns info about a migration running for a specific app
*/
onMigrationDetected: null,
} }
/** /**
@ -171,6 +176,7 @@ export const createAPIClient = config => {
// Handle response // Handle response
if (response.status >= 200 && response.status < 400) { if (response.status >= 200 && response.status < 400) {
handleMigrations(response)
try { try {
if (parseResponse) { if (parseResponse) {
return await parseResponse(response) return await parseResponse(response)
@ -187,6 +193,17 @@ export const createAPIClient = config => {
} }
} }
const handleMigrations = response => {
if (!config.onMigrationDetected) {
return
}
const migration = response.headers.get(Header.MIGRATING_APP)
if (migration) {
config.onMigrationDetected(migration)
}
}
// Performs an API call to the server and caches the response. // Performs an API call to the server and caches the response.
// Future invocation for this URL will return the cached result instead of // Future invocation for this URL will return the cached result instead of
// hitting the server again. // hitting the server again.