This is a simple Client for moodle, that is inspired by the moodle-client by mudrd8mz, it has built in typings and typings for many endpoints of the Moodle API(which are documented here), the typings will be extended in the future till hopefully one day al endpoints are built in this package.
The Documentation is also available on GitBook
If you ever read the documentation of the Moodle API, then you know that there are hundrets of methods and if you dont have the right documentation the docs can be hard to read and understand, you still need the documentation to use this package but it provides you most methods with built in types so that you know what you get back and what you need to supply to the request.
npm install akora-moodle
To create an instance of the Client, you use the static init
method of the Client with an object which contains all needed options to initialize the client. The method will return a Promise wich returns a instance of the Client
class.
Here an example of an basic Login with your username, and password.
const { Client } = require('akora-moodle'); Client.init({ wwwroot: 'https://moodle.your-school.de/', username: 'Bob', password: 'SuPeRsecRet' })
You can also log in with a token. (If credentials and token are provided token will be used).
Client.init({ wwwroot: 'https://moodle.your-school.de/', token: 'yourtokengoesbrrrrrr' })
At the moment all implemented methods can be found in the client.core
property, your IDE will tell you what methods are already implemented and usable, as well as your IDE will tell you what arguments you need to provide and what the response looks like.
Here is a short example which gives you informations about the current logged in user.
const { Client } = require('akora-moodle'); Client.init({ wwwroot: 'https://moodle.your-school.de/', token: 'yourtokengoesbrrrrrr' }).then(async (client) => { var info = await client.core.getInfo(); console.log('You are Logged in as %s %s', info.firstname, info.lastname) }).catch((err) => { console.log('Something went wrong ._.', err); });
Here is a short example which returns you an array of course contents.
const { Client } = require('akora-moodle'); client.init({ wwwroot: 'https://moodle.your-school.de/', token: 'yourtokengoesbrrrrrr' }).then(async (client) => { var contents = await client.core.course.getContents({ courseid: 3272 }); console.log('There are %s Sections in this Course', contents.length) }).catch((err) => { console.log('Something went wrong ._.', err); });
At the moment not even nearly all methods are implemented, so you might often want to use the client.call
method to make a custom API Requests.
An example
const { Client } = require('akora-moodle'); Client.init({ wwwroot: 'https://moodle.your-school.de/', token: 'yourtokengoesbrrrrrr', }).then(async (client) => { var response = await client.call({ wsfunction: 'core_not_implemented_yet', method: 'POST', args: { someid: 123 } //You can also provide settings > for advanced usage }) }).catch((err) => { console.log('Something went wrong ._.', err); });
All custom Settings
const { Client } = require('akora-moodle'); Client.init({ wwwroot: 'https://moodle.your-school.de/', token: 'yourtokengoesbrrrrrr', // The web service to use, default is moodle_mobile_app service: 'moodle_mobile_app', // If set to false, SSL certificates do not need to be valid. strictSSL: true, // Will enable the built-in Logger logger: true })