Last active
August 16, 2023 17:44
-
-
Save ahmedsayedabdelsalam/ee093a934d0b149b19790fa715f86ce2 to your computer and use it in GitHub Desktop.
tenancy-livewire
This file contains hidden or 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
export default function(tenant, headerKey = 'X-Tenant') { | |
handleLivewireCalls(tenant, headerKey) | |
handleFetchCalls(tenant, headerKey) | |
handleXMLHttpCalls(tenant, headerKey) | |
handleAxiosCalls(tenant, headerKey) | |
} | |
function handleLivewireCalls(tenant, headerKey) { | |
if (! window.Livewire) return; | |
window.Livewire.connection.headers = { | |
...window.Livewire.connection.headers, | |
[headerKey]: tenant | |
}; | |
} | |
function handleFetchCalls(tenant, headerKey) { | |
const { fetch: originalFetch } = window; | |
window.fetch = async (...args) => { | |
let [resource, config = {} ] = args; | |
config.headers = { | |
...config.headers, | |
[headerKey]: tenant | |
} | |
return await originalFetch(resource, config); | |
}; | |
} | |
function handleXMLHttpCalls(tenant, headerKey) { | |
XMLHttpRequest.prototype.orignalOpen = XMLHttpRequest.prototype.open; | |
XMLHttpRequest.prototype.open = function () { | |
this.orignalOpen.apply(this, arguments); | |
this.setRequestHeader(headerKey, tenant); | |
}; | |
} | |
function handleAxiosCalls(tenant, headerKey) { | |
if (! window.axios) return; | |
window.axios.defaults.headers.common[headerKey] = tenant; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
add this meta tag to your layout
import it in bootstrap.js in laravel app