Improve webhook (#3459)
* Add trigger record * Merge triggers * Merge creates * Fix libraries * Fix create merged key * Rename file * Remove list Record Ids * Revert "Rename file" This reverts commit 2e72e05793ced4553eec8d9f890d31beae594c85. * Revert "Revert "Rename file"" This reverts commit e2d93fa02716093df6d4d6029af9cc324c06f06b. * Revert "Remove list Record Ids" This reverts commit 6653fb6ccd4307e3958b70923505034d92cf43bb. * Remove namePlural field * Use name singular for webhooks * Send webhook metadata * Extract resource from zapier webhook * Fix package.json * Fix package.json * Update payload * Fix package.json * Update payload * Update payload * Rename file * Use wildcard in webhook events * Fix nameSingular * Code review returns * Code review returns
This commit is contained in:
2
packages/twenty-zapier/src/utils/data.types.ts
Normal file
2
packages/twenty-zapier/src/utils/data.types.ts
Normal file
@ -0,0 +1,2 @@
|
||||
export type InputData = { [x: string]: any };
|
||||
export type ObjectData = { id: string } | { [x: string]: any };
|
||||
@ -1,6 +1,8 @@
|
||||
import { Bundle } from 'zapier-platform-core';
|
||||
|
||||
const getBundle = (inputData?: { [x: string]: any }): Bundle => {
|
||||
import { InputData } from '../utils/data.types';
|
||||
|
||||
const getBundle = (inputData?: InputData): Bundle => {
|
||||
return {
|
||||
authData: { apiKey: String(process.env.API_KEY) },
|
||||
inputData: inputData || {},
|
||||
|
||||
@ -1,5 +1,7 @@
|
||||
const handleQueryParams = (inputData: { [x: string]: any }): string => {
|
||||
const formattedInputData: { [x: string]: any } = {};
|
||||
import { InputData } from '../utils/data.types';
|
||||
|
||||
const handleQueryParams = (inputData: InputData): string => {
|
||||
const formattedInputData: InputData = {};
|
||||
Object.keys(inputData).forEach((key) => {
|
||||
if (key.includes('__')) {
|
||||
const [objectKey, nestedObjectKey] = key.split('__');
|
||||
|
||||
@ -14,9 +14,14 @@ export const requestSchema = async (z: ZObject, bundle: Bundle) => {
|
||||
return z.request(options).then((response) => response.json);
|
||||
};
|
||||
|
||||
const requestDb = async (z: ZObject, bundle: Bundle, query: string) => {
|
||||
const requestDb = async (
|
||||
z: ZObject,
|
||||
bundle: Bundle,
|
||||
query: string,
|
||||
endpoint = 'graphql',
|
||||
) => {
|
||||
const options = {
|
||||
url: `${process.env.SERVER_BASE_URL}/graphql`,
|
||||
url: `${process.env.SERVER_BASE_URL}/${endpoint}`,
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
|
||||
@ -1,5 +1,6 @@
|
||||
import { Bundle, ZObject } from 'zapier-platform-core';
|
||||
|
||||
import { ObjectData } from '../../utils/data.types';
|
||||
import handleQueryParams from '../../utils/handleQueryParams';
|
||||
import requestDb, { requestDbViaRestApi } from '../../utils/requestDb';
|
||||
|
||||
@ -16,7 +17,7 @@ export const subscribe = async (
|
||||
) => {
|
||||
const data = {
|
||||
targetUrl: bundle.targetUrl,
|
||||
operation: `${operation}.${bundle.inputData.namePlural}`,
|
||||
operation: `${operation}.${bundle.inputData.nameSingular}`,
|
||||
};
|
||||
const result = await requestDb(
|
||||
z,
|
||||
@ -39,18 +40,52 @@ export const performUnsubscribe = async (z: ZObject, bundle: Bundle) => {
|
||||
};
|
||||
|
||||
export const perform = (z: ZObject, bundle: Bundle) => {
|
||||
return [bundle.cleanedRequest];
|
||||
return [bundle.cleanedRequest.record];
|
||||
};
|
||||
|
||||
const getNamePluralFromNameSingular = async (
|
||||
z: ZObject,
|
||||
bundle: Bundle,
|
||||
nameSingular: string,
|
||||
): Promise<string> => {
|
||||
const result = await requestDb(
|
||||
z,
|
||||
bundle,
|
||||
`query GetObjects {
|
||||
objects(paging: {first: 1000}) {
|
||||
edges {
|
||||
node {
|
||||
nameSingular
|
||||
namePlural
|
||||
}
|
||||
}
|
||||
}
|
||||
}`,
|
||||
'metadata',
|
||||
);
|
||||
for (const object of result.data.objects.edges) {
|
||||
if (object.node.nameSingular === nameSingular) {
|
||||
return object.node.namePlural;
|
||||
}
|
||||
}
|
||||
throw new Error(`Unknown Object Name Singular ${nameSingular}`);
|
||||
};
|
||||
|
||||
export const listSample = async (
|
||||
z: ZObject,
|
||||
bundle: Bundle,
|
||||
onlyIds = false,
|
||||
) => {
|
||||
): Promise<ObjectData[]> => {
|
||||
const nameSingular = bundle.inputData.nameSingular;
|
||||
const namePlural = await getNamePluralFromNameSingular(
|
||||
z,
|
||||
bundle,
|
||||
nameSingular,
|
||||
);
|
||||
const result: { [key: string]: string }[] = await requestDbViaRestApi(
|
||||
z,
|
||||
bundle,
|
||||
bundle.inputData.namePlural,
|
||||
namePlural,
|
||||
);
|
||||
|
||||
if (onlyIds) {
|
||||
|
||||
Reference in New Issue
Block a user