diff --git a/package.json b/package.json index 34fb049..640ee9d 100644 --- a/package.json +++ b/package.json @@ -32,8 +32,8 @@ "devDependencies": { "@directus/api": "latest", "@directus/extensions-sdk": "latest", - "prettier": "^3.3.1", - "vite": "^5.2.12", + "prettier": "^3.3.3", + "vite": "^5.3.4", "vite-plugin-dts": "^3.9.1" } } diff --git a/src/hooks/index.ts b/src/hooks/index.ts new file mode 100644 index 0000000..e04da25 --- /dev/null +++ b/src/hooks/index.ts @@ -0,0 +1,15 @@ +import type { + ActionHandler, + FilterHandler, + EventContext +} from '@directus/types' + +import type { TFilterHandlerMeta, TActionHandlerMeta } from './types' + +export const defineFilterHandler = ( + fn: (payload: T, meta: TFilterHandlerMeta, context: EventContext) => void +) => fn as FilterHandler + +export const defineActionHandler = ( + fn: (meta: TActionHandlerMeta, context: EventContext) => void +) => fn as ActionHandler diff --git a/src/hooks/types.ts b/src/hooks/types.ts new file mode 100644 index 0000000..449bd59 --- /dev/null +++ b/src/hooks/types.ts @@ -0,0 +1,13 @@ +import type { PrimaryKey } from '@directus/types' + +export type TEventHandlerMeta = { + event: string + collection: string +} + +export type TFilterHandlerMeta = TEventHandlerMeta & {} + +export type TActionHandlerMeta = TEventHandlerMeta & { + keys: PrimaryKey[] + payload: T +} diff --git a/src/index.ts b/src/index.ts index f0618b2..f20627d 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,3 +1,3 @@ -export * from './types' +export * from './hooks' export * from './services' export * from './operations' diff --git a/src/operations/index.ts b/src/operations/index.ts index 316f80a..a91aab4 100644 --- a/src/operations/index.ts +++ b/src/operations/index.ts @@ -1,9 +1,12 @@ import type { Accountability } from '@directus/types' import type { TTrigger } from './trigger' -export type TOperationContextData = { - $trigger: TTrigger, - $last: TTrigger +export type TOperationContextData< + TTriggerBodyEntity extends Record = {}, + TLast extends Record = {}, +> = { + $trigger: TTrigger + $last: TLast $accountability: Accountability $env: Record } diff --git a/src/operations/trigger.ts b/src/operations/trigger.ts index 2bc3333..da4c30d 100644 --- a/src/operations/trigger.ts +++ b/src/operations/trigger.ts @@ -1,10 +1,13 @@ -export type TTrigger = { - path: string - query: any, - body: { +export type TTriggerBody = {}> = + TEntity & { collection: string - }, - method: 'GET' | 'POST', + } + +export type TTrigger = {}> = { + path: string + query: any + body: TTriggerBody + method: 'GET' | 'POST' headers: { host: string connection: string diff --git a/src/types/hook.ts b/src/types/hook.ts deleted file mode 100644 index d1ee0b9..0000000 --- a/src/types/hook.ts +++ /dev/null @@ -1,7 +0,0 @@ -import type { PrimaryKey } from '@directus/types' - -export type TFilterHandlerMeta = Record & { - event: string | 'items.create' | 'items.update', - keys: PrimaryKey[] - collection: string -} diff --git a/src/types/index.ts b/src/types/index.ts deleted file mode 100644 index 516962b..0000000 --- a/src/types/index.ts +++ /dev/null @@ -1 +0,0 @@ -export * from './hook'