@uidu/client 3.4.1 → 3.6.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 +36 -0
- package/dist/declarations/src/bookings.d.ts +1 -1
- package/dist/declarations/src/generated/index.d.ts +8635 -263
- package/dist/declarations/src/index.d.ts +4 -3
- package/dist/declarations/src/mutations.d.ts +15 -3
- package/dist/declarations/src/people.d.ts +16 -1
- package/dist/declarations/src/utils/fieldValues.d.ts +37 -0
- package/dist/uidu-client.cjs.dev.js +137 -3
- package/dist/uidu-client.cjs.prod.js +137 -3
- package/dist/uidu-client.esm.js +136 -4
- package/package.json +1 -1
- package/src/client.test.ts +165 -0
- package/src/client.ts +5 -1
- package/src/generated/index.ts +9837 -297
- package/src/index.ts +8 -15
- package/src/people.test.ts +3 -0
- package/src/people.ts +12 -1
- package/src/queries/people/GetEmployee.graphql +4 -1
- package/src/queries/people/ListEmployees.graphql +4 -1
- package/src/queries/people/ListEmploymentHistory.graphql +4 -1
- package/src/queries/people/PeopleMutations.graphql +8 -2
- package/src/utils/fieldValues.test.ts +268 -0
- package/src/utils/fieldValues.ts +164 -0
package/src/index.ts
CHANGED
|
@@ -5,6 +5,11 @@ export type { UiduClientConfig, UiduErrorOptions } from './types';
|
|
|
5
5
|
export { paginate } from './utils/paginate';
|
|
6
6
|
export type { PaginateOptions } from './utils/paginate';
|
|
7
7
|
|
|
8
|
+
export {
|
|
9
|
+
normalizeFieldValueContent,
|
|
10
|
+
normalizeFieldValuesAttributes,
|
|
11
|
+
} from './utils/fieldValues';
|
|
12
|
+
|
|
8
13
|
export {
|
|
9
14
|
ListApplicationsDocument,
|
|
10
15
|
ListBookingsDocument,
|
|
@@ -38,10 +43,7 @@ export {
|
|
|
38
43
|
} from './cms';
|
|
39
44
|
export type { Page, Template } from './cms';
|
|
40
45
|
|
|
41
|
-
export {
|
|
42
|
-
createWorkspace,
|
|
43
|
-
generateWorkspaceApiCredentials,
|
|
44
|
-
} from './system';
|
|
46
|
+
export { createWorkspace, generateWorkspaceApiCredentials } from './system';
|
|
45
47
|
|
|
46
48
|
export { createSpace } from './spaces';
|
|
47
49
|
export { createTask } from './tasks';
|
|
@@ -169,19 +171,10 @@ export {
|
|
|
169
171
|
} from './courses';
|
|
170
172
|
export type { Course, Enrollment, Lecture } from './courses';
|
|
171
173
|
|
|
172
|
-
export {
|
|
173
|
-
getCampaign,
|
|
174
|
-
listCampaigns,
|
|
175
|
-
listEmailCampaigns,
|
|
176
|
-
} from './campaigns';
|
|
174
|
+
export { getCampaign, listCampaigns, listEmailCampaigns } from './campaigns';
|
|
177
175
|
export type { Campaign } from './campaigns';
|
|
178
176
|
|
|
179
|
-
export {
|
|
180
|
-
getContact,
|
|
181
|
-
listContacts,
|
|
182
|
-
getDeal,
|
|
183
|
-
listDeals,
|
|
184
|
-
} from './contacts';
|
|
177
|
+
export { getContact, listContacts, getDeal, listDeals } from './contacts';
|
|
185
178
|
export type { Contact, Deal } from './contacts';
|
|
186
179
|
|
|
187
180
|
export {
|
package/src/people.test.ts
CHANGED
|
@@ -81,6 +81,7 @@ describe('contractEvents', () => {
|
|
|
81
81
|
endDate: '2025-03-31',
|
|
82
82
|
endReason: 'contract_end',
|
|
83
83
|
jobTitle: 'Senior Dev',
|
|
84
|
+
employmentKind: { id: 'ek1', name: 'determinato' },
|
|
84
85
|
contractType: 'determinato',
|
|
85
86
|
ccnl: null,
|
|
86
87
|
compensations: [],
|
|
@@ -91,6 +92,7 @@ describe('contractEvents', () => {
|
|
|
91
92
|
endDate: null,
|
|
92
93
|
endReason: null,
|
|
93
94
|
jobTitle: 'Dev',
|
|
95
|
+
employmentKind: { id: 'ek2', name: 'indeterminato' },
|
|
94
96
|
contractType: 'indeterminato',
|
|
95
97
|
ccnl: null,
|
|
96
98
|
compensations: [],
|
|
@@ -107,6 +109,7 @@ describe('contractEvents', () => {
|
|
|
107
109
|
endDate: null,
|
|
108
110
|
endReason: null,
|
|
109
111
|
jobTitle: null,
|
|
112
|
+
employmentKind: null,
|
|
110
113
|
contractType: null,
|
|
111
114
|
ccnl: null,
|
|
112
115
|
compensations: [],
|
package/src/people.ts
CHANGED
|
@@ -158,6 +158,14 @@ export type EmploymentRecord = {
|
|
|
158
158
|
endDate: string | null;
|
|
159
159
|
endReason: string | null;
|
|
160
160
|
jobTitle: string | null;
|
|
161
|
+
/**
|
|
162
|
+
* The contract type, as a record. It used to be a free string on the
|
|
163
|
+
* employment (`contractType`); uidu promoted it to `EmploymentKind` so a
|
|
164
|
+
* workspace can configure its own, which is why writes take
|
|
165
|
+
* `employmentKindId`.
|
|
166
|
+
*/
|
|
167
|
+
employmentKind: { id: string; name: string | null } | null;
|
|
168
|
+
/** The contract type's name — `employmentKind.name`, kept flat for display. */
|
|
161
169
|
contractType: string | null;
|
|
162
170
|
/** The CCNL (national collective labor contract) that applies, if any. */
|
|
163
171
|
ccnl: { id: string; name: string | null } | null;
|
|
@@ -194,7 +202,10 @@ export async function listEmploymentHistory(
|
|
|
194
202
|
endDate: e.endDate ?? null,
|
|
195
203
|
endReason: e.endReason ?? null,
|
|
196
204
|
jobTitle: e.jobTitle ?? null,
|
|
197
|
-
|
|
205
|
+
employmentKind: e.employmentKind
|
|
206
|
+
? { id: e.employmentKind.id ?? '', name: e.employmentKind.name ?? null }
|
|
207
|
+
: null,
|
|
208
|
+
contractType: e.employmentKind?.name ?? null,
|
|
198
209
|
ccnl: e.ccnl ? { id: e.ccnl.id ?? '', name: e.ccnl.name ?? null } : null,
|
|
199
210
|
compensations: nodes(e.compensations)
|
|
200
211
|
.map((c) => ({
|
|
@@ -39,7 +39,10 @@ mutation CreateEmployment($input: CreateEmploymentInput!) {
|
|
|
39
39
|
node {
|
|
40
40
|
id
|
|
41
41
|
jobTitle
|
|
42
|
-
|
|
42
|
+
employmentKind {
|
|
43
|
+
id
|
|
44
|
+
name
|
|
45
|
+
}
|
|
43
46
|
classification
|
|
44
47
|
startDate
|
|
45
48
|
endDate
|
|
@@ -59,7 +62,10 @@ mutation UpdateEmployment($input: UpdateEmploymentInput!) {
|
|
|
59
62
|
employment {
|
|
60
63
|
id
|
|
61
64
|
jobTitle
|
|
62
|
-
|
|
65
|
+
employmentKind {
|
|
66
|
+
id
|
|
67
|
+
name
|
|
68
|
+
}
|
|
63
69
|
classification
|
|
64
70
|
startDate
|
|
65
71
|
endDate
|
|
@@ -0,0 +1,268 @@
|
|
|
1
|
+
import { afterEach, describe, expect, it, vi } from 'vitest';
|
|
2
|
+
import {
|
|
3
|
+
normalizeFieldValueContent,
|
|
4
|
+
normalizeFieldValuesAttributes,
|
|
5
|
+
normalizeMutationVariables,
|
|
6
|
+
} from './fieldValues';
|
|
7
|
+
|
|
8
|
+
afterEach(() => {
|
|
9
|
+
vi.restoreAllMocks();
|
|
10
|
+
});
|
|
11
|
+
|
|
12
|
+
describe('normalizeFieldValueContent', () => {
|
|
13
|
+
it('leaves null and undefined alone', () => {
|
|
14
|
+
expect(normalizeFieldValueContent(null)).toBeNull();
|
|
15
|
+
expect(normalizeFieldValueContent(undefined)).toBeUndefined();
|
|
16
|
+
});
|
|
17
|
+
|
|
18
|
+
it('leaves an already-wrapped payload alone', () => {
|
|
19
|
+
const content = { value: 'hello' };
|
|
20
|
+
expect(normalizeFieldValueContent(content)).toBe(content);
|
|
21
|
+
});
|
|
22
|
+
|
|
23
|
+
it('leaves an explicit null value alone', () => {
|
|
24
|
+
const content = { value: null };
|
|
25
|
+
expect(normalizeFieldValueContent(content)).toBe(content);
|
|
26
|
+
});
|
|
27
|
+
|
|
28
|
+
it('wraps a bare string', () => {
|
|
29
|
+
expect(normalizeFieldValueContent('hello')).toEqual({ value: 'hello' });
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
it('wraps a bare number, boolean and zero', () => {
|
|
33
|
+
expect(normalizeFieldValueContent(42)).toEqual({ value: 42 });
|
|
34
|
+
expect(normalizeFieldValueContent(false)).toEqual({ value: false });
|
|
35
|
+
expect(normalizeFieldValueContent(0)).toEqual({ value: 0 });
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
it('wraps an array (multipleSelect answers)', () => {
|
|
39
|
+
expect(normalizeFieldValueContent(['a', 'b'])).toEqual({
|
|
40
|
+
value: ['a', 'b'],
|
|
41
|
+
});
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
it('wraps an object that has no value key (richText / json answers)', () => {
|
|
45
|
+
const doc = { type: 'doc', content: [] };
|
|
46
|
+
expect(normalizeFieldValueContent(doc)).toEqual({ value: doc });
|
|
47
|
+
});
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
describe('normalizeFieldValuesAttributes', () => {
|
|
51
|
+
it('passes null / undefined through', () => {
|
|
52
|
+
expect(normalizeFieldValuesAttributes(null)).toBeNull();
|
|
53
|
+
expect(normalizeFieldValuesAttributes(undefined)).toBeUndefined();
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
it('returns the same array reference when nothing needs wrapping', () => {
|
|
57
|
+
const attributes = [{ fieldId: '1', content: { value: 'a' } }];
|
|
58
|
+
expect(normalizeFieldValuesAttributes(attributes)).toBe(attributes);
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
it('wraps only the entries that need it, keeping order', () => {
|
|
62
|
+
const warn = vi.spyOn(console, 'warn').mockImplementation(() => {});
|
|
63
|
+
const attributes = [
|
|
64
|
+
{ fieldId: '1', content: 'flat' },
|
|
65
|
+
{ fieldId: '2', content: { value: 'wrapped' } },
|
|
66
|
+
{ fieldId: '3', content: 3 },
|
|
67
|
+
];
|
|
68
|
+
|
|
69
|
+
expect(normalizeFieldValuesAttributes(attributes)).toEqual([
|
|
70
|
+
{ fieldId: '1', content: { value: 'flat' } },
|
|
71
|
+
{ fieldId: '2', content: { value: 'wrapped' } },
|
|
72
|
+
{ fieldId: '3', content: { value: 3 } },
|
|
73
|
+
]);
|
|
74
|
+
expect(warn).toHaveBeenCalledTimes(1);
|
|
75
|
+
});
|
|
76
|
+
|
|
77
|
+
it('preserves sibling attributes such as id and linkedRecordId', () => {
|
|
78
|
+
vi.spyOn(console, 'warn').mockImplementation(() => {});
|
|
79
|
+
expect(
|
|
80
|
+
normalizeFieldValuesAttributes([
|
|
81
|
+
{ fieldId: '1', id: 'fv_1', linkedRecordId: 'lr_1', content: 'x' },
|
|
82
|
+
]),
|
|
83
|
+
).toEqual([
|
|
84
|
+
{
|
|
85
|
+
fieldId: '1',
|
|
86
|
+
id: 'fv_1',
|
|
87
|
+
linkedRecordId: 'lr_1',
|
|
88
|
+
content: { value: 'x' },
|
|
89
|
+
},
|
|
90
|
+
]);
|
|
91
|
+
});
|
|
92
|
+
|
|
93
|
+
it('leaves entries without a content key alone', () => {
|
|
94
|
+
const attributes = [{ fieldId: '1', linkedRecordId: 'lr_1' }];
|
|
95
|
+
expect(normalizeFieldValuesAttributes(attributes)).toBe(attributes);
|
|
96
|
+
});
|
|
97
|
+
|
|
98
|
+
it('does not warn in production', () => {
|
|
99
|
+
const warn = vi.spyOn(console, 'warn').mockImplementation(() => {});
|
|
100
|
+
vi.stubEnv('NODE_ENV', 'production');
|
|
101
|
+
|
|
102
|
+
normalizeFieldValuesAttributes([{ fieldId: '1', content: 'flat' }]);
|
|
103
|
+
expect(warn).not.toHaveBeenCalled();
|
|
104
|
+
|
|
105
|
+
vi.unstubAllEnvs();
|
|
106
|
+
});
|
|
107
|
+
});
|
|
108
|
+
|
|
109
|
+
describe('normalizeMutationVariables', () => {
|
|
110
|
+
it('passes non-objects through', () => {
|
|
111
|
+
expect(normalizeMutationVariables(undefined)).toBeUndefined();
|
|
112
|
+
expect(normalizeMutationVariables('x')).toBe('x');
|
|
113
|
+
});
|
|
114
|
+
|
|
115
|
+
it('returns the same object when there is nothing to normalize', () => {
|
|
116
|
+
const variables = {
|
|
117
|
+
input: {
|
|
118
|
+
attributes: {
|
|
119
|
+
formId: 'form_1',
|
|
120
|
+
fieldValuesAttributes: [{ fieldId: '1', content: { value: 'a' } }],
|
|
121
|
+
},
|
|
122
|
+
},
|
|
123
|
+
};
|
|
124
|
+
expect(normalizeMutationVariables(variables)).toBe(variables);
|
|
125
|
+
});
|
|
126
|
+
|
|
127
|
+
it('returns the same object when there are no fieldValuesAttributes', () => {
|
|
128
|
+
const variables = { input: { attributes: { formId: 'form_1' } } };
|
|
129
|
+
expect(normalizeMutationVariables(variables)).toBe(variables);
|
|
130
|
+
});
|
|
131
|
+
|
|
132
|
+
it('rewrites flat content without mutating the caller input', () => {
|
|
133
|
+
vi.spyOn(console, 'warn').mockImplementation(() => {});
|
|
134
|
+
const variables = {
|
|
135
|
+
input: {
|
|
136
|
+
event: 'complete!',
|
|
137
|
+
attributes: {
|
|
138
|
+
formId: 'form_1',
|
|
139
|
+
contactAttributes: { email: 'a@b.c' },
|
|
140
|
+
fieldValuesAttributes: [{ fieldId: '1', content: 'flat' }],
|
|
141
|
+
},
|
|
142
|
+
},
|
|
143
|
+
};
|
|
144
|
+
|
|
145
|
+
expect(normalizeMutationVariables(variables)).toEqual({
|
|
146
|
+
input: {
|
|
147
|
+
event: 'complete!',
|
|
148
|
+
attributes: {
|
|
149
|
+
formId: 'form_1',
|
|
150
|
+
contactAttributes: { email: 'a@b.c' },
|
|
151
|
+
fieldValuesAttributes: [{ fieldId: '1', content: { value: 'flat' } }],
|
|
152
|
+
},
|
|
153
|
+
},
|
|
154
|
+
});
|
|
155
|
+
expect(variables.input.attributes.fieldValuesAttributes[0].content).toBe(
|
|
156
|
+
'flat',
|
|
157
|
+
);
|
|
158
|
+
});
|
|
159
|
+
|
|
160
|
+
it('finds fieldValuesAttributes nested behind other attributes', () => {
|
|
161
|
+
vi.spyOn(console, 'warn').mockImplementation(() => {});
|
|
162
|
+
const result = normalizeMutationVariables({
|
|
163
|
+
input: {
|
|
164
|
+
attributes: {
|
|
165
|
+
contactAttributes: {
|
|
166
|
+
contactableAttributes: {
|
|
167
|
+
fieldValuesAttributes: [{ fieldId: '1', content: 'deep' }],
|
|
168
|
+
},
|
|
169
|
+
},
|
|
170
|
+
},
|
|
171
|
+
},
|
|
172
|
+
});
|
|
173
|
+
|
|
174
|
+
expect(
|
|
175
|
+
result.input.attributes.contactAttributes.contactableAttributes
|
|
176
|
+
.fieldValuesAttributes,
|
|
177
|
+
).toEqual([{ fieldId: '1', content: { value: 'deep' } }]);
|
|
178
|
+
});
|
|
179
|
+
|
|
180
|
+
it('finds fieldValuesAttributes inside arrays of attributes', () => {
|
|
181
|
+
vi.spyOn(console, 'warn').mockImplementation(() => {});
|
|
182
|
+
const result = normalizeMutationVariables({
|
|
183
|
+
input: {
|
|
184
|
+
attributes: {
|
|
185
|
+
pageBlocksAttributes: [
|
|
186
|
+
{ fieldValuesAttributes: [{ fieldId: '1', content: 'a' }] },
|
|
187
|
+
],
|
|
188
|
+
},
|
|
189
|
+
},
|
|
190
|
+
});
|
|
191
|
+
|
|
192
|
+
expect(
|
|
193
|
+
result.input.attributes.pageBlocksAttributes[0].fieldValuesAttributes,
|
|
194
|
+
).toEqual([{ fieldId: '1', content: { value: 'a' } }]);
|
|
195
|
+
});
|
|
196
|
+
|
|
197
|
+
it('shares references for untouched subtrees', () => {
|
|
198
|
+
vi.spyOn(console, 'warn').mockImplementation(() => {});
|
|
199
|
+
const contactAttributes = { email: 'a@b.c' };
|
|
200
|
+
const result = normalizeMutationVariables({
|
|
201
|
+
input: {
|
|
202
|
+
attributes: {
|
|
203
|
+
contactAttributes,
|
|
204
|
+
fieldValuesAttributes: [{ fieldId: '1', content: 'flat' }],
|
|
205
|
+
},
|
|
206
|
+
},
|
|
207
|
+
});
|
|
208
|
+
|
|
209
|
+
expect(result.input.attributes.contactAttributes).toBe(contactAttributes);
|
|
210
|
+
});
|
|
211
|
+
|
|
212
|
+
it('does not descend into a content payload that carries the magic key', () => {
|
|
213
|
+
const doc = {
|
|
214
|
+
fieldValuesAttributes: [{ fieldId: 'nope', content: 'leave me' }],
|
|
215
|
+
};
|
|
216
|
+
const result = normalizeMutationVariables({
|
|
217
|
+
input: {
|
|
218
|
+
attributes: {
|
|
219
|
+
fieldValuesAttributes: [{ fieldId: '1', content: { value: doc } }],
|
|
220
|
+
},
|
|
221
|
+
},
|
|
222
|
+
});
|
|
223
|
+
|
|
224
|
+
expect(result.input.attributes.fieldValuesAttributes[0].content.value).toBe(
|
|
225
|
+
doc,
|
|
226
|
+
);
|
|
227
|
+
});
|
|
228
|
+
|
|
229
|
+
it('normalizes nested field values on a field value entry', () => {
|
|
230
|
+
vi.spyOn(console, 'warn').mockImplementation(() => {});
|
|
231
|
+
const result = normalizeMutationVariables({
|
|
232
|
+
input: {
|
|
233
|
+
attributes: {
|
|
234
|
+
fieldValuesAttributes: [
|
|
235
|
+
{
|
|
236
|
+
fieldId: '1',
|
|
237
|
+
content: { value: 'parent' },
|
|
238
|
+
fieldValuesAttributes: [{ fieldId: '2', content: 'child' }],
|
|
239
|
+
},
|
|
240
|
+
],
|
|
241
|
+
},
|
|
242
|
+
},
|
|
243
|
+
});
|
|
244
|
+
|
|
245
|
+
expect(
|
|
246
|
+
result.input.attributes.fieldValuesAttributes[0].fieldValuesAttributes,
|
|
247
|
+
).toEqual([{ fieldId: '2', content: { value: 'child' } }]);
|
|
248
|
+
});
|
|
249
|
+
|
|
250
|
+
it('warns once for a whole mutation, however many entries were wrapped', () => {
|
|
251
|
+
const warn = vi.spyOn(console, 'warn').mockImplementation(() => {});
|
|
252
|
+
normalizeMutationVariables({
|
|
253
|
+
input: {
|
|
254
|
+
attributes: {
|
|
255
|
+
fieldValuesAttributes: [
|
|
256
|
+
{ fieldId: '1', content: 'a' },
|
|
257
|
+
{ fieldId: '2', content: 'b' },
|
|
258
|
+
],
|
|
259
|
+
pageBlocksAttributes: [
|
|
260
|
+
{ fieldValuesAttributes: [{ fieldId: '3', content: 'c' }] },
|
|
261
|
+
],
|
|
262
|
+
},
|
|
263
|
+
},
|
|
264
|
+
});
|
|
265
|
+
|
|
266
|
+
expect(warn).toHaveBeenCalledTimes(1);
|
|
267
|
+
});
|
|
268
|
+
});
|
|
@@ -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
|
+
}
|