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>;Параметры
Заголовок раздела «Параметры»| Параметр | Тип | Описание |
|---|---|---|
conditions | Record<string, unknown> | Карта-снепшот значений условий. Передавай ту же форму, которую рантайм даёт обработчикам. |
Возвращает
Заголовок раздела «Возвращает»Объект CheckCtx:
| Метод | Сигнатура | Результат |
|---|---|---|
is(key, predicate) | (key, value => boolean) => boolean | true, когда условие существует (не null) и предикат вернул truthy. |
all(map) | ({ key: predicate, … }) => boolean | Каждое перечисленное условие должно существовать и проходить свой предикат. |
any(map) | ({ key: predicate, … }) => boolean | Хотя бы одно перечисленное условие должно существовать и проходить свой предикат. |
Примеры
Заголовок раздела «Примеры»Внутри обработчика (обычный случай)
Заголовок раздела «Внутри обработчика (обычный случай)»Обычно ты используешь ctx.check вместо ручной сборки:
import { createTrigger function 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). } 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). <{
events events: {
ping: void;
}
: { ping ping: void : void };
conditions conditions: {
user: {
active: boolean;
} | null;
tenant: string | null;
}
: { user user: {
active: boolean;
} | null
: { active active: boolean : boolean } | null; tenant tenant: string | null : string | null };
}>({
id id: string : 'demo',
events events: 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;
}
.active active: boolean , tenant tenant: (t: string) => boolean : t t: string => t t: string .length String.length: numberReturns the length of a String object. > 0 })) {
console var console: Console .log Console.log(...data: any[]): voidThe **`console.log()`** static method outputs a message to the console.
[MDN Reference](https://developer.mozilla.org/docs/Web/API/console/log_static) ('go');
}
},
});Standalone (продвинутый — вне обработчика)
Заголовок раздела «Standalone (продвинутый — вне обработчика)»import { createCheck function 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
: { active active: boolean : true, name name: string : 'Alice' } as { active active: boolean : boolean; name name: string : string } | null,
tenant tenant: 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;
}
.active active: 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;
}
.active active: boolean , tenant tenant: (t: string) => boolean : t t: string => t t: string .length String.length: numberReturns 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;
}
.active active: boolean }); // falseNull-safety
Заголовок раздела «Null-safety»import { createCheck function 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 { active active: 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;
}
.active active: boolean ); // falseЗамечания
Заголовок раздела «Замечания»См. также
Заголовок раздела «См. также» createTrigger Обработчик получает экземпляр `check` автоматически.
TriggerCtx Полный тип контекста обработчика, включая `check`.
Руководство по условиям Когда читать условия напрямую, а когда через `check`.
Ограничение required Зачем вообще существует `check` — заметки по сужению типов в V1.