@uidu/client 3.4.0 → 3.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +32 -0
- package/dist/declarations/src/calls.d.ts +1 -0
- package/dist/declarations/src/generated/index.d.ts +4 -1
- package/dist/declarations/src/index.d.ts +4 -3
- package/dist/declarations/src/utils/fieldValues.d.ts +37 -0
- package/dist/uidu-client.cjs.dev.js +132 -2
- package/dist/uidu-client.cjs.prod.js +132 -2
- package/dist/uidu-client.esm.js +131 -3
- package/package.json +1 -1
- package/src/client.test.ts +165 -0
- package/src/client.ts +5 -1
- package/src/generated/index.ts +5 -3
- package/src/index.ts +8 -15
- package/src/queries/calls/ListCalls.graphql +1 -0
- package/src/utils/fieldValues.test.ts +268 -0
- package/src/utils/fieldValues.ts +164 -0
|
@@ -0,0 +1,164 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The API stores every answer in a `content` JSON column and reads it back with
|
|
3
|
+
* `content['value']` — so `content` must always be an object shaped
|
|
4
|
+
* `{ value: <the answer> }`, never the bare answer.
|
|
5
|
+
*
|
|
6
|
+
* Passing a flat scalar (`content: 'hello'`) is the single most common mistake
|
|
7
|
+
* when writing field values by hand or via a code-generating agent: the server
|
|
8
|
+
* accepts the mutation but the typed columns silently stay empty, and later
|
|
9
|
+
* reads of that field blow up. We normalize here so the flat shape is
|
|
10
|
+
* impossible to send rather than merely documented.
|
|
11
|
+
*
|
|
12
|
+
* `fieldValuesAttributes` shows up on 15 different `*Attributes` types and
|
|
13
|
+
* nests arbitrarily (`contactAttributes.contactableAttributes.…`,
|
|
14
|
+
* `pageAttributes.pageBlocksAttributes[].…`), so the walk below is keyed on the
|
|
15
|
+
* property name at any depth rather than on a fixed path. It runs once per
|
|
16
|
+
* mutation in `UiduClient.mutate`.
|
|
17
|
+
*/
|
|
18
|
+
|
|
19
|
+
type FieldValueLike = { content?: unknown } & Record<string, unknown>;
|
|
20
|
+
|
|
21
|
+
const FIELD_VALUES_KEY = 'fieldValuesAttributes';
|
|
22
|
+
|
|
23
|
+
function isPlainObject(value: unknown): value is Record<string, unknown> {
|
|
24
|
+
return typeof value === 'object' && value !== null && !Array.isArray(value);
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
function isDev(): boolean {
|
|
28
|
+
// Read `process` off globalThis: this file is compiled by consumers that
|
|
29
|
+
// don't pull in @types/node (e.g. @uidu/react), and it has to stay safe in
|
|
30
|
+
// browser and edge runtimes where `process` is absent entirely.
|
|
31
|
+
const env = (
|
|
32
|
+
globalThis as {
|
|
33
|
+
process?: { env?: Record<string, string | undefined> };
|
|
34
|
+
}
|
|
35
|
+
).process?.env;
|
|
36
|
+
|
|
37
|
+
return env?.NODE_ENV !== 'production';
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
function warnOnce(state: { rewrote: boolean }): void {
|
|
41
|
+
if (!state.rewrote || !isDev()) return;
|
|
42
|
+
console.warn(
|
|
43
|
+
'[@uidu/client] fieldValuesAttributes: `content` must be `{ value: … }`, ' +
|
|
44
|
+
'not a bare value. Wrapping it for you — update the call site, since ' +
|
|
45
|
+
'other clients of the API will not do this.',
|
|
46
|
+
);
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* Wrap a `content` payload in `{ value }` unless it already carries a `value`
|
|
51
|
+
* key. Returns the input untouched when there is nothing to do.
|
|
52
|
+
*/
|
|
53
|
+
export function normalizeFieldValueContent(content: unknown): unknown {
|
|
54
|
+
if (content === undefined || content === null) return content;
|
|
55
|
+
if (isPlainObject(content) && 'value' in content) return content;
|
|
56
|
+
return { value: content };
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
/** Walk any value, rewriting `fieldValuesAttributes` arrays wherever they sit. */
|
|
60
|
+
function walk(value: unknown, state: { rewrote: boolean }): unknown {
|
|
61
|
+
if (Array.isArray(value)) return walkArray(value, state);
|
|
62
|
+
if (!isPlainObject(value)) return value;
|
|
63
|
+
|
|
64
|
+
let changed = false;
|
|
65
|
+
const next: Record<string, unknown> = {};
|
|
66
|
+
|
|
67
|
+
for (const [key, child] of Object.entries(value)) {
|
|
68
|
+
const rewritten =
|
|
69
|
+
key === FIELD_VALUES_KEY && Array.isArray(child)
|
|
70
|
+
? walkEntries(child, state)
|
|
71
|
+
: walk(child, state);
|
|
72
|
+
|
|
73
|
+
if (rewritten !== child) changed = true;
|
|
74
|
+
next[key] = rewritten;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
return changed ? next : value;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
function walkArray(
|
|
81
|
+
values: ReadonlyArray<unknown>,
|
|
82
|
+
state: { rewrote: boolean },
|
|
83
|
+
): ReadonlyArray<unknown> {
|
|
84
|
+
let changed = false;
|
|
85
|
+
const next = values.map((item) => {
|
|
86
|
+
const rewritten = walk(item, state);
|
|
87
|
+
if (rewritten !== item) changed = true;
|
|
88
|
+
return rewritten;
|
|
89
|
+
});
|
|
90
|
+
|
|
91
|
+
return changed ? next : values;
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
/**
|
|
95
|
+
* Normalize the entries of one `fieldValuesAttributes` array: wrap `content`,
|
|
96
|
+
* and keep walking every sibling key (`linkedRecordAttributes`, nested
|
|
97
|
+
* `fieldValuesAttributes`, …). Deliberately does not descend into `content`
|
|
98
|
+
* itself — that payload is opaque user JSON.
|
|
99
|
+
*/
|
|
100
|
+
function walkEntries(
|
|
101
|
+
entries: ReadonlyArray<unknown>,
|
|
102
|
+
state: { rewrote: boolean },
|
|
103
|
+
): ReadonlyArray<unknown> {
|
|
104
|
+
let arrayChanged = false;
|
|
105
|
+
|
|
106
|
+
const next = entries.map((entry) => {
|
|
107
|
+
if (!isPlainObject(entry)) return entry;
|
|
108
|
+
|
|
109
|
+
let changed = false;
|
|
110
|
+
const rewritten: Record<string, unknown> = {};
|
|
111
|
+
|
|
112
|
+
for (const [key, child] of Object.entries(entry)) {
|
|
113
|
+
const value =
|
|
114
|
+
key === 'content'
|
|
115
|
+
? normalizeFieldValueContent(child)
|
|
116
|
+
: key === FIELD_VALUES_KEY && Array.isArray(child)
|
|
117
|
+
? walkEntries(child, state)
|
|
118
|
+
: walk(child, state);
|
|
119
|
+
|
|
120
|
+
if (value !== child) {
|
|
121
|
+
changed = true;
|
|
122
|
+
if (key === 'content') state.rewrote = true;
|
|
123
|
+
}
|
|
124
|
+
rewritten[key] = value;
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
if (!changed) return entry;
|
|
128
|
+
arrayChanged = true;
|
|
129
|
+
return rewritten;
|
|
130
|
+
});
|
|
131
|
+
|
|
132
|
+
return arrayChanged ? next : entries;
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
/**
|
|
136
|
+
* Normalize a standalone `fieldValuesAttributes` array. Exported for callers
|
|
137
|
+
* that assemble one by hand outside an SDK mutation.
|
|
138
|
+
*/
|
|
139
|
+
export function normalizeFieldValuesAttributes<T extends FieldValueLike>(
|
|
140
|
+
attributes: ReadonlyArray<T | null | undefined> | null | undefined,
|
|
141
|
+
): typeof attributes {
|
|
142
|
+
if (!attributes) return attributes;
|
|
143
|
+
|
|
144
|
+
const state = { rewrote: false };
|
|
145
|
+
const normalized = walkEntries(attributes, state) as typeof attributes;
|
|
146
|
+
warnOnce(state);
|
|
147
|
+
|
|
148
|
+
return normalized;
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
/**
|
|
152
|
+
* Normalize every `fieldValuesAttributes` array reachable from a mutation's
|
|
153
|
+
* variables, at any depth. Returns the input untouched — same reference, and
|
|
154
|
+
* same references for every unchanged subtree — when there is nothing to do.
|
|
155
|
+
*/
|
|
156
|
+
export function normalizeMutationVariables<V>(variables: V): V {
|
|
157
|
+
if (!isPlainObject(variables) && !Array.isArray(variables)) return variables;
|
|
158
|
+
|
|
159
|
+
const state = { rewrote: false };
|
|
160
|
+
const normalized = walk(variables, state) as V;
|
|
161
|
+
warnOnce(state);
|
|
162
|
+
|
|
163
|
+
return normalized;
|
|
164
|
+
}
|