Repository navigation
Expand file tree
/
Copy pathFormstep.node.ts
More file actions
78 lines (71 loc) · 3.3 KB
/
Copy pathFormstep.node.ts
File metadata and controls
78 lines (71 loc) · 3.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
import type { IExecuteFunctions, INodeExecutionData, INodeType, INodeTypeDescription } from 'n8n-workflow'
import { NodeConnectionTypes, NodeOperationError } from 'n8n-workflow'
import { requestProperties } from './actions/RequestDescription'
import type { FieldLookup } from './actions/RequestCreate'
import { isRequestOperation, REQUEST_OPERATIONS } from './actions/RequestOperations'
import { authenticationProperties, formstepCredentials } from './Authentication'
import { listFields, type FormField } from './FormstepCatalog'
import { listSearch, loadOptions, resourceMapping } from './FormstepMethods'
import { isNodeError } from './NodeErrors'
/** `fields.list` for each form an execution creates requests for, called once per form however many items there are. */
function createFieldLookup(context: IExecuteFunctions): FieldLookup {
const byForm = new Map<string, Promise<FormField[]>>()
return (formId) => {
const cached = byForm.get(formId)
if (cached) return cached
const fields = listFields(context, formId)
byForm.set(formId, fields)
return fields
}
}
export class Formstep implements INodeType {
description: INodeTypeDescription = {
displayName: 'Formstep',
name: 'formstep',
icon: { light: 'file:formstep-logo.svg', dark: 'file:formstep-logo.dark.svg' },
group: ['transform'],
version: [1, 2, 3, 4, 5],
defaultVersion: 5,
subtitle: '={{ $parameter["operation"] + ": " + $parameter["resource"] }}',
description: 'Create, read, cancel and remind Formstep requests, and pause a workflow until the recipient completes one',
defaults: {
name: 'Formstep',
},
inputs: [NodeConnectionTypes.Main],
outputs: [NodeConnectionTypes.Main],
usableAsTool: true,
credentials: formstepCredentials,
// Version 4 changes only the default Authentication, to an API key.
properties: [...authenticationProperties(4), ...requestProperties],
}
methods = { loadOptions, listSearch, resourceMapping }
async execute(this: IExecuteFunctions): Promise<INodeExecutionData[][]> {
const items = this.getInputData()
const returnData: INodeExecutionData[] = []
const operation = this.getNodeParameter('operation', 0) as string
if (!isRequestOperation(operation)) {
throw new NodeOperationError(this.getNode(), `The operation "${operation}" is not supported`, {
description: "Pick one of the listed operations under 'Operation'.",
})
}
const run = REQUEST_OPERATIONS[operation]
const fieldsOf = createFieldLookup(this)
for (let itemIndex = 0; itemIndex < items.length; itemIndex++) {
try {
const data = await run(this, itemIndex, fieldsOf)
returnData.push(
...this.helpers.constructExecutionMetaData(this.helpers.returnJsonArray(data), { itemData: { item: itemIndex } })
)
} catch (error) {
if (this.continueOnFail()) {
const message = error instanceof Error ? error.message : String(error)
returnData.push({ json: { error: message }, pairedItem: { item: itemIndex } })
continue
}
// Formstep errors already carry their code and message; anything else gets the failing item.
throw isNodeError(error) ? error : new NodeOperationError(this.getNode(), error as Error, { itemIndex })
}
}
return [returnData]
}
}