Skip to content

Commit

Permalink
Merge branch 'main' of https://github.com/sovity/edc-ui into 406-use-…
Browse files Browse the repository at this point in the history
…new-negotiate-contract-endpoint-in-ui-1
  • Loading branch information
SaadEGI committed Sep 25, 2023
2 parents 70498d4 + 9a177cb commit 64e6f6d
Show file tree
Hide file tree
Showing 17 changed files with 83 additions and 66 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {APP_CONFIG, AppConfig} from '../config/app-config';
import {DataAddressMapper} from './data-address-mapper';

@Injectable()
export class AssetEntryBuilder {
export class AssetRequestBuilder {
constructor(
@Inject(APP_CONFIG) private config: AppConfig,
private dataAddressMapper: DataAddressMapper,
Expand Down
19 changes: 19 additions & 0 deletions src/app/core/services/data-address-properties.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
const EDC = 'https://w3id.org/edc/v0.0.1/ns/';

export const DataAddressProperty = {
authCode: `${EDC}authCode`,
authKey: `${EDC}authKey`,
baseUrl: `${EDC}baseUrl`,
body: `${EDC}body`,
header: `${EDC}header`,
mediaType: `${EDC}mediaType`,
method: `${EDC}method`,
pathSegments: `${EDC}pathSegments`,
proxyBody: `${EDC}proxyBody`,
proxyMethod: `${EDC}proxyMethod`,
proxyPath: `${EDC}proxyPath`,
proxyQueryParams: `${EDC}proxyQueryParams`,
queryParams: `${EDC}queryParams`,
secretName: `${EDC}secretName`,
type: `${EDC}type`,
};
46 changes: 25 additions & 21 deletions src/app/core/services/http-params-mapper.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ import {AssetDatasourceFormValue} from '../../routes/connector-ui/asset-page/ass
import {HttpDatasourceHeaderFormValue} from '../../routes/connector-ui/asset-page/asset-create-dialog/model/http-datasource-header-form-model';
import {HttpDatasourceQueryParamFormValue} from '../../routes/connector-ui/asset-page/asset-create-dialog/model/http-datasource-query-param-form-model';
import {ContractAgreementTransferDialogFormValue} from '../../routes/connector-ui/contract-agreement-page/contract-agreement-transfer-dialog/contract-agreement-transfer-dialog-form-model';
import {removeNullValues} from '../utils/record-utils';
import {mapKeys, removeNullValues} from '../utils/record-utils';
import {everythingAfter, everythingBefore} from '../utils/string-utils';
import {DataAddressProperty} from './data-address-properties';
import {Asset} from './models/asset';
import {HttpRequestParams} from './models/http-request-params';

Expand Down Expand Up @@ -46,33 +47,36 @@ export class HttpRequestParamsMapper {
asset.httpDatasourceHintsProxyBody;

return removeNullValues({
method: proxyMethod ? method : null,
pathSegments: proxyPath ? pathSegments : null,
queryParams: proxyQueryParams ? queryParams : null,
body: proxyBody ? body : null,
mediaType: proxyBody ? contentType : null,
[DataAddressProperty.method]: proxyMethod ? method : null,
[DataAddressProperty.pathSegments]: proxyPath ? pathSegments : null,
[DataAddressProperty.queryParams]: proxyQueryParams ? queryParams : null,
[DataAddressProperty.body]: proxyBody ? body : null,
[DataAddressProperty.mediaType]: proxyBody ? contentType : null,
});
}

encodeHttpRequestParams(
httpRequestParams: HttpRequestParams,
): Record<string, string> {
const bool = (b?: boolean | null) => (b ? 'true' : null);

const props: Record<string, string | null> = {
type: 'HttpData',
baseUrl: httpRequestParams.baseUrl,
method: httpRequestParams.method,
authKey: httpRequestParams.authHeaderName,
authCode: httpRequestParams.authHeaderValue,
secretName: httpRequestParams.authHeaderSecretName,
proxyMethod: httpRequestParams.proxyMethod ? 'true' : null,
proxyPath: httpRequestParams.proxyPath ? 'true' : null,
proxyQueryParams: httpRequestParams.proxyQueryParams ? 'true' : null,
proxyBody: httpRequestParams.proxyBody ? 'true' : null,
queryParams: httpRequestParams.queryParams,
...Object.fromEntries(
Object.entries(httpRequestParams.headers).map(
([headerName, headerValue]) => [`header:${headerName}`, headerValue],
),
[DataAddressProperty.type]: 'HttpData',
[DataAddressProperty.baseUrl]: httpRequestParams.baseUrl,
[DataAddressProperty.method]: httpRequestParams.method,
[DataAddressProperty.authKey]: httpRequestParams.authHeaderName,
[DataAddressProperty.authCode]: httpRequestParams.authHeaderValue,
[DataAddressProperty.secretName]: httpRequestParams.authHeaderSecretName,
[DataAddressProperty.proxyMethod]: bool(httpRequestParams.proxyMethod),
[DataAddressProperty.proxyPath]: bool(httpRequestParams.proxyPath),
[DataAddressProperty.proxyQueryParams]: bool(
httpRequestParams.proxyQueryParams,
),
[DataAddressProperty.proxyBody]: bool(httpRequestParams.proxyBody),
[DataAddressProperty.queryParams]: httpRequestParams.queryParams,
...mapKeys(
httpRequestParams.headers,
(k) => `${DataAddressProperty.header}:${k}`,
),
};
return removeNullValues(props);
Expand Down
16 changes: 16 additions & 0 deletions src/app/core/utils/record-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,19 @@ export function removeNullValues(
Object.entries(obj).filter(([_, v]) => v != null) as [string, string][],
);
}

/**
* Maps keys of a given object
* @param obj object
* @param mapFn key mapper
* @return new object with keys mapped
*/
export function mapKeys<
K extends string | number | symbol,
L extends string | number | symbol,
V,
>(obj: Record<K, V>, mapFn: (key: K) => L): Record<L, V> {
return Object.fromEntries(
Object.entries(obj).map(([k, v]) => [mapFn(k as K), v]),
) as Record<L, V>;
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ import {ValidatorFn, Validators} from '@angular/forms';
* Validates whether value contains whitespaces
* @param control control
*/
export const noWhitespaceValidator: ValidatorFn = Validators.pattern(/^\S*$/);
export const noWhitespacesOrColonsValidator: ValidatorFn =
Validators.pattern(/^[^\s:]*$/);
14 changes: 0 additions & 14 deletions src/app/core/validators/requires-prefix-validator.ts

This file was deleted.

3 changes: 1 addition & 2 deletions src/app/core/validators/validation-messages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,9 @@ import {Injectable} from '@angular/core';
export class ValidationMessages {
invalidUrlMessage = 'Must be valid URL, e.g. https://example.com';
invalidJsonMessage = 'Must be valid JSON';
invalidWhitespacesMessage = 'Must not contain whitespaces.';
invalidWhitespacesOrColonsMessage = 'Must not contain whitespaces or colons.';
invalidPrefix = (field: string, prefix: string): string =>
`${field} must start with "${prefix}".`;
invalidDateRangeMessage = 'Need valid date range.';
invalidDateMessage = 'Must be valid date.';
idExistsErrorMessage = 'ID already exists.';
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,14 @@ <h1 mat-dialog-title>Create New Asset</h1>
<mat-form-field
*ngIf="form.metadata.controls.id; let ctrl"
class="grow">
<mat-label>Id</mat-label>
<mat-label>Asset ID</mat-label>
<input
placeholder="urn:artifact:..."
matInput
autocomplete="new-id"
[formControl]="ctrl" />
<mat-error *ngIf="ctrl.invalid && ctrl.errors?.pattern">
{{ validationMessages.invalidWhitespacesMessage }}
{{ validationMessages.invalidWhitespacesOrColonsMessage }}
</mat-error>
<mat-error *ngIf="ctrl.invalid && ctrl.errors?.requiresPrefix">
{{ validationMessages.invalidPrefix('ID', 'urn:artifact') }}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import {MatDialogRef} from '@angular/material/dialog';
import {Subject} from 'rxjs';
import {finalize, takeUntil} from 'rxjs/operators';
import {EdcApiService} from '../../../../core/services/api/edc-api.service';
import {AssetEntryBuilder} from '../../../../core/services/asset-entry-builder';
import {AssetRequestBuilder} from '../../../../core/services/asset-request-builder';
import {NotificationService} from '../../../../core/services/notification.service';
import {ValidationMessages} from '../../../../core/validators/validation-messages';
import {AssetCreateDialogForm} from './asset-create-dialog-form';
Expand All @@ -19,7 +19,7 @@ import {AssetMetadataFormBuilder} from './model/asset-metadata-form-builder';
AssetAdvancedFormBuilder,
AssetDatasourceFormBuilder,
AssetCreateDialogForm,
AssetEntryBuilder,
AssetRequestBuilder,
AssetMetadataFormBuilder,
],
})
Expand All @@ -32,7 +32,7 @@ export class AssetCreateDialogComponent implements OnDestroy {
private edcApiService: EdcApiService,
public form: AssetCreateDialogForm,
public validationMessages: ValidationMessages,
private assetEntryBuilder: AssetEntryBuilder,
private assetEntryBuilder: AssetRequestBuilder,
private notificationService: NotificationService,
private dialogRef: MatDialogRef<AssetCreateDialogComponent>,
) {}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@ import {FormBuilder, FormControl, FormGroup, Validators} from '@angular/forms';
import {combineLatest, distinctUntilChanged, pairwise} from 'rxjs';
import {map} from 'rxjs/operators';
import {value$} from '../../../../../core/utils/form-group-utils';
import {noWhitespaceValidator} from '../../../../../core/validators/no-whitespace-validator';
import {requiresPrefixValidator} from '../../../../../core/validators/requires-prefix-validator';
import {noWhitespacesOrColonsValidator} from '../../../../../core/validators/no-whitespaces-or-colons-validator';
import {urlValidator} from '../../../../../core/validators/url-validator';
import {LanguageSelectItem} from '../../language-select/language-select-item';
import {LanguageSelectItemService} from '../../language-select/language-select-item.service';
Expand All @@ -24,11 +23,7 @@ export class AssetMetadataFormBuilder {
this.formBuilder.nonNullable.group({
id: [
'',
[
Validators.required,
noWhitespaceValidator,
requiresPrefixValidator('urn:artifact:'),
],
[Validators.required, noWhitespacesOrColonsValidator],
[this.assetsIdValidatorBuilder.assetIdDoesNotExistsValidator()],
],
name: ['', Validators.required],
Expand Down Expand Up @@ -74,12 +69,12 @@ export class AssetMetadataFormBuilder {
}

private generateId(name: string | null, version: string | null) {
if (!name) return 'urn:artifact:';
if (!name) return '';
const normalizedName = name
.replace(':', '')
.replaceAll(' ', '-')
.toLowerCase();
if (version) return `urn:artifact:${normalizedName}:${version}`;
return `urn:artifact:${normalizedName}`;
if (version) return `${normalizedName}-${version}`;
return normalizedName;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import {Subject} from 'rxjs';
import {finalize} from 'rxjs/operators';
import {ContractAgreementTransferRequest} from '@sovity.de/edc-client';
import {EdcApiService} from '../../../../core/services/api/edc-api.service';
import {AssetEntryBuilder} from '../../../../core/services/asset-entry-builder';
import {DataAddressMapper} from '../../../../core/services/data-address-mapper';
import {HttpRequestParamsMapper} from '../../../../core/services/http-params-mapper.service';
import {NotificationService} from '../../../../core/services/notification.service';
Expand All @@ -17,7 +16,7 @@ import {ContractAgreementTransferDialogResult} from './contract-agreement-transf
@Component({
selector: 'contract-agreement-transfer-dialog',
templateUrl: './contract-agreement-transfer-dialog.component.html',
providers: [ContractAgreementTransferDialogForm, AssetEntryBuilder],
providers: [ContractAgreementTransferDialogForm],
})
export class ContractAgreementTransferDialogComponent implements OnDestroy {
loading = false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import {Injectable} from '@angular/core';
import {FormBuilder, FormGroup, Validators} from '@angular/forms';
import {PolicyDefinitionDto} from '@sovity.de/edc-client';
import {Asset} from '../../../../core/services/models/asset';
import {noWhitespaceValidator} from '../../../../core/validators/no-whitespace-validator';
import {noWhitespacesOrColonsValidator} from '../../../../core/validators/no-whitespaces-or-colons-validator';
import {
ContractDefinitionEditorDialogFormModel,
ContractDefinitionEditorDialogFormValue,
Expand All @@ -26,7 +26,7 @@ export class ContractDefinitionEditorDialogForm {

buildFormGroup(): FormGroup<ContractDefinitionEditorDialogFormModel> {
return this.formBuilder.nonNullable.group({
id: ['', [Validators.required, noWhitespaceValidator]],
id: ['', [Validators.required, noWhitespacesOrColonsValidator]],
accessPolicy: [null as PolicyDefinitionDto | null, Validators.required],
contractPolicy: [null as PolicyDefinitionDto | null, Validators.required],
assets: [[] as Asset[], Validators.required],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ <h1 mat-dialog-title>Create New Contract Definition</h1>
<mat-label>ID</mat-label>
<input matInput autocomplete="new-id" [formControl]="ctrl" />
<mat-error *ngIf="ctrl.invalid && ctrl.errors?.pattern">{{
validationMessages.invalidWhitespacesMessage
validationMessages.invalidWhitespacesOrColonsMessage
}}</mat-error>
</mat-form-field>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import {Subject} from 'rxjs';
import {finalize, takeUntil} from 'rxjs/operators';
import {PolicyDefinitionDto} from '@sovity.de/edc-client';
import {EdcApiService} from '../../../../core/services/api/edc-api.service';
import {AssetEntryBuilder} from '../../../../core/services/asset-entry-builder';
import {AssetServiceMapped} from '../../../../core/services/asset-service-mapped';
import {ContractDefinitionBuilder} from '../../../../core/services/contract-definition-builder';
import {Asset} from '../../../../core/services/models/asset';
Expand All @@ -16,7 +15,7 @@ import {ContractDefinitionEditorDialogResult} from './contract-definition-editor
@Component({
selector: 'contract-definition-editor-dialog',
templateUrl: './contract-definition-editor-dialog.component.html',
providers: [ContractDefinitionEditorDialogForm, AssetEntryBuilder],
providers: [ContractDefinitionEditorDialogForm],
})
export class ContractDefinitionEditorDialog implements OnInit, OnDestroy {
policies: PolicyDefinitionDto[] = [];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import {Injectable} from '@angular/core';
import {FormBuilder, FormGroup, Validators} from '@angular/forms';
import {switchDisabledControls} from '../../../../core/utils/form-group-utils';
import {dateRangeRequired} from '../../../../core/validators/date-range-required';
import {noWhitespaceValidator} from '../../../../core/validators/no-whitespace-validator';
import {noWhitespacesOrColonsValidator} from '../../../../core/validators/no-whitespaces-or-colons-validator';
import {
NewPolicyDialogFormModel,
NewPolicyDialogFormValue,
Expand Down Expand Up @@ -32,7 +32,7 @@ export class NewPolicyDialogForm {
buildFormGroup(): FormGroup<NewPolicyDialogFormModel> {
const newPolicyFormGroup: FormGroup<NewPolicyDialogFormModel> =
this.formBuilder.nonNullable.group({
id: ['', [Validators.required, noWhitespaceValidator]],
id: ['', [Validators.required, noWhitespacesOrColonsValidator]],
policyType: [
'Connector-Restricted-Usage' as PolicyType,
Validators.required,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ <h1 mat-dialog-title>Create New Policy</h1>
<mat-label>ID</mat-label>
<input matInput autocomplete="newvalue" [formControl]="ctrl" />
<mat-error *ngIf="ctrl.invalid && ctrl.errors?.pattern">{{
validationMessages.invalidWhitespacesMessage
validationMessages.invalidWhitespacesOrColonsMessage
}}</mat-error>
</mat-form-field>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import {MatDialogRef} from '@angular/material/dialog';
import {Subject} from 'rxjs';
import {finalize, takeUntil} from 'rxjs/operators';
import {EdcApiService} from '../../../../core/services/api/edc-api.service';
import {AssetEntryBuilder} from '../../../../core/services/asset-entry-builder';
import {NotificationService} from '../../../../core/services/notification.service';
import {PolicyDefinitionBuilder} from '../../../../core/services/policy-definition-builder';
import {ValidationMessages} from '../../../../core/validators/validation-messages';
Expand All @@ -13,7 +12,7 @@ import {NewPolicyDialogResult} from './new-policy-dialog-result';
@Component({
selector: 'new-policy-dialog',
templateUrl: './new-policy-dialog.component.html',
providers: [NewPolicyDialogForm, AssetEntryBuilder],
providers: [NewPolicyDialogForm],
})
export class NewPolicyDialogComponent implements OnDestroy {
loading = false;
Expand Down

0 comments on commit 64e6f6d

Please sign in to comment.