Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
/general/community/plugincontribution/marketplace-api/*
!/general/community/plugincontribution/marketplace-api/_category_.yml

/general/projects/restapi/*/openapi/*.mdx

# Misc
.DS_Store
.env.local
Expand Down
33 changes: 33 additions & 0 deletions data/projects.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,39 @@
{
"$schema": "../static/schema/projects.json",
"projects": {
"restapi": {
"title": "REST API Conversion",
"status": "In Progress",
"owners": [
{
"name": "Andrew Lyons",
"githubUsername": "andrewnicols"
}
],
"discussionLinks": [],
"issueLinks": [],
"children": {
"users": {
"title": "User Endpoints",
"status": "Pending",
"owners": [
{
"name": "Andrew Lyons",
"githubUsername": "andrewnicols"
}
],
"discussionLinks": [],
"issueLinks": [
{
"link": "https://moodle.atlassian.net/browse/MDL-90030",
"title": "MDL-90030"
}
],
"isCollection": true,
"hasOpenApi": true
}
}
},
"directoryrestructure/index": {
"title": "Directory Restructure",
"status": "Complete",
Expand Down
19 changes: 18 additions & 1 deletion docusaurus.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ const config = {
[
'docusaurus-plugin-openapi-docs',
{
id: 'openapi',
id: 'marketplace-openapi',
docsPluginId: 'general',
config: {
marketplace: {
Expand All @@ -221,6 +221,23 @@ const config = {
},
},
],
[
'docusaurus-plugin-openapi-docs',
{
id: 'restapi-users-openapi',
docsPluginId: 'general',
config: {
marketplace: {
specPath: 'static/projects/restapi/users/openapi.json',
outputDir: 'general/projects/restapi/users/openapi',
// The reference is read-only: this hides the send button, and
// src/theme/ApiExplorer/Request blocks the form submission.
hideSendButton: true,
showInfoPage: false,
},
},
},
],

// This is the GA-4 tag for all Moodle properties.
[
Expand Down
23 changes: 23 additions & 0 deletions general/projects/restapi/index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
---
title: REST API Conversion
tags:
- Project
- REST
- Users
---

import { ProjectSummary } from '@site/src';

{/* <!-- cspell:ignore restapi --> */}

<ProjectSummary
projectName="restapi"
/>

Moodle's existing web-service system is aging, and has several limitations which make it harder than necessary to use. These mostly revolve around different ways in which it does not follow established protocols, but extend more generally to the shape and nature of REST endpoints, and lack of modern authentication such as OAuth2.

The foundations to deliver a new REST API have been underway for some time with most parts of that foundation now complete, including Moodle's new Routing system, support for automatic OpenAPI specification generation, OAuth2 and Personal Access Token authentication, and OAuth2 scoping.

The new REST API is intended to replace the legacy Web Service system gradually, with endpoints added over time rather than promising one-to-one parity.

User management is the first reference implementation ([MDL-90030](https://tracker.moodle.org/browse/MDL-90030)), with other core services expected to follow including Course, Cohort, Calendar, Messaging, and other core systems, before expanding out to include all Moodle plugins.
Comment thread
andrewnicols marked this conversation as resolved.
Resolved
19 changes: 19 additions & 0 deletions general/projects/restapi/users.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
---
title: User Endpoints
tags:
- Project
- REST
- Users
---

import { ProjectSummary } from '@site/src';

{/* <!-- cspell:ignore restapi --> */}

<ProjectSummary
projectName={["restapi", "users"]}
/>

This work covers REST endpoints for managing Moodle user accounts, including listing, viewing, creating, updating, and deleting accounts, alongside password changes and recovery. User custom fields are still under analysis and will be added to this specification later. It is part of the [Moodle REST API project](https://tracker.moodle.org/browse/MDL-90030).

Explore the endpoint schemas in the **User Endpoints** section of the sidebar, or [download the OpenAPI specification](/projects/restapi/users/openapi.json).
6 changes: 5 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,11 @@
"serve": "docusaurus serve",
"write-translations": "docusaurus write-translations",
"write-heading-ids": "docusaurus write-heading-ids",
"openapi": "scripts/marketplace-openapi.mjs prepare && docusaurus clean-api-docs all && docusaurus gen-api-docs all",
"openapi": "yarn marketplace-openapi && yarn projects-restapi-users-openapi",
"marketplace-openapi": "scripts/marketplace-openapi.mjs prepare && docusaurus clean-api-docs all --plugin-id marketplace-openapi && docusaurus gen-api-docs all --plugin-id marketplace-openapi",
"projects-restapi-users-openapi": "docusaurus clean-api-docs all --plugin-id restapi-users-openapi && docusaurus gen-api-docs all --plugin-id restapi-users-openapi",
"openapi-fetch": "scripts/marketplace-openapi.mjs fetch",
"generate-schema-types": "node scripts/generate-schema-types.mjs",
"gen-api-docs": "docusaurus gen-api-docs",
"clean-api-docs": "docusaurus clean-api-docs",
"wikimedia-sync": "scripts/wikimedia-sync.js",
Expand Down Expand Up @@ -106,6 +109,7 @@
"inquirer": "^9.1.4",
"jest": "^29.3.1",
"js-yaml": "^4.3.2",
"json-schema-to-typescript": "^16.0.0",
"lint-staged": "^13.0.4",
"markdownlint-cli2": "^0.13.0",
"markdownlint-rule-helpers": "^0.17.2",
Expand Down
70 changes: 70 additions & 0 deletions scripts/generate-schema-types.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
#!/usr/bin/env node
/**
* Copyright (c) Moodle Pty Ltd.
*
* Moodle is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Moodle is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Moodle. If not, see <http://www.gnu.org/licenses/>.
*/

import { spawnSync } from 'node:child_process';
import { mkdir, readdir } from 'node:fs/promises';
import {
dirname, extname, join, relative, resolve,
} from 'node:path';

const schemaDirectory = resolve('static/schema');
const outputDirectory = resolve('src/schema');

const findSchemaFiles = async (directory) => {
const entries = await readdir(directory, { withFileTypes: true });
const files = [];

for (const entry of entries) {
const path = join(directory, entry.name);

if (entry.isDirectory()) {
files.push(...await findSchemaFiles(path));
} else if (entry.isFile() && extname(entry.name) === '.json') {
files.push(path);
}
}

return files;
};

const schemaFiles = (await findSchemaFiles(schemaDirectory)).sort();

if (schemaFiles.length === 0) {
throw new Error(`No JSON schemas found in ${schemaDirectory}`);
}

for (const schemaFile of schemaFiles) {
const outputPath = join(
outputDirectory,
relative(schemaDirectory, schemaFile).replace(/\.json$/, '.d.ts'),
);

await mkdir(dirname(outputPath), { recursive: true });

const { error, status } = spawnSync('json2ts', [schemaFile, outputPath], {
stdio: 'inherit',
});

if (error) {
throw error;
}

if (status !== 0) {
throw new Error(`json2ts failed to generate types for ${schemaFile}`);
}
}
56 changes: 51 additions & 5 deletions sidebars/general.js
Original file line number Diff line number Diff line change
Expand Up @@ -237,11 +237,57 @@ const getProjectsWithStates = (states) => allProjects.filter((project) => states
* @param {Array<ProjectSummaryData>} projects
* @returns {Array<NormalizedSidebarItem>}
*/
const convertProjectToSidebarConfig = (projects) => projects.map((project) => ({
label: project.title,
type: 'doc',
id: `projects/${project.key}`,
}));
const convertProjectToSidebarConfig = (projects) => projects.map((project) => {
if (project.children && Object.keys(project.children).length > 0) {
const projectSidebarSnippet = {
label: project.title,
type: 'category',
items: [],
link: {
type: 'doc',
id: `projects/${project.key}/index`,
Comment thread
andrewnicols marked this conversation as resolved.
Resolved
},
};

projectSidebarSnippet.items = convertProjectToSidebarConfig(Object.entries(project.children).map(([childKey, value]) => {
value.key = `${project.key}/${childKey}`;
return value;
}));

return projectSidebarSnippet;
}

if (project.isCollection) {
const items = [];

if (project.hasOpenApi) {
items.push({
type: 'autogenerated',
dirName: `projects/${project.key}/openapi`,
});
} else {
items.push({
type: 'autogenerated',
dirName: `projects/${project.key}`,
});
}
return {
label: project.title,
type: 'category',
items,
link: {
type: 'doc',
id: `projects/${project.key}`,
},
};
}

return {
label: project.title,
type: 'doc',
id: `projects/${project.key}`,
};
});

sidebars.projects = [{
label: 'Projects',
Expand Down
26 changes: 23 additions & 3 deletions src/components/Project/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

import Person, { type PersonProps } from '../Person';
import ProjectData from '@site/data/projects.json';
import type { Project as ProjectSchema } from '@site/schema/projects.d.ts';

export declare interface Link {
title: string,
Expand All @@ -34,12 +35,31 @@ export declare interface ProjectSummaryData {
discussionLinks?: Array<Link>,
}

export const getProject = (projectName: string): ProjectSummaryData => {
if (!ProjectData.projects[projectName]) {
export const getProject = (
projectName: string|string[],
projectData: { [key: string]: ProjectSchema } = ProjectData.projects,
): ProjectSummaryData => {
if (!projectData) {
projectData = ProjectData.projects;
}

if (Array.isArray(projectName)) {
if (projectName.length > 1) {
if (!projectData[projectName[0]]) {
throw new Error(`Unknown project ${projectName[0]}`);
}

const thisProject = projectData[projectName[0]];
return getProject(projectName.slice(1), thisProject.children);
}
projectName = projectName[0];
}

if (!projectData[projectName]) {
throw new Error(`Unknown project ${projectName}`);
}

const project = ProjectData.projects[projectName];
const project = projectData[projectName];

return {
projectName,
Expand Down
2 changes: 1 addition & 1 deletion src/components/ProjectSummary/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import {
import Link from '@docusaurus/Link';

export declare type ProjectSummaryProps = {
projectName: string,
projectName: string|string[],
} & PropsWithRef<'ProjectSummary'>;

export default function ProjectSummary(props: ProjectSummaryProps): ReactNode {
Expand Down
62 changes: 62 additions & 0 deletions src/schema/projects.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/* eslint-disable */
/**
* This file was automatically generated by json-schema-to-typescript.
* DO NOT MODIFY IT BY HAND. Instead, modify the source JSONSchema file,
* and run json-schema-to-typescript to regenerate this file.
*/

export interface JSONSchemaForTheMoodleProjectList {
projects: {
[k: string]: Project;
};
[k: string]: unknown;
}
/**
* This interface was referenced by `undefined`'s JSON-Schema definition
* via the `patternProperty` ".*".
*/
export interface Project {
children?: {
[k: string]: Project;
};
/**
* The parent project that this project relates to
*/
parentProject?: string;
title: string;
/**
* The current status
*/
status: "Pending" | "In Progress" | "Complete" | "Abandoned";
owners?: Person[];
discussionLinks?: Link[];
issueLinks?: Link[];
/**
* Indicates if the project is a collection of sub-projects
*/
isCollection?: boolean;
/**
* Indicates if the project has an OpenAPI specification
*/
hasOpenApi?: boolean;
}
export interface Person {
/**
* The name of the person
*/
name?: string;
/**
* The Github username of the person
*/
githubUsername?: string;
}
export interface Link {
/**
* The link
*/
link?: string;
/**
* A summary title of the content in the link
*/
title?: string;
}
Loading
Loading