Line data Source code
1 : declare const serverFactField: unique symbol
2 :
3 : /**
4 : * A server fact as received, tagged with the API field that answered it. It
5 : * reads as the plain value, but a gate typed as one rejects anything mixed in
6 : * by `||`, `&&`, `!` or a ternary. See ADR-API-SERVER-FACTS-0042.
7 : */
8 : export type ServerFact<Field extends string, T = boolean> = T & {
9 : readonly [serverFactField]: Field
10 : }
11 :
12 : /**
13 : * A `true` literal types as always truthy, so `fact || local` would collapse to
14 : * `fact` and pass the gate.
15 : */
16 : export type FactValue<T> = T extends boolean ? boolean : T
17 :
18 : /**
19 : * The only constructor. Call it where a response is decoded or in a local
20 : * provider for a non-cloud build, never at a gate.
21 : */
22 179 : export function receiveServerFact<Field extends string, T>(
23 : _field: Field,
24 : value: T
25 : ): ServerFact<Field, FactValue<T>> {
26 179 : return value as ServerFact<Field, FactValue<T>>
27 : }
28 :
29 : /** Local transient UI state may withhold a fact, never grant it. */
30 0 : export function narrowServerFact<Field extends string>(
31 : fact: ServerFact<Field>,
32 : localAllows: boolean
33 : ): ServerFact<Field> {
34 0 : return localAllows ? fact : (false as ServerFact<Field>)
35 : }
|