diff --git a/index.js b/index.js new file mode 100644 index 0000000..b16f2c6 --- /dev/null +++ b/index.js @@ -0,0 +1,43 @@ +class Fetch { + static updateConfig(urls){ + this.urls = urls; + } + + static request(uri, options){ + // To keep fetch out of the global scope + // encourages importing this file. + require('whatwg-fetch'); + + let mod_uri = uri; + mod_uri = `${window.location.protocol}//${this.urls[window.location.hostname] || window.location.hostname}${uri}`; + + let finalOptions = Object.assign( + {}, + options, + { credentials: 'include' } + ); + + return fetch(mod_uri, finalOptions); + } + static getServerURL() { + return `${window.location.protocol}//${this.urls[window.location.hostname] || window.location.hostname}`; + } +} + +Fetch.urls = { + localhost: "localhost:3000" +} + +function request(uri, options) { + return Fetch.request(uri, options) +} + +function updateFetchConfig(urls){ + Fetch.updateConfig(urls); +} + +module.exports = { + Fetch: Fetch, + updateFetchConfig: updateFetchConfig, + fetch: request +} diff --git a/package.json b/package.json new file mode 100644 index 0000000..4c0c78b --- /dev/null +++ b/package.json @@ -0,0 +1,15 @@ +{ + "name": "@fyresite/fetch", + "version": "1.0.4", + "description": "Uses whatWG-fetch to provide an easier way to send api requests", + "main": "index.js", + "dependencies": { + "whatwg-fetch": "^2.0.3" + }, + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1" + }, + "author": "Fyresite", + "license": "ISC", + "private": false +}