Skip to content

Commit

Permalink
feature(assets): Allow getting of asset by ID (#93)
Browse files Browse the repository at this point in the history
* feature(assets): Allow getting of asset by ID

* feature(get asset): EpochTimestamp to number

* feature(get assets): Replaced id with blank uid

* feature(assets): Added to readme
  • Loading branch information
jonnyAmplience committed Oct 13, 2023
1 parent 6bed99e commit f7020d2
Show file tree
Hide file tree
Showing 9 changed files with 157 additions and 0 deletions.
35 changes: 35 additions & 0 deletions README.md
Expand Up @@ -238,6 +238,41 @@ Example Content Item:
}
```

## Assets

Use to get assets.

```js
const sdk await init();

const asset = await sdk.assets.getById('123-456-789');
```

Example asset:

```json
{
"srcName": "an-image.jpg",
"revisionNumber": 8,
"bucketID": "00000000-0000-0000-0000-000000000000",
"label": "an-image.jpg",
"mimeType": "image/jpeg",
"type": "image",
"userID": "00000000-0000-0000-0000-000000000000",
"thumbFile": "00000000-0000-0000-0000-000000000000",
"folderID": "00000000-0000-0000-0000-000000000000",
"file": "00000000-0000-0000-0000-000000000000",
"createdDate": 1683798022161,
"name": "an-image",
"subType": null,
"id": "00000000-0000-0000-0000-000000000000",
"thumbURL": "https://thumbs.amplience.net/r/00000000-0000-0000-0000-000000000000",
"publishStatus": "PUBLISHED",
"status": "active",
"timestamp": 1688563779803
}
```

## Frame

### Setting the height
Expand Down
20 changes: 20 additions & 0 deletions src/lib/components/Asset.d.ts
@@ -0,0 +1,20 @@
export type Asset = {
srcName: string;
revisionNumber: number;
bucketID: string;
label: string;
mimeType: string;
type: string;
userID: string;
thumbFile: string;
folderID: string;
file: string;
createdDate: number;
name: string;
subType: string | null;
id: string;
thumbURL: string;
publishStatus: string;
status: string;
timestamp: number;
};
49 changes: 49 additions & 0 deletions src/lib/components/Assets.spec.ts
@@ -0,0 +1,49 @@
import { ClientConnection } from 'message-event-channel';
import { Assets } from './Assets';

const asset = {
srcName: 'an-image.jpg',
revisionNumber: 8,
bucketID: '00000000-0000-0000-0000-000000000000',
label: 'an-image.jpg',
mimeType: 'image/jpeg',
type: 'image',
userID: '00000000-0000-0000-0000-000000000000',
thumbFile: '00000000-0000-0000-0000-000000000000',
folderID: '00000000-0000-0000-0000-000000000000',
file: '00000000-0000-0000-0000-000000000000',
createdDate: 1683798022161,
name: 'an-image',
subType: null,
id: '00000000-0000-0000-0000-000000000000',
thumbURL: 'https://thumbs.amplience.net/r/00000000-0000-0000-0000-000000000000',
publishStatus: 'PUBLISHED',
status: 'active',
timestamp: 1688563779803,
};

describe('Assets', () => {
let connection: ClientConnection;
let assets: Assets;

beforeEach(() => {
connection = new ClientConnection();
assets = new Assets(connection);
});

describe('getById', () => {
it('Should get asset', async () => {
jest.spyOn(connection, 'request').mockResolvedValue(asset);

const response = await assets.getById('123-456-789');

expect(response).toBe(asset);
});
it('Should return an error if get asset fails', () => {
jest.spyOn(connection, 'request').mockRejectedValue(new Error('Could not get asset'));

// eslint-disable-next-line @typescript-eslint/no-floating-promises
expect(assets.getById('123-456-789')).rejects.toThrow('Could not get asset');
});
});
});
33 changes: 33 additions & 0 deletions src/lib/components/Assets.ts
@@ -0,0 +1,33 @@
import { ClientConnection } from 'message-event-channel';
import { ASSETS } from '../constants/Events';
import { Asset } from './Asset';

export class Assets {
/**
* Assets - Used for retrieving assets.
*
* @param connection message-event-channel connection
*/
constructor(private connection: ClientConnection) {}

/**
* Get an asset by id
*
*
* ### Example
* ```typescript
* try {
* const asset = await sdk.asset.getById('123-456');
*
* console.log(asset)
* } catch (e) {
* // Could not get asset
* ...
* }
*
* ```
*/
async getById(id: string): Promise<Asset | Error> {
return this.connection.request(ASSETS.GET_BY_ID, id);
}
}
4 changes: 4 additions & 0 deletions src/lib/constants/Events.ts
Expand Up @@ -46,3 +46,7 @@ export enum FRAME {
HEIGHT_GET = 'height-get',
HEIGHT_SET = 'height-set',
}

export enum ASSETS {
GET_BY_ID = 'get-asset-by-id',
}
Expand Up @@ -7,6 +7,7 @@ import { Frame } from '../../components/Frame';
import { ContentReference } from '../../components/ContentReference';
import { ContentLink } from '../../components/ContentLink';
import { MediaLink } from '../../components/MediaLink';
import { Assets } from '../../components/Assets';

describe('ContentEditorExtension', () => {
const context = {
Expand Down Expand Up @@ -64,6 +65,7 @@ describe('ContentEditorExtension', () => {

expect(instance.connection).toEqual(connection);

expect(instance.assets).toBeInstanceOf(Assets);
expect(instance.mediaLink).toBeInstanceOf(MediaLink);
expect(instance.contentLink).toBeInstanceOf(ContentLink);
expect(instance.contentReference).toBeInstanceOf(ContentReference);
Expand Down
6 changes: 6 additions & 0 deletions src/lib/extensions/content-editor/ContentEditorExtension.ts
Expand Up @@ -10,6 +10,7 @@ import { ContentEditorContextObject } from './ContentEditorContextObject';
import { FieldSchema } from '../../components/Field';
import { ObjectMap } from '../../models/ContentItemModel';
import { Hub } from '../dashboard/DashboardExtension';
import { Assets } from '../../components/Assets';

export type SchemaType = ObjectMap<{
id?: string;
Expand All @@ -27,6 +28,10 @@ export type SchemaType = ObjectMap<{
export class ContentEditorExtension<ParamType extends Params = Params> extends Extension<
ContentEditorContextObject<ParamType>
> {
/**
* Assets - Allows you to get assets
*/
public assets!: Assets;
/**
* Content Item - The model of the Content Item that is being edited.
*/
Expand Down Expand Up @@ -83,6 +88,7 @@ export class ContentEditorExtension<ParamType extends Params = Params> extends E
setupContext(context: ContentEditorContextObject<ParamType>): void {
const { schema, params, locales, stagingEnvironment, readOnly, visualisation, hub } = context;

this.assets = new Assets(this.connection);
this.contentItem = new ContentItem(this.connection);
this.schema = schema;
this.form = new ContentEditorForm(this.connection, readOnly);
Expand Down
Expand Up @@ -7,6 +7,7 @@ import { Frame } from '../../components/Frame';
import { ContentReference } from '../../components/ContentReference';
import { ContentLink } from '../../components/ContentLink';
import { MediaLink } from '../../components/MediaLink';
import { Assets } from '../../components/Assets';

describe('ContentFieldExtension', () => {
const options = {
Expand Down Expand Up @@ -59,6 +60,7 @@ describe('ContentFieldExtension', () => {
expect(instance.contentReference).toBeInstanceOf(ContentReference);
expect(instance.frame).toBeInstanceOf(Frame);

expect(instance.assets).toBeInstanceOf(Assets);
expect(instance.contentItem).toBeInstanceOf(ContentItem);
expect(instance.field).toBeInstanceOf(Field);
expect(instance.form).toBeInstanceOf(Form);
Expand Down
6 changes: 6 additions & 0 deletions src/lib/extensions/content-field/ContentFieldExtension.ts
Expand Up @@ -10,11 +10,16 @@ import { Params } from '../../models/Params';
import { Extension, ExtensionOptions } from '../Extension';
import { ContentFieldContextObject } from './ContentFieldContextObject';
import { Hub } from '../dashboard/DashboardExtension';
import { Assets } from '../../components/Assets';

export class ContentFieldExtension<
FieldType = {},
ParamType extends Params = Params
> extends Extension<ContentFieldContextObject<ParamType>> {
/**
* Assets - Allows you to get assets
*/
public assets!: Assets;
/**
* Content Item - The model of the Content Item that is being edited.
*/
Expand Down Expand Up @@ -84,6 +89,7 @@ export class ContentFieldExtension<
hub,
} = context;

this.assets = new Assets(this.connection);
this.contentItem = new ContentItem(this.connection);
this.field = new Field(this.connection, fieldSchema);
this.form = new Form(this.connection, readOnly);
Expand Down

0 comments on commit f7020d2

Please sign in to comment.