fix: improve code

This commit is contained in:
chenos 2025-06-24 11:57:20 +08:00
parent 1fc045e45c
commit 3f2612ec72
3 changed files with 42 additions and 1 deletions

View File

@ -46,6 +46,16 @@ export class APIResource<TData = any> extends FlowResource<TData> {
this.setMeta({ loading: value }); this.setMeta({ loading: value });
} }
clearRequestParameters() {
this.request.params = {};
return this;
}
setRequestParameters(params: Record<string, any>) {
this.request.params = { ...this.request.params, ...params };
return this;
}
setRequestMethod(method: string) { setRequestMethod(method: string) {
this.request.method = method; this.request.method = method;
return this; return this;
@ -59,6 +69,13 @@ export class APIResource<TData = any> extends FlowResource<TData> {
return this; return this;
} }
removeRequestHeader(key: string) {
if (this.request.headers && key in this.request.headers) {
delete this.request.headers[key];
}
return this;
}
addRequestParameter(key: string, value: any) { addRequestParameter(key: string, value: any) {
if (!this.request.params) { if (!this.request.params) {
this.request.params = {}; this.request.params = {};
@ -67,6 +84,20 @@ export class APIResource<TData = any> extends FlowResource<TData> {
return this; return this;
} }
getRequestParameter(key: string) {
if (this.request.params && key in this.request.params) {
return this.request.params[key];
}
return null;
}
removeRequestParameter(key: string) {
if (this.request.params && key in this.request.params) {
delete this.request.params[key];
}
return this;
}
setRequestBody(data: any) { setRequestBody(data: any) {
this.request.data = data; this.request.data = data;
return this; return this;

View File

@ -22,6 +22,13 @@ import { createJavaScriptLinter } from './linter';
// 自定义自动补全函数 // 自定义自动补全函数
const createCustomCompletion = () => { const createCustomCompletion = () => {
const contextVariables = [ const contextVariables = [
{
label: 'request',
type: 'function',
info: 'Make an API request to NocoBase backend',
detail: '(options: RequestOptions) => Promise<Response>',
boost: 102,
},
{ {
label: 'getModelById', label: 'getModelById',
type: 'function', type: 'function',

View File

@ -202,10 +202,12 @@ element.innerHTML = \`
return ctx.globals.flowEngine.getModel(uid); return ctx.globals.flowEngine.getModel(uid);
}; };
const request = ctx.globals.api.request.bind(ctx.globals.api);
// Create a safe execution context for the code (as async function) // Create a safe execution context for the code (as async function)
// Wrap user code in an async function // Wrap user code in an async function
const wrappedCode = ` const wrappedCode = `
return (async function(element, ctx, model, resource, requirejs, requireAsync, loadCSS, getModelById) { return (async function(element, ctx, model, resource, requirejs, requireAsync, loadCSS, getModelById, request) {
${params.code} ${params.code}
}).apply(this, arguments); }).apply(this, arguments);
`; `;
@ -221,6 +223,7 @@ element.innerHTML = \`
requireAsync, requireAsync,
loadCSS, loadCSS,
getModelById, getModelById,
request,
); );
ctx.model.setProps('loading', false); ctx.model.setProps('loading', false);