import { IControl, IDataSource, SourceCode, ITableDefinition, ComponentControl, ControlType } from '@datahu/core' import {I18n} from '../i18n' import {BaseDataSource, BaseDataSourceOption} from '../base' import {RestfulHelper} from './RestfulHelper' let i18n = I18n.get('zh-cn') export class RestfulDataSourceOption extends BaseDataSourceOption { static controls: Array = [] @ComponentControl({ type: ControlType.text, title: i18n.restful_url }) url: string = '' @ComponentControl({ type: ControlType.text, title: i18n.restful_result_field }) resultField: string = 'data' @ComponentControl({ type: ControlType.select, title: i18n.restful_result_type, options: [ {value: 'Array', label: '单表数组'}, { value: 'Object>', label: '多表对象' } ] }) resultType: string = 'Array' @ComponentControl({ type: ControlType.select, title: i18n.restful_method, options: [ {value: 'get', label: 'GET'}, {value: 'post', label: 'POST'} ] }) method: string = 'get' @ComponentControl({ type: ControlType.boolean, title: i18n.restful_open_auth }) openAuth: boolean = false @ComponentControl({ type: ControlType.select, name: 'authMode', title: i18n.restful_auth_mode, options: [{value: 'basicAuth', label: 'Basic Auth'}], show: `(data) => { return data.openAuth }` }) authMode: string = '' @ComponentControl({ type: ControlType.text, title: i18n.restful_username, show: `(data) => { return data.authMode === 'basicAuth' }` }) username: string = '' @ComponentControl({ type: ControlType.password, title: i18n.restful_password, show: `(data) => { return data.authMode === 'basicAuth' }` }) password: string = '' @ComponentControl({ type: ControlType.textarea, title: i18n.restful_request_body, show: `(data) => { return data.method === 'post' }` }) requestBody: string = '' @ComponentControl({ type: ControlType.objectSet, title: i18n.restful_params }) params: Array = [ { key: '', value: '' } ] @ComponentControl({ type: ControlType.objectSet, title: i18n.restful_headers }) headers: Array = [ { key: '', value: '' } ] } export class RestfulDataSource extends BaseDataSource { helper?: RestfulHelper constructor(language: string, config?: RestfulDataSourceOption) { super(language, config) this.title = 'Restful' this.description = 'Restful API' if (config) { this.helper = new RestfulHelper(config) } } // 数据源默认配置值 config: RestfulDataSourceOption = new RestfulDataSourceOption() icon: string = '' title: string = '' description: string = '' sourceCode: SourceCode = SourceCode.None getTables(): Promise> { return this.helper!.getTables() } getData(tables: Array): Promise> { return this.helper!.getData(tables) } }