From 6f8c7a5abcdc5fc334c7b2514f66ec7c951f5db7 Mon Sep 17 00:00:00 2001 From: Richard Sustek Date: Mon, 31 Jul 2023 11:02:21 +0200 Subject: [PATCH] feat: adds support for 'getPreviewConfiguration' API endpoint (https://github.com/kontent-ai/management-sdk-js/issues/95) --- lib/client/imanagement-client.interface.ts | 7 ++- lib/client/management-client.class.ts | 28 ++++++------ lib/contracts/index.ts | 1 + lib/contracts/preview-contracts.ts | 29 ++++++++++++ lib/mappers/index.ts | 1 + lib/mappers/preview-mapper.ts | 44 ++++++++++++++++++ .../content-management-api-endpoints.ts | 4 ++ lib/models/index.ts | 3 +- lib/models/preview/preview.models.ts | 45 +++++++++++++++++++ .../{space-models.ts => space.models.ts} | 0 lib/queries/index.ts | 1 + .../get-preview-configuration-query.class.ts | 18 ++++++++ lib/queries/preview/index.ts | 2 + ...odify-preview-configuration-query.class.ts | 17 +++++++ lib/responses/index.ts | 1 + lib/responses/preview/preview-responses.ts | 18 ++++++++ .../management-query-service.class.ts | 18 ++++++-- .../fake-get-preview-configuration.json | 42 +++++++++++++++++ .../preview/get-preview-configuration.spec.ts | 31 +++++++++++++ 19 files changed, 291 insertions(+), 19 deletions(-) create mode 100644 lib/contracts/preview-contracts.ts create mode 100644 lib/mappers/preview-mapper.ts create mode 100644 lib/models/preview/preview.models.ts rename lib/models/spaces/{space-models.ts => space.models.ts} (100%) create mode 100644 lib/queries/preview/get-preview-configuration-query.class.ts create mode 100644 lib/queries/preview/index.ts create mode 100644 lib/queries/preview/modify-preview-configuration-query.class.ts create mode 100644 lib/responses/preview/preview-responses.ts create mode 100644 test/browser/fake-responses/preview/fake-get-preview-configuration.json create mode 100644 test/browser/preview/get-preview-configuration.spec.ts diff --git a/lib/client/imanagement-client.interface.ts b/lib/client/imanagement-client.interface.ts index 5c3b75c..4385705 100644 --- a/lib/client/imanagement-client.interface.ts +++ b/lib/client/imanagement-client.interface.ts @@ -128,7 +128,8 @@ import { StartEnvironmentValidationQuery, TaskIdentifierQuery, CheckEnvironmentValidationQuery, - ListEnvironmentValidationIssuesQuery + ListEnvironmentValidationIssuesQuery, + GetPreviewConfigurationQuery } from '../queries'; import { IMappingService } from '../services'; import { GetEnvironmentCloningStateQuery } from '../queries/environments'; @@ -696,4 +697,8 @@ export interface IManagementClient { */ viewSpace(): SpaceIdentifierQuery; + /** + * Gets preview configuration + */ + getPreviewConfiguration(): GetPreviewConfigurationQuery; } diff --git a/lib/client/management-client.class.ts b/lib/client/management-client.class.ts index c1ad684..f467ebc 100644 --- a/lib/client/management-client.class.ts +++ b/lib/client/management-client.class.ts @@ -9,7 +9,7 @@ import { } from '../models'; import { IManagementClientConfig } from '../config'; -import { ContentItemContracts, LanguageVariantContracts} from '../contracts'; +import { ContentItemContracts, LanguageVariantContracts } from '../contracts'; import { AssetFolderModels, AssetModels, @@ -132,7 +132,8 @@ import { ListSpacesQuery, ModifySpaceQuery, ViewSpaceQuery, - SpaceIdentifierQuery + SpaceIdentifierQuery, + GetPreviewConfigurationQuery } from '../queries'; import { sdkInfo } from '../sdk-info.generated'; import { ManagementQueryService, IMappingService, MappingService } from '../services'; @@ -525,7 +526,6 @@ export class ManagementClient implements IManagementClient { DataQuery< UpsertLanguageVariantQuery, (builder: LanguageVariantElementsBuilder) => LanguageVariantContracts.IUpsertLanguageVariantPostContract - > > > { @@ -533,9 +533,9 @@ export class ManagementClient implements IManagementClient { LanguageIdAndCodenameIdentifierQuery< DataQuery< UpsertLanguageVariantQuery, - (builder: LanguageVariantElementsBuilder) => LanguageVariantContracts.IUpsertLanguageVariantPostContract - - + ( + builder: LanguageVariantElementsBuilder + ) => LanguageVariantContracts.IUpsertLanguageVariantPostContract > > >( @@ -548,7 +548,6 @@ export class ManagementClient implements IManagementClient { ( builder: LanguageVariantElementsBuilder ) => LanguageVariantContracts.IUpsertLanguageVariantPostContract - > >( config, @@ -559,7 +558,6 @@ export class ManagementClient implements IManagementClient { ( builder: LanguageVariantElementsBuilder ) => LanguageVariantContracts.IUpsertLanguageVariantPostContract - >( nConfig, nQueryService, @@ -613,7 +611,8 @@ export class ManagementClient implements IManagementClient { return new TaskIdentifierQuery( this.config, this.queryService, - (config, queryService, identifier) => new ListEnvironmentValidationIssuesQuery(config, queryService, identifier) + (config, queryService, identifier) => + new ListEnvironmentValidationIssuesQuery(config, queryService, identifier) ); } @@ -1146,6 +1145,10 @@ export class ManagementClient implements IManagementClient { ); } + getPreviewConfiguration(): GetPreviewConfigurationQuery { + return new GetPreviewConfigurationQuery(this.config, this.queryService); + } + inviteUser(): DataQuery { return new DataQuery( this.config, @@ -1223,17 +1226,14 @@ export class ManagementClient implements IManagementClient { } modifySpace(): SpaceIdentifierQuery> { - return new SpaceIdentifierQuery< - DataQuery - >( + return new SpaceIdentifierQuery>( this.config, this.queryService, (config, queryService, identifier) => new DataQuery( config, queryService, - (nConfig, nQueryService, data) => - new ModifySpaceQuery(nConfig, nQueryService, identifier, data) + (nConfig, nQueryService, data) => new ModifySpaceQuery(nConfig, nQueryService, identifier, data) ) ); } diff --git a/lib/contracts/index.ts b/lib/contracts/index.ts index e88aded..41eaad9 100644 --- a/lib/contracts/index.ts +++ b/lib/contracts/index.ts @@ -18,3 +18,4 @@ export * from './project-user-contracts'; export * from './environment-contracts'; export * from './asset-rendition-contracts'; export * from './space-contracts'; +export * from './preview-contracts'; diff --git a/lib/contracts/preview-contracts.ts b/lib/contracts/preview-contracts.ts new file mode 100644 index 0000000..081c4b9 --- /dev/null +++ b/lib/contracts/preview-contracts.ts @@ -0,0 +1,29 @@ +export namespace PreviewContracts { + export interface IPreviewSpaceContract { + id: string; + } + + export interface IPreviewContentTypeContract { + id: string; + } + + export interface IPreviewUrlPatternContract { + space: IPreviewSpaceContract | null; + url_pattern: string; + } + + export interface IPreviewUrlPatternsContract { + content_type: IPreviewContentTypeContract; + url_patterns: IPreviewUrlPatternContract[]; + } + + export interface IPreviewSpaceDomainContract { + space: IPreviewSpaceContract; + domain: string; + } + + export interface IPreviewConfigurationContract { + space_domains: IPreviewSpaceDomainContract[]; + preview_url_patterns: IPreviewUrlPatternsContract[]; + } +} diff --git a/lib/mappers/index.ts b/lib/mappers/index.ts index 3c7b9cf..98d4d63 100644 --- a/lib/mappers/index.ts +++ b/lib/mappers/index.ts @@ -18,3 +18,4 @@ export * from './role-mapper'; export * from './project-user-mapper'; export * from './asset-rendition-mapper'; export * from './space-mapper'; +export * from './preview-mapper'; diff --git a/lib/mappers/preview-mapper.ts b/lib/mappers/preview-mapper.ts new file mode 100644 index 0000000..bd464af --- /dev/null +++ b/lib/mappers/preview-mapper.ts @@ -0,0 +1,44 @@ +import { IResponse } from '@kontent-ai/core-sdk'; + +import { PreviewContracts } from '../contracts'; +import { PreviewModels } from '../models'; +import { BaseMapper } from './base-mapper'; +import { PreviewResponses } from '../responses/preview/preview-responses'; + +export class PreviewMapper extends BaseMapper { + mapPreviewConfigurationResponse( + response: IResponse + ): PreviewResponses.PreviewConfigurationResponse { + return new PreviewResponses.PreviewConfigurationResponse( + super.mapResponseDebug(response), + response.data, + this.mapPreviewConfiguration(response.data) + ); + } + + private mapPreviewConfiguration( + rawItem: PreviewContracts.IPreviewConfigurationContract + ): PreviewModels.PreviewConfiguration { + return new PreviewModels.PreviewConfiguration({ + _raw: rawItem, + previewUrlPatterns: rawItem.preview_url_patterns.map((urlPatternsContract) => { + const urlPatterns: PreviewModels.IPreviewUrlPatterns = { + contentType: urlPatternsContract.content_type, + urlPatterns: urlPatternsContract.url_patterns.map((urlPatternContract) => { + const urlPattern: PreviewModels.IPreviewUrlPattern = { + space: urlPatternContract.space, + urlPattern: urlPatternContract.url_pattern + }; + + return urlPattern; + }) + }; + + return urlPatterns; + }), + spaceDomains: rawItem.space_domains + }); + } +} + +export const previewMapper = new PreviewMapper(); diff --git a/lib/models/content-management-api-endpoints.ts b/lib/models/content-management-api-endpoints.ts index c93fd85..5e232e0 100644 --- a/lib/models/content-management-api-endpoints.ts +++ b/lib/models/content-management-api-endpoints.ts @@ -350,6 +350,10 @@ export class ContentManagementApiEndpoints { return `${this.getEnvironmentsPath()}/roles/${identifier.getParamValue()}`; } + getPreviewConfigruation(): string { + return `${this.getEnvironmentsPath()}/preview-configuration`; + } + inviteProjectUser(): string { return `${this.getEnvironmentsPath()}/users`; } diff --git a/lib/models/index.ts b/lib/models/index.ts index d1a05a8..1bda761 100644 --- a/lib/models/index.ts +++ b/lib/models/index.ts @@ -24,4 +24,5 @@ export * from './project-users/project-users.models'; export * from './asset-renditions/asset-rendition.models'; export * from './environments/environment.models'; export * from './assets/asset-elements.builder'; -export * from './spaces/space-models'; +export * from './spaces/space.models'; +export * from './preview/preview.models'; diff --git a/lib/models/preview/preview.models.ts b/lib/models/preview/preview.models.ts new file mode 100644 index 0000000..2c7f47f --- /dev/null +++ b/lib/models/preview/preview.models.ts @@ -0,0 +1,45 @@ +import { SharedModels } from '../shared/shared-models'; +import { PreviewContracts } from '../../contracts'; + +export namespace PreviewModels { + export interface IPreviewSpace { + id: string; + } + + export interface IPreviewContentType { + id: string; + } + + export interface IPreviewUrlPattern { + space: IPreviewSpace | null; + urlPattern: string; + } + + export interface IPreviewUrlPatterns { + contentType: IPreviewContentType; + urlPatterns: IPreviewUrlPattern[]; + } + + export interface IPreviewSpaceDomain { + space: IPreviewSpace; + domain: string; + } + + export class PreviewConfiguration + implements SharedModels.IBaseModel + { + public spaceDomains: IPreviewSpaceDomain[]; + public previewUrlPatterns: IPreviewUrlPatterns[]; + public _raw: PreviewContracts.IPreviewConfigurationContract; + + constructor(data: { + spaceDomains: IPreviewSpaceDomain[]; + previewUrlPatterns: IPreviewUrlPatterns[]; + _raw: PreviewContracts.IPreviewConfigurationContract; + }) { + this.spaceDomains = data.spaceDomains; + this.previewUrlPatterns = data.previewUrlPatterns; + this._raw = data._raw; + } + } +} diff --git a/lib/models/spaces/space-models.ts b/lib/models/spaces/space.models.ts similarity index 100% rename from lib/models/spaces/space-models.ts rename to lib/models/spaces/space.models.ts diff --git a/lib/queries/index.ts b/lib/queries/index.ts index 2e449f1..d275693 100644 --- a/lib/queries/index.ts +++ b/lib/queries/index.ts @@ -19,3 +19,4 @@ export * from './project-users'; export * from './asset-renditions'; export * from './spaces'; export * from './environments'; +export * from './preview'; diff --git a/lib/queries/preview/get-preview-configuration-query.class.ts b/lib/queries/preview/get-preview-configuration-query.class.ts new file mode 100644 index 0000000..d489af3 --- /dev/null +++ b/lib/queries/preview/get-preview-configuration-query.class.ts @@ -0,0 +1,18 @@ +import { PreviewResponses } from '../../responses'; +import { IManagementClientConfig } from '../../config'; +import { ManagementQueryService } from '../../services'; +import { BaseQuery } from '../base-query'; + +export class GetPreviewConfigurationQuery extends BaseQuery { + constructor(protected config: IManagementClientConfig, protected queryService: ManagementQueryService) { + super(config, queryService); + } + + toPromise(): Promise { + return this.queryService.getPreviewConfigurationAsync(this.getUrl(), this.queryConfig); + } + + protected getAction(): string { + return this.apiEndpoints.getPreviewConfigruation(); + } +} diff --git a/lib/queries/preview/index.ts b/lib/queries/preview/index.ts new file mode 100644 index 0000000..0d07f64 --- /dev/null +++ b/lib/queries/preview/index.ts @@ -0,0 +1,2 @@ +export * from './get-preview-configuration-query.class'; +export * from './modify-preview-configuration-query.class'; diff --git a/lib/queries/preview/modify-preview-configuration-query.class.ts b/lib/queries/preview/modify-preview-configuration-query.class.ts new file mode 100644 index 0000000..21cc470 --- /dev/null +++ b/lib/queries/preview/modify-preview-configuration-query.class.ts @@ -0,0 +1,17 @@ +import { IManagementClientConfig } from '../../config'; +import { ManagementQueryService } from '../../services'; +import { BaseQuery } from '../base-query'; + +export class ModifyPreviewConfigurationQuery extends BaseQuery { + constructor(protected config: IManagementClientConfig, protected queryService: ManagementQueryService) { + super(config, queryService); + } + + toPromise(): Promise { + return this.queryService.getPreviewConfigurationAsync(this.getUrl(), this.queryConfig); + } + + protected getAction(): string { + return this.apiEndpoints.getPreviewConfigruation(); + } +} diff --git a/lib/responses/index.ts b/lib/responses/index.ts index db023fe..bf4bccd 100644 --- a/lib/responses/index.ts +++ b/lib/responses/index.ts @@ -17,3 +17,4 @@ export * from './project-users/project-users-responses'; export * from './asset-renditions/asset-rendition-responses'; export * from './spaces/space-responses'; export * from './environments/environment-responses'; +export * from './preview/preview-responses'; diff --git a/lib/responses/preview/preview-responses.ts b/lib/responses/preview/preview-responses.ts new file mode 100644 index 0000000..86adb63 --- /dev/null +++ b/lib/responses/preview/preview-responses.ts @@ -0,0 +1,18 @@ +import { PreviewContracts } from '../../contracts'; +import { PreviewModels } from '../../models'; +import { BaseResponses } from '../base-responses'; + +export namespace PreviewResponses { + export class PreviewConfigurationResponse extends BaseResponses.BaseContentManagementResponse< + PreviewContracts.IPreviewConfigurationContract, + PreviewModels.PreviewConfiguration + > { + constructor( + debug: BaseResponses.IContentManagementResponseDebug, + rawData: PreviewContracts.IPreviewConfigurationContract, + data: PreviewModels.PreviewConfiguration + ) { + super(debug, rawData, data); + } + } +} diff --git a/lib/services/management-query-service.class.ts b/lib/services/management-query-service.class.ts index 46c4206..86bc3b2 100644 --- a/lib/services/management-query-service.class.ts +++ b/lib/services/management-query-service.class.ts @@ -18,7 +18,8 @@ import { SpaceContracts, TaxonomyContracts, WebhookContracts, - WorkflowContracts + WorkflowContracts, + PreviewContracts } from '../contracts'; import { assetFolderMapper, @@ -37,7 +38,8 @@ import { roleMapper, projectUserMapper, assetRenditionMapper, - spacesMapper + spacesMapper, + previewMapper } from '../mappers'; import { webhookMapper } from '../mappers/webhook-mapper'; import { @@ -74,7 +76,8 @@ import { RoleResponses, ProjectUsersResponses, AssetRenditionResponses, - SpaceResponses + SpaceResponses, + PreviewResponses } from '../responses'; import { BaseManagementQueryService } from './base-management-service.class'; import { EnvironmentResponses } from '../responses/environments/environment-responses'; @@ -986,6 +989,15 @@ export class ManagementQueryService extends BaseManagementQueryService { ); } + async getPreviewConfigurationAsync( + url: string, + config: IContentManagementQueryConfig + ): Promise { + return previewMapper.mapPreviewConfigurationResponse( + await this.getResponseAsync(url, {}, config) + ); + } + async setCollectionsAsync( url: string, config: IContentManagementQueryConfig, diff --git a/test/browser/fake-responses/preview/fake-get-preview-configuration.json b/test/browser/fake-responses/preview/fake-get-preview-configuration.json new file mode 100644 index 0000000..7a28597 --- /dev/null +++ b/test/browser/fake-responses/preview/fake-get-preview-configuration.json @@ -0,0 +1,42 @@ +{ + "space_domains": [ + { + "space": { + "id": "fb2c6c2b-fb79-4d1b-856a-b59762e304b0" + }, + "domain": "www.mysite1.com" + }, + { + "space": { + "id": "24dace50-3cf6-4378-8704-453af9cdb60c" + }, + "domain": "www.mysite2.com" + } + ], + "preview_url_patterns": [ + { + "content_type": { + "id": "cf75f19d-6ed4-4820-a541-cde50dd79054" + }, + "url_patterns": [ + { + "space": { + "id": "24dace50-3cf6-4378-8704-453af9cdb60c" + }, + "url_pattern": "https://{Space}/{Lang}/articles/history/" + } + ] + }, + { + "content_type": { + "id": "f21f6c3b-f63e-4aa1-866f-55fd85ad262e" + }, + "url_patterns": [ + { + "space": null, + "url_pattern": "https://www.mysite1.com/{Lang}/articles/" + } + ] + } + ] +} diff --git a/test/browser/preview/get-preview-configuration.spec.ts b/test/browser/preview/get-preview-configuration.spec.ts new file mode 100644 index 0000000..8e76a35 --- /dev/null +++ b/test/browser/preview/get-preview-configuration.spec.ts @@ -0,0 +1,31 @@ +import { PreviewModels, PreviewResponses } from '../../../lib'; +import * as responseJson from '../fake-responses/preview/fake-get-preview-configuration.json'; +import { cmLiveClient, getTestClientWithJson, testEnvironmentId } from '../setup'; + +describe('Get preview configuration', () => { + let response: PreviewResponses.PreviewConfigurationResponse; + + beforeAll(async () => { + response = await getTestClientWithJson(responseJson).getPreviewConfiguration().toPromise(); + }); + + it(`url should be correct`, () => { + const url = cmLiveClient.getPreviewConfiguration().getUrl(); + + expect(url).toEqual(`https://manage.kontent.ai/v2/projects/${testEnvironmentId}/preview-configuration`); + + }); + + it(`response should be instance of PreviewResponses.PreviewConfigurationResponse class`, () => { + expect(response).toEqual(jasmine.any(PreviewResponses.PreviewConfigurationResponse)); + }); + + it(`response should contain debug data`, () => { + expect(response.debug).toBeDefined(); + }); + + it(`response should contain data`, () => { + expect(response.data).toBeDefined(); + expect(response.data).toEqual(jasmine.any(PreviewModels.PreviewConfiguration)); + }); +});