This gives access to everything included in metro, like the json and thrower middleware. If you want a lighter version, use the exact src/ files, e.g.:
The rest of this document assumes the functions provided by MetroJS will be available as metro.*.
MetroJS is built on top of the Fetch API, which is standard in all modern web browsers and in Node from version 18. If you are not familiar with this API, it is recommended to read the mdn documentation linked here first.
The main difference with the Fetch API here is that there is a metro.client object, which can be re-used for multiple requests. It also allows you to set default options for subsequent requests.
In addition, the single fetch() function is replaced with seperate functions for each HTTP method. In this case get().
The jsonmw middleware automatically converts data to json, adds the correct headers, and parses responses.
The response body, if parseable as json, is made available as response.data
Just like Fetch, Metro automatically uses CORS mode if you send a request to a different domain. But if you need more than basic CORS, you can use all the options available in the Fetch API:
If you allow the browser to show a prompt for the user to login with a username and password, then there is no difference in code between a request with or without Basic Authorization: