ItemConsulting / enonic-types

TypeScript types for Enonic XP
MIT License
8 stars 2 forks source link

Possible error with content.data.media.attachment using <Content> #10

Closed Bellfalasch closed 3 years ago

Bellfalasch commented 3 years ago

Hi,

If I use this library to validate Content that is a media-type content it looks like it doesn't except .media.attachment as something to exist within content.data. But if you upload an SVG to Content Studio, it stores some data here. Today we bypass this with a @ts-ignore.

import { Content, ContentLibrary } from 'enonic-types/content';
const libContent: ContentLibrary = __non_webpack_require__('/lib/xp/content');

// ...

const attachmentContent: Content = libPortal.getContent<Content>();
log.info(attachmentContent?.data?.media?.attachment); // ts linter doesn't like this row

Is this expected? Maybe I'm using it wrong. I'm using 1.4.0 of this lib, but used 0.0.74 before and recently upgrading didn't solve the problem.

Skjermbilde 2020-11-11 kl  10 23 47
tajakobsen commented 3 years ago

Hi,

I think the problem might be that libPortal.getContent() shouldn't take Content as it's type parameter. Try with Image instead.

Can this be what you are looking for?

import { Content, ContentLibrary, Image } from 'enonic-types/content';

const libContent: ContentLibrary = __non_webpack_require__('/lib/xp/content');

// ...

// const attachmentContent: Content<Image> | null = libPortal.getContent<Image>();
const attachmentContent: Content<Image> | null = libContent.get<Image>({ key: imageId });
log.info(attachmentContent?.data.media.attachment);
tajakobsen commented 3 years ago

Another little tip. :wink:

If you redefine __non_webpack_require__() to the one on the readme, you shouldn't need setting the ContentLibrary type for libContent explicitly since the return type always is correct.

And you can get type competion on the string parameter for __non_webpack_require__().