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-fields.ts
51 lines · 1.5 KBRaw
1import { checkedValue } from './checked-value'
2import type { Fields } from './fields'
3import { fieldsOf } from './fields-of'
4import { isRecord } from './is-record'
5import { PROP_LIMIT } from './prop-limit'
6import { refusal } from './refusal'
7import { TOKEN } from './token'
8
9/**
10 * The row's fields from one entry, or a refusal naming the first thing
11 * wrong: the entry's shape, the event, then each property in turn.
12 *
13 * Nothing is sent from an entry refused here: the row reaches the ingest as
14 * written, so this check is the gate between the argument and the wire.
15 *
16 * @param entry the event's name and properties as the caller passed them
17 * @returns the entry's checked event name and properties, as `Fields`
18 */
19export function checkedFields(entry: unknown): Fields {
20 if (!isRecord(entry)) {
21 throw refusal('takes one entry, { event, props? }')
22 }
23
24 const { event, props = {} } = entry
25
26 if (typeof event !== 'string' || !TOKEN.test(event)) {
27 throw refusal('takes an event name, a snake_case token')
28 }
29
30 if (!isRecord(props)) {
31 throw refusal('props: an object of properties by key')
32 }
33
34 const entries = Object.entries(props)
35
36 if (entries.length > PROP_LIMIT) {
37 throw refusal(`props: at most ${PROP_LIMIT} properties`)
38 }
39
40 const checked: Record<string, string | number | boolean> = {}
41
42 for (const [key, value] of entries) {
43 if (!TOKEN.test(key)) {
44 throw refusal('props: every key is a snake_case token')
45 }
46
47 checked[key] = checkedValue(key, value)
48 }
49
50 return fieldsOf(event, checked)
51}