Page MenuHomePhabricator

Fix JavaScript examples in the MediaWiki REST API reference docs
Closed, DeclinedPublic

Description

Until T282259 is resolved, the MediaWiki REST API does not support the Api-User-Agent header. For now, let's remove the Api-User-Agent header from JavaScript examples in mw:API:REST API/Reference to reflect the current state.

For example:

async function doFetch() {
  const rsp = await fetch(
    "https://en.wikipedia.org/w/rest.php/v1/search/page?q=jupiter&limit=20",
    {'Api-User-Agent': 'MediaWiki REST API docs examples/0.1 (https://www.mediawiki.org/wiki/API_talk:REST_API)'}
  );
  const data = await rsp.json();
  return data;
}

async function fetchAsync()
{
  try {
    let result = await doFetch();
    console.log(result);
  } catch( err ) {
    console.error( err.message );
  }
}

fetchAsync();

At a minimum, this should be changed to:

async function doFetch() {
  const rsp = await fetch(
    "https://en.wikipedia.org/w/rest.php/v1/search/page?q=jupiter&limit=20",
  );
  const data = await rsp.json();
  return data;
}

async function fetchAsync()
{
  try {
    let result = await doFetch();
    console.log(result);
  } catch( err ) {
    console.error( err.message );
  }
}

fetchAsync();

However, additional improvements to this sample code are very welcome! For example, the following code is clearer and in better alignment with mw:Manual:Coding_conventions/JavaScript:

let url = 'https://en.wikipedia.org/w/rest.php/v1/search/page?q=jupiter&limit=20';
let response = await fetch( url );
response.json()
    .then(console.log).catch(console.error);

Code samples on mw:API:REST API/Reference are transcluded from subpages. API:REST_API/About provides an index of subpages used in these docs.

This bug was originally spotted in T268791#7066560.

Event Timeline

@Lucas_Werkmeister_WMDE I'd love to get your feedback on what you think would be the best style for these JavaScript examples. Would you prefer something simpler like the shorter example in the task description?

Declining this task since there's an open patch to add support for Api-User-Agent headers in the Core REST API (https://gerrit.wikimedia.org/r/c/mediawiki/core/+/693508)