Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Resources: Deal with inferring resource types from widgets with custom properties #891

Merged
merged 1 commit into from
Mar 23, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 8 additions & 12 deletions src/core/middleware/resources.ts
Original file line number Diff line number Diff line change
Expand Up @@ -245,20 +245,16 @@ type WidgetFactory<T extends WNodeFactoryTypes> =
| WNodeFactory<T>
| OptionalWNodeFactory<T>;

type WidgetResourceData<W extends WidgetFactory<any>> = W extends WidgetFactory<
WNodeFactoryTypes<ResourceProperties<infer D, any>>
>
? D
type WidgetResourceData<W extends WidgetFactory<any>> = W extends WidgetFactory<WNodeFactoryTypes<infer P>>
? P extends ResourceProperties<infer D, any> ? D : void
: void;
type WidgetResourceApi<W extends WidgetFactory<any>> = W extends WidgetFactory<
WNodeFactoryTypes<ResourceProperties<infer D, infer R>>
>
? R extends CustomTemplate ? R : DefaultApi
type WidgetResourceApi<W extends WidgetFactory<any>> = W extends WidgetFactory<WNodeFactoryTypes<infer P>>
? P extends ResourceProperties<any, infer R> ? (R extends CustomTemplate ? R : DefaultApi) : DefaultApi
: DefaultApi;
type WidgetResourceTemplateApi<W extends WidgetFactory<any>> = W extends WidgetFactory<
WNodeFactoryTypes<ResourceProperties<infer D, infer R>>
>
? R extends CustomTemplate ? CustomTemplateApi<R, D> : CustomTemplateApi<DefaultApi, D>
type WidgetResourceTemplateApi<W extends WidgetFactory<any>> = W extends WidgetFactory<WNodeFactoryTypes<infer P>>
? P extends ResourceProperties<infer D, infer R>
? R extends CustomTemplate ? CustomTemplateApi<R, D> : CustomTemplateApi<DefaultApi, D>
: CustomTemplateApi<DefaultApi, WidgetResourceData<W>>
: CustomTemplateApi<DefaultApi, WidgetResourceData<W>>;

export function createResourceTemplate<
Expand Down
2 changes: 1 addition & 1 deletion tests/core/unit/middleware/resources.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ jsdomDescribe('Resources Middleware', () => {

it('Should be able to infer the resource data from widget', () => {
const resource = createResourceMiddleware<TestData>();
const factory = create({ resource });
const factory = create({ resource }).properties<{ foo?: string }>();
const Widget = factory(function App({ properties, middleware: { resource } }) {
const {
resource: { template }
Expand Down