UNPKG

save-server

Version:

A powerful ShareX image and URL server, with support for multiple users.

63 lines (53 loc) 1.48 kB
const Api = {}; const BaseUrl = location.origin; window.BaseUrl = BaseUrl; const ApiUrl = `${BaseUrl}/api`; Api.get = function (url, options) { if (!options) options = {}; options.method = "GET"; return this._makeRequest(url, options); }; Api.post = function (url, options) { try { if (!options) options = {}; options.method = "POST"; return this._makeRequest(url, options); } catch (e) { console.error(e); } }; Api.patch = function (url, options) { try { if (!options) options = {}; options.method = "PATCH"; return this._makeRequest(url, options); } catch (e) { console.error(e); } }; Api.delete = function (url, options) { try { if (!options) options = {}; options.method = "DELETE"; return this._makeRequest(url, options); } catch (e) { console.error(e); } }; const protectedMethods = ["post", "patch", "delete", "put"]; Api._makeRequest = async function (url, options) { const startChar = url.substr(0, 1); options.credentials = "include"; if (!options.headers) options.headers = {}; if (options.body && typeof options.body !== "string") { options.body = JSON.stringify(options.body); options.headers["Content-Type"] = "application/json"; } if (protectedMethods.includes(options.method.toLowerCase())) { options.headers["CSRF-Token"] = window.getCookie("CSRF-Token"); } url = (startChar === "/") ? `${ApiUrl}${url}` : `/${url}`; const req = await fetch(url, options); return await req.json(); }; window.Api = Api;