| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 | import "whatwg-fetch"; import { getUrl, getQuery, bodyParser, validateStatus, bodyResponseParser } from "./utils"; export default class BimFetch { state = { /** * default Headers as an object, will be converted to Headers right before the request * @type {Object} */ baseHeaders: { "Content-Type": "application/json", Accept: "application/json, text/plain, */*, multipart/form-data" }, /** * Default url used when accessing a ressource, if the request doens't start with https * @type {String} */ baseUrl: "", /** * Mode used to fetch data. It can be either: * - cors * - same-origin * - no-cors * - navigate * @type {String} */ mode: "cors" }; /** * Set headers on every request using rest operators. * It will handle the case when headers already set needs to be changed. * Thanks to rest operators, newHeaders will overwrite old headers values. * @param {Object} newHeaders Custom headers to be used for the current request */ setHeaders(newHeaders) { this.state.baseHeaders = { ...this.state.baseHeaders, ...newHeaders }; } /** * Set default url to be used when making fetch requests * @param {string} url Default url to be used (https://my-great-api.com) */ setDefaultUrl(url) { this.state.baseUrl = url; } /** * Perform a GET request. Parameters are sent as an object and will be parsed and transformed * into a valid query * @param {String} url Relative or absolute URL of the request * @param {Object} [params={}] Parameters to send with the request * @param {Object} [headers={}] Custom headers * @return {Promise} Return a promise of the result. */ async get(url, params = {}, headers = {}) { const fullUrl = getUrl(url, this.state.baseUrl); const response = await fetch(fullUrl + getQuery(params), { method: "GET", headers: new Headers({ ...this.state.baseHeaders, ...headers }), mode: this.state.mode }); await validateStatus(response, fullUrl, "GET"); return bodyResponseParser(response); } /** * Perform a POST request. Parameters are sent as an object. * @param {String} url Relative or absolute URL of the request * @param {Object} [params={}] Parameters to send with the request * @param {Object} [headers={}] Custom headers * @return {Promise} Return a promise of the result. */ async post(url, bodyToTransform = {}, headers = {}) { const body = bodyParser(bodyToTransform); const fullUrl = getUrl(url, this.state.baseUrl); const response = await fetch(fullUrl, { method: "POST", headers: new Headers({ ...this.state.baseHeaders, ...headers }), body, mode: this.state.mode }); await validateStatus(response, fullUrl, "POST"); return bodyResponseParser(response); } /** * Perform a PUT request. Parameters are sent as an object. * @param {String} url Relative or absolute URL of the request * @param {Object} [params={}] Parameters to send with the request * @param {Object} [headers={}] Custom headers * @return {Promise} Return a promise of the result. */ async put(url, bodyToTransform, headers = {}) { const body = bodyParser(bodyToTransform); const fullUrl = getUrl(url, this.state.baseUrl); const response = await fetch(fullUrl, { method: "PUT", headers: new Headers({ ...this.state.baseHeaders, ...headers }), mode: this.state.mode, body }); await validateStatus(response, fullUrl, "PUT"); return bodyResponseParser(response); } /** * Perform a DELETE request. There should be no parameters to be sent. * @param {String} url Relative or absolute URL of the request * @param {Object} [params={}] Parameters to send with the request * @param {Object} [headers={}] Custom headers * @return {Promise} Return a promise of the result. */ async delete(url, headers = {}) { const fullUrl = getUrl(url, this.state.baseUrl); const response = await fetch(fullUrl, { method: "DELETE", headers: new Headers({ ...this.state.baseHeaders, ...headers }), mode: this.state.mode }); return validateStatus(response, fullUrl, "DELETE"); } } |