{"_id":"@audacioustux/moodle-webservice","_rev":"2-3da9cd884f6713fa4900442114835d04","name":"@audacioustux/moodle-webservice","dist-tags":{"latest":"1.9.2"},"versions":{"1.9.0":{"name":"@audacioustux/moodle-webservice","version":"1.9.0","keywords":["moodle","webservice"],"author":{"name":"Tanjim H.","email":"mwoo-bridge+gh@audacioustux.com"},"license":"MIT","_id":"@audacioustux/moodle-webservice@1.9.0","maintainers":[{"name":"tanjimhossain","email":"tangimhossain1@gmail.com"}],"homepage":"https://github.com/audacioustux/mwoo-bridge#readme","bugs":{"url":"https://github.com/audacioustux/mwoo-bridge/issues"},"dist":{"shasum":"54f52fad3cb2b75f60da8c10bbfd53b309a94302","tarball":"https://registry.npmjs.org/@audacioustux/moodle-webservice/-/moodle-webservice-1.9.0.tgz","fileCount":6,"integrity":"sha512-EDPngdz3iF7dYxlijmfYgErMR9FvhiqjdBAB1+iz9OVrsjyKkjWGofNqJxr/ijRdsXhpBpa5QUZ+B8C0u2WIZw==","signatures":[{"sig":"MEUCIH/F4Pam31KdzcedF6pI9I0F9M2xQZRYAj0CmPg34m1eAiEAg5rBBo0ypRC+3Q8IytaxQVMMcws3ch8YZXz04fG2Xbc=","keyid":"SHA256:DhQ8wR5APBvFHLF/+Tc+AYvPOdTpcIDqOhxsBHRwC7U"}],"unpackedSize":295465},"main":"index.js","gitHead":"f2366ade5a5cef0fbef63db3e42a088e3e51bce9","scripts":{"test":"tsrun tests/test-moodle.ts","tsrun":"tsrun","prepublish":"tsc"},"_npmUser":{"name":"tanjimhossain","email":"tangimhossain1@gmail.com"},"repository":{"url":"git+https://github.com/WebHare/moodle-webservice.git","type":"git"},"_npmVersion":"10.9.2","description":"Moodle Web Service API client with intellisense and typechecking","directories":{},"_nodeVersion":"23.7.0","dependencies":{"debug":"^4.3.4","node-html-parser":"^6.1.5"},"_hasShrinkwrap":false,"devDependencies":{"dotenv":"^16.0.3","typescript":"^5.0.3","@types/node":"^18.15.11","@types/debug":"^4.1.7","@webhare/tsrun":"^0.505.0"},"_npmOperationalInternal":{"tmp":"tmp/moodle-webservice_1.9.0_1740044870368_0.050187133769274084","host":"s3://npm-registry-packages-npm-production"}},"1.9.1":{"name":"@audacioustux/moodle-webservice","version":"1.9.1","keywords":["moodle","webservice"],"author":{"name":"Tanjim H.","email":"mwoo-bridge+gh@audacioustux.com"},"license":"MIT","_id":"@audacioustux/moodle-webservice@1.9.1","maintainers":[{"name":"tanjimhossain","email":"tangimhossain1@gmail.com"}],"homepage":"https://github.com/audacioustux/mwoo-bridge#readme","bugs":{"url":"https://github.com/audacioustux/mwoo-bridge/issues"},"dist":{"shasum":"38f07255d711308781cbd716f6af9fa331cc5937","tarball":"https://registry.npmjs.org/@audacioustux/moodle-webservice/-/moodle-webservice-1.9.1.tgz","fileCount":180,"integrity":"sha512-9wI/BhyNs5TKA4aMRDxBaUqryCgF0FK0g5NFeUmhDDkgPMGPYzo+uLbrOk5Dt8Ma7ZWMAYtEQdOEFJHVm2hZPA==","signatures":[{"sig":"MEQCIH/7yjs5LZGjAC6r31dDLQ0XdYotbK4XQDhevMVNxmEJAiBw94AaAGZKDWepodBm6iM/Hr1lXOOWwGR1Tgf/fGmRww==","keyid":"SHA256:DhQ8wR5APBvFHLF/+Tc+AYvPOdTpcIDqOhxsBHRwC7U"}],"unpackedSize":458516},"main":"index.js","types":"./index.d.ts","gitHead":"f2366ade5a5cef0fbef63db3e42a088e3e51bce9","scripts":{"test":"tsrun tests/test-moodle.ts","tsrun":"tsrun","prepare":"tsc"},"_npmUser":{"name":"tanjimhossain","email":"tangimhossain1@gmail.com"},"repository":{"url":"git+https://github.com/WebHare/moodle-webservice.git","type":"git"},"_npmVersion":"10.9.2","description":"Moodle Web Service API client with intellisense and typechecking","directories":{},"_nodeVersion":"23.7.0","dependencies":{"debug":"^4.3.4","node-html-parser":"^6.1.5"},"_hasShrinkwrap":false,"devDependencies":{"dotenv":"^16.0.3","typescript":"^5.7.3","@types/node":"^18.15.11","@types/debug":"^4.1.7","@webhare/tsrun":"^0.505.0"},"_npmOperationalInternal":{"tmp":"tmp/moodle-webservice_1.9.1_1740048837998_0.8202804030590105","host":"s3://npm-registry-packages-npm-production"}},"1.9.2":{"name":"@audacioustux/moodle-webservice","version":"1.9.2","description":"Moodle Web Service API client with intellisense and typechecking","main":"index.js","types":"index.d.ts","scripts":{"prepare":"tsc","test":"tsrun tests/test-moodle.ts","tsrun":"tsrun"},"dependencies":{"debug":"^4.3.4","node-html-parser":"^6.1.5"},"repository":{"type":"git","url":"git+https://github.com/WebHare/moodle-webservice.git"},"keywords":["moodle","webservice"],"devDependencies":{"@types/debug":"^4.1.7","@types/node":"^18.15.11","@webhare/tsrun":"^0.505.0","dotenv":"^16.0.3","typescript":"^5.7.3"},"author":{"name":"Tanjim H.","email":"mwoo-bridge+gh@audacioustux.com"},"license":"MIT","bugs":{"url":"https://github.com/audacioustux/mwoo-bridge/issues"},"homepage":"https://github.com/audacioustux/mwoo-bridge#readme","_id":"@audacioustux/moodle-webservice@1.9.2","gitHead":"f2366ade5a5cef0fbef63db3e42a088e3e51bce9","_nodeVersion":"23.7.0","_npmVersion":"10.9.2","dist":{"integrity":"sha512-7Y4NaPHtbPB5h3/VwcpLz6V7/ZM2hENr2ClFpqvhTUEVMY/L/WZmx5hd9XpkpGDdVlys6+NWYC9T5HF9f0ZNfA==","shasum":"f1a42380aca8dd38fb678393c2ddfa4792da31d6","tarball":"https://registry.npmjs.org/@audacioustux/moodle-webservice/-/moodle-webservice-1.9.2.tgz","fileCount":180,"unpackedSize":458541,"signatures":[{"keyid":"SHA256:DhQ8wR5APBvFHLF/+Tc+AYvPOdTpcIDqOhxsBHRwC7U","sig":"MEYCIQCy6aByAAV4L7L22npbhdHJCpSrQ+Njn5U7q7hRyPiVeQIhAMYebWIFabIeNEaeEXXNtdpZH0RcquSTfTfd7pnQG8oY"}]},"_npmUser":{"name":"tanjimhossain","email":"tangimhossain1@gmail.com"},"directories":{},"maintainers":[{"name":"tanjimhossain","email":"tangimhossain1@gmail.com"}],"_npmOperationalInternal":{"host":"s3://npm-registry-packages-npm-production","tmp":"tmp/moodle-webservice_1.9.2_1740049049343_0.8759976864477499"},"_hasShrinkwrap":false}},"time":{"created":"2025-02-20T09:47:50.305Z","modified":"2025-02-20T10:57:29.729Z","1.9.0":"2025-02-20T09:47:50.675Z","1.9.1":"2025-02-20T10:53:58.239Z","1.9.2":"2025-02-20T10:57:29.532Z"},"bugs":{"url":"https://github.com/audacioustux/mwoo-bridge/issues"},"author":{"name":"Tanjim H.","email":"mwoo-bridge+gh@audacioustux.com"},"license":"MIT","homepage":"https://github.com/audacioustux/mwoo-bridge#readme","keywords":["moodle","webservice"],"repository":{"type":"git","url":"git+https://github.com/WebHare/moodle-webservice.git"},"description":"Moodle Web Service API client with intellisense and typechecking","maintainers":[{"name":"tanjimhossain","email":"tangimhossain1@gmail.com"}],"readme":"# About this fork\r\nWe have forked this project from https://github.com/IHAVENOARMS/moodle-webservice.\r\n\r\n# TypeScript Moodle API client for Node.js\r\n\r\nA simple and developer friendly TypeScript module to perform Web Service (API) calls to the Moodle site.\r\n\r\n## Getting started\r\n\r\nInstall the module\r\n\r\n```bash\r\nnpm install @webhare/moodle-webservice\r\n```\r\n\r\nGet site info (using Promised callbacks)\r\n\r\n```ts\r\nimport { MoodleApi } from \"@webhare/moodle-webservice\";\r\n\r\nconst moodle = MoodleApi({\r\n  baseUrl: \"https://moodle.example.com\", //<-- Put your Moodle URL here\r\n  token: \"exppsBdQwLvNwYRoAuaiBO5j0aWTzxU6\", //<-- Put your token here\r\n});\r\n\r\nmoodle.core.webservice //<-- with intellisense and type checking\r\n  .getSiteInfo()\r\n  .then((res) => console.log(res)) //<-- Response of type IMoodleSiteInfo\r\n  .catch((err) => console.error(err.message));\r\n```\r\n\r\nGet course contents (using async-await approach)\r\n\r\n```ts\r\nimport { MoodleApi } from '@webhare/moodle-webservice';\r\nconst moodle = MoodleApi({ ... });\r\n\r\nasync function main() {\r\n  try {\r\n    const sections =\r\n      await moodle.core.course.getContents({courseid: 1234});//<-- returns IMoodleCourseSection[]\r\n\r\n    //Response data\r\n    console.log(sections[0].name);\r\n    /*\r\n    Introduction\r\n    */\r\n  }\r\n  catch (err) {\r\n    console.log(err);\r\n  }\r\n}\r\n\r\nmain();\r\n```\r\n\r\nif a `token` param is provided, it will override the token supplied when instantiating the api\r\n\r\n```ts\r\nimport { MoodleApi } from \"@webhare/moodle-webservice\";\r\n\r\nconst moodle = MoodleApi({\r\n  baseUrl: \"https://moodle.example.com\",\r\n  token: \"exppsBdQwLvNwYRoAuaiBO5j0aWTzxU6\",\r\n});\r\n\r\nmoodle.core.webservice\r\n  .getSiteInfo({ token: \"37512512c24112b1235cc123f821bd27cd\" }) //<-- This token will be used instead\r\n  .then((res) => console.log(res))\r\n  .catch((err) => console.error(err.message));\r\n\r\nmoodle.config.token = \"37512512c24112b1235cc123f821bd27cd\"; //<-- You can also do this.\r\n//..many function calls with the same token above.\r\n```\r\n\r\n## Intellisense + Typechecking\r\n\r\n![](https://media.giphy.com/media/GA0jKe1PWuDcIP7rO7/giphy.gif)\r\n\r\n## Getting your api token.\r\n\r\nif you don't immediately have your token to pass to the api you can import MoodleClient directly\r\nand use the authenticate method to get one.\r\n\r\n```ts\r\nimport { MoodleApi, MoodleClient } from \"@webhare/moodle-webservice\";\r\nconst YOUR_WEBSITE_URL = \"https://moodle.example.com\";\r\n//No token provided beforehand\r\nconst moodle = MoodleApi({\r\n  baseUrl: YOUR_WEBSITE_URL, //<-- Put your Moodle URL here\r\n});\r\n\r\nasync function main() {\r\n  try {\r\n    //result type of IMoodleWSAuthResponse\r\n    const { token } = await MoodleClient.authenticate({\r\n      baseUrl: YOUR_WEBSITE_URL,\r\n      credentials: { username: \"AwesomeJohn\", password: \"SmartPa33word\" },\r\n    });\r\n\r\n    moodle.config.token = token;\r\n\r\n    const { fullname: firstName } = await moodle.core.webservice.getSiteInfo();\r\n    /*\r\n    logs:\r\n      John Smith\r\n    */\r\n  } catch (err) {\r\n    console.log(err);\r\n  }\r\n}\r\n\r\nmain();\r\n```\r\n\r\n## Helper classes\r\n\r\nThe moodle webservice has some caveats here and there such as how when getting an attempt\r\nmoodle doesn't send you all the raw question data, instead some is raw data while other is\r\nthe rendered HTML question element.\r\n`moodle-webservice` aims to solve that by providing a number of helper classes such as `MoodleAttempt` , these are abstract classes that have a number of functions that aim to solve\r\nthese issues.\r\n\r\n```ts\r\nimport { MoodleApi, MoodleAttempt } from \"@webhare/moodle-webservice\";\r\n\r\nconst moodle = MoodleApi({\r\n  baseUrl: \"https://moodle.example.com\",\r\n  token: \"375208h132h1222h20h202b823b227cd\",\r\n});\r\n\r\nmoodle.mod.quiz.getAttemptReview({ attemptid: 3665103 }).then((res) => {\r\n  const questions = res.questions; //<-- of type IMoodleQuestion[].\r\n\r\n  //IMoodleQuestion does not contain the actual question text or choices.\r\n  questions[0].text; //<-- Error no such property on questions\r\n\r\n  const parsedReview = MoodleAttempt.parse(res);\r\n  const parsedQuestions = parsedReview.questions; //<-- of type IMoodleParsedQuestion[].\r\n\r\n  //IMoodleParsedQuestion contains additional data such as the name, instance number, and choices\r\n  console.log(parsedQuestions[0].text);\r\n  //Output: \"The muscles of facial expression are supplied by the ____ nerve.\"\r\n});\r\n```\r\n\r\nor parse a single question\r\n\r\n```ts\r\nimport { MoodleApi, MoodleQuestion } from \"@webhare/moodle-webservice\";\r\n\r\nconst moodle = MoodleApi({\r\n  baseUrl: \"https://moodle.example.com\",\r\n  token: \"375208h132h1222h20h202b823b227cd\",\r\n});\r\n\r\nmoodle.mod.quiz.getAttemptReview({ attemptid: 3665103 }).then((res) => {\r\n  const questions = res.questions; //<-- of type IMoodleQuestion[].\r\n  //IMoodleQuestion does not contain the actual question text or choices.\r\n  questions[0].text; //<-- Error no such property on questions\r\n\r\n  //IMoodleParsedQuestion contains additional data such as the name, instance number and choices\r\n  const parsedQuestion = MoodleQuestion.parse(questions[0]); //<-- of type IMoodleParsedQuestion.\r\n\r\n  console.log(parsedQuestion.text);\r\n  //Output: \"The muscles of facial expression are supplied by the ____ nerve.\"\r\n});\r\n```\r\n\r\n## JSON to form data\r\n\r\nMoodle has a peculiar way of specifying request parameters.\r\nParameters can either be sent in URL query string or POST form body.\r\n\r\nThus a JSON object needs to be converted to form data\r\n\r\n```json\r\n{\r\n  \"users\": [\r\n    {\r\n      \"firstname\": \"Foo\",\r\n      \"lastname\": \"Bar\",\r\n      \"username\": \"foo\",\r\n      \"password\": \"FooBar123!\",\r\n      \"email\": \"foo@example.com\"\r\n    }\r\n  ]\r\n}\r\n```\r\n\r\nAs `Content-Type: application/x-www-form-urlencoded`\r\n\r\n```\r\nusers[0][firstname]=Foo&users[0][lastname]=Bar&users[0][username]=foo&users[0][password]=FooBar123%21&users[0][email]=foo%40email.com\r\n```\r\n\r\nTo perform the conversion in the code invoke the `flatten` function\r\n\r\n```ts\r\nimport { MoodleClient } from \"@webhare/moodle-webservice\";\r\n\r\nconst form = MoodleClient.flatten({\r\n  users: [\r\n    {\r\n      firstname: \"Foo\",\r\n      lastname: \"Bar\",\r\n      username: \"foo\",\r\n      password: \"FooBar123!\",\r\n      email: \"foo@example.com\",\r\n    },\r\n  ],\r\n});\r\n\r\nconsole.log(form); //users[0][firstname]=Foo&users[0][lastname]=Bar...\r\n```\r\n\r\nHowever, there is no need to invoke the function outside the code as anything passed as `data` parameter to api functions is automatically flattened and formatted in the right way.\r\n\r\n## List of functions\r\n\r\nOffical list of functions can be found at [Web service API functions](https://docs.moodle.org/dev/Web_service_API_functions)\r\n\r\nThe following table represents mapping between JavaScript function names and Moodle Web Service function names.\r\n\r\n| JS function                                                                         | API function                   | Description                                                    |\r\n| ----------------------------------------------------------------------------------- | ------------------------------ | -------------------------------------------------------------- |\r\n| auth.email.getSignupSettings(data)                                                  | auth_email_get_signup_settings | Get the signup required settings and profile fields.           |\r\n| auth.email.signupUser(data)                                                         | auth_email_signup_user         | Adds a new user (pendingto be confirmed) in the site.          |\r\n| core.user.createUsers(data)                                                         | core_user_create_users         | Create users - admin function                                  |\r\n| core.webservice.getSiteInfo(data)                                                   | core_webservice_get_site_info  | Return some site info / user info / list web service functions |\r\n| [Read more...](https://github.com/papnkukn/node-moodle/blob/main/wiki/functions.md) |                                |                                                                |\r\n\r\nNote that the `data` argument is not always required and can be omitted.\r\n\r\n## Note regarding types\r\n\r\nDue to poor moodle webservice function documentation not all functions have definite parameter types\r\nand instead have `IMoodleWSParams` as its parameter type and `any` as its return type.\r\n\r\nI've added types for the most important functions first, and I'll keep adding more as I go,\r\ncontact me if you want to add types for a function that you know about.\r\n\r\n## Running your own moodle\r\nFor development/testing against a Moodle consider using the docker containers defined in `my-moodle`:\r\n\r\n```bash\r\ncd my-moodle/\r\ndocker-compose up\r\n# now: open http://127.0.0.1:8051/\r\n```\r\n\r\nSee https://hub.docker.com/r/bitnami/moodle for details about these containers\r\n\r\nAfter starting, login at http://127.0.0.1:8051/ using: user/bitnami\r\n\r\n### Enabling the API\r\n- http://127.0.0.1:8051/admin/settings.php?section=optionalsubsystems\r\n  - Enable webservices\r\n- http://127.0.0.1:8051/admin/settings.php?section=webserviceprotocols\r\n  - Enable REST\r\n  - Enable documentation\r\n- http://127.0.0.1:8051/admin/settings.php?section=webservicesoverview helps you enroll the service - shows the steps and links\r\n- http://127.0.0.1:8051/admin/settings.php?section=externalservices\r\n  - Add service. Configure as needed, don't forget to enable the functions you need\r\n  - This is also where you need to go back to add more functions later\r\n- http://127.0.0.1:8051/admin/webservice/tokens.php?action=create\r\n  - Get the token. You'll need it for MODULE_TOKEN below\r\n\r\nTo test the module:\r\n```bash\r\nexport MOODLE_BASEURL=http://127.0.0.1:8051\r\nexport MOODLE_TOKEN=XXX\r\nnpm run tsrun tests/test-moodle.ts\r\n```\r\n\r\nto add more functions\r\n- http://127.0.0.1:8051/admin/webservice/service_functions.php?id=2\r\n\r\n## Updating the module\r\n```bash\r\nnpm run prepublish\r\nnpm publish\r\n```\r\n","readmeFilename":"README.md"}