mirror of
https://github.com/Abdulazizzn/n8n-enterprise-unlocked.git
synced 2025-12-22 04:10:01 +00:00
refactor(Peekalink Node): Stricter typing for Peekalink api call + Tests (no-changelog) (#8125)
This PR is an example for how we can 1. improve typing and remove boilerplate code in may of our nodes 2. use nock to write effective unit tests for nodes that make external calls ## Review / Merge checklist - [x] PR title and summary are descriptive - [x] Add tests
This commit is contained in:
committed by
GitHub
parent
21788d9153
commit
1d2666b37c
@@ -1,14 +1,17 @@
|
||||
import type {
|
||||
IExecuteFunctions,
|
||||
IDataObject,
|
||||
INodeExecutionData,
|
||||
INodeType,
|
||||
INodeTypeDescription,
|
||||
import {
|
||||
Node,
|
||||
NodeApiError,
|
||||
type IExecuteFunctions,
|
||||
type INodeExecutionData,
|
||||
type INodeTypeDescription,
|
||||
type JsonObject,
|
||||
} from 'n8n-workflow';
|
||||
|
||||
import { peekalinkApiRequest } from './GenericFunctions';
|
||||
export const apiUrl = 'https://api.peekalink.io';
|
||||
|
||||
export class Peekalink implements INodeType {
|
||||
type Operation = 'preview' | 'isAvailable';
|
||||
|
||||
export class Peekalink extends Node {
|
||||
description: INodeTypeDescription = {
|
||||
displayName: 'Peekalink',
|
||||
name: 'peekalink',
|
||||
@@ -61,44 +64,31 @@ export class Peekalink implements INodeType {
|
||||
],
|
||||
};
|
||||
|
||||
async execute(this: IExecuteFunctions): Promise<INodeExecutionData[][]> {
|
||||
const items = this.getInputData();
|
||||
const returnData: IDataObject[] = [];
|
||||
const length = items.length;
|
||||
let responseData;
|
||||
const operation = this.getNodeParameter('operation', 0);
|
||||
async execute(context: IExecuteFunctions): Promise<INodeExecutionData[][]> {
|
||||
const items = context.getInputData();
|
||||
const operation = context.getNodeParameter('operation', 0) as Operation;
|
||||
const credentials = await context.getCredentials('peekalinkApi');
|
||||
|
||||
for (let i = 0; i < length; i++) {
|
||||
try {
|
||||
if (operation === 'isAvailable') {
|
||||
const url = this.getNodeParameter('url', i) as string;
|
||||
const body: IDataObject = {
|
||||
link: url,
|
||||
};
|
||||
|
||||
responseData = await peekalinkApiRequest.call(this, 'POST', '/is-available/', body);
|
||||
const returnData = await Promise.all(
|
||||
items.map(async (_, i) => {
|
||||
try {
|
||||
const link = context.getNodeParameter('url', i) as string;
|
||||
// eslint-disable-next-line @typescript-eslint/no-unsafe-return
|
||||
return await context.helpers.request({
|
||||
method: 'POST',
|
||||
uri: operation === 'preview' ? apiUrl : `${apiUrl}/is-available/`,
|
||||
body: { link },
|
||||
headers: { 'X-API-Key': credentials.apiKey },
|
||||
json: true,
|
||||
});
|
||||
} catch (error) {
|
||||
if (context.continueOnFail()) {
|
||||
return { error: error.message };
|
||||
}
|
||||
throw new NodeApiError(context.getNode(), error as JsonObject);
|
||||
}
|
||||
if (operation === 'preview') {
|
||||
const url = this.getNodeParameter('url', i) as string;
|
||||
const body: IDataObject = {
|
||||
link: url,
|
||||
};
|
||||
|
||||
responseData = await peekalinkApiRequest.call(this, 'POST', '/', body);
|
||||
}
|
||||
if (Array.isArray(responseData)) {
|
||||
returnData.push.apply(returnData, responseData as IDataObject[]);
|
||||
} else {
|
||||
returnData.push(responseData as IDataObject);
|
||||
}
|
||||
} catch (error) {
|
||||
if (this.continueOnFail()) {
|
||||
returnData.push({ error: error.message });
|
||||
continue;
|
||||
}
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
return [this.helpers.returnJsonArray(returnData)];
|
||||
}),
|
||||
);
|
||||
return [context.helpers.returnJsonArray(returnData)];
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user