@@ -309,6 +309,116 @@ To avoid just relying on default parameter values, you have several options:
309309 you may be able to process both ` formatversion=1 ` and ` formatversion=2 ` responses
310310 (see also the ` responseBoolean ` helper function).
311311
312+ ### Usage recommendations
313+
314+ While m3api can be used in different ways,
315+ the following defaults are generally recommended:
316+
317+ ``` js
318+ const session = new Session ( ' en.wikipedia.org' , { // or other domain
319+ formatversion: 2 ,
320+ // (optional) errorformat: TODO,
321+ }, {
322+ userAgent: TODO ,
323+ } );
324+ ```
325+
326+ - Include ` formatversion: 2 ` in the default parameters for all requests.
327+ There is no reason to use ` formatversion: 1 ` (the default),
328+ it only exists for compatibility with existing code.
329+
330+ - Set a good user agent in the ` userAgent ` request option,
331+ in accordance with the [ User-Agent Policy] [ ] .
332+ For example:
333+ ` userAgent: 'ExampleTool (https://example.toolforge.org/; https://gitlab.wikimedia.org/toolforge-repos/example/)' `
334+
335+ - Optionally, include a suitable ` errorformat ` in the default parameters for all requests.
336+ The best value will depend on the context where you use m3api;
337+ ` errorformat: 'plaintext' ` is probably a safe choice in most cases.
338+
339+ - Use extension packages (see below) if they are available for the API modules you are using.
340+
341+ #### Recommendations for interactive tools
342+
343+ Interactive tools may additionally consider:
344+
345+ ``` js
346+ const session = new Session ( ' en.wikipedia.org' , { // or other domain
347+ formatversion: 2 ,
348+ errorformat: ' html' , // assuming the tool is web-based
349+ }, {
350+ userAgent: TODO ,
351+ maxRetriesSeconds: 5 , // or other low value
352+ } );
353+ ```
354+
355+ - Consider choosing a lower ` maxRetriesSeconds ` value than the default (65 seconds),
356+ as it may be preferable to show an error to the user sooner.
357+ (Keep in mind that you can override options for individual requests too;
358+ you may want to choose a higher value for operations where the user expects to wait a bit,
359+ e.g. when showing the user a “saving edit…” banner and making an edit request.)
360+
361+ - For web-based tools, ` errorformat: 'html' ` is a good choice,
362+ assuming you trust the HTML returned by the MediaWiki API.
363+
364+ - If you want to make edits on behalf of users,
365+ use the [ m3api-oauth2] [ ] extension package (see below).
366+
367+ #### Recommendations for bots
368+
369+ Non-interactive bots may additionally consider:
370+
371+ ``` js
372+ const accessToken = process .env .OAUTH_ACCESS_TOKEN ; // or other secret source
373+ const session = new Session ( ' en.wikipedia.org' , { // or other domain
374+ formatversion: 2 ,
375+ errorformat: ' plaintext' ,
376+ maxlag: 5 ,
377+ assert: ' user' ,
378+ }, {
379+ userAgent: TODO ,
380+ maxRetriesSeconds: 3600 , // or other high value
381+ authorization: ` Bearer ${ accessToken} ` ,
382+ } );
383+
384+ // ...
385+
386+ await session .request ( {
387+ action: ' edit' ,
388+ bot: true ,
389+ // ...
390+ }, {
391+ method: ' POST' ,
392+ tokentype: ' csrf' ,
393+ } );
394+ ```
395+
396+ - Specify the [ maxlag parameter] [ ] .
397+ 5 (seconds) is a common choice.
398+ This will make requests fail if the site is overloaded;
399+ m3api will automatically sleep and retry the request later.
400+
401+ - Consider choosing a higher ` maxRetriesSeconds ` value than the default (65 seconds),
402+ as your bot can probably just wait if e.g. maxlag is currently too high.
403+
404+ - For bots whose output goes to a log file or console,
405+ ` errorformat: 'plaintext' ` is probably a reasonable choice.
406+ (Alternatively, ` 'raw' ` may also be useful.)
407+
408+ - When making edits, specify the ` 'bot' ` parameter.
409+ (However, because this parameter is specific to individual API modules,
410+ you probably don’t want to add it to the default parameters of the whole session.)
411+
412+ - To authenticate the bot, create an owner-only OAuth 2 client,
413+ save the access token in a suitable secret store,
414+ specify <code >Bearer <var >accessToken</var ></code > as the ` authorization ` option,
415+ and include ` assert: 'user' ` (and, if you like, <code >assertuser: '<var >user name</var >'</code >) in the default parameters.
416+ If the wiki you’re targeting doesn’t support OAuth 2,
417+ you may instead want to use the [ m3api-botpassword] [ ] extension package (see below).
418+ (If OAuth is supported, you should use it.
419+ Bot passwords are strictly inferior, and an <em >owner-only</em > client is easy to use –
420+ you do not need [ m3api-oauth2] [ ] in that case.)
421+
312422## Extension packages
313423
314424While m3api itself aims to be a minimal library,
@@ -484,8 +594,10 @@ you agree to publish your contribution under the same license.
484594[ API sandbox ] : https://en.wikipedia.org/wiki/Special:ApiSandbox
485595[ m3api-ApiSandbox-helper ] : https://meta.wikimedia.org/wiki/User:Lucas_Werkmeister/m3api-ApiSandbox-helper
486596[ mediawiki-js ] : https://github.com/brettz9/mediawiki-js
597+ [ User-Agent Policy ] : https://foundation.wikimedia.org/wiki/Special:MyLanguage/Policy:Wikimedia_Foundation_User-Agent_Policy
487598[ m3api-examples ] : https://github.com/lucaswerkmeister/m3api-examples
488599[ m3api-oauth2 ] : https://www.npmjs.com/package/m3api-oauth2
600+ [ maxlag parameter ] : https://www.mediawiki.org/wiki/Special:MyLanguage/Manual:Maxlag_parameter
489601[ m3api-query ] : https://www.npmjs.com/package/m3api-query
490602[ m3api-botpassword ] : https://www.npmjs.com/package/m3api-botpassword
491603[ bot password ] : https://www.mediawiki.org/wiki/Special:MyLanguage/Manual:Bot_passwords
0 commit comments