UNPKG

jdoodle-client

Version:
85 lines (74 loc) 2.08 kB
(function (global, factory) { typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() : typeof define === 'function' && define.amd ? define(factory) : (global = global || self, global.jdoodle = factory()); }(this, function () { 'use strict'; const name = "jdoodle-client"; const version = "1.0.2"; const jdoodle = { get [Symbol.toStringTag]() { return name; }, version: version, defaultExecutePath: '/jdoodleExecute', defaultCreditSpentPath: '/jdoodleCreditSpent' }; function _callAPI(url, opts = {}) { return new Promise((resolve, reject) => { let content = JSON.stringify(opts); fetch(url, { method: 'POST', headers: { 'Content-Type': 'application/json', 'Content-Length': content.length }, body: content }). then(res => { if (res.ok) { res.json(). then(json => resolve(json)). catch(err => reject(err)); } else { reject(new Error( `HTTP Error: ${res.status} (${res.statusText}) ${res.url}` )); } }). catch(err => reject(new Error( `Network Error (cross-domain request?) ${url}` ))); }); } function callExecuteAPI({ endpoint = `${location.origin}${jdoodle.defaultExecutePath}`, language, versionIndex, stdin, script } = {}) { return _callAPI(endpoint, { language, versionIndex, stdin, script }); } function callCreditSpentAPI({ endpoint = `${location.origin}${jdoodle.defaultCreditSpentPath}` } = {}) { return _callAPI(endpoint); } const execute = opts => callExecuteAPI(opts). catch(err => ({error: `${err}`})); const creditSpent = opts => callCreditSpentAPI(opts). catch(err => ({error: `${err}`})); Object.assign(jdoodle, { callExecuteAPI, callCreditSpentAPI, execute, creditSpent }); return jdoodle; })); //# sourceMappingURL=jdoodle-client.browser.js.map