Skip to content

Commit

Permalink
fix [Feature]: 新增url类型的对象触发器, 以post方式发送数据 #7095
Browse files Browse the repository at this point in the history
  • Loading branch information
baozhoutao committed Nov 25, 2024
1 parent 5a15b98 commit 5c2a07d
Show file tree
Hide file tree
Showing 9 changed files with 117 additions and 1 deletion.
1 change: 1 addition & 0 deletions packages/objectql/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
"@steedos/schemas": "2.7.11-beta.2",
"@steedos/standard-objects": "2.7.11-beta.2",
"amis-formula": "~6.3.0",
"axios": "^0.21.1",
"app-root-path": "^2.2.1",
"body-parser": "^1.18.1",
"bunyan-sfdx-no-dtrace": "^1.8.2",
Expand Down
64 changes: 63 additions & 1 deletion packages/objectql/src/triggers/trigger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@
* @Author: [email protected]
* @Date: 2023-04-23 13:35:17
* @LastEditors: [email protected]
* @LastEditTime: 2024-04-02 14:00:47
* @LastEditTime: 2024-11-25 10:41:20
* @Description:
*/
const { NodeVM } = require('vm2');
const _ = require('lodash');
import { ObjectId } from "mongodb";
import axios from 'axios';

function str2function(
contents,
Expand All @@ -21,8 +23,68 @@ function str2function(
}
}

const sendPost = async (url, body, options)=>{
try {
return await axios.post(url, body, options);
} catch (error) {
throw new Error(`请求失败(${error.message}): ${url}`)
}
}



/**
*
* 请求参数(body): {
objectName,
userId,
spaceId,
doc
* }
* 接口返回参数结构:
{
"error": {
"code": "",
"message": "",
},
"data": {
}
}
*/
const runUrlTrigger = async (trigger, thisArg, args)=>{
if(!trigger.url){
throw new Error(`触发器「${trigger.name}」缺少URL`)
}

const headers = {
'Content-Type': 'application/json',
};

if(trigger.authentication_type == 'header'){
headers[trigger.authentication_header_key] = trigger.authentication_header_value
}

const { data: rsBody, status: httpStatus } = await sendPost(trigger.url, args && args.length > 0 ? args[0].params : {}, { headers });
if(httpStatus !== 200){
throw new Error(`请求失败: ${trigger.url}`)
}
let { error, data } = rsBody;

if(error && error.message){
throw new Error(error.message)
}
return data
}


export const runTriggerFunction = async (trigger, thisArg, ...args)=>{

if(trigger.type === 'url'){
return await runUrlTrigger(trigger, thisArg, args)
}

// ----- code trigger -----
const vm = new NodeVM({
sandbox: {
str2function,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,10 @@
position: absolute
}

.relative {
position: relative
}

.sticky {
position: sticky
}
Expand All @@ -75,6 +79,10 @@
left: 0px
}

.isolate {
isolation: isolate
}

.z-20 {
z-index: 20
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
name: authentication_header_key
type: text
label: Header Name
group: 认证
sort_no: 180
visible_on: ${type === 'url' && authentication_type === 'header'}
amis:
- options:
- label: authorization
value: authorization
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
name: authentication_header_value
type: text
label: Header Value
group: 认证
sort_no: 190
visible_on: ${type === 'url' && authentication_type === 'header'}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
name: authentication_type
label: 认证类型
type: select
group: 认证
options:
- label: 不认证
value: none
- label: Header Auth
value: header
defaultValue: none
sort_no: 170
visible_on: ${type === 'url'}
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,4 @@ editorDidMount: >-
result
);
sort_no: 150
visible_on: ${type != 'url'}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
name: type
label: 类型
type: select
options:
- label: 代码
value: code
- label: 接口
value: url
defaultValue: code
sort_no: 132
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
name: url
type: url
label: URL
sort_no: 160
is_wide: true
visible_on: ${type === 'url'}

0 comments on commit 5c2a07d

Please sign in to comment.