Перейти к содержимому
GitHubXDiscord

createCheck

Стабильный · с 0.1.0

Строит хелпер check, который обработчики получают через ctx.check. Рантайм собирает его за тебя на каждом запуске — createCheck экспортируется только для редких случаев, когда нужно использовать тот же предикатный DSL вне контекста обработчика (кастомные middleware, тестирование по условиям, панели devtools).

is, all и any принимают предикаты над NonNullable<C[K]>. Когда значение условия равно undefined или null, предикат не вызывается, а результат по этому ключу — false.

import { createCheck } from '@triggery/core';
function createCheck<C extends Record<string, unknown>>(
  conditions: C,
): CheckCtx<C>;
ПараметрТипОписание
conditionsRecord<string, unknown>Карта-снепшот значений условий. Передавай ту же форму, которую рантайм даёт обработчикам.

Объект CheckCtx:

МетодСигнатураРезультат
is(key, predicate)(key, value => boolean) => booleantrue, когда условие существует (не null) и предикат вернул truthy.
all(map)({ key: predicate, … }) => booleanКаждое перечисленное условие должно существовать и проходить свой предикат.
any(map)({ key: predicate, … }) => booleanХотя бы одно перечисленное условие должно существовать и проходить свой предикат.

Обычно ты используешь ctx.check вместо ручной сборки:

import { createTriggerfunction createTrigger<S extends TriggerSchema>(config: CreateTriggerConfig<S>, runtime?: Runtime): Trigger<S>
Create a trigger and register it in a runtime (the default runtime if none is passed).
@example```ts const messageTrigger = createTrigger<{ events: { 'new-message': { author: string; text: string } }; conditions: { user: { id: string }; settings: { sound: boolean } }; actions: { showToast: { title: string }; playSound: void }; }>({ id: 'message-received', events: ['new-message'], required: ['user', 'settings'], handler({ event, conditions, actions, check }) { if (!conditions.user || !conditions.settings) return; // V1: manual narrowing if (check.is('settings', (s) => s.sound)) actions.playSound?.(); actions.showToast?.({ title: event.payload.author }); }, }); ```
} from '@triggery/core';
createTrigger
createTrigger<{
    events: {
        ping: void;
    };
    conditions: {
        user: {
            active: boolean;
        } | null;
        tenant: string | null;
    };
}>(config: CreateTriggerConfig<{
    events: {
        ping: void;
    };
    conditions: {
        user: {
            active: boolean;
        } | null;
        tenant: string | null;
    };
}>, runtime?: Runtime): Trigger<{
    events: {
        ping: void;
    };
    conditions: {
        user: {
            active: boolean;
        } | null;
        tenant: string | null;
    };
}>
Create a trigger and register it in a runtime (the default runtime if none is passed).
@example```ts const messageTrigger = createTrigger<{ events: { 'new-message': { author: string; text: string } }; conditions: { user: { id: string }; settings: { sound: boolean } }; actions: { showToast: { title: string }; playSound: void }; }>({ id: 'message-received', events: ['new-message'], required: ['user', 'settings'], handler({ event, conditions, actions, check }) { if (!conditions.user || !conditions.settings) return; // V1: manual narrowing if (check.is('settings', (s) => s.sound)) actions.playSound?.(); actions.showToast?.({ title: event.payload.author }); }, }); ```
<{
events
events: {
    ping: void;
}
: { pingping: void: void };
conditions
conditions: {
    user: {
        active: boolean;
    } | null;
    tenant: string | null;
}
: { user
user: {
    active: boolean;
} | null
: { activeactive: boolean: boolean } | null; tenanttenant: string | null: string | null };
}>({ idid: string: 'demo', eventsevents: readonly "ping"[]: ['ping'], handler
handler: TriggerHandler<{
    events: {
        ping: void;
    };
    conditions: {
        user: {
            active: boolean;
        } | null;
        tenant: string | null;
    };
}, never>
({ check
check: CheckCtx<{
    user: {
        active: boolean;
    } | null;
    tenant: string | null;
}>
}) {
if (check
check: CheckCtx<{
    user: {
        active: boolean;
    } | null;
    tenant: string | null;
}>
.all
all<{
    user: (u: {
        active: boolean;
    }) => boolean;
    tenant: (t: string) => boolean;
}>(map: {
    user: (u: {
        active: boolean;
    }) => boolean;
    tenant: (t: string) => boolean;
}): boolean
({ user
user: (u: {
    active: boolean;
}) => boolean
: u
u: {
    active: boolean;
}
=> u
u: {
    active: boolean;
}
.activeactive: boolean, tenanttenant: (t: string) => boolean: tt: string => tt: string.lengthString.length: number
Returns the length of a String object.
> 0 })) {
consolevar console: Console.logConsole.log(...data: any[]): void
The **`console.log()`** static method outputs a message to the console. [MDN Reference](https://developer.mozilla.org/docs/Web/API/console/log_static)
('go');
} }, });
import { createCheckfunction createCheck<C extends Record<string, unknown>>(conditions: C): CheckCtx<C>
Builds the `check` helper bound to a specific conditions snapshot. `is`, `all` and `any` accept predicates over `NonNullable<C[K]>`: if a condition is absent (`undefined` or `null`), the predicate is not invoked and the result is `false` for that key.
} from '@triggery/core';
const check
const check: CheckCtx<{
    user: {
        active: boolean;
        name: string;
    } | null;
    tenant: string | null;
}>
= createCheck
createCheck<{
    user: {
        active: boolean;
        name: string;
    } | null;
    tenant: string | null;
}>(conditions: {
    user: {
        active: boolean;
        name: string;
    } | null;
    tenant: string | null;
}): CheckCtx<{
    user: {
        active: boolean;
        name: string;
    } | null;
    tenant: string | null;
}>
Builds the `check` helper bound to a specific conditions snapshot. `is`, `all` and `any` accept predicates over `NonNullable<C[K]>`: if a condition is absent (`undefined` or `null`), the predicate is not invoked and the result is `false` for that key.
({
user
user: {
    active: boolean;
    name: string;
} | null
: { activeactive: boolean: true, namename: string: 'Alice' } as { activeactive: boolean: boolean; namename: string: string } | null,
tenanttenant: string | null: 'acme-corp' as string | null, }); check
const check: CheckCtx<{
    user: {
        active: boolean;
        name: string;
    } | null;
    tenant: string | null;
}>
.is
is<"user">(key: "user", predicate: (value: {
    active: boolean;
    name: string;
}) => boolean): boolean
('user', u
u: {
    active: boolean;
    name: string;
}
=> u
u: {
    active: boolean;
    name: string;
}
.activeactive: boolean); // true
check
const check: CheckCtx<{
    user: {
        active: boolean;
        name: string;
    } | null;
    tenant: string | null;
}>
.all
all<{
    user: (u: {
        active: boolean;
        name: string;
    }) => boolean;
    tenant: (t: string) => boolean;
}>(map: {
    user: (u: {
        active: boolean;
        name: string;
    }) => boolean;
    tenant: (t: string) => boolean;
}): boolean
({ user
user: (u: {
    active: boolean;
    name: string;
}) => boolean
: u
u: {
    active: boolean;
    name: string;
}
=> u
u: {
    active: boolean;
    name: string;
}
.activeactive: boolean, tenanttenant: (t: string) => boolean: tt: string => tt: string.lengthString.length: number
Returns the length of a String object.
> 0 }); // true
check
const check: CheckCtx<{
    user: {
        active: boolean;
        name: string;
    } | null;
    tenant: string | null;
}>
.any
any<{
    user: (u: {
        active: boolean;
        name: string;
    }) => boolean;
}>(map: {
    user: (u: {
        active: boolean;
        name: string;
    }) => boolean;
}): boolean
({ user
user: (u: {
    active: boolean;
    name: string;
}) => boolean
: u
u: {
    active: boolean;
    name: string;
}
=> !u
u: {
    active: boolean;
    name: string;
}
.activeactive: boolean }); // false
import { createCheckfunction createCheck<C extends Record<string, unknown>>(conditions: C): CheckCtx<C>
Builds the `check` helper bound to a specific conditions snapshot. `is`, `all` and `any` accept predicates over `NonNullable<C[K]>`: if a condition is absent (`undefined` or `null`), the predicate is not invoked and the result is `false` for that key.
} from '@triggery/core';
const check
const check: CheckCtx<{
    user: {
        active: boolean;
    } | null;
}>
= createCheck
createCheck<{
    user: {
        active: boolean;
    } | null;
}>(conditions: {
    user: {
        active: boolean;
    } | null;
}): CheckCtx<{
    user: {
        active: boolean;
    } | null;
}>
Builds the `check` helper bound to a specific conditions snapshot. `is`, `all` and `any` accept predicates over `NonNullable<C[K]>`: if a condition is absent (`undefined` or `null`), the predicate is not invoked and the result is `false` for that key.
({ user
user: {
    active: boolean;
} | null
: null as { activeactive: boolean: boolean } | null });
// Predicate is never invoked when the value is null — no NPE risk. check
const check: CheckCtx<{
    user: {
        active: boolean;
    } | null;
}>
.is
is<"user">(key: "user", predicate: (value: {
    active: boolean;
}) => boolean): boolean
('user', u
u: {
    active: boolean;
}
=> u
u: {
    active: boolean;
}
.activeactive: boolean); // false