-
Notifications
You must be signed in to change notification settings - Fork 391
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix [Feature]: 新增url类型的对象触发器, 以post方式发送数据 #7095
- Loading branch information
1 parent
5a15b98
commit 5c2a07d
Showing
9 changed files
with
117 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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, | ||
|
@@ -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, | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 10 additions & 0 deletions
10
...-database/main/default/objects/object_triggers/fields/authentication_header_key.field.yml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
6 changes: 6 additions & 0 deletions
6
...atabase/main/default/objects/object_triggers/fields/authentication_header_value.field.yml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'} |
12 changes: 12 additions & 0 deletions
12
...object-database/main/default/objects/object_triggers/fields/authentication_type.field.yml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -23,3 +23,4 @@ editorDidMount: >- | |
result | ||
); | ||
sort_no: 150 | ||
visible_on: ${type != 'url'} |
10 changes: 10 additions & 0 deletions
10
services/standard-object-database/main/default/objects/object_triggers/fields/type.field.yml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
6 changes: 6 additions & 0 deletions
6
services/standard-object-database/main/default/objects/object_triggers/fields/url.field.yml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'} |