I created fetch-baseurl
package to use fetch
with base url. Based on Fetch API
npm i fetch-baseurl
If I want use it in browser, just copy the index.browser.js
codes to workspace. And, import it.
NodeJS
import { FetchPrefixUrl } from 'fetch-baseurl';
const request = new FetchPrefixUrl('https://site.com');
Browser:
....
<script src="./your-path/index.browser.js">
<script>
const request = new FetchPrefix.FetchPrefixUrl('http://site.com');
</script>
....
// method: GET
request.get('./api/users').then((response) => {
console.log(response.status);
});
// method: POST
requst
.post('./api/users', {
token: 'ee2823jds92....',
username: 'john',
password: Buffer.from('safe-password').toString('hex'),
})
.then(console.log);
// others: PUT, DELETE, HEAD, and, OPTION.