JDoodle API Client
JavaScript JDoodle API client
Distribution
Install
Node
npm install jdoodle-client
All distribution files are in node_modules/jdoodle-client/dist/
Browser
<script src="https://unpkg.com/jdoodle-client@latest/dist/jdoodle-client.browser.js"></script>
Use
Node
const jdoodle = require('jdoodle-client');
require('dotenv').load();
httpRouter.post(jdoodle.defaultExecutePath, async ctx => {
ctx.body = await jdoodle.execute(ctx.request.body);
});
httpRouter.post(jdoodle.defaultCreditSpentPath, async ctx => {
ctx.body = await jdoodle.creditSpent();
});
Browser
(() => { 'use strict';
const elem = id => document.getElementById(id);
const on = (t, e, h) => t.addEventListener(e, h);
const ready = h => on(document, 'DOMContentLoaded', h);
ready(() => {
on(elem('execute'), 'click', async () => {
let json = await jdoodle.execute({
language,
versionIndex: Number(versionIndex) || 0,
stdin,
script
});
});
on(elem('creditSpent'), 'click', async () => {
let json = await jdoodle.creditSpent();
});
});
})();
ES module
import jdoodle from 'jdoodle-client/dist/jdoodle-client.node';
import jdoodle from 'jdoodle-client';
API
The following properties/functions are available through the jdoodle namespace object returned from require('jdoodle-client')
on node or available as window.jdoodle
in the browser (the ES module has the namespace object as default export):
-
version
- library version
-
execute([opts])
- execute code, the opts are:
endpoint
- node: JDoodle API endpoint, browser: your server endpointclientId
- (node only) JDoodle clientId, set via .envclientSecret
- (node only) JDoodle clientSecret, set via .envlanguage
- language codeversionIndex
- language version code (details about language and versionIndex codes)stdin
- standard inputscript
- program source
-
creditSpent([opts])
- check spent credit, the opts are:
endpoint
- node: JDoodle API endpoint, browser: your server endpointclientId
- (node only) JDoodle clientId, set via .envclientSecret
- (node only) JDoodle clientSecret, set via .env
All opts have defaults so just calling the functions is a quick test if things are properly set up, the return value is a promise resolving to JSON even in the presence of errors (containing limited information about the error), for more control use the callExecuteAPI
and callCreditSpentAPI
variants with appropriate error handling either via promise then-catch chain or try-catch block with async/await
See the example repo for complete code example (the relevant code is in app.js
, layout.pug
and index.pug
)