exportclassNasaPicsimplementsINodeType{description:INodeTypeDescription={// Basic node details will go hereproperties:[// Resources and operations will go here]};}
资源对象定义了节点使用的 API 资源。在本教程中,您将创建一个节点来访问 NASA 的两个 API 端点:planetary/apod 和 mars-photos。这意味着您需要在 NasaPics.node.ts 中定义两个资源选项。使用资源对象更新 properties 数组:
1 2 3 4 5 6 7 8 9101112131415161718192021
properties:[{displayName:'Resource',name:'resource',type:'options',noDataExpression:true,options:[{name:'Astronomy Picture of the Day',value:'astronomyPictureOfTheDay',},{name:'Mars Rover Photos',value:'marsRoverPhotos',},],default:'astronomyPictureOfTheDay',},// Operations will go here]
{displayName:'Operation',name:'operation',type:'options',noDataExpression:true,displayOptions:{show:{resource:['astronomyPictureOfTheDay',],},},options:[{name:'Get',value:'get',action:'Get the APOD',description:'Get the Astronomy Picture of the day',routing:{request:{method:'GET',url:'/planetary/apod',},},},],default:'get',},{displayName:'Operation',name:'operation',type:'options',noDataExpression:true,displayOptions:{show:{resource:['marsRoverPhotos',],},},options:[{name:'Get',value:'get',action:'Get Mars Rover photos',description:'Get photos from the Mars Rover',routing:{request:{method:'GET',},},},],default:'get',},{displayName:'Rover name',description:'Choose which Mars Rover to get a photo from',required:true,name:'roverName',type:'options',options:[{name:'Curiosity',value:'curiosity'},{name:'Opportunity',value:'opportunity'},{name:'Perseverance',value:'perseverance'},{name:'Spirit',value:'spirit'},],routing:{request:{url:'=/mars-photos/api/v1/rovers/{{$value}}/photos',},},default:'curiosity',displayOptions:{show:{resource:['marsRoverPhotos',],},},},{displayName:'Date',description:'Earth date',required:true,name:'marsRoverDate',type:'dateTime',default:'',displayOptions:{show:{resource:['marsRoverPhotos',],},},routing:{request:{// You've already set up the URL. qs appends the value of the field as a query stringqs:{earth_date:'={{ new Date($value).toISOString().substr(0,10) }}',},},},},// Optional/additional fields will go here
这段代码创建了两个操作:一个用于获取今天的 APOD 图片,另一个用于发送 get 请求以获取某一个火星探测器的照片。名为 roverName 的对象要求用户选择他们想要获取照片的探测器。火星探测器操作中的 routing 对象引用这个选项来创建 API 调用的 URL。
{displayName:'Additional Fields',name:'additionalFields',type:'collection',default:{},placeholder:'Add Field',displayOptions:{show:{resource:['astronomyPictureOfTheDay',],operation:['get',],},},options:[{displayName:'Date',name:'apodDate',type:'dateTime',default:'',routing:{request:{// You've already set up the URL. qs appends the value of the field as a query stringqs:{date:'={{ new Date($value).toISOString().substr(0,10) }}',},},},},],}
import{IAuthenticateGeneric,ICredentialType,INodeProperties,}from'n8n-workflow';exportclassNasaPicsApiimplementsICredentialType{name='NasaPicsApi';displayName='NASA Pics API';// Uses the link to this tutorial as an example// Replace with your own docs links when building your own nodesdocumentationUrl='https://docs.n8n.io/integrations/creating-nodes/build/declarative-style-node/';properties:INodeProperties[]=[{displayName:'API Key',name:'apiKey',type:'string',default:'',},];authenticate={type:'generic',properties:{qs:{'api_key':'={{$credentials.apiKey}}'}},}asIAuthenticateGeneric;}
{// All node names must start with "n8n-nodes-""name":"n8n-nodes-nasapics","version":"0.1.0","description":"n8n node to call NASA's APOD and Mars Rover Photo services.","keywords":[// This keyword is required for community nodes"n8n-community-node-package"],"license":"MIT","homepage":"https://n8n.io","author":{"name":"Test","email":"[email protected]"},"repository":{"type":"git",// Change the git remote to your own repository// Add the new URL here"url":"git+<your-repo-url>"},"main":"index.js","scripts":{// don't change},"files":["dist"],// Link the credentials and node"n8n":{"n8nNodesApiVersion":1,"credentials":["dist/credentials/NasaPicsApi.credentials.js"],"nodes":["dist/nodes/NasaPics/NasaPics.node.js"]},"devDependencies":{// don't change},"peerDependencies":{// don't change}}