n8n-nodes-browser-smart-automation 0.1.2 → 0.1.5
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.
- package/dist/McpClientTool/McpClientTool.node.js +2 -13
- package/dist/McpClientTool/McpClientTool.node.js.map +1 -1
- package/dist/McpClientTool/utils.js +1 -1
- package/dist/McpClientTool/utils.js.map +1 -1
- package/dist/McpTrigger/McpTrigger.node.js +1 -1
- package/dist/McpTrigger/McpTrigger.node.js.map +1 -1
- package/dist/shared/N8nBinaryLoader.js +203 -0
- package/dist/shared/N8nBinaryLoader.js.map +1 -0
- package/dist/shared/N8nJsonLoader.js +89 -0
- package/dist/shared/N8nJsonLoader.js.map +1 -0
- package/dist/shared/N8nTool.js +106 -0
- package/dist/shared/N8nTool.js.map +1 -0
- package/dist/shared/embeddingInputValidation.js +55 -0
- package/dist/shared/embeddingInputValidation.js.map +1 -0
- package/dist/shared/helpers.js +220 -13
- package/dist/shared/helpers.js.map +1 -1
- package/dist/shared/httpProxyAgent.js +40 -2
- package/dist/shared/httpProxyAgent.js.map +1 -1
- package/dist/shared/logWrapper.js +347 -2
- package/dist/shared/logWrapper.js.map +1 -1
- package/dist/shared/schemaParsing.js +47 -4
- package/dist/shared/schemaParsing.js.map +1 -1
- package/dist/shared/sharedFields.js +142 -7
- package/dist/shared/sharedFields.js.map +1 -1
- package/dist/shared/typesN8nTool.js +17 -0
- package/dist/shared/typesN8nTool.js.map +1 -0
- package/dist/shared/utils.js +1 -1
- package/dist/shared/utils.js.map +1 -1
- package/package.json +25 -7
- package/jest.config.js +0 -24
- package/nodes/McpClient/McpClient.node.ts +0 -327
- package/nodes/McpClient/__test__/McpClient.node.test.ts +0 -221
- package/nodes/McpClient/__test__/utils.test.ts +0 -302
- package/nodes/McpClient/listSearch.ts +0 -48
- package/nodes/McpClient/resourceMapping.ts +0 -48
- package/nodes/McpClient/utils.ts +0 -281
- package/nodes/McpClientTool/McpClientTool.node.ts +0 -468
- package/nodes/McpClientTool/__test__/McpClientTool.node.test.ts +0 -730
- package/nodes/McpClientTool/loadOptions.ts +0 -45
- package/nodes/McpClientTool/types.ts +0 -1
- package/nodes/McpClientTool/utils.ts +0 -116
- package/nodes/McpTrigger/FlushingTransport.ts +0 -61
- package/nodes/McpTrigger/McpServer.ts +0 -317
- package/nodes/McpTrigger/McpTrigger.node.ts +0 -204
- package/nodes/McpTrigger/__test__/FlushingTransport.test.ts +0 -102
- package/nodes/McpTrigger/__test__/McpServer.test.ts +0 -532
- package/nodes/McpTrigger/__test__/McpTrigger.node.test.ts +0 -171
- package/nodes/shared/__test__/utils.test.ts +0 -318
- package/nodes/shared/descriptions.ts +0 -65
- package/nodes/shared/helpers.ts +0 -31
- package/nodes/shared/httpProxyAgent.ts +0 -11
- package/nodes/shared/logWrapper.ts +0 -13
- package/nodes/shared/schemaParsing.ts +0 -9
- package/nodes/shared/sharedFields.ts +0 -20
- package/nodes/shared/types.ts +0 -12
- package/nodes/shared/utils.ts +0 -296
- package/officail/package.json +0 -255
- package/tsconfig.json +0 -32
- package/tsup.config.ts +0 -16
|
@@ -1,302 +0,0 @@
|
|
|
1
|
-
import type { JSONSchema7Definition, JSONSchema7 } from 'json-schema';
|
|
2
|
-
|
|
3
|
-
import {
|
|
4
|
-
convertJsonSchemaToResourceMapperFields,
|
|
5
|
-
jsonSchemaTypeToDefaultValue,
|
|
6
|
-
jsonSchemaTypeToFieldType,
|
|
7
|
-
} from '../utils';
|
|
8
|
-
|
|
9
|
-
describe('utils', () => {
|
|
10
|
-
describe('jsonSchemaTypeToFieldType', () => {
|
|
11
|
-
it.each([
|
|
12
|
-
[{ schema: { type: 'string', format: 'date-time' } as JSONSchema7, expected: 'dateTime' }],
|
|
13
|
-
[{ schema: { type: 'string' } as JSONSchema7, expected: 'string' }],
|
|
14
|
-
[{ schema: { type: 'number' } as JSONSchema7, expected: 'number' }],
|
|
15
|
-
[{ schema: { type: 'integer' } as JSONSchema7, expected: 'number' }],
|
|
16
|
-
[{ schema: { type: 'boolean' } as JSONSchema7, expected: 'boolean' }],
|
|
17
|
-
[{ schema: { type: 'array' } as JSONSchema7, expected: 'array' }],
|
|
18
|
-
[{ schema: { type: 'object' } as JSONSchema7, expected: 'object' }],
|
|
19
|
-
])('should return the correct field type for the schema', ({ schema, expected }) => {
|
|
20
|
-
expect(jsonSchemaTypeToFieldType(schema)).toEqual(expected);
|
|
21
|
-
});
|
|
22
|
-
});
|
|
23
|
-
|
|
24
|
-
describe('jsonSchemaTypeToDefaultValue', () => {
|
|
25
|
-
it.each([
|
|
26
|
-
[{ schema: false as JSONSchema7Definition, expected: null }],
|
|
27
|
-
[{ schema: true as JSONSchema7Definition, expected: 'any' }],
|
|
28
|
-
[{ schema: { type: 'string' } as JSONSchema7Definition, expected: 'string' }],
|
|
29
|
-
[{ schema: { type: 'number' } as JSONSchema7Definition, expected: 0 }],
|
|
30
|
-
[{ schema: { type: 'integer' } as JSONSchema7Definition, expected: 0 }],
|
|
31
|
-
[{ schema: { type: 'number', minimum: -1 } as JSONSchema7Definition, expected: -1 }],
|
|
32
|
-
[{ schema: { type: 'number', maximum: 1 } as JSONSchema7Definition, expected: 1 }],
|
|
33
|
-
[{ schema: { type: 'boolean' } as JSONSchema7Definition, expected: false }],
|
|
34
|
-
[
|
|
35
|
-
{
|
|
36
|
-
schema: { type: 'string', format: 'date-time' } as JSONSchema7Definition,
|
|
37
|
-
expected: '2025-01-01T00:00:00Z',
|
|
38
|
-
},
|
|
39
|
-
],
|
|
40
|
-
[
|
|
41
|
-
{
|
|
42
|
-
schema: { type: 'string', format: 'uri' } as JSONSchema7Definition,
|
|
43
|
-
expected: 'https://example.com',
|
|
44
|
-
},
|
|
45
|
-
],
|
|
46
|
-
[
|
|
47
|
-
{
|
|
48
|
-
schema: { type: 'string', format: 'url' } as JSONSchema7Definition,
|
|
49
|
-
expected: 'https://example.com',
|
|
50
|
-
},
|
|
51
|
-
],
|
|
52
|
-
[
|
|
53
|
-
{
|
|
54
|
-
schema: { type: 'string', format: 'date' } as JSONSchema7Definition,
|
|
55
|
-
expected: '2025-01-01',
|
|
56
|
-
},
|
|
57
|
-
],
|
|
58
|
-
[
|
|
59
|
-
{
|
|
60
|
-
schema: { type: 'string', format: 'time' } as JSONSchema7Definition,
|
|
61
|
-
expected: '00:00:00',
|
|
62
|
-
},
|
|
63
|
-
],
|
|
64
|
-
[{ schema: { type: 'array' } as JSONSchema7Definition, expected: [] }],
|
|
65
|
-
[
|
|
66
|
-
{
|
|
67
|
-
schema: { type: 'array', items: { type: 'string' } } as JSONSchema7Definition,
|
|
68
|
-
expected: ['string'],
|
|
69
|
-
},
|
|
70
|
-
],
|
|
71
|
-
[
|
|
72
|
-
{
|
|
73
|
-
schema: {
|
|
74
|
-
type: 'array',
|
|
75
|
-
items: [{ type: 'number' }, { type: 'string' }],
|
|
76
|
-
} as JSONSchema7Definition,
|
|
77
|
-
expected: [0, 'string'],
|
|
78
|
-
},
|
|
79
|
-
],
|
|
80
|
-
[
|
|
81
|
-
{
|
|
82
|
-
schema: {
|
|
83
|
-
type: 'object',
|
|
84
|
-
properties: {
|
|
85
|
-
name: { type: 'string' },
|
|
86
|
-
age: { type: 'number' },
|
|
87
|
-
},
|
|
88
|
-
required: ['name', 'age'],
|
|
89
|
-
additionalProperties: false,
|
|
90
|
-
} as JSONSchema7Definition,
|
|
91
|
-
expected: { name: 'string', age: 0 },
|
|
92
|
-
},
|
|
93
|
-
],
|
|
94
|
-
[
|
|
95
|
-
{
|
|
96
|
-
schema: {
|
|
97
|
-
type: 'object',
|
|
98
|
-
properties: {
|
|
99
|
-
name: { type: 'string' },
|
|
100
|
-
age: { type: 'number' },
|
|
101
|
-
},
|
|
102
|
-
required: ['name', 'age'],
|
|
103
|
-
additionalProperties: { type: 'string' },
|
|
104
|
-
} as JSONSchema7Definition,
|
|
105
|
-
expected: { name: 'string', age: 0, '<additionalProperty>': 'string' },
|
|
106
|
-
},
|
|
107
|
-
],
|
|
108
|
-
[
|
|
109
|
-
{
|
|
110
|
-
schema: {
|
|
111
|
-
type: 'string',
|
|
112
|
-
enum: ['foo', 'bar'],
|
|
113
|
-
} as JSONSchema7Definition,
|
|
114
|
-
expected: 'foo',
|
|
115
|
-
},
|
|
116
|
-
],
|
|
117
|
-
[
|
|
118
|
-
{
|
|
119
|
-
schema: {
|
|
120
|
-
oneOf: [{ type: 'string' }, { type: 'number' }],
|
|
121
|
-
} as JSONSchema7Definition,
|
|
122
|
-
expected: 'string',
|
|
123
|
-
},
|
|
124
|
-
],
|
|
125
|
-
[
|
|
126
|
-
{
|
|
127
|
-
schema: {
|
|
128
|
-
anyOf: [{ type: 'string' }, { type: 'number' }],
|
|
129
|
-
} as JSONSchema7Definition,
|
|
130
|
-
expected: 'string',
|
|
131
|
-
},
|
|
132
|
-
],
|
|
133
|
-
[
|
|
134
|
-
{
|
|
135
|
-
schema: {
|
|
136
|
-
allOf: [
|
|
137
|
-
{
|
|
138
|
-
type: 'object',
|
|
139
|
-
properties: {
|
|
140
|
-
age: { type: 'number' },
|
|
141
|
-
},
|
|
142
|
-
required: ['age'],
|
|
143
|
-
additionalProperties: false,
|
|
144
|
-
},
|
|
145
|
-
{
|
|
146
|
-
type: 'object',
|
|
147
|
-
properties: {
|
|
148
|
-
name: { type: 'string' },
|
|
149
|
-
},
|
|
150
|
-
required: ['name'],
|
|
151
|
-
additionalProperties: false,
|
|
152
|
-
},
|
|
153
|
-
],
|
|
154
|
-
} as JSONSchema7Definition,
|
|
155
|
-
expected: { age: 0, name: 'string' },
|
|
156
|
-
},
|
|
157
|
-
],
|
|
158
|
-
])('should return the correct default value for the schema', ({ schema, expected }) => {
|
|
159
|
-
expect(jsonSchemaTypeToDefaultValue(schema)).toEqual(expected);
|
|
160
|
-
});
|
|
161
|
-
});
|
|
162
|
-
|
|
163
|
-
describe('convertJsonSchemaToResourceMapperFields', () => {
|
|
164
|
-
it.each([
|
|
165
|
-
[
|
|
166
|
-
{
|
|
167
|
-
schema: { type: 'string' } as JSONSchema7,
|
|
168
|
-
expected: [],
|
|
169
|
-
},
|
|
170
|
-
],
|
|
171
|
-
[
|
|
172
|
-
{
|
|
173
|
-
schema: { type: 'object' } as JSONSchema7,
|
|
174
|
-
expected: [],
|
|
175
|
-
},
|
|
176
|
-
],
|
|
177
|
-
[
|
|
178
|
-
{
|
|
179
|
-
schema: {
|
|
180
|
-
type: 'object',
|
|
181
|
-
properties: { name: { type: 'string' }, age: { type: 'number' } },
|
|
182
|
-
required: ['name'],
|
|
183
|
-
} as JSONSchema7,
|
|
184
|
-
expected: [
|
|
185
|
-
{
|
|
186
|
-
id: 'name',
|
|
187
|
-
displayName: 'name',
|
|
188
|
-
defaultMatch: false,
|
|
189
|
-
required: true,
|
|
190
|
-
display: true,
|
|
191
|
-
type: 'string',
|
|
192
|
-
},
|
|
193
|
-
{
|
|
194
|
-
id: 'age',
|
|
195
|
-
displayName: 'age',
|
|
196
|
-
defaultMatch: false,
|
|
197
|
-
required: false,
|
|
198
|
-
display: true,
|
|
199
|
-
type: 'number',
|
|
200
|
-
},
|
|
201
|
-
],
|
|
202
|
-
},
|
|
203
|
-
],
|
|
204
|
-
[
|
|
205
|
-
{
|
|
206
|
-
schema: { type: 'object', properties: { name: true, age: false } } as JSONSchema7,
|
|
207
|
-
expected: [
|
|
208
|
-
{
|
|
209
|
-
id: 'name',
|
|
210
|
-
displayName: 'name',
|
|
211
|
-
defaultMatch: false,
|
|
212
|
-
required: false,
|
|
213
|
-
display: true,
|
|
214
|
-
type: 'string',
|
|
215
|
-
},
|
|
216
|
-
],
|
|
217
|
-
},
|
|
218
|
-
],
|
|
219
|
-
])(
|
|
220
|
-
'should return the correct resource mapper fields for the schema',
|
|
221
|
-
({ schema, expected }) => {
|
|
222
|
-
expect(convertJsonSchemaToResourceMapperFields(schema)).toEqual(expected);
|
|
223
|
-
},
|
|
224
|
-
);
|
|
225
|
-
|
|
226
|
-
it.each([
|
|
227
|
-
[
|
|
228
|
-
{
|
|
229
|
-
schema: {
|
|
230
|
-
type: 'object',
|
|
231
|
-
properties: { names: { type: 'array', items: { type: 'string' } } },
|
|
232
|
-
required: ['names'],
|
|
233
|
-
} as JSONSchema7,
|
|
234
|
-
expected: [
|
|
235
|
-
{
|
|
236
|
-
id: 'names',
|
|
237
|
-
displayName: 'names',
|
|
238
|
-
defaultMatch: false,
|
|
239
|
-
required: true,
|
|
240
|
-
display: true,
|
|
241
|
-
type: 'array',
|
|
242
|
-
defaultValue: JSON.stringify(['string'], null, 2),
|
|
243
|
-
},
|
|
244
|
-
],
|
|
245
|
-
},
|
|
246
|
-
],
|
|
247
|
-
[
|
|
248
|
-
{
|
|
249
|
-
schema: {
|
|
250
|
-
type: 'object',
|
|
251
|
-
properties: {
|
|
252
|
-
user: {
|
|
253
|
-
type: 'object',
|
|
254
|
-
properties: { name: { type: 'string' }, age: { type: 'number' } },
|
|
255
|
-
},
|
|
256
|
-
},
|
|
257
|
-
required: ['user'],
|
|
258
|
-
} as JSONSchema7,
|
|
259
|
-
expected: [
|
|
260
|
-
{
|
|
261
|
-
id: 'user',
|
|
262
|
-
displayName: 'user',
|
|
263
|
-
defaultMatch: false,
|
|
264
|
-
required: true,
|
|
265
|
-
display: true,
|
|
266
|
-
type: 'object',
|
|
267
|
-
defaultValue: JSON.stringify({ name: 'string', age: 0 }, null, 2),
|
|
268
|
-
},
|
|
269
|
-
],
|
|
270
|
-
},
|
|
271
|
-
],
|
|
272
|
-
])('should add defaultValue for arrays and objects', ({ schema, expected }) => {
|
|
273
|
-
expect(convertJsonSchemaToResourceMapperFields(schema)).toEqual(expected);
|
|
274
|
-
});
|
|
275
|
-
|
|
276
|
-
it('should add options for enums', () => {
|
|
277
|
-
const schema = {
|
|
278
|
-
type: 'object',
|
|
279
|
-
properties: { color: { type: 'string', enum: ['red', 'green', 'blue'] } },
|
|
280
|
-
} as JSONSchema7;
|
|
281
|
-
const expected = [
|
|
282
|
-
{
|
|
283
|
-
id: 'color',
|
|
284
|
-
displayName: 'color',
|
|
285
|
-
defaultMatch: false,
|
|
286
|
-
required: false,
|
|
287
|
-
display: true,
|
|
288
|
-
type: 'options',
|
|
289
|
-
options: [
|
|
290
|
-
// eslint-disable-next-line n8n-nodes-base/node-param-display-name-miscased
|
|
291
|
-
{ name: 'red', value: 'red' },
|
|
292
|
-
// eslint-disable-next-line n8n-nodes-base/node-param-display-name-miscased
|
|
293
|
-
{ name: 'green', value: 'green' },
|
|
294
|
-
// eslint-disable-next-line n8n-nodes-base/node-param-display-name-miscased
|
|
295
|
-
{ name: 'blue', value: 'blue' },
|
|
296
|
-
],
|
|
297
|
-
},
|
|
298
|
-
];
|
|
299
|
-
expect(convertJsonSchemaToResourceMapperFields(schema)).toEqual(expected);
|
|
300
|
-
});
|
|
301
|
-
});
|
|
302
|
-
});
|
|
@@ -1,48 +0,0 @@
|
|
|
1
|
-
import type { ILoadOptionsFunctions, INodeListSearchResult } from 'n8n-workflow';
|
|
2
|
-
|
|
3
|
-
import type { McpAuthenticationOption, McpServerTransport } from '../shared/types';
|
|
4
|
-
import {
|
|
5
|
-
connectMcpClient,
|
|
6
|
-
getAuthHeaders,
|
|
7
|
-
mapToNodeOperationError,
|
|
8
|
-
tryRefreshOAuth2Token,
|
|
9
|
-
} from '../shared/utils';
|
|
10
|
-
|
|
11
|
-
export async function getTools(
|
|
12
|
-
this: ILoadOptionsFunctions,
|
|
13
|
-
filter?: string,
|
|
14
|
-
paginationToken?: string,
|
|
15
|
-
): Promise<INodeListSearchResult> {
|
|
16
|
-
const authentication = this.getNodeParameter('authentication') as McpAuthenticationOption;
|
|
17
|
-
const serverTransport = this.getNodeParameter('serverTransport') as McpServerTransport;
|
|
18
|
-
const endpointUrl = this.getNodeParameter('endpointUrl') as string;
|
|
19
|
-
const node = this.getNode();
|
|
20
|
-
const { headers } = await getAuthHeaders(this, authentication);
|
|
21
|
-
const client = await connectMcpClient({
|
|
22
|
-
serverTransport,
|
|
23
|
-
endpointUrl,
|
|
24
|
-
headers,
|
|
25
|
-
name: node.type,
|
|
26
|
-
version: node.typeVersion,
|
|
27
|
-
onUnauthorized: async (headers) => await tryRefreshOAuth2Token(this, authentication, headers),
|
|
28
|
-
});
|
|
29
|
-
|
|
30
|
-
if (!client.ok) {
|
|
31
|
-
throw mapToNodeOperationError(node, client.error);
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
const result = await client.result.listTools({ cursor: paginationToken });
|
|
35
|
-
const tools = filter
|
|
36
|
-
? result.tools.filter((tool) => tool.name.toLowerCase().includes(filter.toLowerCase()))
|
|
37
|
-
: result.tools;
|
|
38
|
-
|
|
39
|
-
return {
|
|
40
|
-
results: tools.map((tool) => ({
|
|
41
|
-
name: tool.name,
|
|
42
|
-
value: tool.name,
|
|
43
|
-
description: tool.description,
|
|
44
|
-
inputSchema: tool.inputSchema,
|
|
45
|
-
})),
|
|
46
|
-
paginationToken: result.nextCursor,
|
|
47
|
-
};
|
|
48
|
-
}
|
|
@@ -1,48 +0,0 @@
|
|
|
1
|
-
import type { ILoadOptionsFunctions, ResourceMapperFields } from 'n8n-workflow';
|
|
2
|
-
import { NodeOperationError } from 'n8n-workflow';
|
|
3
|
-
|
|
4
|
-
import { convertJsonSchemaToResourceMapperFields } from './utils';
|
|
5
|
-
import type { McpAuthenticationOption, McpServerTransport } from '../shared/types';
|
|
6
|
-
import {
|
|
7
|
-
getAuthHeaders,
|
|
8
|
-
connectMcpClient,
|
|
9
|
-
getAllTools,
|
|
10
|
-
tryRefreshOAuth2Token,
|
|
11
|
-
mapToNodeOperationError,
|
|
12
|
-
} from '../shared/utils';
|
|
13
|
-
|
|
14
|
-
export async function getToolParameters(
|
|
15
|
-
this: ILoadOptionsFunctions,
|
|
16
|
-
): Promise<ResourceMapperFields> {
|
|
17
|
-
const toolId = this.getNodeParameter('tool', 0, {
|
|
18
|
-
extractValue: true,
|
|
19
|
-
}) as string;
|
|
20
|
-
const authentication = this.getNodeParameter('authentication') as McpAuthenticationOption;
|
|
21
|
-
const serverTransport = this.getNodeParameter('serverTransport') as McpServerTransport;
|
|
22
|
-
const endpointUrl = this.getNodeParameter('endpointUrl') as string;
|
|
23
|
-
const node = this.getNode();
|
|
24
|
-
const { headers } = await getAuthHeaders(this, authentication);
|
|
25
|
-
const client = await connectMcpClient({
|
|
26
|
-
serverTransport,
|
|
27
|
-
endpointUrl,
|
|
28
|
-
headers,
|
|
29
|
-
name: node.type,
|
|
30
|
-
version: node.typeVersion,
|
|
31
|
-
onUnauthorized: async (headers) => await tryRefreshOAuth2Token(this, authentication, headers),
|
|
32
|
-
});
|
|
33
|
-
|
|
34
|
-
if (!client.ok) {
|
|
35
|
-
throw mapToNodeOperationError(node, client.error);
|
|
36
|
-
}
|
|
37
|
-
|
|
38
|
-
const result = await getAllTools(client.result);
|
|
39
|
-
const tool = result.find((tool) => tool.name === toolId);
|
|
40
|
-
if (!tool) {
|
|
41
|
-
throw new NodeOperationError(this.getNode(), 'Tool not found');
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
const fields = convertJsonSchemaToResourceMapperFields(tool.inputSchema);
|
|
45
|
-
return {
|
|
46
|
-
fields,
|
|
47
|
-
};
|
|
48
|
-
}
|
package/nodes/McpClient/utils.ts
DELETED
|
@@ -1,281 +0,0 @@
|
|
|
1
|
-
import type { JSONSchema7, JSONSchema7Definition } from 'json-schema';
|
|
2
|
-
import type {
|
|
3
|
-
ResourceMapperField,
|
|
4
|
-
FieldType,
|
|
5
|
-
INodePropertyOptions,
|
|
6
|
-
IDataObject,
|
|
7
|
-
} from 'n8n-workflow';
|
|
8
|
-
|
|
9
|
-
function pickFirstSchema(schema: JSONSchema7Definition): JSONSchema7Definition {
|
|
10
|
-
if (typeof schema === 'object' && (schema?.anyOf || schema?.oneOf)) {
|
|
11
|
-
if (Array.isArray(schema.anyOf) && schema.anyOf[0] !== undefined) {
|
|
12
|
-
return schema.anyOf[0];
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
if (Array.isArray(schema.oneOf) && schema.oneOf[0] !== undefined) {
|
|
16
|
-
return schema.oneOf[0];
|
|
17
|
-
}
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
return schema;
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
function mergeTwoSchemas(
|
|
24
|
-
a?: JSONSchema7Definition,
|
|
25
|
-
b?: JSONSchema7Definition,
|
|
26
|
-
): JSONSchema7Definition | undefined {
|
|
27
|
-
if (a === undefined) {
|
|
28
|
-
return b;
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
if (b === undefined) {
|
|
32
|
-
return a;
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
a = pickFirstSchema(a);
|
|
36
|
-
b = pickFirstSchema(b);
|
|
37
|
-
if (a === false || b === false) {
|
|
38
|
-
return false;
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
if (a === true || b === true) {
|
|
42
|
-
return true;
|
|
43
|
-
}
|
|
44
|
-
|
|
45
|
-
if (a.type === 'object' && b.type === 'object') {
|
|
46
|
-
const properties = { ...(a.properties ?? {}), ...(b.properties ?? {}) };
|
|
47
|
-
const required = [...(a.required ?? []), ...(b.required ?? [])];
|
|
48
|
-
const additionalProperties = mergeTwoSchemas(a.additionalProperties, b.additionalProperties);
|
|
49
|
-
return { ...a, ...b, properties, required, additionalProperties };
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
if (a.type === 'array' && b.type === 'array') {
|
|
53
|
-
if (Array.isArray(a.items) && Array.isArray(b.items)) {
|
|
54
|
-
// Two tuples -> pick the longer one
|
|
55
|
-
return a.items.length > b.items.length ? a : b;
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
if (Array.isArray(a.items) || Array.isArray(b.items)) {
|
|
59
|
-
// One tuple -> pick the tuple
|
|
60
|
-
return Array.isArray(a.items) ? a : b;
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
const items = mergeTwoSchemas(a.items, b.items);
|
|
64
|
-
return { ...a, ...b, items };
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
return undefined;
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
function mergeAllOfSchemas(schemas: JSONSchema7Definition[]): JSONSchema7Definition | undefined {
|
|
71
|
-
if (schemas.length === 0) {
|
|
72
|
-
return undefined;
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
if (schemas.length === 1) {
|
|
76
|
-
return schemas[0];
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
return schemas.reduce(
|
|
80
|
-
(acc, schema) => mergeTwoSchemas(acc, schema),
|
|
81
|
-
undefined as JSONSchema7Definition | undefined,
|
|
82
|
-
);
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
export function jsonSchemaTypeToDefaultValue(
|
|
86
|
-
schema: JSONSchema7Definition,
|
|
87
|
-
): string | number | boolean | object | null {
|
|
88
|
-
if (schema === false) {
|
|
89
|
-
return null;
|
|
90
|
-
}
|
|
91
|
-
|
|
92
|
-
if (schema === true) {
|
|
93
|
-
return 'any';
|
|
94
|
-
}
|
|
95
|
-
|
|
96
|
-
if (schema.allOf) {
|
|
97
|
-
const mergedSchema = mergeAllOfSchemas(schema.allOf);
|
|
98
|
-
if (mergedSchema !== undefined) {
|
|
99
|
-
return jsonSchemaTypeToDefaultValue(mergedSchema);
|
|
100
|
-
}
|
|
101
|
-
}
|
|
102
|
-
|
|
103
|
-
if (schema.anyOf) {
|
|
104
|
-
const anyOfSchemas = schema.anyOf;
|
|
105
|
-
for (const anyOfSchema of anyOfSchemas) {
|
|
106
|
-
const defaultValue = jsonSchemaTypeToDefaultValue(anyOfSchema);
|
|
107
|
-
if (defaultValue !== null) {
|
|
108
|
-
return defaultValue;
|
|
109
|
-
}
|
|
110
|
-
}
|
|
111
|
-
}
|
|
112
|
-
|
|
113
|
-
if (schema.oneOf) {
|
|
114
|
-
const oneOfSchemas = schema.oneOf;
|
|
115
|
-
for (const oneOfSchema of oneOfSchemas) {
|
|
116
|
-
const defaultValue = jsonSchemaTypeToDefaultValue(oneOfSchema);
|
|
117
|
-
if (defaultValue !== null) {
|
|
118
|
-
return defaultValue;
|
|
119
|
-
}
|
|
120
|
-
}
|
|
121
|
-
}
|
|
122
|
-
|
|
123
|
-
if (schema.enum && Array.isArray(schema.enum)) {
|
|
124
|
-
return schema.enum[0];
|
|
125
|
-
}
|
|
126
|
-
|
|
127
|
-
if (Array.isArray(schema.type)) {
|
|
128
|
-
const types = schema.type;
|
|
129
|
-
for (const type of types) {
|
|
130
|
-
const defaultValue = jsonSchemaTypeToDefaultValue({ type });
|
|
131
|
-
if (defaultValue !== null) {
|
|
132
|
-
return defaultValue;
|
|
133
|
-
}
|
|
134
|
-
}
|
|
135
|
-
}
|
|
136
|
-
|
|
137
|
-
if (schema.type === 'number' || schema.type === 'integer') {
|
|
138
|
-
if (schema.minimum !== undefined) {
|
|
139
|
-
return schema.minimum;
|
|
140
|
-
}
|
|
141
|
-
|
|
142
|
-
if (schema.maximum !== undefined) {
|
|
143
|
-
return schema.maximum;
|
|
144
|
-
}
|
|
145
|
-
|
|
146
|
-
return 0;
|
|
147
|
-
}
|
|
148
|
-
|
|
149
|
-
if (schema.type === 'boolean') {
|
|
150
|
-
return false;
|
|
151
|
-
}
|
|
152
|
-
|
|
153
|
-
if (schema.type === 'string') {
|
|
154
|
-
if (schema.format === 'date-time') {
|
|
155
|
-
return '2025-01-01T00:00:00Z';
|
|
156
|
-
}
|
|
157
|
-
|
|
158
|
-
if (schema.format === 'uri' || schema.format === 'url') {
|
|
159
|
-
return 'https://example.com';
|
|
160
|
-
}
|
|
161
|
-
|
|
162
|
-
if (schema.format === 'date') {
|
|
163
|
-
return '2025-01-01';
|
|
164
|
-
}
|
|
165
|
-
|
|
166
|
-
if (schema.format === 'time') {
|
|
167
|
-
return '00:00:00';
|
|
168
|
-
}
|
|
169
|
-
|
|
170
|
-
return 'string';
|
|
171
|
-
}
|
|
172
|
-
|
|
173
|
-
if (schema.type === 'array') {
|
|
174
|
-
if (!schema.items) {
|
|
175
|
-
return [];
|
|
176
|
-
}
|
|
177
|
-
|
|
178
|
-
if (Array.isArray(schema.items)) {
|
|
179
|
-
return schema.items.map((item) => jsonSchemaTypeToDefaultValue(item));
|
|
180
|
-
}
|
|
181
|
-
|
|
182
|
-
return [jsonSchemaTypeToDefaultValue(schema.items)];
|
|
183
|
-
}
|
|
184
|
-
|
|
185
|
-
if (schema.type === 'object') {
|
|
186
|
-
const properties = schema.properties ?? {};
|
|
187
|
-
const exampleObject: IDataObject = {};
|
|
188
|
-
for (const [key, propertySchema] of Object.entries(properties)) {
|
|
189
|
-
const propertyValue = jsonSchemaTypeToDefaultValue(propertySchema);
|
|
190
|
-
if (propertyValue !== null) {
|
|
191
|
-
exampleObject[key] = propertyValue;
|
|
192
|
-
}
|
|
193
|
-
}
|
|
194
|
-
|
|
195
|
-
if (schema.additionalProperties) {
|
|
196
|
-
const additionalProperties = jsonSchemaTypeToDefaultValue(schema.additionalProperties);
|
|
197
|
-
if (additionalProperties !== null) {
|
|
198
|
-
exampleObject['<additionalProperty>'] = additionalProperties;
|
|
199
|
-
}
|
|
200
|
-
}
|
|
201
|
-
|
|
202
|
-
return exampleObject;
|
|
203
|
-
}
|
|
204
|
-
|
|
205
|
-
return null;
|
|
206
|
-
}
|
|
207
|
-
|
|
208
|
-
export function jsonSchemaTypeToFieldType(schema: JSONSchema7): FieldType {
|
|
209
|
-
if (schema.type === 'string' && schema.format === 'date-time') {
|
|
210
|
-
return 'dateTime';
|
|
211
|
-
}
|
|
212
|
-
|
|
213
|
-
if (schema.type === 'number' || schema.type === 'integer') {
|
|
214
|
-
return 'number';
|
|
215
|
-
}
|
|
216
|
-
|
|
217
|
-
if (schema.type === 'boolean' || schema.type === 'array' || schema.type === 'object') {
|
|
218
|
-
return schema.type;
|
|
219
|
-
}
|
|
220
|
-
|
|
221
|
-
return 'string';
|
|
222
|
-
}
|
|
223
|
-
|
|
224
|
-
export function convertJsonSchemaToResourceMapperFields(
|
|
225
|
-
schema: JSONSchema7,
|
|
226
|
-
): ResourceMapperField[] {
|
|
227
|
-
const fields: ResourceMapperField[] = [];
|
|
228
|
-
if (schema.type !== 'object' || !schema.properties) {
|
|
229
|
-
return fields;
|
|
230
|
-
}
|
|
231
|
-
|
|
232
|
-
const required = Array.isArray(schema.required) ? schema.required : [];
|
|
233
|
-
for (const [key, propertySchema] of Object.entries(schema.properties)) {
|
|
234
|
-
if (propertySchema === false) {
|
|
235
|
-
continue;
|
|
236
|
-
}
|
|
237
|
-
|
|
238
|
-
if (propertySchema === true) {
|
|
239
|
-
fields.push({
|
|
240
|
-
id: key,
|
|
241
|
-
displayName: key,
|
|
242
|
-
defaultMatch: false,
|
|
243
|
-
required: required.includes(key),
|
|
244
|
-
display: true,
|
|
245
|
-
type: 'string', // use string as a "catch all" for any values
|
|
246
|
-
});
|
|
247
|
-
continue;
|
|
248
|
-
}
|
|
249
|
-
|
|
250
|
-
const schemaType = jsonSchemaTypeToFieldType(propertySchema);
|
|
251
|
-
let defaultValue: string | undefined;
|
|
252
|
-
if (schemaType === 'object' || schemaType === 'array') {
|
|
253
|
-
const result = jsonSchemaTypeToDefaultValue(propertySchema);
|
|
254
|
-
if (result !== null) {
|
|
255
|
-
defaultValue = JSON.stringify(result, null, 2);
|
|
256
|
-
}
|
|
257
|
-
}
|
|
258
|
-
|
|
259
|
-
const field: ResourceMapperField = {
|
|
260
|
-
id: key,
|
|
261
|
-
displayName: propertySchema.title ?? key,
|
|
262
|
-
defaultMatch: false,
|
|
263
|
-
required: required.includes(key),
|
|
264
|
-
display: true,
|
|
265
|
-
type: schemaType,
|
|
266
|
-
defaultValue,
|
|
267
|
-
};
|
|
268
|
-
|
|
269
|
-
if (propertySchema.enum && Array.isArray(propertySchema.enum)) {
|
|
270
|
-
field.type = 'options';
|
|
271
|
-
field.options = propertySchema.enum.map((value) => ({
|
|
272
|
-
name: value,
|
|
273
|
-
value,
|
|
274
|
-
})) as INodePropertyOptions[];
|
|
275
|
-
}
|
|
276
|
-
|
|
277
|
-
fields.push(field);
|
|
278
|
-
}
|
|
279
|
-
|
|
280
|
-
return fields;
|
|
281
|
-
}
|