All files / ext.wikilambda.app/store/stores functionCall.js

97.22% Statements 70/72
87.5% Branches 7/8
100% Functions 3/3
97.22% Lines 70/72

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 60 61 62 63 64 65 66 67 68 69 70 71 72 73126x 126x 126x 126x 126x 126x 126x 126x 126x 126x 126x 126x 126x 126x 126x 126x 126x 126x 126x 126x 126x 126x 126x 126x 126x 3x 3x 3x 3x 3x     3x 3x 3x 3x 126x 126x 126x 126x 126x 126x 126x 126x 126x 126x 126x 126x 3x 3x 3x 3x 2x 2x 2x 2x 2x 2x 2x 2x 2x 3x 1x 1x 1x 1x 1x 1x 3x 3x 126x 126x  
/*!
 * WikiLambda Vue editor: Handle the request of function calls to the orchestrator. (Pinia)
 *
 * @copyright 2020– Abstract Wikipedia team; see AUTHORS.txt
 * @license MIT
 */
'use strict';
 
const Constants = require( '../../Constants.js' );
const { performFunctionCall } = require( '../../utils/apiUtils.js' );
const { extractZIDs } = require( '../../utils/schemata.js' );
const { getZStringTerminalValue } = require( '../../utils/zobjectUtils.js' );
 
module.exports = {
	state: {},
 
	getters: {
		/**
		 * Returns whether the metadata object from the
		 * response contains any errors. An error is a
		 * map item where the key value is 'errors'.
		 *
		 * @return {boolean}
		 */
		hasMetadataErrors: function () {
			const metadata = this.getZObjectByKeyPath( [
				Constants.STORED_OBJECTS.RESPONSE,
				Constants.Z_RESPONSEENVELOPE_METADATA
			] );
			if ( !metadata ) {
				return undefined;
			}
			const map = metadata[ Constants.Z_TYPED_OBJECT_ELEMENT_1 ].slice( 1 );
			return map.find( ( item ) => getZStringTerminalValue(
				item[ Constants.Z_TYPED_OBJECT_ELEMENT_1 ] ) === 'errors' );
		}
	},
 
	actions: {
		/**
		 * Calls orchestrator and sets orchestrationResult
		 *
		 * @param {Object} payload
		 * @param {Object} payload.functionCall
		 * @param {Array} payload.resultKeyPath
		 * @return {Promise}
		 */
		callZFunction: function ( { functionCall, resultKeyPath } ) {
			return performFunctionCall( {
				functionCall,
				language: this.getUserLangCode
			} ).then( ( data ) => {
				// Asynchronously collect the necessary labels
				const zids = extractZIDs( data.response );
				this.fetchZids( { zids } );
 
				// Success, we store the response
				this.setValueByKeyPath( {
					keyPath: resultKeyPath,
					value: data.response
				} );
			} ).catch( ( error ) => {
				// Error, we set response error
				this.setError( {
					errorId: Constants.STORED_OBJECTS.RESPONSE,
					errorType: Constants.ERROR_TYPES.ERROR,
					errorMessage: error.messageOrFallback( 'wikilambda-unknown-exec-error-message' )
				} );
			} );
		}
	}
};