spacehub

surshar / claude-code

Claude Code is an agentic coding tool that lives in your terminal, understands your codebase, and helps you code faster by executing routine tasks, explaining complex code, and handling git workflows - all through natural language commands.

Built by Anthropic · github.com/anthropics

claude-code / mods / telemetry / hooks / entries / checked-mark.ts
55 lines · 1.5 KBRaw
1import { checkedProps } from './checked-props'
2import type { Fields } from './fields'
3import { isMarkKind } from './is-mark-kind'
4import { isRecord } from './is-record'
5import { markFieldsOf } from './mark-fields-of'
6import { refusal } from './refusal'
7import { TOKEN } from './token'
8
9/**
10 * The row's fields from one mark, or a refusal naming the first thing
11 * wrong: the entry's shape, the feature, the kind, the reason, then props.
12 *
13 * Each property is checked in turn, as a log's are.
14 *
15 * @param entry the feature, kind, reason and properties as the caller passed
16 * them
17 * @returns the mark's row fields once the entry passes every check
18 */
19export function checkedMark(entry: unknown): Fields {
20 if (!isRecord(entry)) {
21 throw refusal('takes one entry, { feature, kind, reason?, props? }', 'mark')
22 }
23
24 const { feature, kind, reason, props = {} } = entry
25
26 if (typeof feature !== 'string' || !TOKEN.test(feature)) {
27 throw refusal('takes a feature name, a snake_case token', 'mark')
28 }
29
30 if (!isMarkKind(kind)) {
31 throw refusal("kind: 'ok', 'sad' or 'bad'", 'mark')
32 }
33
34 if (kind === 'ok') {
35 if (reason !== undefined) {
36 throw refusal('reason: an ok mark carries none', 'mark')
37 }
38
39 return markFieldsOf({ kind, feature, props: checkedProps(props, 'mark') })
40 }
41
42 if (typeof reason !== 'string' || !TOKEN.test(reason)) {
43 throw refusal(
44 `reason: a ${kind} mark names why, a snake_case token`,
45 'mark',
46 )
47 }
48
49 return markFieldsOf({
50 kind,
51 feature,
52 reason,
53 props: checkedProps(props, 'mark'),
54 })
55}