import { ref, computed } from 'vue'; import { axios, Environment, $t, Tools, EnumTools, Options } from 'platform-core'; import { Processor } from '../Processor'; import { PlaceHolder } from '@/utils/PlaceHolder'; class HttpRequest extends Processor { #HttpMethodTypeEnum: any; #httpMethodTypeOptionsRef = ref([]); #HttpAuthorizationTypeEnum: any; #httpAuthorizationTypeOptionsRef = ref([]); constructor(targetType: string, context?: any) { super(targetType, context); this.PROCESSOR_TYPE = 'HTTP_REQUEST'; this.EDITOR_DIALOG_WIDTH = 1024; EnumTools.fetch(['io.sc.engine.rule.core.enums.HttpMethodType', 'io.sc.engine.rule.core.enums.HttpAuthorizationType']).then((value) => { this.#HttpMethodTypeEnum = value.HttpMethodType; this.#httpMethodTypeOptionsRef = Options.enum(this.#HttpMethodTypeEnum); this.#HttpAuthorizationTypeEnum = value.HttpAuthorizationType; this.#httpAuthorizationTypeOptionsRef = Options.enum(this.#HttpAuthorizationTypeEnum); }); } public getToolbarAction(): any { return { extend: 'add', name: 'http', label: $t('io.sc.engine.rule.core.enums.ProcessorType.' + this.PROCESSOR_TYPE), icon: 'sync_alt', enableIf: (args: any) => { const type = this.context.target.type; return type !== 'RULE_RESULT' && type !== 'SINGLE_RULE_RESULT'; }, afterClick: (args: any) => { args.grid.getEditorForm().setFieldValue('type', this.PROCESSOR_TYPE); }, }; } public format(row: any): any { return row.httpMethod + ', ' + PlaceHolder.replace(row.httpUrl); } public getEditorFields(): any { return [ { colSpan: 3, name: 'httpMethod', label: $t('re.processor.grid.entity.httpMethod'), type: 'w-select', options: this.#httpMethodTypeOptionsRef, rows: 1, showIf: (args: any) => { return args.form.getFieldValue('type') === this.PROCESSOR_TYPE; }, }, { colSpan: 9, name: 'httpUrl', label: $t('re.processor.grid.entity.httpUrl'), type: 'w-text', showIf: (args: any) => { return args.form.getFieldValue('type') === this.PROCESSOR_TYPE; }, }, { colSpan: 3, name: 'httpAuthType', label: $t('re.processor.grid.entity.httpAuthType'), type: 'w-select', options: this.#httpAuthorizationTypeOptionsRef, showIf: (args: any) => { return args.form.getFieldValue('type') === this.PROCESSOR_TYPE; }, }, { colSpan: 3, name: 'httpAuthApikey', label: $t('re.processor.grid.entity.httpAuthApikey'), type: 'w-text', showIf: (args: any) => { return args.form.getFieldValue('type') === this.PROCESSOR_TYPE && args.form.getFieldValue('httpAuthType') === 'API_KEY'; }, }, { colSpan: 6, name: 'httpAuthApiValue', label: $t('re.processor.grid.entity.httpAuthApiValue'), type: 'w-text', showIf: (args: any) => { return args.form.getFieldValue('type') === this.PROCESSOR_TYPE && args.form.getFieldValue('httpAuthType') === 'API_KEY'; }, }, { colSpan: 3, name: 'httpAuthBasicUsername', label: $t('re.processor.grid.entity.httpAuthBasicUsername'), type: 'w-text', showIf: (args: any) => { return args.form.getFieldValue('type') === this.PROCESSOR_TYPE && args.form.getFieldValue('httpAuthType') === 'BASIC_AUTH'; }, }, { colSpan: 6, name: 'httpAuthBasicPassword', label: $t('re.processor.grid.entity.httpAuthBasicPassword'), type: 'w-text', showIf: (args: any) => { return args.form.getFieldValue('type') === this.PROCESSOR_TYPE && args.form.getFieldValue('httpAuthType') === 'BASIC_AUTH'; }, }, { colSpan: 9, name: 'httpAuthBearerToken', label: $t('re.processor.grid.entity.httpAuthBearerToken'), type: 'w-text', showIf: (args: any) => { return args.form.getFieldValue('type') === this.PROCESSOR_TYPE && args.form.getFieldValue('httpAuthType') === 'BEARER'; }, }, { colSpan: 12, name: 'httpRequestBody', label: $t('re.processor.grid.entity.httpRequestBody'), showIf: (args: any) => { return args.form.getFieldValue('type') === this.PROCESSOR_TYPE; }, type: 'w-code-mirror', rows: 4, toolbar: false, lineNumber: false, lang: 'json', lineWrap: false, lineBreak: true, placeholder: true, autoCompletion: this.autoCompletionManager.autoCompletion(), userDefinedFunctions: this.userDefinedFunctionsManager.userDefinedFunctions(), }, { colSpan: 12, name: 'httpResponseBody', label: $t('re.processor.grid.entity.httpResponseBody'), showIf: (args: any) => { return args.form.getFieldValue('type') === this.PROCESSOR_TYPE; }, type: 'w-code-mirror', lang: 'json', rows: 5, toolbar: false, lineNumber: false, lineWrap: false, lineBreak: true, }, { colSpan: 12, name: 'httpResponseMapping', label: $t('re.processor.grid.entity.httpResponseMapping'), showIf: (args: any) => { return args.form.getFieldValue('type') === this.PROCESSOR_TYPE; }, type: 'w-code-mirror', rows: 8, toolbar: false, lineNumber: false, lang: 'json', lineWrap: false, lineBreak: true, placeholder: true, autoCompletion: this.autoCompletionManager.autoCompletion(), userDefinedFunctions: this.userDefinedFunctionsManager.userDefinedFunctions(), }, ]; } public getViewerFields(): any { return [ { name: 'httpMethod', label: $t('re.processor.grid.entity.httpMethod') }, { name: 'httpUrl', label: $t('re.processor.grid.entity.httpUrl') }, { name: 'httpAuthType', label: $t('re.processor.grid.entity.httpAuthType') }, { name: 'httpAuthApikey', label: $t('re.processor.grid.entity.httpAuthApikey') }, { name: 'httpAuthApiValue', label: $t('re.processor.grid.entity.httpAuthApiValue') }, { name: 'httpAuthBasicUsername', label: $t('re.processor.grid.entity.httpAuthBasicUsername') }, { name: 'httpAuthBasicPassword', label: $t('re.processor.grid.entity.httpAuthBasicPassword') }, { name: 'httpAuthBearerToken', label: $t('re.processor.grid.entity.httpAuthBearerToken') }, { name: 'httpRequestBody', label: $t('re.processor.grid.entity.httpRequestBody') }, { name: 'httpResponseMapping', label: $t('re.processor.grid.entity.httpResponseMapping') }, ]; } public beforeEditorDataSubmit(args: any): void {} public afterEditorOpen(args: any): void { args.grid.getEditorDialog().setWidth(this.EDITOR_DIALOG_WIDTH); this.initAutoCompletionManager(); this.initUserDefinedFunctionsManager(); } } export { HttpRequest };