When interacting with the network in our frontend, we use the @simonbackx/simple-networking library. This library handles a couple of things for us, such as providing middleware support (which enabled retry behaviour when there are network issues, auto renewing access tokens…). It also provides support for canceling requests and associating owners with requests.
A server is a combination of middlewares and the API host you want to call:
const server = new Server("https://"+STAMHOOFD.domains.api)
server.middlewares.push(this)
In Stamhoofd NetworkManager provides the main server to communicate with the API. It also self assigns it as a middleware. This middleware is implemented in methods such as shouldRetryNetworkError and definces how we handle errors, timeouts, retries, in the frontend.
Key features
NetworkManager will present an update dialog.Some classes expand on this, such as the SessionContext. The sessionContext provides two servers: one with authentication and one without. The authenticated server will auto inject the Authorization header in every HTTP request (which contains the current access_token) - so the server knows the currently signed in user.
Key features
You can access the current context from anywhere in Vue using the useContext hook:
<script lang="ts" setup>
const context = useContext();
const owner = useRequestOwner();
async function doRequest(patch: AutoEncoderPatchType<Organization>) {
await context.value.authenticatedServer.request({
method: 'PATCH',
path: '/organizations',
body: patch,
shouldRetry: false,
owner
});
}
</script>
GET requests