Skip to content

Commit

Permalink
Merge pull request #395 from kontent-ai/models-update
Browse files Browse the repository at this point in the history
Models update + RTE resolvers removal + name resolvers removal
  • Loading branch information
Enngage authored Sep 16, 2024
2 parents 6e05ba9 + 57f47cb commit 81dde93
Show file tree
Hide file tree
Showing 56 changed files with 1,696 additions and 5,017 deletions.
17 changes: 17 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,23 @@

All notable changes to this project will be documented in this file. See [standard-version](https://github.com/conventional-changelog/standard-version) for commit guidelines.

## [15.0.0-0](https://github.com/kontent-ai/delivery-sdk-js/compare/v14.11.0...v15.0.0-0) (2024-09-10)


### ⚠ BREAKING CHANGES

* Removes parser & rich text resolver. To work with RTE use the newer and better `@kontent-ai/rich-text-resolver` library instead.
* Removes `propertyName` resolver configuration. All elements are now referenced only by their codenames present in Kontent.ai

### Features

* Adds narrowing types for Taxonomy & Multiple choice elements ([3118752](https://github.com/kontent-ai/delivery-sdk-js/commit/3118752e05fe27f09b72c7bab21804b114fa057e))
* Adds optional generic types to `IContentItem` narrowing down available values of system attributes ([8c894af](https://github.com/kontent-ai/delivery-sdk-js/commit/8c894afde62ee2ed40b44fbebc9dd9b8891115c5))
* Makes `RichTextElement` take generic parameter narrowing down allowed types of linked items ([68b31a8](https://github.com/kontent-ai/delivery-sdk-js/commit/68b31a81431646a7ab06647652592b81d075527b))
* Removes `propertyName` resolver configuration. All elements are now referenced only by their codenames present in Kontent.ai ([7ef5951](https://github.com/kontent-ai/delivery-sdk-js/commit/7ef5951c505b1acf2424c99255f435aa1a26a53e))
* Removes parser & rich text resolver. To work with RTE use the newer and better `@kontent-ai/rich-text-resolver` library instead. ([2bd30c3](https://github.com/kontent-ai/delivery-sdk-js/commit/2bd30c3d4527b470848984d70e03136c2e01885e))
* updates deps ([82c2c11](https://github.com/kontent-ai/delivery-sdk-js/commit/82c2c115482b0ee5ab74e3a94d9e8ebb55112532))

## [14.11.0](https://github.com/kontent-ai/delivery-sdk-js/compare/v14.10.0...v14.11.0) (2024-08-15)


Expand Down
7 changes: 1 addition & 6 deletions lib/config/delivery-configs.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { IHeader, IHttpService, IRetryStrategyOptions } from '@kontent-ai/core-sdk';

import { ElementResolver } from '../elements';
import { LinkedItemsReferenceHandler, IProxyUrlData, IQueryConfig, PropertyNameResolver } from '../models';
import { IProxyUrlData, IQueryConfig, LinkedItemsReferenceHandler } from '../models';

export interface IDeliveryClientProxyConfig {
/**
Expand All @@ -28,11 +28,6 @@ export interface IDeliveryClientConfig {
*/
environmentId: string;

/**
* Resolver used to rename content item elements. Can be used to e.g. transform underscored element codenames to camelCase format
*/
propertyNameResolver?: PropertyNameResolver;

/**
* Preview API key
*/
Expand Down
16 changes: 5 additions & 11 deletions lib/elements/element-models.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,8 @@
import { IContentItemSystemAttributes } from '../models/item-models';
import { Contracts } from '../contracts';
import { IContentItemSystemAttributes } from '../models/item-models';
import { ElementType } from './element-type';

export namespace ElementModels {
export interface IRichTextResolverData {
html: string;
linkedItemCodenames: string[];
componentCodenames: string[];
}

export interface IElementWrapper {
element: string;
system: IContentItemSystemAttributes;
Expand Down Expand Up @@ -83,13 +77,13 @@ export namespace ElementModels {
url: string;
}

export interface MultipleChoiceOption {
export interface MultipleChoiceOption<TOptionCodename extends string = string> {
name: string;
codename: string;
codename: TOptionCodename;
}

export interface TaxonomyTerm<TaxonomyCodename extends string = string> {
export interface TaxonomyTerm<TTaxonomyCodename extends string = string> {
name: string;
codename: TaxonomyCodename;
codename: TTaxonomyCodename;
}
}
17 changes: 10 additions & 7 deletions lib/elements/elements.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ export namespace Elements {
linkedItems: TContentItem[];
};

export type MultipleChoiceElement = ElementModels.IElement<ElementModels.MultipleChoiceOption[]>;
export type MultipleChoiceElement<TOptionCodenames extends string = string> = ElementModels.IElement<
ElementModels.MultipleChoiceOption<TOptionCodenames>[]
>;

export type DateTimeElement = ElementModels.IElement<string | null> & {
/**
Expand All @@ -22,7 +24,7 @@ export namespace Elements {
displayTimeZone: string | null;
};

export type RichTextElement = ElementModels.IElement<string> & {
export type RichTextElement<TContentItem extends IContentItem = IContentItem> = ElementModels.IElement<string> & {
/**
* Links
*/
Expand All @@ -43,7 +45,7 @@ export namespace Elements {
* as it depends on the `depth` parameter of query.
* The `linkedItemsReferenceHandler` configuration can be used to disable mapping of linked items
*/
linkedItems: IContentItem[];
linkedItems: TContentItem[];
};

export type NumberElement = ElementModels.IElement<number | null>;
Expand All @@ -52,13 +54,14 @@ export namespace Elements {

export type UrlSlugElement = ElementModels.IElement<string>;

export type TaxonomyElement<TaxonomyCodename extends string = string> = ElementModels.IElement<
ElementModels.TaxonomyTerm<TaxonomyCodename>[]
> & {
export type TaxonomyElement<
TaxonomyCodenames extends string = string,
TaxonomyGroupCodename extends string = string
> = ElementModels.IElement<ElementModels.TaxonomyTerm<TaxonomyCodenames>[]> & {
/**
* Taxonomy group
*/
taxonomyGroup: string | null;
taxonomyGroup: TaxonomyGroupCodename;
};

export type UnknownElement = ElementModels.IElement<any>;
Expand Down
11 changes: 4 additions & 7 deletions lib/index.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@
// Public API
export * from './client';
export * from './sdk-info.generated';
export * from './config';
export * from './contracts';
export * from './elements';
export * from './parser';
export * from './resolvers';
export * from './services';
export * from './images';
export * from './mappers';
export * from './query';
export * from './models';
export * from './images';
export * from './query';
export * from './sdk-info.generated';
export * from './services';
export * from './utilities';

61 changes: 18 additions & 43 deletions lib/mappers/element.mapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ import { ElementModels, Elements, ElementType } from '../elements';
import {
IContentItem,
IContentItemsContainer,
IMapElementsResult,
ILink,
IRichTextImage,
IContentItemWithRawDataContainer,
IContentItemWithRawElements
IContentItemWithRawElements,
ILink,
IMapElementsResult,
IRichTextImage
} from '../models';

interface IRichTextImageUrlRecord {
Expand Down Expand Up @@ -41,7 +41,8 @@ export class ElementMapper {
};
}

const preparedItem = data.preparedItems[codenameHelper.escapeCodenameInCodenameIndexer(data.dataToMap.item.system.codename)];
const preparedItem =
data.preparedItems[codenameHelper.escapeCodenameInCodenameIndexer(data.dataToMap.item.system.codename)];
const itemInstance = preparedItem?.item as TContentItem;

if (!itemInstance) {
Expand All @@ -53,24 +54,22 @@ export class ElementMapper {
const elementCodenames = Object.getOwnPropertyNames(data.dataToMap.rawItem.elements);

for (const elementCodename of elementCodenames) {
const elementMap = this.resolveElementMap(itemInstance, elementCodename);
const elementWrapper: ElementModels.IElementWrapper = {
system: data.dataToMap.item.system,
rawElement: data.dataToMap.rawItem.elements[elementCodename],
element: elementMap.resolvedName
element: elementCodename
};
if (elementMap.shouldMapElement) {
const mappedElement = this.mapElement({
elementWrapper: elementWrapper,
item: itemInstance,
preparedItems: data.preparedItems,
processingStartedForCodenames: data.processingStartedForCodenames,
processedItems: data.processedItems
});

// set mapped elements
itemInstance.elements[elementMap.resolvedName] = mappedElement;
}
const mappedElement = this.mapElement({
elementWrapper: elementWrapper,
item: itemInstance,
preparedItems: data.preparedItems,
processingStartedForCodenames: data.processingStartedForCodenames,
processedItems: data.processedItems
});

// set mapped elements
itemInstance.elements[elementCodename] = mappedElement;
}

return {
Expand Down Expand Up @@ -308,7 +307,7 @@ export class ElementMapper {
private mapTaxonomyElement(elementWrapper: ElementModels.IElementWrapper): Elements.TaxonomyElement {
return {
...this.buildElement(elementWrapper, ElementType.Taxonomy, () => elementWrapper.rawElement.value),
taxonomyGroup: elementWrapper.rawElement.taxonomy_group ?? null
taxonomyGroup: elementWrapper.rawElement.taxonomy_group ?? ''
};
}

Expand Down Expand Up @@ -484,30 +483,6 @@ export class ElementMapper {
};
}

private resolveElementMap(
item: IContentItem,
originalElementCodename: string
): {
shouldMapElement: boolean;
resolvedName: string;
} {
let resolvedElementPropertyName: string | undefined = undefined;

if (this.config.propertyNameResolver) {
resolvedElementPropertyName = this.config.propertyNameResolver(item.system.type, originalElementCodename);
}

if (!resolvedElementPropertyName) {
// use original element codename
resolvedElementPropertyName = originalElementCodename;
}

return {
resolvedName: resolvedElementPropertyName,
shouldMapElement: true
};
}

private buildElement<TValue>(
elementWrapper: ElementModels.IElementWrapper,
type: ElementType,
Expand Down
39 changes: 28 additions & 11 deletions lib/models/item-models.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Contracts } from '../contracts';
import { IQueryConfig } from './common/common-models';
import { ElementModels } from '../elements/element-models';
import { IQueryConfig } from './common/common-models';

export interface IMapElementsResult<TContentItem extends IContentItem = IContentItem> {
item: TContentItem;
Expand All @@ -9,7 +9,13 @@ export interface IMapElementsResult<TContentItem extends IContentItem = IContent
processingStartedForCodenames: string[];
}

export interface IContentItemSystemAttributes {
export interface IContentItemSystemAttributes<
TTypeCodename extends string = string,
TLanguageCodenames extends string = string,
TCollectionCodenames extends string = string,
TWorkflowCodenames extends string = string,
TWorkflowStepCodenames extends string = string
> {
/**
* Id of the item
*/
Expand All @@ -28,7 +34,7 @@ export interface IContentItemSystemAttributes {
/**
* Codename of the type this item is using
*/
type: string;
type: TTypeCodename;

/**
* Date when the item was last modified
Expand All @@ -38,7 +44,7 @@ export interface IContentItemSystemAttributes {
/**
* Codename of the language
*/
language: string;
language: TLanguageCodenames;

/**
* Array of sitemap locations (obsolete)
Expand All @@ -48,17 +54,17 @@ export interface IContentItemSystemAttributes {
/**
* Codename of the collection this item belongs to
*/
collection: string;
collection: TCollectionCodenames;

/**
* Workflow step of the item
*/
workflowStep: string | null;
workflowStep: TWorkflowStepCodenames | null;

/**
* Workflow of the item
*/
workflow: string | null;
workflow: TWorkflowCodenames | null;
}

/**
Expand All @@ -75,7 +81,14 @@ export interface IContentItemElements {
[key: string]: ContentItemElementsIndexer;
}

export interface IContentItem<TElements extends IContentItemElements = IContentItemElements> {
export interface IContentItem<
TElements extends IContentItemElements = IContentItemElements,
TTypeCodename extends string = string,
TLanguageCodenames extends string = string,
TCollectionCodenames extends string = string,
TWorkflowCodenames extends string = string,
TWorkflowStepCodenames extends string = string
> {
/**
* Elements of the content item
*/
Expand All @@ -84,7 +97,13 @@ export interface IContentItem<TElements extends IContentItemElements = IContentI
/**
* System data of the content item
*/
system: IContentItemSystemAttributes;
system: IContentItemSystemAttributes<
TTypeCodename,
TLanguageCodenames,
TCollectionCodenames,
TWorkflowCodenames,
TWorkflowStepCodenames
>;
}

export interface ILink {
Expand Down Expand Up @@ -135,5 +154,3 @@ export interface IItemQueryConfig extends IQueryConfig {}
export interface IItemFeedQueryConfig extends IQueryConfig {
disableItemLinking?: boolean;
}

export type PropertyNameResolver = (contentTypeCodename: string, elementCodename: string) => string;
Loading

0 comments on commit 81dde93

Please sign in to comment.