Skip to content

Commit

Permalink
Merge pull request #8 from amplience/release/1.1.0
Browse files Browse the repository at this point in the history
Release/1.1.0
  • Loading branch information
pixelscript authored May 7, 2020
2 parents 21daaed + 7a55b7d commit f224c20
Show file tree
Hide file tree
Showing 12 changed files with 259 additions and 26 deletions.
12 changes: 6 additions & 6 deletions package-lock.json

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

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@
]
},
"dependencies": {
"message-event-channel": "^1.0.1"
"message-event-channel": "^1.1.0"
},
"devDependencies": {
"@commitlint/cli": "^8.3.3",
Expand Down
40 changes: 39 additions & 1 deletion src/lib/ContentLink.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,38 @@ export interface ContentItemLink {
id: string;
contentType: string;
}

export interface CotnentLinkOptions {
max?: number | null;
}
export class ContentLink {
/**
* Content Link - Use to open the content browser.
* @param connection message-event-channel connection
*/
constructor(private connection: ClientConnection) {}
/**
* This method will trigger a content browser. It returns a promise that will resolve to the chosen Content References.
* @param contentTypeIds list of Content Type Ids to filter the Content Browser by.
* @param options.max Max amount of selected contnet links
*
* ### Example
* ```typescript
* const enums = sdk.field.schema.allOf[1].properties.contentType.enum;
* const contentLink = await sdk.contentLink.getMultiple(enums);
*
* console.log(contentLink);
* ```
*/
getMultiple(
contentTypeIds: Array<string>,
options: CotnentLinkOptions = { max: null }
): Promise<ContentItemLink[]> {
if (options.max === undefined) {
options.max = null;
}
return this.fetchLinks(contentTypeIds, options.max);
}
/**
* This method will trigger a content browser. It returns a promise that will resolve to the chosen Content Link.
* @param contentTypeIds list of Content Type Ids to filter the Content Browser by.
Expand All @@ -27,9 +53,21 @@ export class ContentLink {
* ```
*/
async get(contentTypeIds: Array<string>): Promise<ContentItemLink> {
return this.fetchLinks(contentTypeIds);
}

async fetchLinks(contentTypeIds: Array<string>, max?: number | null) {
if (!contentTypeIds || !Array.isArray(contentTypeIds) || !contentTypeIds.length) {
throw new Error(ERRORS_CONTENT_ITEM.NO_IDS);
}
return this.connection.request(CONTENT_LINK.CONTENT_GET, contentTypeIds, { timeout: false });

const response = {
contentTypeIds,
...((max !== undefined && { max }) || {})
};

return this.connection.request(CONTENT_LINK.CONTENT_GET, response, {
timeout: false
});
}
}
38 changes: 36 additions & 2 deletions src/lib/ContentReference.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,38 @@ export interface ContentItemReference {
contentType: string;
}

export interface ContentReferenceOptions {
max?: number | null;
}

export class ContentReference {
/**
* Content Link - Use to open the content browser.
* @param connection message-event-channel connection
*/
constructor(private connection: ClientConnection) {}

/**
* This method will trigger a content browser. It returns a promise that will resolve to the chosen Content Link.
* This method will trigger a content browser. It returns a promise that will resolve to the chosen Content References.
* @param contentTypeIds list of Content Type Ids to filter the Content Browser by.
* @param options.max Max amount of selected contnet references
*
* ### Example
* ```typescript
* const enums = sdk.field.schema.allOf[1].properties.contentType.enum;
* const contentRef = await sdk.contentReference.get(enums);
*
* console.log(contentRef);
* ```
*/
getMultiple(
contentTypeIds: Array<string>,
options: ContentReferenceOptions = { max: null }
): Promise<ContentItemReference[]> {
return this.fetchReferences(contentTypeIds, options.max);
}
/**
* This method will trigger a content browser. It returns a promise that will resolve to the chosen Content Reference.
* @param contentTypeIds list of Content Type Ids to filter the Content Browser by.
*
* ### Example
Expand All @@ -29,10 +53,20 @@ export class ContentReference {
* ```
*/
async get(contentTypeIds: Array<string>): Promise<ContentItemReference> {
return this.fetchReferences(contentTypeIds);
}

private fetchReferences(contentTypeIds: Array<string>, max?: number | null) {
if (!contentTypeIds || !Array.isArray(contentTypeIds) || !contentTypeIds.length) {
throw new Error(ERRORS_CONTENT_ITEM.NO_IDS);
}
return this.connection.request(CONTENT_REFERENCE.CONTENT_REF_GET, contentTypeIds, {

const response = {
contentTypeIds,
...((max !== undefined && { max }) || {})
};

return this.connection.request(CONTENT_REFERENCE.CONTENT_REF_GET, response, {
timeout: false
});
}
Expand Down
54 changes: 52 additions & 2 deletions src/lib/MediaLink.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ export interface MediaVideoLink {
schema: string;
};
}

export interface MediaOptions {
max: number | null;
}
export class MediaLink {
/**
* Media Link - Use to open the media browser.
Expand Down Expand Up @@ -70,7 +74,27 @@ export class MediaLink {
* ```
*/
getImage(): Promise<MediaImageLink> {
return this.connection.request(MEDIA_LINK.IMAGE_GET, null, { timeout: false });
return this.connection.request(MEDIA_LINK.IMAGE_GET, null, {
timeout: false
});
}
/**
* This method will trigger an image browser. It returns a promise that will resolve to the chosen Image Links.
*
* Example of fetching an image from DC
* ```typescript
* try {
* // open browser and waits for image
* // returns object that should be saved to the model
* const images = await sdk.mediaLink.getImages({ max: 10 })
*
* } catch (e) {
* // no image returned
* }
* ```
*/
getImages({ max }: MediaOptions = { max: null }): Promise<MediaImageLink[]> {
return this.connection.request(MEDIA_LINK.IMAGE_GET, { max }, { timeout: false });
}
/**
* This method will trigger a video browser. It returns a promise that will resolve to the chosen Video Link.
Expand All @@ -88,6 +112,32 @@ export class MediaLink {
* ```
*/
getVideo(): Promise<MediaVideoLink> {
return this.connection.request(MEDIA_LINK.VIDEO_GET, null, { timeout: false });
return this.connection.request(MEDIA_LINK.VIDEO_GET, null, {
timeout: false
});
}
/**
* This method will trigger a video browser. It returns a promise that will resolve to the chosen Video Links.
*
* Example of fetching an video from DC
* ```typescript
* try {
* // open browser and waits for video
* // returns object that should be saved to the model
* const videos = await sdk.mediaLink.getVideos()
*
* } catch (e) {
* // no video returned
* }
* ```
*/
getVideos({ max }: MediaOptions = { max: null }): Promise<MediaVideoLink[]> {
return this.connection.request(
MEDIA_LINK.VIDEO_GET,
{ max },
{
timeout: false
}
);
}
}
9 changes: 6 additions & 3 deletions src/lib/SDK.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,14 @@ export interface IClientConnection extends ClientConnection {}
*/
export interface OptionsObject {
window?: Window;
connectionTimeout?: number;
connectionTimeout?: number | boolean;
timeout?: number | boolean;
debug?: boolean;
}
export interface Options {
window: Window;
connectionTimeout: number;
connectionTimeout: number | boolean;
timeout: number | boolean;
debug: boolean;
}
export interface Params {
Expand Down Expand Up @@ -91,7 +93,8 @@ export class SDK<FieldType = any, ParamType extends Params = Params> {
protected options: Options;
protected readonly defaultOptions: Options = {
window: window,
connectionTimeout: 4500,
connectionTimeout: false,
timeout: false,
debug: false
};

Expand Down
41 changes: 38 additions & 3 deletions test/ContentLink.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { ClientConnection } from 'message-event-channel';
import { ContentLink } from '../src/lib/ContentLink';
import { CONTENT_LINK } from '../src/lib/Events';

describe('ContentItem', () => {
let connection: ClientConnection;
Expand All @@ -22,9 +23,13 @@ describe('ContentItem', () => {
.get(['123', '564'])
.then()
.catch();
expect(connection.request).toHaveBeenCalledWith('content-link-get', ['123', '564'], {
timeout: false
});
expect(connection.request).toHaveBeenCalledWith(
'content-link-get',
{ contentTypeIds: ['123', '564'] },
{
timeout: false
}
);
});

it('should throw an error if no ids are passed', () => {
Expand All @@ -44,4 +49,34 @@ describe('ContentItem', () => {
.catch();
expect(connection.request).not.toHaveBeenCalled();
});

it('should beable to return multiple items', () => {
spyOn(connection, 'request');

contentLink.getMultiple(['123']);

expect(connection.request).toHaveBeenCalledWith(
CONTENT_LINK.CONTENT_GET,
{
contentTypeIds: ['123'],
max: null
},
{ timeout: false }
);
});

it('should set max to number if passed', () => {
spyOn(connection, 'request');

contentLink.getMultiple(['123'], { max: 2 });

expect(connection.request).toHaveBeenCalledWith(
CONTENT_LINK.CONTENT_GET,
{
contentTypeIds: ['123'],
max: 2
},
{ timeout: false }
);
});
});
41 changes: 38 additions & 3 deletions test/ContentReference.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { ClientConnection } from 'message-event-channel';
import { ContentReference } from '../src/lib/ContentReference';
import { CONTENT_REFERENCE } from '../src/lib/Events';

describe('ContentReference', () => {
let connection: ClientConnection;
Expand All @@ -22,9 +23,13 @@ describe('ContentReference', () => {
.get(['123', '564'])
.then()
.catch();
expect(connection.request).toHaveBeenCalledWith('content-reference-get', ['123', '564'], {
timeout: false
});
expect(connection.request).toHaveBeenCalledWith(
'content-reference-get',
{ contentTypeIds: ['123', '564'] },
{
timeout: false
}
);
});

it('should throw an error if no ids are passed', () => {
Expand All @@ -44,4 +49,34 @@ describe('ContentReference', () => {
.catch();
expect(connection.request).not.toHaveBeenCalled();
});

it('should beable to return multiple items', () => {
spyOn(connection, 'request');

contentReference.getMultiple(['123']);

expect(connection.request).toHaveBeenCalledWith(
CONTENT_REFERENCE.CONTENT_REF_GET,
{
contentTypeIds: ['123'],
max: null
},
{ timeout: false }
);
});

it('should set max to number if passed', () => {
spyOn(connection, 'request');

contentReference.getMultiple(['123'], { max: 2 });

expect(connection.request).toHaveBeenCalledWith(
CONTENT_REFERENCE.CONTENT_REF_GET,
{
contentTypeIds: ['123'],
max: 2
},
{ timeout: false }
);
});
});
3 changes: 2 additions & 1 deletion test/Frame.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,8 +126,9 @@ describe('Frame', () => {
});

describe('Frame.startAutoResizer()', () => {
xit('should start auto resizer', done => {
it('should start auto resizer', done => {
const frame: Frame = new Frame(connection);
body.style.height = '0px';
const emitSpy = spyOn(connection, 'emit');
frame.startAutoResizer();
body.style.height = '100px';
Expand Down
Loading

0 comments on commit f224c20

Please sign in to comment.