Skip to content

Commit

Permalink
Refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
ndr-brt committed Jul 28, 2023
1 parent a684f77 commit 1ddbe01
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 27 deletions.
17 changes: 4 additions & 13 deletions src/entities/catalog.ts
Original file line number Diff line number Diff line change
@@ -1,41 +1,32 @@
import { QuerySpec } from ".";
import { JsonLdId, JsonLdValue } from "./jsonld";
import { JsonLdId } from "./jsonld";

export interface CatalogRequest {
providerUrl: string;
querySpec?: QuerySpec;
}

export class Catalog extends JsonLdId {
"https://www.w3.org/ns/dcat/dataset": Dataset[];

get datasets(): Dataset[] {
return this["https://www.w3.org/ns/dcat/dataset"].map((it) =>
Object.assign(new Dataset(), it),
);
return this.arrayOf(() => new Dataset(), 'dcat', 'dataset');
}
}

export class Dataset extends JsonLdId {
"http://www.w3.org/ns/odrl/2/hasPolicy": Offer[];

get offers(): Offer[] {
return this["http://www.w3.org/ns/odrl/2/hasPolicy"].map((it) =>
Object.assign(new Offer(), it),
);
return this.arrayOf(() => new Offer(), 'odrl', 'hasPolicy');
}
}

export class Offer extends JsonLdId {
"http://www.w3.org/ns/odrl/2/target": JsonLdValue<string>[];

get assetId(): string {
return this.target;
}

get target(): string {
return this["http://www.w3.org/ns/odrl/2/target"].map((it) =>
Object.assign(new JsonLdValue(), it),
)[0].value;
return this.mandatoryValue('odrl', 'target');
}
}
25 changes: 15 additions & 10 deletions src/entities/contract-agreement.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,24 @@
import { Policy } from "./policy";
import { JsonLdId, JsonLdValue } from "./jsonld"
import { JsonLdId } from "./jsonld"

export class ContractAgreement extends JsonLdId {
'https://w3id.org/edc/v0.0.1/ns/assetId': JsonLdValue<string>[];
consumerAgentId?: string;
contractEndDate?: number;
constractSigningDate?: number;
constractStartDate?: number;

policy?: Policy;
providerAgentId?: string;

get assetId(): string {
return this['https://w3id.org/edc/v0.0.1/ns/assetId']
.map(it => Object.assign(new JsonLdValue(), it))[0]
.value
return this.mandatoryValue('edc', 'assetId');
}

get providerId(): string {
return this.mandatoryValue('edc', 'providerId');
}

get consumerId(): string {
return this.mandatoryValue('edc', 'providerId');
}

get contractSigningDate(): number {
return this.mandatoryValue('edc', 'providerId');
}

}
36 changes: 35 additions & 1 deletion src/entities/jsonld.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,38 @@
export class JsonLdId {
import { EDC_CONTEXT } from "./context";

export class JsonLdObject {

[propertyName: string]: any;

mandatoryValue<T>(prefix: string, name: string): T {
return this.optionalValue(prefix, name)!;
}

optionalValue<T>(prefix: string, name: string): T | undefined {
var namespace = this.getNamespaceUrl(prefix);
return (this[`${namespace}${name}`] as JsonLdValue<T>[])
?.map(it => Object.assign(new JsonLdValue(), it))
?.at(0)?.value;
}

arrayOf<T extends Object>(newInstance: (() => T), prefix: string, name: string): T[] {
var namespace = this.getNamespaceUrl(prefix);
return (this[`${namespace}${name}`] as T[]).map(it =>
Object.assign(newInstance(), it),
);
}

private getNamespaceUrl(prefix: string): string {
switch (prefix) {
case 'edc': return EDC_CONTEXT;
case 'odrl': return 'http://www.w3.org/ns/odrl/2/';
case 'dcat': return 'https://www.w3.org/ns/dcat/';
default: throw new Error(`JSON-LD context ${prefix} not supported`);
}
}
}

export class JsonLdId extends JsonLdObject {
'@id': string;

get id() {
Expand Down
2 changes: 1 addition & 1 deletion src/entities/policy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export interface Policy {
obligations?: Duty[];
permissions?: Permission[];
prohibitions?: Prohibition[];
traget?: string;
target?: string;
}

export class PolicyDefinition extends JsonLdId {
Expand Down
5 changes: 3 additions & 2 deletions tests/test-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,10 +118,11 @@ export async function createContractNegotiation(
providerUrl: providerContext.protocol,
});


const offer = catalog
.datasets
.flatMap((it) => it.offers)
.find((offer) => offer.assetId === assetId)!;
.flatMap(it => it.offers)
.find(offer => offer.assetId === assetId)!;

// Initiate contract negotiation on the consumer's side
const idResponse = await client.management.initiateContractNegotiation(
Expand Down

0 comments on commit 1ddbe01

Please sign in to comment.