Skip to content

Commit

Permalink
feat: Adds support for fetching assets by codename (fixes #124)
Browse files Browse the repository at this point in the history
  • Loading branch information
Enngage committed Nov 1, 2023
1 parent 42cc023 commit 6ee4603
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 42 deletions.
20 changes: 10 additions & 10 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
{
"singleQuote": true,
"printWidth": 120,
"proseWrap": "always",
"tabWidth": 4,
"requireConfig": false,
"useTabs": false,
"trailingComma": "none",
"bracketSpacing": true,
"semi": true
}
"singleQuote": true,
"printWidth": 120,
"proseWrap": "always",
"tabWidth": 4,
"requireConfig": false,
"useTabs": false,
"trailingComma": "none",
"bracketSpacing": true,
"semi": true
}
8 changes: 5 additions & 3 deletions lib/models/identifiers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ export namespace Identifiers {

export enum AssetIdentifierEnum {
InternalId = 'internalId',
ExternalId = 'externalId'
ExternalId = 'externalId',
Codename = 'codename'
}

export enum WebhookIdentifierEnum {
Expand All @@ -72,7 +73,6 @@ export namespace Identifiers {
Codename = 'codename'
}


export class AssetIdentifier {
constructor(public identifier: AssetIdentifierEnum, public value: string) {}

Expand All @@ -83,6 +83,9 @@ export namespace Identifiers {
if (this.identifier === AssetIdentifierEnum.ExternalId) {
return `external-id/${this.value}`;
}
if (this.identifier === AssetIdentifierEnum.Codename) {
return `codename/${this.value}`;
}
throw Error(`Unsupported identifier '${this.identifier}'`);
}
}
Expand Down Expand Up @@ -279,5 +282,4 @@ export namespace Identifiers {
throw Error(`Unsupported identifier '${this.identifier}'`);
}
}

}
30 changes: 13 additions & 17 deletions lib/queries/assets/view-assets-query.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,23 @@


import { IManagementClientConfig } from '../../config';
import { Identifiers } from '../../models';
import { AssetResponses } from '../../responses';
import { ManagementQueryService } from '../../services';
import { BaseQuery } from '../base-query';

export class ViewAssetsQuery extends BaseQuery<AssetResponses.ViewAssetResponse> {
constructor(
protected config: IManagementClientConfig,
protected queryService: ManagementQueryService,
public identifier: Identifiers.AssetIdentifier
) {
super(config, queryService);
}

constructor(
protected config: IManagementClientConfig,
protected queryService: ManagementQueryService,
public identifier: Identifiers.AssetIdentifier,
) {
super(config, queryService);
}
toPromise(): Promise<AssetResponses.ViewAssetResponse> {
return this.queryService.viewAssetAsync(this.getUrl(), this.queryConfig);
}

toPromise(): Promise<AssetResponses.ViewAssetResponse> {
return this.queryService.viewAssetAsync(this.getUrl(), this.queryConfig);
}

protected getAction(): string {
return this.apiEndpoints.viewAsset(this.identifier);
}
protected getAction(): string {
return this.apiEndpoints.viewAsset(this.identifier);
}
}

43 changes: 31 additions & 12 deletions lib/query-builders/asset-identifier-query.class.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,30 +3,49 @@ import { Identifiers } from '../models';
import { ManagementQueryService } from '../services';

export class AssetIdentifierQuery<TResult> {

constructor(
protected config: IManagementClientConfig,
protected queryService: ManagementQueryService,
protected buildResult: (
config: IManagementClientConfig,
queryService: ManagementQueryService,
identifier: Identifiers.AssetIdentifier) => TResult
) {
}
identifier: Identifiers.AssetIdentifier
) => TResult
) {}

/**
* Gets using internal Id
* @param id Internal Id of content item
*/
* Gets asset using internal Id
* @param id Internal Id of the asset
*/
byAssetId(id: string): TResult {
return this.buildResult(this.config, this.queryService, new Identifiers.AssetIdentifier(Identifiers.AssetIdentifierEnum.InternalId, id));
return this.buildResult(
this.config,
this.queryService,
new Identifiers.AssetIdentifier(Identifiers.AssetIdentifierEnum.InternalId, id)
);
}

/**
* Gets query using external Id
* @param id External Id of content item
*/
* Gets asset using external Id
* @param id External Id of the asset
*/
byAssetExternalId(id: string): TResult {
return this.buildResult(this.config, this.queryService, new Identifiers.AssetIdentifier(Identifiers.AssetIdentifierEnum.ExternalId, id));
return this.buildResult(
this.config,
this.queryService,
new Identifiers.AssetIdentifier(Identifiers.AssetIdentifierEnum.ExternalId, id)
);
}

/**
* Gets asset using codename
* @param id Codename of the asset
*/
byAssetCodename(id: string): TResult {
return this.buildResult(
this.config,
this.queryService,
new Identifiers.AssetIdentifier(Identifiers.AssetIdentifierEnum.Codename, id)
);
}
}
4 changes: 4 additions & 0 deletions test/browser/assets/view-asset.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,15 @@ describe('View assets', () => {
it(`url should be correct`, () => {
const internalIdUrl = cmLiveClient.viewAsset().byAssetId('xInternalId').getUrl();
const externalIdUrl = cmLiveClient.viewAsset().byAssetExternalId('xExternalId').getUrl();
const codenameUrl = cmLiveClient.viewAsset().byAssetCodename('xCodename').getUrl();

expect(internalIdUrl).toEqual(`https://manage.kontent.ai/v2/projects/${testEnvironmentId}/assets/xInternalId`);
expect(externalIdUrl).toEqual(
`https://manage.kontent.ai/v2/projects/${testEnvironmentId}/assets/external-id/xExternalId`
);
expect(codenameUrl).toEqual(
`https://manage.kontent.ai/v2/projects/${testEnvironmentId}/assets/codename/xCodename`
);
});

it(`response should be instance of ViewAssetResponse class`, () => {
Expand Down

0 comments on commit 6ee4603

Please sign in to comment.