Skip to content

Commit

Permalink
align naming
Browse files Browse the repository at this point in the history
  • Loading branch information
madsrasmussen committed Feb 20, 2025
1 parent 32c3d17 commit 2b0ceab
Show file tree
Hide file tree
Showing 8 changed files with 23 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import { UmbDataMappingResolver } from './mapping/data-mapping-resolver.js';
import { UmbControllerBase } from '@umbraco-cms/backoffice/class-api';

export interface UmbDataMapperMapArgs<fromModelType = unknown, toModelType = unknown> {
dataModelIdentifier: string;
dataSourceIdentifier: string;
forDataModel: string;
forDataSource: string;
data: fromModelType;
fallback?: (data: fromModelType) => Promise<toModelType>;
}
Expand All @@ -12,19 +12,19 @@ export class UmbDataMapper<fromModelType = unknown, toModelType = unknown> exten
#dataMappingResolver = new UmbDataMappingResolver(this);

async map(args: UmbDataMapperMapArgs<fromModelType, toModelType>) {
if (!args.dataSourceIdentifier) {
if (!args.forDataSource) {
throw new Error('data source identifier is required');
}

if (!args.dataModelIdentifier) {
if (!args.forDataModel) {
throw new Error('data identifier is required');
}

if (!args.data) {
throw new Error('data is required');
}

const dataMapping = await this.#dataMappingResolver.resolve(args.dataSourceIdentifier, args.dataModelIdentifier);
const dataMapping = await this.#dataMappingResolver.resolve(args.forDataSource, args.forDataModel);

if (!dataMapping && !args.fallback) {
throw new Error('Data mapping not found and no fallback provided.');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ export class UmbManagementApiDataMapper extends UmbControllerBase {
super(host);
}

map(args: Omit<UmbDataMapperMapArgs, 'dataSourceIdentifier'>) {
map(args: Omit<UmbDataMapperMapArgs, 'forDataSource'>) {
return this.#dataMapper.map({
...args,
dataSourceIdentifier: UMB_MANAGEMENT_API_DATA_SOURCE_IDENTIFIER,
forDataSource: UMB_MANAGEMENT_API_DATA_SOURCE_IDENTIFIER,
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,16 @@ import { umbExtensionsRegistry } from '@umbraco-cms/backoffice/extension-registr
export class UmbDataMappingResolver extends UmbControllerBase {
#apiCache = new Map<string, UmbDataMapping>();

async resolve(dataSourceIdentifier: string, dataModelIdentifier: string): Promise<UmbDataMapping | undefined> {
if (!dataSourceIdentifier) {
async resolve(forDataSource: string, forDataModel: string): Promise<UmbDataMapping | undefined> {
if (!forDataSource) {
throw new Error('data source identifier is required');
}

if (!dataModelIdentifier) {
if (!forDataModel) {
throw new Error('data identifier is required');
}

const manifest = this.#getManifestWithBestFit(dataSourceIdentifier, dataModelIdentifier);
const manifest = this.#getManifestWithBestFit(forDataSource, forDataModel);

if (!manifest) {
return undefined;
Expand All @@ -42,8 +42,8 @@ export class UmbDataMappingResolver extends UmbControllerBase {
return dataMapping;
}

Check warning on line 43 in src/Umbraco.Web.UI.Client/src/packages/core/repository/data-mapper/mapping/data-mapping-resolver.ts

View check run for this annotation

CodeScene Delta Analysis / CodeScene Cloud Delta Analysis (v15/dev)

❌ New issue: Complex Method

UmbDataMappingResolver.resolve has a cyclomatic complexity of 10, threshold = 9. This function has many conditional statements (e.g. if, for, while), leading to lower code health. Avoid adding more conditionals and code to it without refactoring.

#getManifestWithBestFit(dataSourceIdentifier: string, dataModelIdentifier: string) {
const supportedManifests = this.#getSupportedManifests(dataSourceIdentifier, dataModelIdentifier);
#getManifestWithBestFit(forDataSource: string, forDataModel: string) {
const supportedManifests = this.#getSupportedManifests(forDataSource, forDataModel);

if (!supportedManifests.length) {
return undefined;
Expand All @@ -54,11 +54,9 @@ export class UmbDataMappingResolver extends UmbControllerBase {
return supportedManifests.sort((a: ManifestBase, b: ManifestBase): number => (b.weight || 0) - (a.weight || 0))[0];
}

#getSupportedManifests(dataSourceIdentifier: string, dataModelIdentifier: string) {
#getSupportedManifests(forDataSource: string, forDataModel: string) {
const supportedManifests = umbExtensionsRegistry.getByTypeAndFilter('dataMapping', (manifest) => {
return (
manifest.dataSourceIdentifier === dataSourceIdentifier && manifest.dataModelIdentifier === dataModelIdentifier
);
return manifest.forDataSource === forDataSource && manifest.forDataModel === forDataModel;
});

return supportedManifests;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import type { ManifestApi } from '@umbraco-cms/backoffice/extension-api';
export interface ManifestDataMapping<MetaType extends MetaDataMapping = MetaDataMapping>
extends ManifestApi<UmbDataMapping> {
type: 'dataMapping';
dataSourceIdentifier: string;
dataModelIdentifier: string;
forDataSource: string;
forDataModel: string;
meta: MetaType;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export class UmbDocumentReferenceServerDataSource extends UmbControllerBase {
if (data) {
const promises = data.items.map(async (item) => {
return this.#dataMapper.map({
dataModelIdentifier: item.$type,
forDataModel: item.$type,
data: item,
fallback: async () => {
return {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export const manifests: Array<UmbExtensionManifest> = [
alias: 'Umb.DataMapping.ManagementApi.DocumentReferenceResponse',
name: 'Document Reference Response Management Api Data Mapping',
api: () => import('./document-reference-response.management-api.mapping.js'),
dataSourceIdentifier: UMB_MANAGEMENT_API_DATA_SOURCE_IDENTIFIER,
dataModelIdentifier: 'DocumentReferenceResponseModel',
forDataSource: UMB_MANAGEMENT_API_DATA_SOURCE_IDENTIFIER,
forDataModel: 'DocumentReferenceResponseModel',
},
];
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export const manifests: Array<UmbExtensionManifest> = [
alias: 'Umb.DataMapping.ManagementApi.MediaReferenceResponse',
name: 'Media Reference Response Management Api Data Mapping',
api: () => import('./media-reference-response.management-api.mapping.js'),
dataSourceIdentifier: UMB_MANAGEMENT_API_DATA_SOURCE_IDENTIFIER,
dataModelIdentifier: 'MediaReferenceResponseModel',
forDataSource: UMB_MANAGEMENT_API_DATA_SOURCE_IDENTIFIER,
forDataModel: 'MediaReferenceResponseModel',
},
];
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export class UmbMediaReferenceServerDataSource extends UmbControllerBase {
if (data) {
const promises = data.items.map(async (item) => {
return this.#dataMapper.map({
dataModelIdentifier: item.$type,
forDataModel: item.$type,
data: item,
fallback: async () => {
return {
Expand Down

0 comments on commit 2b0ceab

Please sign in to comment.