n8n/packages/editor-ui/src/mixins/externalHooks.ts
Milorad FIlipović 5059c57f4a
refactor(editor): Refactor utils files and mixins (#4654)
*  Added `utils` module. Moved `canvasHelpers` and old `utils.ts` file to it
*  Moved rest of utils and helpers
*  Fixing sytax errors
* 🔨 Refactoring new utils files
* 🔨 Organizing imports, adding comments and a bit more refactoring
* ✔️ Fixing tests
* 🔨 Moving mixins to `src`
2022-11-23 13:41:53 +01:00

42 lines
1 KiB
TypeScript

import { IExternalHooks, IRootState } from '@/Interface';
import { store } from '@/store';
import { useWebhooksStore } from '@/stores/webhooks';
import { IDataObject } from 'n8n-workflow';
import { Store } from 'pinia';
import Vue from 'vue';
export async function runExternalHook(
eventName: string,
store: Store,
metadata?: IDataObject,
) {
// @ts-ignore
if (!window.n8nExternalHooks) {
return;
}
const [resource, operator] = eventName.split('.');
// @ts-ignore
if (window.n8nExternalHooks[resource] && window.n8nExternalHooks[resource][operator]) {
// @ts-ignore
const hookMethods = window.n8nExternalHooks[resource][operator];
for (const hookmethod of hookMethods) {
await hookmethod(store, metadata);
}
}
}
export const externalHooks = Vue.extend({
methods: {
$externalHooks(): IExternalHooks {
return {
run: async (eventName: string, metadata?: IDataObject): Promise<void> => {
await runExternalHook.call(this, eventName, useWebhooksStore(), metadata);
},
};
},
},
});