UNPKG

jdoodle-client

Version:
88 lines (78 loc) 2.22 kB
import https from 'https'; 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); let req = https.request(url, { method: 'POST', headers: { 'Content-Type': 'application/json', 'Content-Length': content.length } }, res => { let received = []; res. on('data', data => received.push(String(data))). on('end', () => { // Wrap in a try-catch because JSON.parse might throw and // we're in an unprotected callback after the promise is created try { resolve(JSON.parse(received.join(''))); } catch (err) { reject(err); } }). on('error', err => reject(err)); }).on('error', err => reject(err)); req.write(content); req.end(); }); } function callExecuteAPI({ endpoint = process.env.JDOODLE_ENDPOINT_EXECUTE || 'https://api.jdoodle.com/v1/execute', clientId = process.env.JDOODLE_CLIENT_ID, clientSecret = process.env.JDOODLE_CLIENT_SECRET, language = 'ruby', versionIndex = 2, stdin = 'Hi', script = 'puts "ruby \#{RUBY_VERSION}: \#{ARGF.read}"' } = {}) { return _callAPI(endpoint, { clientId, clientSecret, language, versionIndex, stdin, script }); } function callCreditSpentAPI({ endpoint = process.env.JDOODLE_ENDPOINT_CREDIT_SPENT || 'https://api.jdoodle.com/v1/credit-spent', clientId = process.env.JDOODLE_CLIENT_ID, clientSecret = process.env.JDOODLE_CLIENT_SECRET } = {}) { return _callAPI(endpoint, { clientId, clientSecret }); } const execute = opts => callExecuteAPI(opts). catch(err => ({error: `${err}`, statusCode: 500})); const creditSpent = opts => callCreditSpentAPI(opts). catch(err => ({error: `${err}`, statusCode: 500})); Object.assign(jdoodle, { callExecuteAPI, callCreditSpentAPI, execute, creditSpent }); export default jdoodle; //# sourceMappingURL=jdoodle-client.node.mjs.map