File size: 1,042 Bytes
fcfabd3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
import { checkedProps } from './checked-props'
import type { Fields } from './fields'
import { fieldsOf } from './fields-of'
import { isRecord } from './is-record'
import { refusal } from './refusal'
import { TOKEN } from './token'

/**
 * The row's fields from one entry, or a refusal naming the first thing
 * wrong: the entry's shape, the event, then each property in turn.
 *
 * Nothing is sent from an entry refused here: the row reaches the ingest as
 * written, so this check is the gate between the argument and the wire.
 *
 * @param entry the event's name and properties as the caller passed them
 * @returns the entry's checked event name and properties, as `Fields`
 */
export function checkedFields(entry: unknown): Fields {
  if (!isRecord(entry)) {
    throw refusal('takes one entry, { event, props? }')
  }

  const { event, props = {} } = entry

  if (typeof event !== 'string' || !TOKEN.test(event)) {
    throw refusal('takes an event name, a snake_case token')
  }

  return fieldsOf(event, checkedProps(props, 'log'))
}