Visitar URL original
feat(editor): Distinguish official verified nodes from community buil… · n8n-io/n8n@7f0c6d6 · GitHub
Skip to content

Commit 7f0c6d6

Browse files
authored
feat(editor): Distinguish official verified nodes from community built nodes (#15630)
1 parent dc0802b commit 7f0c6d6

21 files changed

Lines changed: 437 additions & 283 deletions

File tree

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import type { INodeTypeDescription } from 'n8n-workflow';
22

3-
export interface CommunityNodeAttributes {
3+
export type CommunityNodeType = {
44
authorGithubUrl: string;
55
authorName: string;
66
checksum: string;
@@ -13,11 +13,8 @@ export interface CommunityNodeAttributes {
1313
createdAt: string;
1414
updatedAt: string;
1515
npmVersion: string;
16-
}
17-
18-
export interface CommunityNodeData {
19-
id: number;
20-
attributes: CommunityNodeAttributes & {
21-
nodeDescription: INodeTypeDescription;
22-
};
23-
}
16+
isOfficialNode: boolean;
17+
companyName?: string;
18+
nodeDescription: INodeTypeDescription;
19+
isInstalled: boolean;
20+
};

‎packages/cli/src/controllers/__tests__/community-packages.controller.test.ts‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1-
import type { CommunityNodeAttributes } from '@n8n/api-types';
1+
import type { CommunityNodeType } from '@n8n/api-types';
22
import type { InstalledPackages } from '@n8n/db';
33
import { mock } from 'jest-mock-extended';
4-
import type { INodeTypeDescription } from 'n8n-workflow';
54

65
import { CommunityPackagesController } from '@/controllers/community-packages.controller';
76
import type { NodeRequest } from '@/requests';
@@ -46,7 +45,7 @@ describe('CommunityPackagesController', () => {
4645
body: { name: 'n8n-nodes-test', verify: true, version: '1.0.0' },
4746
});
4847
communityNodeTypesService.findVetted.mockReturnValue(
49-
mock<CommunityNodeAttributes & { nodeDescription: INodeTypeDescription }>({
48+
mock<CommunityNodeType>({
5049
checksum: 'checksum',
5150
}),
5251
);
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { CommunityNodeAttributes } from '@n8n/api-types';
1+
import type { CommunityNodeType } from '@n8n/api-types';
22
import { Get, RestController } from '@n8n/decorators';
33
import { Request } from 'express';
44

@@ -9,12 +9,12 @@ export class CommunityNodeTypesController {
99
constructor(private readonly communityNodeTypesService: CommunityNodeTypesService) {}
1010

1111
@Get('/:name')
12-
async getCommunityNodeAttributes(req: Request): Promise<CommunityNodeAttributes | null> {
13-
return this.communityNodeTypesService.getCommunityNodeAttributes(req.params.name);
12+
async getCommunityNodeType(req: Request): Promise<CommunityNodeType | null> {
13+
return await this.communityNodeTypesService.getCommunityNodeType(req.params.name);
1414
}
1515

1616
@Get('/')
1717
async getCommunityNodeTypes() {
18-
return await this.communityNodeTypesService.getDescriptions();
18+
return await this.communityNodeTypesService.getCommunityNodeTypes();
1919
}
2020
}
Lines changed: 50 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,35 @@
1-
import type { CommunityNodeAttributes, CommunityNodeData } from '@n8n/api-types';
1+
import type { CommunityNodeType } from '@n8n/api-types';
22
import { GlobalConfig } from '@n8n/config';
33
import { Service } from '@n8n/di';
44
import { Logger } from 'n8n-core';
5-
import type { INodeTypeDescription } from 'n8n-workflow';
6-
import { ensureError } from 'n8n-workflow';
5+
import { ensureError, type INodeTypeDescription } from 'n8n-workflow';
76

87
import { CommunityPackagesService } from './community-packages.service';
9-
import { paginatedRequest } from '../utils/community-nodes-request-utils';
8+
import { getCommunityNodeTypes } from '../utils/community-node-types-utils';
109

1110
const UPDATE_INTERVAL = 8 * 60 * 60 * 1000;
1211

13-
const N8N_VETTED_NODE_TYPES_STAGING_URL = 'https://api-staging.n8n.io/api/community-nodes';
14-
const N8N_VETTED_NODE_TYPES_PRODUCTION_URL = 'https://api.n8n.io/api/community-nodes';
12+
export type StrapiCommunityNodeType = {
13+
authorGithubUrl: string;
14+
authorName: string;
15+
checksum: string;
16+
description: string;
17+
displayName: string;
18+
name: string;
19+
numberOfStars: number;
20+
numberOfDownloads: number;
21+
packageName: string;
22+
createdAt: string;
23+
updatedAt: string;
24+
npmVersion: string;
25+
isOfficialNode: boolean;
26+
companyName?: string;
27+
nodeDescription: INodeTypeDescription;
28+
};
1529

1630
@Service()
1731
export class CommunityNodeTypesService {
18-
private communityNodes: {
19-
[key: string]: CommunityNodeAttributes & {
20-
nodeDescription: INodeTypeDescription;
21-
};
22-
} = {};
32+
private communityNodeTypes: Map<string, StrapiCommunityNodeType> = new Map();
2333

2434
private lastUpdateTimestamp = 0;
2535

@@ -31,76 +41,71 @@ export class CommunityNodeTypesService {
3141

3242
private async fetchNodeTypes() {
3343
try {
34-
let data: CommunityNodeData[] = [];
44+
let data: StrapiCommunityNodeType[] = [];
3545
if (
3646
this.globalConfig.nodes.communityPackages.enabled &&
3747
this.globalConfig.nodes.communityPackages.verifiedEnabled
3848
) {
3949
const environment = this.globalConfig.license.tenantId === 1 ? 'production' : 'staging';
40-
const url =
41-
environment === 'production'
42-
? N8N_VETTED_NODE_TYPES_PRODUCTION_URL
43-
: N8N_VETTED_NODE_TYPES_STAGING_URL;
44-
data = await paginatedRequest(url);
50+
data = await getCommunityNodeTypes(environment);
4551
}
4652

47-
this.updateData(data);
53+
this.updateCommunityNodeTypes(data);
4854
} catch (error) {
4955
this.logger.error('Failed to fetch community node types', { error: ensureError(error) });
5056
}
5157
}
5258

53-
private updateData(data: CommunityNodeData[]) {
54-
if (!data?.length) return;
59+
private updateCommunityNodeTypes(nodeTypes: StrapiCommunityNodeType[]) {
60+
if (!nodeTypes?.length) return;
5561

56-
this.resetData();
62+
this.resetCommunityNodeTypes();
5763

58-
for (const entry of data) {
59-
this.communityNodes[entry.attributes.name] = entry.attributes;
60-
}
64+
this.communityNodeTypes = new Map(nodeTypes.map((nodeType) => [nodeType.name, nodeType]));
6165

6266
this.lastUpdateTimestamp = Date.now();
6367
}
6468

65-
private resetData() {
66-
this.communityNodes = {};
69+
private resetCommunityNodeTypes() {
70+
this.communityNodeTypes = new Map();
6771
}
6872

6973
private updateRequired() {
7074
if (!this.lastUpdateTimestamp) return true;
7175
return Date.now() - this.lastUpdateTimestamp > UPDATE_INTERVAL;
7276
}
7377

74-
async getDescriptions(): Promise<INodeTypeDescription[]> {
75-
const nodesDescriptions: INodeTypeDescription[] = [];
78+
private async createIsInstalled() {
79+
const installedPackages = (await this.communityPackagesService.getAllInstalledPackages()) ?? [];
80+
const installedPackageNames = new Set(installedPackages.map((p) => p.packageName));
81+
82+
return (nodeTypeName: string) => installedPackageNames.has(nodeTypeName.split('.')[0]);
83+
}
7684

77-
if (this.updateRequired() || !Object.keys(this.communityNodes).length) {
85+
async getCommunityNodeTypes(): Promise<CommunityNodeType[]> {
86+
if (this.updateRequired() || !this.communityNodeTypes.size) {
7887
await this.fetchNodeTypes();
7988
}
8089

81-
const installedPackages = (
82-
(await this.communityPackagesService.getAllInstalledPackages()) ?? []
83-
).map((p) => p.packageName);
84-
85-
for (const node of Object.values(this.communityNodes)) {
86-
if (installedPackages.includes(node.name.split('.')[0])) continue;
87-
nodesDescriptions.push(node.nodeDescription);
88-
}
90+
const isInstalled = await this.createIsInstalled();
8991

90-
return nodesDescriptions;
92+
return Array.from(this.communityNodeTypes.values()).map((nodeType) => ({
93+
...nodeType,
94+
isInstalled: isInstalled(nodeType.name),
95+
}));
9196
}
9297

93-
getCommunityNodeAttributes(type: string): CommunityNodeAttributes | null {
94-
const node = this.communityNodes[type];
95-
if (!node) return null;
96-
const { nodeDescription, ...attributes } = node;
97-
return attributes;
98+
async getCommunityNodeType(type: string): Promise<CommunityNodeType | null> {
99+
const nodeType = this.communityNodeTypes.get(type);
100+
const isInstalled = await this.createIsInstalled();
101+
if (!nodeType) return null;
102+
return { ...nodeType, isInstalled: isInstalled(nodeType.name) };
98103
}
99104

100105
findVetted(packageName: string) {
101-
const vettedTypes = Object.keys(this.communityNodes);
106+
const vettedTypes = Array.from(this.communityNodeTypes.keys());
102107
const nodeName = vettedTypes.find((t) => t.includes(packageName));
103108
if (!nodeName) return;
104-
return this.communityNodes[nodeName];
109+
return this.communityNodeTypes.get(nodeName);
105110
}
106111
}

‎packages/cli/src/utils/__tests__/community-nodes-request.test.ts‎

Lines changed: 0 additions & 125 deletions
This file was deleted.

0 commit comments

Comments
 (0)