1- import type { CommunityNodeAttributes , CommunityNodeData } from '@n8n/api-types' ;
1+ import type { CommunityNodeType } from '@n8n/api-types' ;
22import { GlobalConfig } from '@n8n/config' ;
33import { Service } from '@n8n/di' ;
44import { 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
87import { CommunityPackagesService } from './community-packages.service' ;
9- import { paginatedRequest } from '../utils/community-nodes-request -utils' ;
8+ import { getCommunityNodeTypes } from '../utils/community-node-types -utils' ;
109
1110const 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 ( )
1731export 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}
0 commit comments