npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

node-moo_hv

v1.0.0

Published

A simple and developer friendly JavaScript module to perform Web Service (API) calls to the Moodle site.

Downloads

12

Readme

Moodle API client for Node.js

A simple and developer friendly JavaScript module to perform Web Service (API) calls to the Moodle site.

Getting started

Install the module

npm install node-moodle

Get site info (using Promised callbacks)

const { MoodleClient } = require('node-moodle');

const moodle = new MoodleClient({
  baseUrl: "https://moodle.example.com", //<-- Put your Moodle URL here
  token: "exppsBdQwLvNwYRoAuaiBO5j0fWTzxU6" //<-- Put your token here
});

moodle.core.webservice.getSiteInfo()
  .then(res => console.log(res))
  .catch(err => console.error(err.message));

Create a new user (using async-await approach)

const { MoodleClient } = require('node-moodle');
const moodle = new MoodleClient({ ... });

async function main() {
  try {
    //Request data
    var req = {
      users: [
        {
          firstname: "Foo",
          lastname: "Bar",
          username: "foo",
          password: "FooBar123!",
          email: "[email protected]",
        }
      ]
    };

    //Invoke a method
    var res = await moodle.core.user.createUsers(req);
    
    //Response data
    console.log(res);
    /*
    [
      {
        "id": 3,
        "username": "foo"
      }
    ]
    */
  }
  catch (err) {
    console.log(err);
  }
}

main();

Event handlers

MoodleClient extends EventEmitter

const { MoodleClient } = require('node-moodle');
const moodle = new MoodleClient({ ... });

//Before request
moodle.on("request", function(data) {
  console.log(data);
});

//After request
moodle.on("resposne", function(data) {
  console.log(data);
});

//Catch error
moodle.on("error", function(err) {
  console.error(err);
});

Convert JSON to form data

Moodle has a peculiar way of specifying request parameters. Parameters can either be sent in URL query string or POST form body.

Thus a JSON object needs to be converted to form data

{
  "users": [
    {
      "firstname": "Foo",
      "lastname": "Bar",
      "username": "foo",
      "password": "FooBar123!",
      "email": "[email protected]"
    }
  ]
}

As Content-Type: application/x-www-form-urlencoded

users[0][firstname]=Foo&users[0][lastname]=Bar&users[0][username]=foo&users[0][password]=FooBar123%21&users[0][email]=foo%40email.com

To perform the conversion in the code invoke the flatten function

const { MoodleClient } = require('node-moodle');
const moodle = new MoodleClient({ ... });

const form = moodle.flatten({
  "users": [
    {
      "firstname": "Foo",
      "lastname": "Bar",
      "username": "foo",
      "password": "FooBar123!",
      "email": "[email protected]"
    }
  ]
});

console.log(form); //users[0][firstname]=Foo&users[0][lastname]=Bar...

List of functions

Offical list of functions can be found at Web service API functions

The following table represents mapping between JavaScript function names and Moodle Web Service function names.

| JS function | API function | Description | |-------------|--------------|-------------| | auth.email.getSignupSettings(data) | auth_email_get_signup_settings | Get the signup required settings and profile fields. | | auth.email.signupUser(data) | auth_email_signup_user | Adds a new user (pendingto be confirmed) in the site. | | core.user.createUsers(data) | core_user_create_users | Create users - admin function | | core.webservice.getSiteInfo(data) | core_webservice_get_site_info | Return some site info / user info / list web service functions | | Read more... | | |

Functions are used in the following manner

const { MoodleClient } = require('node-moodle');
const moodle = new MoodleClient({ ... });

//core_webservice_get_site_info => core.webservice.getSiteInfo
moodle.core.webservice.getSiteInfo(); //Returns `Promise`

Handling Promise

Promise can be handled with .then().catch() function chain

moodle.core.webservice.getSiteInfo()
.then(function(res) {
  console.log(res);
})
.catch(function(err) {
  console.error(err);
});

or using async-await syntax for later versions of JavaScript

async function sample() {
  try {
    var res = await moodle.core.webservice.getSiteInfo();
    console.log(res);
  }
  catch (err) {
    console.error(err);
  }
}

sample();

Note that the data argument is not always required and can be omitted.

Updating function list

Run the extract script to scrape function names from the official website

npm run extract

The script downloads the HTML from Web service API functions and parses rows from the Core web service functions table.

To update the table in the wiki/functions.md file run

npm run table

Unit test

Run unit test

npm run test

The test will be performed on a built-in mockup server.