Skip to content

Commit

Permalink
feat(CMP-5384): Moved content reference from media to content/models
Browse files Browse the repository at this point in the history
Wanting review on any helper functions a content reference would benefit from, maybe being able to request the reference?
  • Loading branch information
dev-warner committed Jun 28, 2019
1 parent 150aa08 commit 0910b3f
Show file tree
Hide file tree
Showing 8 changed files with 49 additions and 28 deletions.
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ export * from './lib/ContentClientConfig';
export * from './lib/content/model/ContentBody';
export * from './lib/content/model/ContentItem';
export * from './lib/content/model/ContentLifecycle';
export * from './lib/content/model/ContentReference';
export * from './lib/content/model/ContentMeta';
export * from './lib/content/model/Edition';
export * from './lib/content/model/FragmentMeta';
Expand All @@ -14,6 +15,5 @@ export * from './lib/media/Image';
export * from './lib/media/ImageUrlBuilder';
export * from './lib/media/model/ImageFormat';
export * from './lib/media/Video';
export * from './lib/media/ContentReference';

export * from './lib/rendering/model/RenderedContentItem';
5 changes: 2 additions & 3 deletions src/lib/content/mapper/ContentMapper.spec.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import { expect } from 'chai';
import { ContentMapper } from './ContentMapper';
import { ContentMeta } from '../model/ContentMeta';
import { ContentMeta, ContentReferenceMeta } from '../model/ContentMeta';
import { Image } from '../../media/Image';
import { Video } from '../../media/Video';
import { ContentReference } from '../../media/ContentReference';
import { ContentReferenceMeta } from '../../media/MediaMeta';
import { ContentReference } from '../model/ContentReference';

const config = {
account: 'test'
Expand Down
4 changes: 2 additions & 2 deletions src/lib/content/mapper/ContentMapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { ContentMeta } from '../model/ContentMeta';
import { ContentClientConfig } from '../../ContentClientConfig';
import { Image } from '../../media/Image';
import { Video } from '../../media/Video';
import { ContentReference } from '../../media/ContentReference';
import { ContentReference } from '../model/ContentReference';

/**
* @hidden
Expand Down Expand Up @@ -113,7 +113,7 @@ export class ContentMapper {
*/
protected convertContentReference(fragment: any): any {
if (ContentReference.isContentReference(fragment)) {
const result = new ContentReference(fragment, this.config);
const result = new ContentReference(fragment);
return result;
}
}
Expand Down
9 changes: 9 additions & 0 deletions src/lib/content/model/ContentMeta.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,15 @@ import { Edition } from './Edition';
import { ContentLifecycle } from './ContentLifecycle';
import { FragmentMeta } from './FragmentMeta';

/**
* Class providing meta data about an Content Reference resource.
*/
export class ContentReferenceMeta extends FragmentMeta {
constructor(data?: any) {
super(data);
}
}

/**
* Class providing meta data about a content item with helper functions.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ describe('Content Reference', () => {
name: 'content ref'
};

const content = new ContentReference(json, { account: 'test' });
const content = new ContentReference(json);
expect(content.toJSON()).to.deep.eq(json);
});

Expand All @@ -28,7 +28,7 @@ describe('Content Reference', () => {
contentType: 'http://some-content-type.com/type.json'
};

const content = new ContentReference(json, { account: 'test' });
const content = new ContentReference(json);
expect(content.toJSON()).to.deep.eq(json);
});
});
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,32 @@
import { Media } from './Media';
import { ContentReferenceMeta } from './MediaMeta';
import { ContentClientConfig } from '../ContentClientConfig';
import { FragmentMeta } from '../content/model/FragmentMeta';
import { FragmentMeta } from './FragmentMeta';
import { ContentReferenceMeta } from './ContentMeta';

export class ContentReference extends Media {
export type ContentReferenceSchema = 'http://bigcontent.io/cms/schema/v1/core#/definitions/content-reference';

/**
* Required params for creating an content reference
*/
export type RequriedContentReference<T extends {} = {}> = T & {
id: string;
name: string;
contentType?: string;
_meta?: {
schema: ContentReferenceSchema;
};
};

/**
* Class representing an Content Reference.
*/
export class ContentReference {

id: string;
name: string;
contentType: string;
_meta: ContentReferenceMeta;

constructor(data: any, config: ContentClientConfig) {
super(data, config);
constructor(data: RequriedContentReference) {
Object.assign(this, data);

if (data && data._meta) {
this._meta = new ContentReferenceMeta(data._meta);
Expand All @@ -22,8 +41,11 @@ export class ContentReference extends Media {
* Export ContentReference to JSON
*/
toJSON(): any {
const json = super.toJSON();
const { defaultHost, endpoint, ...result } = json;
const { name, id } = this;
const result: RequriedContentReference = {
id,
name
};

if (this._meta) {
result._meta = this._meta.toJSON();
Expand Down
4 changes: 2 additions & 2 deletions src/lib/media/Media.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { ContentClientConfig } from '../ContentClientConfig';
import { ImageMeta, VideoMeta, ContentReferenceMeta } from './MediaMeta';
import { ImageMeta, VideoMeta } from './MediaMeta';

/**
* @hidden
Expand All @@ -9,7 +9,7 @@ export abstract class Media {
/**
* Metadata about the Media object
*/
_meta: ImageMeta | VideoMeta | ContentReferenceMeta;
_meta: ImageMeta | VideoMeta;

/**
* Default host name to use when constructing a URL to the resource.
Expand Down
9 changes: 0 additions & 9 deletions src/lib/media/MediaMeta.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,6 @@ export class ImageMeta extends MediaMeta {
}
}

/**
* Class providing meta data about an Content Reference resource.
*/
export class ContentReferenceMeta extends MediaMeta {
constructor(data?: any) {
super(data);
}
}

/**
* Class providing meta data about a Video resource.
*/
Expand Down

0 comments on commit 0910b3f

Please sign in to comment.