All files / ext.wikilambda.edit/mixins api.js

93.33% Statements 14/15
75% Branches 3/4
83.33% Functions 5/6
93.33% Lines 14/15

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59                    16x 16x   16x     3x 3x               2x 2x     2x 2x 2x   2x                 17x   17x           17x                
/**
 * WikiLambda Vue editor: callZFunction mixin
 * Mixin with util function to invoke a ZFunctionCall, canonicalize
 * the result, and return the ZObject and its respective pairs.
 *
 * @copyright 2020– Abstract Wikipedia team; see AUTHORS.txt
 * @license MIT
 */
'use strict';
 
var Constants = require( '../Constants.js' ),
	canonicalize = require( './schemata.js' ).methods.canonicalizeZObject;
 
module.exports = exports = {
	methods: {
		performFunctionCall: function ( zobject, shouldNormalize ) {
			var api = new mw.Api();
			return api.post( {
				action: 'wikilambda_function_call',
				// eslint-disable-next-line camelcase
				wikilambda_function_call_zobject: JSON.stringify(
					canonicalize( zobject )
				)
			} ).then( function ( data ) {
				// eslint-disable-next-line compat/compat
				return new Promise( function ( resolve ) {
					var normalResponse = JSON.parse(
							data.query.wikilambda_function_call.data
						),
						response = !shouldNormalize ? canonicalize( normalResponse ) : normalResponse,
						result = response[ Constants.Z_RESPONSEENVELOPE_VALUE ],
						metadata = response[ Constants.Z_RESPONSEENVELOPE_METADATA ];
 
					resolve( {
						response: response,
						result: result,
						metadata: metadata
					} );
				} );
			} );
		},
		saveZObject: function ( zobject, zid, summary ) {
			var api = new mw.Api();
 
			return api.postWithEditToken( {
				action: 'wikilambda_edit',
				summary: summary || '',
				zid: zid,
				zobject: JSON.stringify( zobject )
			} ).then( function ( result ) {
				return result.wikilambda_edit;
			} ).catch( function ( error, result ) {
				// Pass the error up the chain
				throw ( result );
			} );
		}
	}
};