Reorganize repo structure and standards references
This commit is contained in:
1 parent
3cb93a81be
commit
be2054274d
5885 files changed
+2588
-195
No files matched your search
@@ -0,0 +1,57 @@
|
||||
import { getDbClient } from "@toolctrl/db/client.server";
|
||||
import { publishOutboxEvent, type OutboxPublisherTransaction } from "@toolctrl/db/outbox";
|
||||
import { emitPluginHook, PluginHookValidationError, type PluginHookDefinition } from "@toolctrl/plugin-runtime";
|
||||
import { apiLogger } from "./logger.js";
|
||||
|
||||
const pluginHookLogger = apiLogger.child?.({ component: "plugin-hook-publisher" }) ?? apiLogger;
|
||||
|
||||
export async function emitPluginHookFireAndForget<TPayload>(
|
||||
def: PluginHookDefinition<TPayload>,
|
||||
payload: TPayload,
|
||||
options: {
|
||||
tenantId?: string | null;
|
||||
dispatchKey?: string | null;
|
||||
} = {},
|
||||
): Promise<void> {
|
||||
try {
|
||||
const db = getDbClient() as unknown as {
|
||||
$transaction: <T>(runner: (tx: OutboxPublisherTransaction) => Promise<T>) => Promise<T>;
|
||||
};
|
||||
await db.$transaction(async (tx) => {
|
||||
await emitPluginHook(
|
||||
tx,
|
||||
def,
|
||||
payload,
|
||||
async (innerTx, args) => {
|
||||
await publishOutboxEvent(innerTx as OutboxPublisherTransaction, {
|
||||
tenantId: args.tenantId,
|
||||
type: args.type,
|
||||
payload: args.payload as unknown as Parameters<typeof publishOutboxEvent>[1]["payload"],
|
||||
dispatchKey: args.dispatchKey,
|
||||
});
|
||||
},
|
||||
options,
|
||||
);
|
||||
});
|
||||
} catch (error) {
|
||||
if (error instanceof PluginHookValidationError) {
|
||||
pluginHookLogger.warn(
|
||||
{
|
||||
hookName: def.name,
|
||||
tenantId: options.tenantId ?? null,
|
||||
issues: error.issues,
|
||||
},
|
||||
"plugin.hook.payload_invalid",
|
||||
);
|
||||
return;
|
||||
}
|
||||
pluginHookLogger.error(
|
||||
{
|
||||
hookName: def.name,
|
||||
tenantId: options.tenantId ?? null,
|
||||
error: error instanceof Error ? error.message : String(error),
|
||||
},
|
||||
"plugin.hook.emit_failed",
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user