mirror of
				https://github.com/n8n-io/n8n-nodes-starter.git
				synced 2025-10-30 23:02:25 -05:00 
			
		
		
		
	Test authentication
This commit is contained in:
		
					parent
					
						
							
								58b5d0d9c0
							
						
					
				
			
			
				commit
				
					
						936fc68b5a
					
				
			
		
					 2 changed files with 57 additions and 6 deletions
				
			
		|  | @ -3,10 +3,13 @@ import { | |||
| } from 'n8n-core'; | ||||
| 
 | ||||
| import { | ||||
| 	ICredentialsDecrypted, | ||||
| 	ICredentialTestFunctions, | ||||
| 	IDataObject, | ||||
| 	INodeExecutionData, | ||||
| 	INodeType, | ||||
| 	INodeTypeDescription, | ||||
| 	NodeCredentialTestResult, | ||||
| } from 'n8n-workflow'; | ||||
| 
 | ||||
| import { | ||||
|  | @ -18,6 +21,10 @@ import { | |||
| 	friendGridApiRequest, | ||||
| } from './GenericFunctions'; | ||||
| 
 | ||||
| import { | ||||
| 	OptionsWithUri | ||||
| } from 'request'; | ||||
| 
 | ||||
| export class FriendGrid implements INodeType { | ||||
| 	description: INodeTypeDescription = { | ||||
| 		displayName: 'FriendGrid', | ||||
|  | @ -37,6 +44,7 @@ export class FriendGrid implements INodeType { | |||
| 			{ | ||||
| 				name: 'friendGridApi', | ||||
| 				required: true, | ||||
| 				testedBy: 'testFriendGridApiAuth', | ||||
| 			}, | ||||
| 		], | ||||
| 		properties: [ | ||||
|  | @ -59,17 +67,59 @@ export class FriendGrid implements INodeType { | |||
| 		], | ||||
| 	}; | ||||
| 
 | ||||
| 	methods = { | ||||
| 		credentialTest: { | ||||
| 			async testFriendGridApiAuth(this: ICredentialTestFunctions, credential: ICredentialsDecrypted): Promise<NodeCredentialTestResult> { | ||||
| 
 | ||||
| 				// https://docs.sendgrid.com/api-reference/users-api/retrieve-your-username
 | ||||
| 				const options: OptionsWithUri = { | ||||
| 					method: 'GET', | ||||
| 					headers: { | ||||
| 						'Accept': ' application/json', | ||||
| 						'Authorization': `Bearer ${credential!.data!.apiKey}`, | ||||
| 					}, | ||||
| 					uri: 'https://api.sendgrid.com/v3/marketing/user/username', | ||||
| 					json: true, | ||||
| 				}; | ||||
| 
 | ||||
| 				try { | ||||
| 					const response = await this.helpers.request(options); | ||||
| 
 | ||||
| 					if (response.error) { | ||||
| 						return { | ||||
| 							status: 'Error', | ||||
| 							message: `${response.error}`, | ||||
| 						}; | ||||
| 					} | ||||
| 				} catch (err) { | ||||
| 					return { | ||||
| 						status: 'Error', | ||||
| 						message: `${err.message}`, | ||||
| 					}; | ||||
| 				} | ||||
| 
 | ||||
| 				return { | ||||
| 					status: 'OK', | ||||
| 					message: 'Connection successful!', | ||||
| 				}; | ||||
| 			}, | ||||
| 		}, | ||||
| 	}; | ||||
| 
 | ||||
| 	async execute(this: IExecuteFunctions): Promise<INodeExecutionData[][]> { | ||||
| 		const items = this.getInputData(); | ||||
| 		let responseData; | ||||
| 		const returnData: IDataObject[] = []; | ||||
| 		const resource = this.getNodeParameter('resource', 0) as string; | ||||
| 		const operation = this.getNodeParameter('operation', 0) as string; | ||||
| 		let body: IDataObject = {}; | ||||
| 		const qs: IDataObject = {}; | ||||
| 
 | ||||
| 		for (let i = 0; i < items.length; i++) { | ||||
| 			try { | ||||
| 				if (resource === 'contact') { | ||||
| 					if (operation === 'create') { | ||||
| 						// https://docs.sendgrid.com/api-reference/contacts/add-or-update-a-contact
 | ||||
| 
 | ||||
| 						const email = this.getNodeParameter('email', i) as string; | ||||
| 
 | ||||
|  | @ -80,18 +130,19 @@ export class FriendGrid implements INodeType { | |||
| 
 | ||||
| 						Object.assign(data, additionalFields); | ||||
| 
 | ||||
| 						const body: IDataObject = { | ||||
| 						body = { | ||||
| 							contacts: [ | ||||
| 								data, | ||||
| 							], | ||||
| 						}; | ||||
| 
 | ||||
| 						responseData = await friendGridApiRequest.call(this, 'POST', '/contact', body); | ||||
| 						responseData = await friendGridApiRequest.call(this, 'POST', '/marketing/contact', body); | ||||
| 					} | ||||
| 				} | ||||
| 
 | ||||
| 				if (Array.isArray(responseData)) { | ||||
| 					returnData.push.apply(returnData, responseData as IDataObject[]); | ||||
| 				} else { | ||||
| 				} else if (responseData !== undefined) { | ||||
| 					returnData.push(responseData as IDataObject); | ||||
| 				} | ||||
| 			} catch (error) { | ||||
|  |  | |||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue