import type { OptionalField, Resolvable } from './common-types.js'; import type { Result, Tx } from '../database-layer/db.js'; import type { ODataRequest, ParsedODataRequest } from './uri-parser.js'; import type { AnyObject } from 'pinejs-client-core'; import { type User, type ApiKey, api, type Response } from './sbvr-utils.js'; export interface HookReq { user?: User; apiKey?: ApiKey; method: string; url: string; query: AnyObject; params: AnyObject; body: AnyObject; custom?: AnyObject; tx?: Tx; hooks?: InstantiatedHooks; is?: (type: string | string[]) => string | false | null; } export interface HookArgs { req: HookReq; request: ODataRequest; api: (typeof api)[Vocab]; tx?: Tx | undefined; } export type HookResponse = PromiseLike | null | void; export interface Hooks { PREPARSE?: (options: Omit, 'request' | 'api'>) => HookResponse; POSTPARSE?: (options: HookArgs) => HookResponse; PRERUN?: (options: HookArgs & { tx: Tx; }) => HookResponse; POSTRUN?: (options: HookArgs & { tx: Tx; result: Result | number | undefined; }) => HookResponse; PRERESPOND?: (options: HookArgs & { tx: Tx; result?: Result | number | AnyObject; response: Response; }) => HookResponse; 'POSTRUN-ERROR'?: (options: HookArgs & { tx: Tx; error: unknown; }) => HookResponse; } export type HookBlueprints = { [key in keyof Hooks]: Array>>; }; export type RollbackAction = () => Resolvable; export type HookFn = (...args: any[]) => any; export interface HookBlueprint { hookFn: T; sideEffects: boolean; readOnlyTx: boolean; } export type InstantiatedHooks = { [key in keyof Hooks]: Array>>; }; declare class Hook { private hookFn; readOnlyTx: HookBlueprint['readOnlyTx']; constructor(hook: HookBlueprint); run(...args: any[]): Promise; } export declare const rollbackRequestHooks: (hooksList: Array<[modelName: string, hooks: T]> | undefined) => void; export type HookMethod = keyof typeof apiHooks; export declare const getHooks: { (request: Pick, "resourceName" | "method" | "vocabulary">, includeAllVocab: boolean): InstantiatedHooks; clear(): void; }; interface VocabHooks { [resourceName: string]: HookBlueprints; } interface MethodHooks { [vocab: string]: VocabHooks; } declare const apiHooks: { all: MethodHooks; GET: MethodHooks; PUT: MethodHooks; POST: MethodHooks; PATCH: MethodHooks; MERGE: MethodHooks; DELETE: MethodHooks; OPTIONS: MethodHooks; }; export declare const addHook: (method: keyof typeof apiHooks, vocabulary: Vocab, resourceName: string, hooks: { [key in keyof Hooks]: HookBlueprint>; } | ({ [key in keyof Hooks]: NonNullable; } & { sideEffects: HookBlueprint["sideEffects"]; readOnlyTx: HookBlueprint["readOnlyTx"]; })) => void; export declare const addSideEffectHook: (method: HookMethod, apiRoot: Vocab, resourceName: string, hooks: Hooks>) => void; export declare const addPureHook: (method: HookMethod, apiRoot: Vocab, resourceName: string, hooks: Hooks>) => void; type RunHookArgs = Omit>[0], 'api'>; export declare const runHooks: (hookName: T, hooksList: Array<[modelName: string, hooks: InstantiatedHooks]> | undefined, args: RunHookArgs) => Promise; export {};