import { Token, TokenGroup } from "../../../types/Token"; import { ScriptAjaxDescriptor } from "../../../types/script/ScriptAjaxDescriptor"; import { ScriptDescriptorType } from "../../../types/script/ScriptDescriptorType"; import { HttpMethod } from "../../../types/HttpMethod"; export function parseAjax( tokens: Token[] ): ScriptAjaxDescriptor | null { const strings = tokens.filter( x => x.section === 'string' ); const startIndex = tokens.findIndex( x => x.section === 'initiator' ); const urlIndex = tokens.findIndex( x => x.section === 'string' && x.key.indexOf( 'http' ) !== -1 ); if( strings.length === 0 || urlIndex === -1 ) return null; let requestedProperties = []; for( let i=startIndex + 1; i < urlIndex && i < tokens.length; i++ ) { if( tokens[i]?.group === TokenGroup.Custom ) { requestedProperties.push( tokens[i].key ); } } return { type: ScriptDescriptorType.Ajax, isAsync: true, method: getAjaxMethod( tokens ), variables: requestedProperties, properties: requestedProperties, //TODO: May want to remove `properties` url: strings[0].key } } function getAjaxMethod( tokens: Token[] ): HttpMethod { let method: HttpMethod = HttpMethod.Get; if( tokens.some( x => ['post', 'upload'].some( methodType => x.key === methodType ) ) ) { method = HttpMethod.Post; } if( tokens.some( x => ['delete', 'remove'].some( methodType => x.key === methodType ) ) ) { method = HttpMethod.Delete; } return method; }