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                    22x 22x   22x     8x 8x             7x 7x 7x 7x 7x 7x         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';
 
const Constants = require( '../Constants.js' ),
	hybridToCanonical = require( './schemata.js' ).methods.hybridToCanonical;
 
module.exports = exports = {
	methods: {
		performFunctionCall: function ( zobject, shouldNormalize ) {
			const api = new mw.Api();
			return api.post( {
				action: 'wikilambda_function_call',
				// eslint-disable-next-line camelcase
				wikilambda_function_call_zobject: JSON.stringify(
					hybridToCanonical( zobject )
				)
			} ).then( function ( data ) {
				return new Promise( function ( resolve ) {
					const normalResponse = JSON.parse( data.query.wikilambda_function_call.data );
					const response = !shouldNormalize ? hybridToCanonical( normalResponse ) : normalResponse;
					const result = response[ Constants.Z_RESPONSEENVELOPE_VALUE ];
					const metadata = response[ Constants.Z_RESPONSEENVELOPE_METADATA ];
					resolve( { response, result, metadata } );
				} );
			} );
		},
		saveZObject: function ( zobject, zid, summary ) {
			const 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 );
			} );
		}
	}
};