@nocobase/plugin-workflow-request 0.17.0-alpha.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,183 @@
1
+ import { ArrayItems } from '@formily/antd-v5';
2
+
3
+ import { defaultFieldNames } from '@nocobase/client';
4
+
5
+ import { Instruction, WorkflowVariableInput, WorkflowVariableJSON } from '@nocobase/plugin-workflow/client';
6
+
7
+ import { NAMESPACE } from '../locale';
8
+
9
+ export default class extends Instruction {
10
+ title = `{{t("HTTP request", { ns: "${NAMESPACE}" })}}`;
11
+ type = 'request';
12
+ group = 'extended';
13
+ description = `{{t("Send HTTP request to a URL. You can use the variables in the upstream nodes as request headers, parameters and request body.", { ns: "${NAMESPACE}" })}}`;
14
+ fieldset = {
15
+ method: {
16
+ type: 'string',
17
+ required: true,
18
+ title: `{{t("HTTP method", { ns: "${NAMESPACE}" })}}`,
19
+ 'x-decorator': 'FormItem',
20
+ 'x-component': 'Select',
21
+ 'x-component-props': {
22
+ showSearch: false,
23
+ allowClear: false,
24
+ className: 'auto-width',
25
+ },
26
+ enum: [
27
+ { label: 'GET', value: 'GET' },
28
+ { label: 'POST', value: 'POST' },
29
+ { label: 'PUT', value: 'PUT' },
30
+ { label: 'PATCH', value: 'PATCH' },
31
+ { label: 'DELETE', value: 'DELETE' },
32
+ ],
33
+ default: 'POST',
34
+ },
35
+ url: {
36
+ type: 'string',
37
+ required: true,
38
+ title: `{{t("URL", { ns: "${NAMESPACE}" })}}`,
39
+ 'x-decorator': 'FormItem',
40
+ 'x-decorator-props': {},
41
+ 'x-component': 'Input',
42
+ 'x-component-props': {
43
+ placeholder: 'https://www.nocobase.com',
44
+ },
45
+ },
46
+ headers: {
47
+ type: 'array',
48
+ 'x-component': 'ArrayItems',
49
+ 'x-decorator': 'FormItem',
50
+ title: `{{t("Headers", { ns: "${NAMESPACE}" })}}`,
51
+ description: `{{t('"Content-Type" only support "application/json", and no need to specify', { ns: "${NAMESPACE}" })}}`,
52
+ items: {
53
+ type: 'object',
54
+ properties: {
55
+ space: {
56
+ type: 'void',
57
+ 'x-component': 'Space',
58
+ properties: {
59
+ name: {
60
+ type: 'string',
61
+ 'x-decorator': 'FormItem',
62
+ 'x-component': 'Input',
63
+ 'x-component-props': {
64
+ placeholder: `{{t("Name")}}`,
65
+ },
66
+ },
67
+ value: {
68
+ type: 'string',
69
+ 'x-decorator': 'FormItem',
70
+ 'x-component': 'WorkflowVariableInput',
71
+ 'x-component-props': {
72
+ useTypedConstant: true,
73
+ },
74
+ },
75
+ remove: {
76
+ type: 'void',
77
+ 'x-decorator': 'FormItem',
78
+ 'x-component': 'ArrayItems.Remove',
79
+ },
80
+ },
81
+ },
82
+ },
83
+ },
84
+ properties: {
85
+ add: {
86
+ type: 'void',
87
+ title: `{{t("Add request header", { ns: "${NAMESPACE}" })}}`,
88
+ 'x-component': 'ArrayItems.Addition',
89
+ },
90
+ },
91
+ },
92
+ params: {
93
+ type: 'array',
94
+ 'x-component': 'ArrayItems',
95
+ 'x-decorator': 'FormItem',
96
+ title: `{{t("Parameters", { ns: "${NAMESPACE}" })}}`,
97
+ items: {
98
+ type: 'object',
99
+ properties: {
100
+ space: {
101
+ type: 'void',
102
+ 'x-component': 'Space',
103
+ properties: {
104
+ name: {
105
+ type: 'string',
106
+ 'x-decorator': 'FormItem',
107
+ 'x-component': 'Input',
108
+ 'x-component-props': {
109
+ placeholder: `{{t("Name")}}`,
110
+ },
111
+ },
112
+ value: {
113
+ type: 'string',
114
+ 'x-decorator': 'FormItem',
115
+ 'x-component': 'WorkflowVariableInput',
116
+ 'x-component-props': {
117
+ useTypedConstant: true,
118
+ },
119
+ },
120
+ remove: {
121
+ type: 'void',
122
+ 'x-decorator': 'FormItem',
123
+ 'x-component': 'ArrayItems.Remove',
124
+ },
125
+ },
126
+ },
127
+ },
128
+ },
129
+ properties: {
130
+ add: {
131
+ type: 'void',
132
+ title: `{{t("Add parameter", { ns: "${NAMESPACE}" })}}`,
133
+ 'x-component': 'ArrayItems.Addition',
134
+ },
135
+ },
136
+ },
137
+ data: {
138
+ type: 'string',
139
+ title: `{{t("Body", { ns: "${NAMESPACE}" })}}`,
140
+ 'x-decorator': 'FormItem',
141
+ 'x-decorator-props': {},
142
+ 'x-component': 'WorkflowVariableJSON',
143
+ 'x-component-props': {
144
+ changeOnSelect: true,
145
+ autoSize: {
146
+ minRows: 10,
147
+ },
148
+ placeholder: `{{t("Input request data", { ns: "${NAMESPACE}" })}}`,
149
+ },
150
+ description: `{{t("Only support standard JSON data", { ns: "${NAMESPACE}" })}}`,
151
+ },
152
+ timeout: {
153
+ type: 'number',
154
+ title: `{{t("Timeout config", { ns: "${NAMESPACE}" })}}`,
155
+ 'x-decorator': 'FormItem',
156
+ 'x-decorator-props': {},
157
+ 'x-component': 'InputNumber',
158
+ 'x-component-props': {
159
+ addonAfter: `{{t("ms", { ns: "${NAMESPACE}" })}}`,
160
+ min: 1,
161
+ step: 1000,
162
+ defaultValue: 5000,
163
+ },
164
+ },
165
+ ignoreFail: {
166
+ type: 'boolean',
167
+ title: `{{t("Ignore failed request and continue workflow", { ns: "${NAMESPACE}" })}}`,
168
+ 'x-decorator': 'FormItem',
169
+ 'x-component': 'Checkbox',
170
+ },
171
+ };
172
+ components = {
173
+ ArrayItems,
174
+ WorkflowVariableInput,
175
+ WorkflowVariableJSON,
176
+ };
177
+ useVariables({ key, title }, { types, fieldNames = defaultFieldNames }) {
178
+ return {
179
+ [fieldNames.value]: key,
180
+ [fieldNames.label]: title,
181
+ };
182
+ }
183
+ }
@@ -0,0 +1,19 @@
1
+ import { Plugin } from '@nocobase/client';
2
+ import WorkflowPlugin from '@nocobase/plugin-workflow/client';
3
+
4
+ import RequestInstruction from './RequestInstruction';
5
+
6
+ export default class extends Plugin {
7
+ async afterAdd() {
8
+ // await this.app.pm.add()
9
+ }
10
+
11
+ async beforeLoad() {}
12
+
13
+ // You can get and modify the app instance here
14
+ async load() {
15
+ const workflow = this.app.pm.get('workflow') as WorkflowPlugin;
16
+ const requestInstruction = new RequestInstruction();
17
+ workflow.instructions.register(requestInstruction.type, requestInstruction);
18
+ }
19
+ }
package/src/index.ts ADDED
@@ -0,0 +1,2 @@
1
+ export * from './server';
2
+ export { default } from './server';
@@ -0,0 +1,20 @@
1
+ {
2
+ "HTTP request": "HTTP request",
3
+ "Send HTTP request to a URL. You can use the variables in the upstream nodes as request headers, parameters and request body.": "Send HTTP request to a URL. You can use the variables in the upstream nodes as request headers, parameters and request body.",
4
+ "HTTP method": "HTTP method",
5
+ "URL": "URL",
6
+ "Headers": "Headers",
7
+ "Add request header": "Add request header",
8
+ "Parameters": "Parameters",
9
+ "Add parameter": "Add parameter",
10
+ "Body": "Body",
11
+ "Use variable": "Use variable",
12
+ "Format": "Format",
13
+ "Insert": "Insert",
14
+ "Timeout config": "Timeout config",
15
+ "ms": "ms",
16
+ "Input request data": "Input request data",
17
+ "Only support standard JSON data": "Only support standard JSON data",
18
+ "\"Content-Type\" only support \"application/json\", and no need to specify": "\"Content-Type\" only support \"application/json\", and no need to specify",
19
+ "Ignore failed request and continue workflow": "Ignore failed request and continue workflow"
20
+ }
@@ -0,0 +1,19 @@
1
+ {
2
+ "HTTP request": "Petición HTTP",
3
+ "HTTP method": "Método HTTP",
4
+ "URL": "URL",
5
+ "Headers": "Encabezados",
6
+ "Add request header": "Añadir encabezado de petición",
7
+ "Parameters": "Parámetros",
8
+ "Add parameter": "Añadir parámetro",
9
+ "Body": "Cuerpo",
10
+ "Use variable": "Utilizar variable",
11
+ "Format": "Formato",
12
+ "Insert": "Insertar",
13
+ "Timeout config": "Tiempo de espera config",
14
+ "ms": "ms",
15
+ "Input request data": "Datos de solicitud de entrada",
16
+ "Only support standard JSON data": "Sólo admite datos JSON estándar",
17
+ "\"Content-Type\" only support \"application/json\", and no need to specify": "\"Content-Type\" sólo admite \"application/json\", y no es necesario especificar",
18
+ "Ignore fail request and continue workflow": "Ignorar solicitud fallida y continuar flujo de trabajo"
19
+ }
@@ -0,0 +1,19 @@
1
+ {
2
+ "HTTP request": "Requête HTTP",
3
+ "HTTP method": "Méthode HTTP",
4
+ "URL": "URL",
5
+ "Headers": "En-têtes",
6
+ "Add request header": "Ajouter un en-tête de requête",
7
+ "Parameters": "Paramètres",
8
+ "Add parameter": "Ajouter un paramètre",
9
+ "Body": "Corps",
10
+ "Use variable": "Utiliser une variable",
11
+ "Format": "Format",
12
+ "Insert": "Insérer",
13
+ "Timeout config": "Configuration du délai d'expiration",
14
+ "ms": "ms",
15
+ "Input request data": "Entrée des données de requête",
16
+ "Only support standard JSON data": "Prend uniquement en charge les données JSON standard",
17
+ "\"Content-Type\" only support \"application/json\", and no need to specify": "\"Content-Type\" prend uniquement en charge \"application/json\" et n'a pas besoin d'être spécifié",
18
+ "Ignore fail request and continue workflow": "Ignorer l'échec de la requête et continuer le workflow"
19
+ }
@@ -0,0 +1,12 @@
1
+ import { useTranslation } from 'react-i18next';
2
+
3
+ export const NAMESPACE = 'workflow-request';
4
+
5
+ export function useLang(key: string, options = {}) {
6
+ const { t } = usePluginTranslation(options);
7
+ return t(key);
8
+ }
9
+
10
+ export function usePluginTranslation(options) {
11
+ return useTranslation(NAMESPACE, options);
12
+ }
@@ -0,0 +1,19 @@
1
+ {
2
+ "HTTP request": "Requisição HTTP",
3
+ "HTTP method": "Método HTTP",
4
+ "URL": "URL",
5
+ "Headers": "Cabeçalhos",
6
+ "Add request header": "Adicionar cabeçalho de requisição",
7
+ "Parameters": "Parâmetros",
8
+ "Add parameter": "Adicionar parâmetro",
9
+ "Body": "Corpo",
10
+ "Use variable": "Usar variável",
11
+ "Format": "Formato",
12
+ "Insert": "Inserir",
13
+ "Timeout config": "Configuração de tempo limite",
14
+ "ms": "ms",
15
+ "Input request data": "Dados de entrada da requisição",
16
+ "Only support standard JSON data": "Suporta somente dados JSON padrão",
17
+ "\"Content-Type\" only support \"application/json\", and no need to specify": "\"Content-Type\" somente suporta \"application/json\" e não precisa ser especificado",
18
+ "Ignore fail request and continue workflow": "Ignorar falhas na requisição e continuar o fluxo de trabalho"
19
+ }
@@ -0,0 +1,20 @@
1
+ {
2
+ "HTTP request": "HTTP 请求",
3
+ "Send HTTP request to a URL. You can use the variables in the upstream nodes as request headers, parameters and request body.": "向指定 URL 发送 HTTP 请求。可以使用上游节点里的变量作为请求头、参数和请求体。",
4
+ "HTTP method": "HTTP 方法",
5
+ "URL": "地址",
6
+ "Headers": "请求头",
7
+ "Add request header": "添加请求头",
8
+ "Parameters": "参数",
9
+ "Add parameter": "添加参数",
10
+ "Body": "请求体",
11
+ "Use variable": "使用变量",
12
+ "Format": "格式化",
13
+ "Insert": "插入",
14
+ "Timeout config": "超时设置",
15
+ "ms": "毫秒",
16
+ "Input request data": "输入请求数据",
17
+ "Only support standard JSON data": "仅支持标准 JSON 数据",
18
+ "\"Content-Type\" only support \"application/json\", and no need to specify": "\"Content-Type\" 请求头仅支持 \"application/json\",无需填写",
19
+ "Ignore failed request and continue workflow": "忽略失败的请求并继续工作流"
20
+ }
@@ -0,0 +1,14 @@
1
+ import { Plugin } from '@nocobase/server';
2
+ import WorkflowPlugin from '@nocobase/plugin-workflow';
3
+
4
+ import RequestInstruction from './RequestInstruction';
5
+
6
+ export default class extends Plugin {
7
+ workflow: WorkflowPlugin;
8
+
9
+ async load() {
10
+ const workflowPlugin = this.app.getPlugin('workflow') as WorkflowPlugin;
11
+ this.workflow = workflowPlugin;
12
+ workflowPlugin.instructions.register('request', new RequestInstruction(workflowPlugin));
13
+ }
14
+ }
@@ -0,0 +1,83 @@
1
+ import axios, { AxiosRequestConfig } from 'axios';
2
+
3
+ import { Processor, Instruction, JOB_STATUS, FlowNodeModel } from '@nocobase/plugin-workflow';
4
+
5
+ export interface Header {
6
+ name: string;
7
+ value: string;
8
+ }
9
+
10
+ export type RequestConfig = Pick<AxiosRequestConfig, 'url' | 'method' | 'params' | 'data' | 'timeout'> & {
11
+ headers: Array<Header>;
12
+ ignoreFail: boolean;
13
+ };
14
+
15
+ async function request(config) {
16
+ // default headers
17
+ const { url, method = 'POST', data, timeout = 5000 } = config;
18
+ const headers = (config.headers ?? []).reduce((result, header) => {
19
+ if (header.name.toLowerCase() === 'content-type') {
20
+ return result;
21
+ }
22
+ return Object.assign(result, { [header.name]: header.value });
23
+ }, {});
24
+ const params = (config.params ?? []).reduce(
25
+ (result, param) => Object.assign(result, { [param.name]: param.value }),
26
+ {},
27
+ );
28
+
29
+ // TODO(feat): only support JSON type for now, should support others in future
30
+ headers['Content-Type'] = 'application/json';
31
+
32
+ return axios.request({
33
+ url,
34
+ method,
35
+ headers,
36
+ params,
37
+ data,
38
+ timeout,
39
+ });
40
+ }
41
+
42
+ export default class extends Instruction {
43
+ async run(node: FlowNodeModel, prevJob, processor: Processor) {
44
+ const job = await processor.saveJob({
45
+ status: JOB_STATUS.PENDING,
46
+ nodeId: node.id,
47
+ upstreamId: prevJob?.id ?? null,
48
+ });
49
+
50
+ const config = processor.getParsedValue(node.config, node.id) as RequestConfig;
51
+
52
+ // eslint-disable-next-line promise/catch-or-return
53
+ request(config)
54
+ .then((response) => {
55
+ job.set({
56
+ status: JOB_STATUS.RESOLVED,
57
+ result: response.data,
58
+ });
59
+ })
60
+ .catch((error) => {
61
+ job.set({
62
+ status: JOB_STATUS.FAILED,
63
+ result: error.isAxiosError ? error.toJSON() : error.message,
64
+ });
65
+ })
66
+ .finally(() => {
67
+ processor.logger.info(`request (#${node.id}) response received, status: ${job.get('status')}`);
68
+ this.plugin.resume(job);
69
+ });
70
+
71
+ processor.logger.info(`request (#${node.id}) sent to "${config.url}", waiting for response...`);
72
+
73
+ return processor.exit();
74
+ }
75
+
76
+ async resume(node: FlowNodeModel, job, processor: Processor) {
77
+ const { ignoreFail } = node.config as RequestConfig;
78
+ if (ignoreFail) {
79
+ job.set('status', JOB_STATUS.RESOLVED);
80
+ }
81
+ return job;
82
+ }
83
+ }