@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/package.json
CHANGED
|
@@ -0,0 +1,165 @@
|
|
|
1
|
+
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
|
|
2
|
+
|
|
3
|
+
const request = vi.fn();
|
|
4
|
+
|
|
5
|
+
vi.mock('graphql-request', () => ({
|
|
6
|
+
GraphQLClient: class {
|
|
7
|
+
request = request;
|
|
8
|
+
},
|
|
9
|
+
ClientError: class extends Error {},
|
|
10
|
+
}));
|
|
11
|
+
|
|
12
|
+
const { createClient } = await import('./client');
|
|
13
|
+
const { createFormResponse } = await import('./forms');
|
|
14
|
+
const { CreateFormResponseDocument, CreatePageDocument, GetFormDocument } =
|
|
15
|
+
await import('./generated');
|
|
16
|
+
|
|
17
|
+
const client = createClient({ workspace: 'acme', apiKey: 'k' });
|
|
18
|
+
|
|
19
|
+
beforeEach(() => {
|
|
20
|
+
request.mockResolvedValue({});
|
|
21
|
+
vi.spyOn(console, 'warn').mockImplementation(() => {});
|
|
22
|
+
});
|
|
23
|
+
|
|
24
|
+
afterEach(() => {
|
|
25
|
+
vi.restoreAllMocks();
|
|
26
|
+
request.mockReset();
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
const sentVariables = () => request.mock.calls[0][1];
|
|
30
|
+
|
|
31
|
+
describe('UiduClient.mutate — fieldValuesAttributes normalization', () => {
|
|
32
|
+
it('wraps a flat `content` on a form response', async () => {
|
|
33
|
+
await createFormResponse(client, {
|
|
34
|
+
input: {
|
|
35
|
+
event: 'complete!',
|
|
36
|
+
attributes: {
|
|
37
|
+
formId: 'form_1',
|
|
38
|
+
fieldValuesAttributes: [
|
|
39
|
+
{ fieldId: 'f1', content: 'hello' },
|
|
40
|
+
{ fieldId: 'f2', content: { value: 'already wrapped' } },
|
|
41
|
+
],
|
|
42
|
+
},
|
|
43
|
+
},
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
expect(sentVariables().input.attributes.fieldValuesAttributes).toEqual([
|
|
47
|
+
{ fieldId: 'f1', content: { value: 'hello' } },
|
|
48
|
+
{ fieldId: 'f2', content: { value: 'already wrapped' } },
|
|
49
|
+
]);
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
it('reaches nested attributes (contactAttributes.contactableAttributes)', async () => {
|
|
53
|
+
await client.mutate(CreateFormResponseDocument, {
|
|
54
|
+
input: {
|
|
55
|
+
attributes: {
|
|
56
|
+
formId: 'form_1',
|
|
57
|
+
contactAttributes: {
|
|
58
|
+
email: 'a@b.c',
|
|
59
|
+
contactableAttributes: {
|
|
60
|
+
kind: 'person',
|
|
61
|
+
fieldValuesAttributes: [{ fieldId: 'f9', content: 'nested' }],
|
|
62
|
+
},
|
|
63
|
+
},
|
|
64
|
+
},
|
|
65
|
+
},
|
|
66
|
+
} as never);
|
|
67
|
+
|
|
68
|
+
expect(
|
|
69
|
+
sentVariables().input.attributes.contactAttributes.contactableAttributes
|
|
70
|
+
.fieldValuesAttributes,
|
|
71
|
+
).toEqual([{ fieldId: 'f9', content: { value: 'nested' } }]);
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
it('reaches through arrays (pageBlocksAttributes[])', async () => {
|
|
75
|
+
await client.mutate(CreatePageDocument, {
|
|
76
|
+
input: {
|
|
77
|
+
attributes: {
|
|
78
|
+
name: 'Home',
|
|
79
|
+
pageBlocksAttributes: [
|
|
80
|
+
{ fieldValuesAttributes: [{ fieldId: 'f1', content: 'a' }] },
|
|
81
|
+
{
|
|
82
|
+
fieldValuesAttributes: [
|
|
83
|
+
{ fieldId: 'f2', content: { value: 'b' } },
|
|
84
|
+
],
|
|
85
|
+
},
|
|
86
|
+
],
|
|
87
|
+
},
|
|
88
|
+
},
|
|
89
|
+
} as never);
|
|
90
|
+
|
|
91
|
+
expect(sentVariables().input.attributes.pageBlocksAttributes).toEqual([
|
|
92
|
+
{ fieldValuesAttributes: [{ fieldId: 'f1', content: { value: 'a' } }] },
|
|
93
|
+
{ fieldValuesAttributes: [{ fieldId: 'f2', content: { value: 'b' } }] },
|
|
94
|
+
]);
|
|
95
|
+
});
|
|
96
|
+
|
|
97
|
+
it('does not descend into the `content` payload itself', async () => {
|
|
98
|
+
const doc = {
|
|
99
|
+
type: 'doc',
|
|
100
|
+
// a rich text document that happens to carry the magic key
|
|
101
|
+
fieldValuesAttributes: [{ fieldId: 'nope', content: 'leave me' }],
|
|
102
|
+
};
|
|
103
|
+
|
|
104
|
+
await client.mutate(CreateFormResponseDocument, {
|
|
105
|
+
input: {
|
|
106
|
+
attributes: {
|
|
107
|
+
fieldValuesAttributes: [{ fieldId: 'f1', content: { value: doc } }],
|
|
108
|
+
},
|
|
109
|
+
},
|
|
110
|
+
} as never);
|
|
111
|
+
|
|
112
|
+
expect(
|
|
113
|
+
sentVariables().input.attributes.fieldValuesAttributes[0].content.value,
|
|
114
|
+
).toEqual(doc);
|
|
115
|
+
});
|
|
116
|
+
|
|
117
|
+
it('leaves mutations without field values untouched, by reference', async () => {
|
|
118
|
+
const variables = {
|
|
119
|
+
input: { attributes: { name: 'Home', slug: 'home' } },
|
|
120
|
+
};
|
|
121
|
+
await client.mutate(CreatePageDocument, variables as never);
|
|
122
|
+
|
|
123
|
+
expect(sentVariables()).toBe(variables);
|
|
124
|
+
});
|
|
125
|
+
|
|
126
|
+
it('does not mutate the caller’s object', async () => {
|
|
127
|
+
const variables = {
|
|
128
|
+
input: {
|
|
129
|
+
attributes: {
|
|
130
|
+
fieldValuesAttributes: [{ fieldId: 'f1', content: 'flat' }],
|
|
131
|
+
},
|
|
132
|
+
},
|
|
133
|
+
};
|
|
134
|
+
await client.mutate(CreateFormResponseDocument, variables as never);
|
|
135
|
+
|
|
136
|
+
expect(variables.input.attributes.fieldValuesAttributes[0].content).toBe(
|
|
137
|
+
'flat',
|
|
138
|
+
);
|
|
139
|
+
});
|
|
140
|
+
|
|
141
|
+
it('warns once per mutation, not once per entry', async () => {
|
|
142
|
+
await client.mutate(CreateFormResponseDocument, {
|
|
143
|
+
input: {
|
|
144
|
+
attributes: {
|
|
145
|
+
fieldValuesAttributes: [
|
|
146
|
+
{ fieldId: 'f1', content: 'a' },
|
|
147
|
+
{ fieldId: 'f2', content: 'b' },
|
|
148
|
+
{ fieldId: 'f3', content: 'c' },
|
|
149
|
+
],
|
|
150
|
+
},
|
|
151
|
+
},
|
|
152
|
+
} as never);
|
|
153
|
+
|
|
154
|
+
expect(console.warn).toHaveBeenCalledTimes(1);
|
|
155
|
+
});
|
|
156
|
+
});
|
|
157
|
+
|
|
158
|
+
describe('UiduClient.query', () => {
|
|
159
|
+
it('does not normalize — reads have no field value payloads', async () => {
|
|
160
|
+
const variables = { id: 'form_1' };
|
|
161
|
+
await client.query(GetFormDocument, variables);
|
|
162
|
+
|
|
163
|
+
expect(sentVariables()).toBe(variables);
|
|
164
|
+
});
|
|
165
|
+
});
|
package/src/client.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import type { TypedDocumentNode } from '@graphql-typed-document-node/core';
|
|
2
2
|
import { ClientError, GraphQLClient } from 'graphql-request';
|
|
3
3
|
import { UiduError, type UiduClientConfig } from './types';
|
|
4
|
+
import { normalizeMutationVariables } from './utils/fieldValues';
|
|
4
5
|
|
|
5
6
|
export class UiduClient {
|
|
6
7
|
readonly endpoint: string;
|
|
@@ -35,7 +36,10 @@ export class UiduClient {
|
|
|
35
36
|
document: TypedDocumentNode<TResult, TVariables>,
|
|
36
37
|
variables?: TVariables,
|
|
37
38
|
): Promise<TResult> {
|
|
38
|
-
|
|
39
|
+
// `fieldValuesAttributes[].content` must be `{ value: … }`; a bare value is
|
|
40
|
+
// accepted by the API but silently discarded. Fix it up for every mutation
|
|
41
|
+
// rather than at each of the ~50 call sites. See ./utils/fieldValues.
|
|
42
|
+
return this.query(document, normalizeMutationVariables(variables));
|
|
39
43
|
}
|
|
40
44
|
}
|
|
41
45
|
|