feat(data-source,editor): http数据源params,data,headers支持函数配置

This commit is contained in:
roymondchen 2024-11-22 18:21:51 +08:00 committed by roymondchen
parent 7a47315bc1
commit 03942dc49e
4 changed files with 40 additions and 15 deletions

View File

@ -18,7 +18,7 @@
import type { HttpOptions, RequestFunction } from '@tmagic/core'; import type { HttpOptions, RequestFunction } from '@tmagic/core';
import { getValueByKeyPath } from '@tmagic/core'; import { getValueByKeyPath } from '@tmagic/core';
import { DataSourceOptions, HttpDataSourceSchema } from '@data-source/types'; import type { DataSourceOptions, HttpDataSourceSchema, HttpOptionsSchema } from '@data-source/types';
import DataSource from './Base'; import DataSource from './Base';
@ -74,7 +74,7 @@ export default class HttpDataSource extends DataSource<HttpDataSourceSchema> {
code?: string | number; code?: string | number;
}; };
/** 请求配置 */ /** 请求配置 */
public httpOptions: HttpOptions; public httpOptions: HttpOptionsSchema;
/** 请求函数 */ /** 请求函数 */
#fetch?: RequestFunction; #fetch?: RequestFunction;
@ -115,7 +115,7 @@ export default class HttpDataSource extends DataSource<HttpDataSourceSchema> {
public async init() { public async init() {
if (this.schema.autoFetch) { if (this.schema.autoFetch) {
await this.request(this.httpOptions); await this.request();
} }
super.init(); super.init();
@ -124,8 +124,14 @@ export default class HttpDataSource extends DataSource<HttpDataSourceSchema> {
public async request(options: Partial<HttpOptions> = {}) { public async request(options: Partial<HttpOptions> = {}) {
this.isLoading = true; this.isLoading = true;
let reqOptions = { const { url, params, data, headers, ...otherHttpOptions } = this.httpOptions;
...this.httpOptions,
let reqOptions: HttpOptions = {
url: typeof url === 'function' ? url({ app: this.app, dataSource: this }) : url,
params: typeof params === 'function' ? params({ app: this.app, dataSource: this }) : params,
data: typeof data === 'function' ? data({ app: this.app, dataSource: this }) : data,
headers: typeof headers === 'function' ? headers({ app: this.app, dataSource: this }) : headers,
...otherHttpOptions,
...options, ...options,
}; };

View File

@ -1,4 +1,4 @@
import type { DataSourceSchema, default as TMagicApp, HttpOptions, RequestFunction } from '@tmagic/core'; import type { DataSourceSchema, default as TMagicApp, HttpOptions, Method, RequestFunction } from '@tmagic/core';
import type DataSource from './data-sources/Base'; import type DataSource from './data-sources/Base';
import type HttpDataSource from './data-sources/Http'; import type HttpDataSource from './data-sources/Http';
@ -16,9 +16,23 @@ export interface DataSourceOptions<T extends DataSourceSchema = DataSourceSchema
[key: string]: any; [key: string]: any;
} }
export interface HttpOptionsSchema {
/** 请求链接 */
url: string | ((data: { app: TMagicApp; dataSource: HttpDataSource }) => string);
/** query参数 */
params?: Record<string, string> | ((data: { app: TMagicApp; dataSource: HttpDataSource }) => Record<string, string>);
/** body数据 */
data?: Record<string, any> | ((data: { app: TMagicApp; dataSource: HttpDataSource }) => Record<string, string>);
/** 请求头 */
headers?: Record<string, string> | ((data: { app: TMagicApp; dataSource: HttpDataSource }) => Record<string, string>);
/** 请求方法 GET/POST */
method?: Method;
[key: string]: any;
}
export interface HttpDataSourceSchema extends DataSourceSchema { export interface HttpDataSourceSchema extends DataSourceSchema {
type: 'http'; type: 'http';
options: HttpOptions; options: HttpOptionsSchema;
responseOptions?: { responseOptions?: {
dataPath?: string; dataPath?: string;
}; };

View File

@ -39,7 +39,7 @@
v-if="config.advanced && showCode" v-if="config.advanced && showCode"
height="200px" height="200px"
:init-values="model[name]" :init-values="model[name]"
language="json" language="javascript"
:options="{ :options="{
readOnly: disabled, readOnly: disabled,
}" }"
@ -94,6 +94,9 @@ const records = ref<[string, string][]>([]);
const showCode = ref(false); const showCode = ref(false);
watchEffect(() => { watchEffect(() => {
if (typeof props.model[props.name] === 'function') {
showCode.value = true;
} else {
const initValues: [string, any][] = Object.entries(props.model[props.name] || {}); const initValues: [string, any][] = Object.entries(props.model[props.name] || {});
for (const [, value] of initValues) { for (const [, value] of initValues) {
@ -102,8 +105,8 @@ watchEffect(() => {
break; break;
} }
} }
records.value = initValues; records.value = initValues;
}
}); });
const getValue = () => { const getValue = () => {

View File

@ -40,6 +40,7 @@ export default [
name: 'params', name: 'params',
type: 'key-value', type: 'key-value',
defaultValue: {}, defaultValue: {},
advanced: true,
text: '参数', text: '参数',
}, },
{ {
@ -53,6 +54,7 @@ export default [
name: 'headers', name: 'headers',
type: 'key-value', type: 'key-value',
defaultValue: {}, defaultValue: {},
advanced: true,
text: '请求头', text: '请求头',
}, },
], ],