|
|
@ -1,11 +1,26 @@ |
|
|
|
import { T } from 'nc-help'; |
|
|
|
import { T } from 'nc-help'; |
|
|
|
import { validatePayload } from '../meta/api/helpers'; |
|
|
|
import { validatePayload } from '../meta/api/helpers'; |
|
|
|
|
|
|
|
import { NcError } from '../meta/helpers/catchError'; |
|
|
|
import { Hook, Model } from '../models'; |
|
|
|
import { Hook, Model } from '../models'; |
|
|
|
|
|
|
|
|
|
|
|
import { invokeWebhook } from '../meta/helpers/webhookHelpers'; |
|
|
|
import { invokeWebhook } from '../meta/helpers/webhookHelpers'; |
|
|
|
import populateSamplePayload from '../meta/helpers/populateSamplePayload'; |
|
|
|
import populateSamplePayload from '../meta/helpers/populateSamplePayload'; |
|
|
|
import type { HookReqType, HookTestReqType } from 'nocodb-sdk'; |
|
|
|
import type { HookReqType, HookTestReqType } from 'nocodb-sdk'; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function validateHookPayload(notificationJsonOrObject: string | object) { |
|
|
|
|
|
|
|
let notification: { type?: string } = {}; |
|
|
|
|
|
|
|
try { |
|
|
|
|
|
|
|
notification = |
|
|
|
|
|
|
|
typeof notificationJsonOrObject === 'string' |
|
|
|
|
|
|
|
? JSON.parse(notificationJsonOrObject) |
|
|
|
|
|
|
|
: notificationJsonOrObject; |
|
|
|
|
|
|
|
} catch {} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (notification.type !== 'URL' && process.env.NC_CLOUD === 'true') { |
|
|
|
|
|
|
|
NcError.badRequest('Only URL notification is supported'); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
export async function hookList(param: { tableId: string }) { |
|
|
|
export async function hookList(param: { tableId: string }) { |
|
|
|
// todo: pagination
|
|
|
|
// todo: pagination
|
|
|
|
return await Hook.list({ fk_model_id: param.tableId }); |
|
|
|
return await Hook.list({ fk_model_id: param.tableId }); |
|
|
@ -17,6 +32,8 @@ export async function hookCreate(param: { |
|
|
|
}) { |
|
|
|
}) { |
|
|
|
validatePayload('swagger.json#/components/schemas/HookReq', param.hook); |
|
|
|
validatePayload('swagger.json#/components/schemas/HookReq', param.hook); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
validateHookPayload(param.hook.notification); |
|
|
|
|
|
|
|
|
|
|
|
T.emit('evt', { evt_type: 'webhooks:created' }); |
|
|
|
T.emit('evt', { evt_type: 'webhooks:created' }); |
|
|
|
// todo: type correction
|
|
|
|
// todo: type correction
|
|
|
|
const hook = await Hook.insert({ |
|
|
|
const hook = await Hook.insert({ |
|
|
@ -37,6 +54,8 @@ export async function hookUpdate(param: { hookId: string; hook: HookReqType }) { |
|
|
|
|
|
|
|
|
|
|
|
T.emit('evt', { evt_type: 'webhooks:updated' }); |
|
|
|
T.emit('evt', { evt_type: 'webhooks:updated' }); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
validateHookPayload(param.hook.notification); |
|
|
|
|
|
|
|
|
|
|
|
// todo: correction in swagger
|
|
|
|
// todo: correction in swagger
|
|
|
|
return await Hook.update(param.hookId, param.hook as any); |
|
|
|
return await Hook.update(param.hookId, param.hook as any); |
|
|
|
} |
|
|
|
} |
|
|
@ -50,6 +69,8 @@ export async function hookTest(param: { |
|
|
|
param.hookTest |
|
|
|
param.hookTest |
|
|
|
); |
|
|
|
); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
validateHookPayload(param.hookTest.hook?.notification); |
|
|
|
|
|
|
|
|
|
|
|
const model = await Model.getByIdOrName({ id: param.tableId }); |
|
|
|
const model = await Model.getByIdOrName({ id: param.tableId }); |
|
|
|
|
|
|
|
|
|
|
|
const { |
|
|
|
const { |
|
|
@ -69,6 +90,7 @@ export async function hookTest(param: { |
|
|
|
|
|
|
|
|
|
|
|
return true; |
|
|
|
return true; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
export async function tableSampleData(param: { |
|
|
|
export async function tableSampleData(param: { |
|
|
|
tableId: string; |
|
|
|
tableId: string; |
|
|
|
operation: 'insert' | 'update'; |
|
|
|
operation: 'insert' | 'update'; |
|
|
|