Skip to content

Commit

Permalink
Bump Plug JS to 0.11.6 (#370)
Browse files Browse the repository at this point in the history
  • Loading branch information
marcospassos committed Jan 14, 2023
1 parent e52d532 commit da66757
Show file tree
Hide file tree
Showing 11 changed files with 92 additions and 44 deletions.
2 changes: 1 addition & 1 deletion .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ require('@rushstack/eslint-patch/modern-module-resolution');

module.exports = {
root: true,
plugins: ['@croct'],
extends: [
'plugin:@croct/react',
'plugin:@croct/typescript',
],
plugins: ['@croct'],
parserOptions: {
project: ['./tsconfig.json'],
ecmaFeatures: {
Expand Down
32 changes: 16 additions & 16 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@
"react-dom": "^16.8.0 || ^17.0.0 || ^18.0.0"
},
"dependencies": {
"@croct/plug": "^0.11.4",
"@croct/sdk": "^0.12.0"
"@croct/plug": "^0.11.6",
"@croct/sdk": "^0.12.3"
},
"devDependencies": {
"@babel/core": "^7.20.2",
Expand All @@ -47,11 +47,11 @@
"@babel/preset-typescript": "^7.18.6",
"@croct/eslint-plugin": "^0.6.3",
"@storybook/addon-actions": "^6.5.13",
"@storybook/addon-essentials": "^6.5.13",
"@storybook/addon-essentials": "^6.4.22",
"@storybook/addon-links": "^6.5.13",
"@storybook/builder-webpack5": "^6.5.13",
"@storybook/manager-webpack5": "^6.5.13",
"@storybook/react": "^6.5.13",
"@storybook/react": "^6.1.21",
"@testing-library/jest-dom": "^5.16.5",
"@testing-library/react": "^13.4.0",
"@types/jest": "^29.2.3",
Expand Down
10 changes: 9 additions & 1 deletion src/api/fetchContent.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {ContentFetcher} from '@croct/sdk/contentFetcher';
import {FetchResponse} from '@croct/plug/plug';
import {SlotContent} from '@croct/plug/slot';
import {fetchContent, FetchOptions} from './fetchContent';

const mockFetch: ContentFetcher['fetch'] = jest.fn();
Expand Down Expand Up @@ -33,10 +34,14 @@ describe('fetchContent', () => {
apiKey: apiKey,
baseEndpointUrl: 'https://croct.example.com',
timeout: 100,
fallback: {
_component: 'component-id',
},
};

const result: FetchResponse<typeof slotId> = {
content: {
_component: 'component',
id: 'test',
},
};
Expand Down Expand Up @@ -67,6 +72,7 @@ describe('fetchContent', () => {

const result: FetchResponse<typeof slotId> = {
content: {
_component: 'component',
id: 'test',
},
};
Expand Down Expand Up @@ -97,6 +103,7 @@ describe('fetchContent', () => {

const result: FetchResponse<typeof slotId> = {
content: {
_component: 'component',
id: 'test',
},
};
Expand All @@ -117,7 +124,8 @@ describe('fetchContent', () => {
it('should return the fallback value on error', async () => {
const slotId = 'slot-id';

const fallback = {
const fallback: SlotContent = {
_component: 'component-id',
id: 'fallback',
};

Expand Down
2 changes: 1 addition & 1 deletion src/api/fetchContent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export type DynamicContentOptions<T extends JsonObject = JsonObject> =
export type StaticContentOptions<T extends JsonObject = JsonObject> =
Omit<BaseStaticOptions, 'version'> & ServerSideOptions<T>;

export type FetchOptions<T extends JsonObject = JsonObject> = DynamicContentOptions<T> | StaticContentOptions<T>;
export type FetchOptions<T extends JsonObject = SlotContent> = DynamicContentOptions<T> | StaticContentOptions<T>;

export function fetchContent<I extends VersionedSlotId, C extends JsonObject>(
slotId: I,
Expand Down
8 changes: 4 additions & 4 deletions src/components/Slot/index.d.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ describe('<Slot /> typing', () => {

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

expect(getParameterType(code)).toBe('HomeBannerProps');
expect(getParameterType(code)).toBe('HomeBannerProps & {_component: string | null;}');
});

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

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

expect(getParameterType(code)).toBe('boolean | HomeBannerProps');
expect(getParameterType(code)).toBe('boolean | (HomeBannerProps & {_component: string | null;})');
});

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

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

expect(getParameterType(code)).toBe('boolean | HomeBannerProps');
expect(getParameterType(code)).toBe('boolean | (HomeBannerProps & {_component: string | null;})');
});

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

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

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

it('should allow a renderer that accepts both the initial and fallback values for mapped slots', () => {
Expand Down
4 changes: 2 additions & 2 deletions src/components/Slot/index.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {Fragment, ReactElement, ReactNode} from 'react';
import {SlotContent, SlotId, VersionedSlotId, VersionedSlotMap} from '@croct/plug/slot';
import {SlotContent, VersionedSlotId, VersionedSlotMap} from '@croct/plug/slot';
import {JsonObject} from '@croct/plug/sdk/json';
import {useContent, UseContentOptions} from '../../hooks';

Expand Down Expand Up @@ -31,7 +31,7 @@ type SlotComponent = {

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

return <Fragment>{children(data)}</Fragment>;
};
62 changes: 52 additions & 10 deletions src/hooks/useContent.d.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,6 @@ import {create} from 'ts-node';
const tsService = create({
cwd: __dirname,
transpileOnly: false,
ignore: [
'lib/slots.d.ts',
],
});

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

const slotMapping = `
type HomeBannerProps = {
type HomeBanner = {
title: string,
subtitle: string,
};
type Banner = {
title: string,
subtitle: string,
};
type Carousel = {
title: string,
subtitle: string,
};
declare module '@croct/plug/slot' {
interface SlotMap {
'home-banner': HomeBannerProps;
interface VersionedSlotMap {
'home-banner': {
'latest': HomeBanner,
'1': HomeBanner,
};
}
}
declare module '@croct/plug/component' {
interface VersionedComponentMap {
'banner': {
'latest': Banner,
'1': Banner,
};
'carousel': {
'latest': Carousel,
'1': Carousel,
};
}
}
`;
Expand Down Expand Up @@ -99,6 +122,25 @@ describe('useContent typing', () => {
expect(getReturnType(code)).toBe('JsonObject');
});

it('should define the return type as an union of component for unknown slots', () => {
const code: CodeOptions = {
code: `
useContent('dynamic-id' as any);
`,
mapping: true,
};

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

expect(getTypeName(code)).toBe(
'useContent<any>',
);

expect(getReturnType(code)).toBe(
'(Banner & {_component: "banner@1";}) | (Carousel & {_component: "carousel@1";})',
);
});

it('should include the type of the initial value on the return type for unmapped slots', () => {
const code: CodeOptions = {
code: `
Expand Down Expand Up @@ -241,7 +283,7 @@ describe('useContent typing', () => {

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

expect(getReturnType(code)).toBe('HomeBannerProps');
expect(getReturnType(code)).toBe('HomeBanner & {_component: string | null;}');
});

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

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

expect(getReturnType(code)).toBe('boolean | HomeBannerProps');
expect(getReturnType(code)).toBe('boolean | (HomeBanner & {_component: string | null;})');
});

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

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

expect(getReturnType(code)).toBe('number | HomeBannerProps');
expect(getReturnType(code)).toBe('number | (HomeBanner & {_component: string | null;})');
});

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

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

expect(getReturnType(code)).toBe('number | boolean | HomeBannerProps');
expect(getReturnType(code)).toBe('number | boolean | (HomeBanner & {...;})');
});

it('should not allow overriding the return type for mapped slots', () => {
Expand Down
4 changes: 2 additions & 2 deletions src/hooks/useContent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export type UseContentOptions<I, F> = FetchOptions & {
function useCsrContent<I, F>(
id: VersionedSlotId,
options: UseContentOptions<I, F> = {},
): SlotContent<VersionedSlotId> | I | F {
): SlotContent | I | F {
const {fallback, initial, cacheKey, expiration, ...fetchOptions} = options;
const croct = useCroct();

Expand All @@ -31,7 +31,7 @@ function useCsrContent<I, F>(
function useSsrContent<I, F>(
_: VersionedSlotId,
{initial}: UseContentOptions<I, F> = {},
): SlotContent<VersionedSlotId> | I | F {
): SlotContent | I | F {
if (initial === undefined) {
throw new Error('The initial value is required for server-side rendering (SSR).');
}
Expand Down
3 changes: 0 additions & 3 deletions src/hooks/useEvaluation.d.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,6 @@ import {create} from 'ts-node';
const tsService = create({
cwd: __dirname,
transpileOnly: false,
ignore: [
'lib/slots.d.ts',
],
});

const testFilename = pathJoin(__dirname, 'test.tsx');
Expand Down
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
export * from '@croct/plug/sdk/json';
export * from '@croct/plug/slot';
export * from '@croct/plug/component';
export * from './CroctProvider';
export * from './hooks';
export * from './components';

0 comments on commit da66757

Please sign in to comment.