define action and fileters

This commit is contained in:
AntoXa PRO 2024-07-18 10:16:09 +03:00
parent 7980661e6d
commit f4c7ff4c5c
8 changed files with 46 additions and 20 deletions

View File

@ -32,8 +32,8 @@
"devDependencies": { "devDependencies": {
"@directus/api": "latest", "@directus/api": "latest",
"@directus/extensions-sdk": "latest", "@directus/extensions-sdk": "latest",
"prettier": "^3.3.1", "prettier": "^3.3.3",
"vite": "^5.2.12", "vite": "^5.3.4",
"vite-plugin-dts": "^3.9.1" "vite-plugin-dts": "^3.9.1"
} }
} }

15
src/hooks/index.ts Normal file
View File

@ -0,0 +1,15 @@
import type {
ActionHandler,
FilterHandler,
EventContext
} from '@directus/types'
import type { TFilterHandlerMeta, TActionHandlerMeta } from './types'
export const defineFilterHandler = <T = unknown>(
fn: (payload: T, meta: TFilterHandlerMeta, context: EventContext) => void
) => fn as FilterHandler<T>
export const defineActionHandler = <T = unknown>(
fn: (meta: TActionHandlerMeta<T>, context: EventContext) => void
) => fn as ActionHandler

13
src/hooks/types.ts Normal file
View File

@ -0,0 +1,13 @@
import type { PrimaryKey } from '@directus/types'
export type TEventHandlerMeta = {
event: string
collection: string
}
export type TFilterHandlerMeta = TEventHandlerMeta & {}
export type TActionHandlerMeta<T = unknown> = TEventHandlerMeta & {
keys: PrimaryKey[]
payload: T
}

View File

@ -1,3 +1,3 @@
export * from './types' export * from './hooks'
export * from './services' export * from './services'
export * from './operations' export * from './operations'

View File

@ -1,9 +1,12 @@
import type { Accountability } from '@directus/types' import type { Accountability } from '@directus/types'
import type { TTrigger } from './trigger' import type { TTrigger } from './trigger'
export type TOperationContextData = { export type TOperationContextData<
$trigger: TTrigger, TTriggerBodyEntity extends Record<string, unknown> = {},
$last: TTrigger TLast extends Record<string, unknown> = {},
> = {
$trigger: TTrigger<TTriggerBodyEntity>
$last: TLast
$accountability: Accountability $accountability: Accountability
$env: Record<string, string> $env: Record<string, string>
} }

View File

@ -1,10 +1,13 @@
export type TTrigger = { export type TTriggerBody<TEntity extends Record<string, unknown> = {}> =
path: string TEntity & {
query: any,
body: {
collection: string collection: string
}, }
method: 'GET' | 'POST',
export type TTrigger<TBody extends Record<string, unknown> = {}> = {
path: string
query: any
body: TTriggerBody<TBody>
method: 'GET' | 'POST'
headers: { headers: {
host: string host: string
connection: string connection: string

View File

@ -1,7 +0,0 @@
import type { PrimaryKey } from '@directus/types'
export type TFilterHandlerMeta = Record<string, any> & {
event: string | 'items.create' | 'items.update',
keys: PrimaryKey[]
collection: string
}

View File

@ -1 +0,0 @@
export * from './hook'