Skip to content

Commit da66757

Browse files
authored
Bump Plug JS to 0.11.6 (#370)
1 parent e52d532 commit da66757

File tree

11 files changed

+92
-44
lines changed

11 files changed

+92
-44
lines changed

.eslintrc.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@ require('@rushstack/eslint-patch/modern-module-resolution');
33

44
module.exports = {
55
root: true,
6+
plugins: ['@croct'],
67
extends: [
78
'plugin:@croct/react',
89
'plugin:@croct/typescript',
910
],
10-
plugins: ['@croct'],
1111
parserOptions: {
1212
project: ['./tsconfig.json'],
1313
ecmaFeatures: {

package-lock.json

Lines changed: 16 additions & 16 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,8 @@
3737
"react-dom": "^16.8.0 || ^17.0.0 || ^18.0.0"
3838
},
3939
"dependencies": {
40-
"@croct/plug": "^0.11.4",
41-
"@croct/sdk": "^0.12.0"
40+
"@croct/plug": "^0.11.6",
41+
"@croct/sdk": "^0.12.3"
4242
},
4343
"devDependencies": {
4444
"@babel/core": "^7.20.2",
@@ -47,11 +47,11 @@
4747
"@babel/preset-typescript": "^7.18.6",
4848
"@croct/eslint-plugin": "^0.6.3",
4949
"@storybook/addon-actions": "^6.5.13",
50-
"@storybook/addon-essentials": "^6.5.13",
50+
"@storybook/addon-essentials": "^6.4.22",
5151
"@storybook/addon-links": "^6.5.13",
5252
"@storybook/builder-webpack5": "^6.5.13",
5353
"@storybook/manager-webpack5": "^6.5.13",
54-
"@storybook/react": "^6.5.13",
54+
"@storybook/react": "^6.1.21",
5555
"@testing-library/jest-dom": "^5.16.5",
5656
"@testing-library/react": "^13.4.0",
5757
"@types/jest": "^29.2.3",

src/api/fetchContent.test.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import {ContentFetcher} from '@croct/sdk/contentFetcher';
22
import {FetchResponse} from '@croct/plug/plug';
3+
import {SlotContent} from '@croct/plug/slot';
34
import {fetchContent, FetchOptions} from './fetchContent';
45

56
const mockFetch: ContentFetcher['fetch'] = jest.fn();
@@ -33,10 +34,14 @@ describe('fetchContent', () => {
3334
apiKey: apiKey,
3435
baseEndpointUrl: 'https://croct.example.com',
3536
timeout: 100,
37+
fallback: {
38+
_component: 'component-id',
39+
},
3640
};
3741

3842
const result: FetchResponse<typeof slotId> = {
3943
content: {
44+
_component: 'component',
4045
id: 'test',
4146
},
4247
};
@@ -67,6 +72,7 @@ describe('fetchContent', () => {
6772

6873
const result: FetchResponse<typeof slotId> = {
6974
content: {
75+
_component: 'component',
7076
id: 'test',
7177
},
7278
};
@@ -97,6 +103,7 @@ describe('fetchContent', () => {
97103

98104
const result: FetchResponse<typeof slotId> = {
99105
content: {
106+
_component: 'component',
100107
id: 'test',
101108
},
102109
};
@@ -117,7 +124,8 @@ describe('fetchContent', () => {
117124
it('should return the fallback value on error', async () => {
118125
const slotId = 'slot-id';
119126

120-
const fallback = {
127+
const fallback: SlotContent = {
128+
_component: 'component-id',
121129
id: 'fallback',
122130
};
123131

src/api/fetchContent.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ export type DynamicContentOptions<T extends JsonObject = JsonObject> =
1919
export type StaticContentOptions<T extends JsonObject = JsonObject> =
2020
Omit<BaseStaticOptions, 'version'> & ServerSideOptions<T>;
2121

22-
export type FetchOptions<T extends JsonObject = JsonObject> = DynamicContentOptions<T> | StaticContentOptions<T>;
22+
export type FetchOptions<T extends JsonObject = SlotContent> = DynamicContentOptions<T> | StaticContentOptions<T>;
2323

2424
export function fetchContent<I extends VersionedSlotId, C extends JsonObject>(
2525
slotId: I,

src/components/Slot/index.d.test.tsx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ describe('<Slot /> typing', () => {
199199

200200
expect(() => compileCode(code)).not.toThrow();
201201

202-
expect(getParameterType(code)).toBe('HomeBannerProps');
202+
expect(getParameterType(code)).toBe('HomeBannerProps & {_component: string | null;}');
203203
});
204204

205205
it('should allow a covariant renderer parameter type for mapped slots', () => {
@@ -240,7 +240,7 @@ describe('<Slot /> typing', () => {
240240

241241
expect(() => compileCode(code)).not.toThrow();
242242

243-
expect(getParameterType(code)).toBe('boolean | HomeBannerProps');
243+
expect(getParameterType(code)).toBe('boolean | (HomeBannerProps & {_component: string | null;})');
244244
});
245245

246246
it('should allow a renderer that accepts the initial value for mapped slots', () => {
@@ -281,7 +281,7 @@ describe('<Slot /> typing', () => {
281281

282282
expect(() => compileCode(code)).not.toThrow();
283283

284-
expect(getParameterType(code)).toBe('boolean | HomeBannerProps');
284+
expect(getParameterType(code)).toBe('boolean | (HomeBannerProps & {_component: string | null;})');
285285
});
286286

287287
it('should allow a renderer that accepts the fallback value for mapped slots', () => {
@@ -322,7 +322,7 @@ describe('<Slot /> typing', () => {
322322

323323
expect(() => compileCode(code)).not.toThrow();
324324

325-
expect(getParameterType(code)).toBe('number | boolean | HomeBannerProps');
325+
expect(getParameterType(code)).toBe('number | boolean | (HomeBannerProps & {_component: string | null;})');
326326
});
327327

328328
it('should allow a renderer that accepts both the initial and fallback values for mapped slots', () => {

src/components/Slot/index.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import {Fragment, ReactElement, ReactNode} from 'react';
2-
import {SlotContent, SlotId, VersionedSlotId, VersionedSlotMap} from '@croct/plug/slot';
2+
import {SlotContent, VersionedSlotId, VersionedSlotMap} from '@croct/plug/slot';
33
import {JsonObject} from '@croct/plug/sdk/json';
44
import {useContent, UseContentOptions} from '../../hooks';
55

@@ -31,7 +31,7 @@ type SlotComponent = {
3131

3232
export const Slot: SlotComponent = <I, F>(props: SlotProps<JsonObject, I, F>): ReactElement => {
3333
const {id, children, ...options} = props;
34-
const data: SlotContent<SlotId> | I | F = useContent(id, options);
34+
const data = useContent(id, options);
3535

3636
return <Fragment>{children(data)}</Fragment>;
3737
};

src/hooks/useContent.d.test.tsx

Lines changed: 52 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,6 @@ import {create} from 'ts-node';
44
const tsService = create({
55
cwd: __dirname,
66
transpileOnly: false,
7-
ignore: [
8-
'lib/slots.d.ts',
9-
],
107
});
118

129
const testFilename = pathJoin(__dirname, 'test.tsx');
@@ -17,14 +14,40 @@ describe('useContent typing', () => {
1714
`;
1815

1916
const slotMapping = `
20-
type HomeBannerProps = {
17+
type HomeBanner = {
18+
title: string,
19+
subtitle: string,
20+
};
21+
22+
type Banner = {
23+
title: string,
24+
subtitle: string,
25+
};
26+
27+
type Carousel = {
2128
title: string,
2229
subtitle: string,
2330
};
2431
2532
declare module '@croct/plug/slot' {
26-
interface SlotMap {
27-
'home-banner': HomeBannerProps;
33+
interface VersionedSlotMap {
34+
'home-banner': {
35+
'latest': HomeBanner,
36+
'1': HomeBanner,
37+
};
38+
}
39+
}
40+
41+
declare module '@croct/plug/component' {
42+
interface VersionedComponentMap {
43+
'banner': {
44+
'latest': Banner,
45+
'1': Banner,
46+
};
47+
'carousel': {
48+
'latest': Carousel,
49+
'1': Carousel,
50+
};
2851
}
2952
}
3053
`;
@@ -99,6 +122,25 @@ describe('useContent typing', () => {
99122
expect(getReturnType(code)).toBe('JsonObject');
100123
});
101124

125+
it('should define the return type as an union of component for unknown slots', () => {
126+
const code: CodeOptions = {
127+
code: `
128+
useContent('dynamic-id' as any);
129+
`,
130+
mapping: true,
131+
};
132+
133+
expect(() => compileCode(code)).not.toThrow();
134+
135+
expect(getTypeName(code)).toBe(
136+
'useContent<any>',
137+
);
138+
139+
expect(getReturnType(code)).toBe(
140+
'(Banner & {_component: "banner@1";}) | (Carousel & {_component: "carousel@1";})',
141+
);
142+
});
143+
102144
it('should include the type of the initial value on the return type for unmapped slots', () => {
103145
const code: CodeOptions = {
104146
code: `
@@ -241,7 +283,7 @@ describe('useContent typing', () => {
241283

242284
expect(getTypeName(code)).toBe('useContent<"home-banner">');
243285

244-
expect(getReturnType(code)).toBe('HomeBannerProps');
286+
expect(getReturnType(code)).toBe('HomeBanner & {_component: string | null;}');
245287
});
246288

247289
it('should include the type of the initial value on the return type for mapped slots', () => {
@@ -256,7 +298,7 @@ describe('useContent typing', () => {
256298

257299
expect(getTypeName(code)).toBe('useContent<boolean, "home-banner">');
258300

259-
expect(getReturnType(code)).toBe('boolean | HomeBannerProps');
301+
expect(getReturnType(code)).toBe('boolean | (HomeBanner & {_component: string | null;})');
260302
});
261303

262304
it('should include the type of the fallback value on the return type for mapped slots', () => {
@@ -271,7 +313,7 @@ describe('useContent typing', () => {
271313

272314
expect(getTypeName(code)).toBe('useContent<number, "home-banner">');
273315

274-
expect(getReturnType(code)).toBe('number | HomeBannerProps');
316+
expect(getReturnType(code)).toBe('number | (HomeBanner & {_component: string | null;})');
275317
});
276318

277319
it('should include the types of both the initial and fallback values on the return type for mapped slots', () => {
@@ -286,7 +328,7 @@ describe('useContent typing', () => {
286328

287329
expect(getTypeName(code)).toBe('useContent<boolean, number, "home-banner">');
288330

289-
expect(getReturnType(code)).toBe('number | boolean | HomeBannerProps');
331+
expect(getReturnType(code)).toBe('number | boolean | (HomeBanner & {...;})');
290332
});
291333

292334
it('should not allow overriding the return type for mapped slots', () => {

src/hooks/useContent.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ export type UseContentOptions<I, F> = FetchOptions & {
1515
function useCsrContent<I, F>(
1616
id: VersionedSlotId,
1717
options: UseContentOptions<I, F> = {},
18-
): SlotContent<VersionedSlotId> | I | F {
18+
): SlotContent | I | F {
1919
const {fallback, initial, cacheKey, expiration, ...fetchOptions} = options;
2020
const croct = useCroct();
2121

@@ -31,7 +31,7 @@ function useCsrContent<I, F>(
3131
function useSsrContent<I, F>(
3232
_: VersionedSlotId,
3333
{initial}: UseContentOptions<I, F> = {},
34-
): SlotContent<VersionedSlotId> | I | F {
34+
): SlotContent | I | F {
3535
if (initial === undefined) {
3636
throw new Error('The initial value is required for server-side rendering (SSR).');
3737
}

src/hooks/useEvaluation.d.test.tsx

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,6 @@ import {create} from 'ts-node';
44
const tsService = create({
55
cwd: __dirname,
66
transpileOnly: false,
7-
ignore: [
8-
'lib/slots.d.ts',
9-
],
107
});
118

129
const testFilename = pathJoin(__dirname, 'test.tsx');

src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
export * from '@croct/plug/sdk/json';
22
export * from '@croct/plug/slot';
3+
export * from '@croct/plug/component';
34
export * from './CroctProvider';
45
export * from './hooks';
56
export * from './components';

0 commit comments

Comments
 (0)