Skip to content

Commit 5104544

Browse files
quantsinigyDBD
andauthored
Forces nullable batchKeys to be non-nullable (#339)
* Forces nullable batchKeys to be non-nullable * Adds a test to ensure null batchKeys would throw a TypeError * Update __tests__/implementation.test.js Co-authored-by: Yue Guo <[email protected]> Co-authored-by: Yue Guo <[email protected]>
1 parent 69cbcb5 commit 5104544

File tree

3 files changed

+58
-2
lines changed

3 files changed

+58
-2
lines changed

__tests__/genTypeFlow.test.js

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { getResourceTypeReference, getNewKeyTypeFromBatchKeySetType } from '../src/genTypeFlow';
1+
import { getResourceTypeReference, getNewKeyTypeFromBatchKeySetType, getLoaderTypeKey } from '../src/genTypeFlow';
22

33
it('getResourceTypeReference converts a resource path to a valid reference', () => {
44
expect(getResourceTypeReference(null, ['foo', 'bar', 'baz'])).toBe(
@@ -18,3 +18,30 @@ it('getNewKeyTypeFromBatchKeySetType returns a newKey type with a valid value',
1818
[$PropertyType<$PropertyType<$PropertyType<$PropertyType<$PropertyType<ResourcesType, 'foo'>, 'bar'>, 'baz'>, 'bKey'>, 'has'>]
1919
>`);
2020
});
21+
22+
it('getLoaderTypeKey forces a nullable batchKey to be strictly non-nullable', () => {
23+
expect(
24+
getLoaderTypeKey(
25+
{
26+
isBatchResource: true,
27+
newKey: 'test_id',
28+
batchKey: 'test_ids',
29+
},
30+
['a', 'b'],
31+
),
32+
).toBe(`\{|
33+
...$Diff< $Call<
34+
ExtractArg,
35+
[$PropertyType<$PropertyType<ResourcesType, 'a'>, 'b'>]
36+
>, {
37+
test_ids: $PropertyType< $Call<
38+
ExtractArg,
39+
[$PropertyType<$PropertyType<ResourcesType, 'a'>, 'b'>]
40+
>, 'test_ids'>
41+
}>,
42+
...{| test_id: $NonMaybeType<$ElementType<$PropertyType< $Call<
43+
ExtractArg,
44+
[$PropertyType<$PropertyType<ResourcesType, 'a'>, 'b'>]
45+
>, 'test_ids'>, 0>> |}
46+
|}`);
47+
});

__tests__/implementation.test.js

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,34 @@ test('batch endpoint', async () => {
130130
});
131131
});
132132

133+
test("batch endpoint can't be called with null", async () => {
134+
const config = {
135+
resources: {
136+
foo: {
137+
isBatchResource: true,
138+
docsLink: 'example.com/docs/bar',
139+
batchKey: 'foo_ids',
140+
newKey: 'foo_id',
141+
},
142+
},
143+
};
144+
145+
const resources = {
146+
foo: ({ foo_ids }) => {
147+
expect(foo_ids).toEqual([1]);
148+
return Promise.resolve([{ foo_id: 1, foo_value: 'hello' }]);
149+
},
150+
};
151+
152+
await createDataLoaders(config, async (getLoaders) => {
153+
const loaders = getLoaders(resources);
154+
155+
expect(() => {
156+
loaders.foo.load(null);
157+
}).toThrow(TypeError);
158+
});
159+
});
160+
133161
test('batch endpoint (with reorderResultsByKey)', async () => {
134162
const config = {
135163
resources: {

src/genTypeFlow.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,8 @@ export function getLoaderTypeKey(resourceConfig: ResourceConfig, resourcePath: R
5555

5656
if (resourceConfig.isBatchResource) {
5757
// Extract newKeyType from the batch key's Array's type
58-
let newKeyType = `${resourceConfig.newKey}: $ElementType<$PropertyType<${resourceArgs}, '${resourceConfig.batchKey}'>, 0>`;
58+
// We add NonMaybeType before batch key element type to force the batch key to be required, regardless if the OpenAPI spec specifies it as being optional
59+
let newKeyType = `${resourceConfig.newKey}: $NonMaybeType<$ElementType<$PropertyType<${resourceArgs}, '${resourceConfig.batchKey}'>, 0>>`;
5960

6061
if (resourceConfig.isBatchKeyASet) {
6162
/**

0 commit comments

Comments
 (0)