mirror of
https://github.com/Abdulazizzn/n8n-enterprise-unlocked.git
synced 2025-12-18 02:21:13 +00:00
Co-authored-by: Eugene <eugene@n8n.io> Co-authored-by: Daria <daria.staferova@n8n.io> Co-authored-by: Alex Grozav <alex@grozav.com> Co-authored-by: Tomi Turtiainen <10324676+tomi@users.noreply.github.com> Co-authored-by: Guillaume Jacquart <jacquart.guillaume@gmail.com> Co-authored-by: Charlie Kolb <charlie@n8n.io> Co-authored-by: Elias Meire <elias@meire.dev> Co-authored-by: Michael Kret <88898367+michael-radency@users.noreply.github.com> Co-authored-by: shortstacked <declan@n8n.io> Co-authored-by: oleg <me@olegivaniv.com> Co-authored-by: Csaba Tuncsik <csaba@n8n.io> Co-authored-by: Jaakko Husso <jaakko@n8n.io> Co-authored-by: Raúl Gómez Morales <raul00gm@gmail.com> Co-authored-by: Suguru Inoue <suguru@n8n.io> Co-authored-by: Milorad FIlipović <milorad@n8n.io> Co-authored-by: Danny Martini <danny@n8n.io> Co-authored-by: Iván Ovejero <ivov.src@gmail.com> Co-authored-by: RomanDavydchuk <roman.davydchuk@n8n.io> Co-authored-by: Mutasem Aldmour <4711238+mutdmour@users.noreply.github.com> Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> Co-authored-by: cubic-dev-ai[bot] <191113872+cubic-dev-ai[bot]@users.noreply.github.com> Co-authored-by: Ricardo Espinoza <ricardo@n8n.io> Co-authored-by: Dana <152518854+dana-gill@users.noreply.github.com> Co-authored-by: Michael Kret <michael.k@radency.com> Co-authored-by: Mutasem Aldmour <mutasem@n8n.io>
58 lines
1.8 KiB
TypeScript
58 lines
1.8 KiB
TypeScript
import type { IExecuteFunctions, IDataObject } from 'n8n-workflow';
|
|
|
|
import {
|
|
validateSessionAndWindowId,
|
|
createSessionAndWindow,
|
|
shouldCreateNewSession,
|
|
validateAirtopApiResponse,
|
|
} from '../../GenericFunctions';
|
|
import { apiRequest } from '../../transport';
|
|
import type { IAirtopResponse } from '../../transport/types';
|
|
|
|
/**
|
|
* Execute the node operation. Creates and terminates a new session if needed.
|
|
* @param this - The execution context
|
|
* @param index - The index of the node
|
|
* @param request - The request to execute
|
|
* @returns The response from the request
|
|
*/
|
|
export async function executeRequestWithSessionManagement(
|
|
this: IExecuteFunctions,
|
|
index: number,
|
|
request: {
|
|
method: 'POST' | 'DELETE';
|
|
path: string;
|
|
body: IDataObject;
|
|
},
|
|
): Promise<IAirtopResponse> {
|
|
let airtopSessionId = '';
|
|
try {
|
|
const { sessionId, windowId } = shouldCreateNewSession.call(this, index)
|
|
? await createSessionAndWindow.call(this, index)
|
|
: validateSessionAndWindowId.call(this, index);
|
|
airtopSessionId = sessionId;
|
|
|
|
const shouldTerminateSession = this.getNodeParameter('autoTerminateSession', index, false);
|
|
|
|
const endpoint = request.path.replace('{sessionId}', sessionId).replace('{windowId}', windowId);
|
|
const response = await apiRequest.call(this, request.method, endpoint, request.body);
|
|
|
|
validateAirtopApiResponse(this.getNode(), response);
|
|
|
|
if (shouldTerminateSession) {
|
|
await apiRequest.call(this, 'DELETE', `/sessions/${sessionId}`);
|
|
this.logger.info(`[${this.getNode().name}] Session terminated.`);
|
|
return response;
|
|
}
|
|
|
|
return { sessionId, windowId, ...response };
|
|
} catch (error) {
|
|
// terminate session on error
|
|
if (airtopSessionId) {
|
|
await apiRequest.call(this, 'DELETE', `/sessions/${airtopSessionId}`);
|
|
this.logger.info(`[${this.getNode().name}] Session terminated.`);
|
|
}
|
|
throw error;
|
|
}
|
|
}
|