Skip to content

Commit

Permalink
refactor: Change constructor so it is supported by Components.js
Browse files Browse the repository at this point in the history
  • Loading branch information
joachimvh committed Nov 2, 2020
1 parent 712a690 commit dee4eef
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 11 deletions.
23 changes: 15 additions & 8 deletions src/storage/RepresentationConvertingStore.ts
Expand Up @@ -29,17 +29,20 @@ export class RepresentationConvertingStore<T extends ResourceStore = ResourceSto
private readonly inConverter?: RepresentationConverter;
private readonly outConverter?: RepresentationConverter;

private readonly inPreferences?: RepresentationPreferences;
private readonly inType?: string;

/**
* TODO: This should take RepresentationPreferences instead of a type string when supported by Components.js.
*/
public constructor(source: T, options: {
outConverter?: RepresentationConverter;
inConverter?: RepresentationConverter;
inPreferences?: RepresentationPreferences;
inType?: string;
}) {
super(source);
this.inConverter = options.inConverter;
this.outConverter = options.outConverter;
this.inPreferences = options.inPreferences;
this.inType = options.inType;
}

public async getRepresentation(identifier: ResourceIdentifier, preferences: RepresentationPreferences,
Expand All @@ -56,13 +59,13 @@ export class RepresentationConvertingStore<T extends ResourceStore = ResourceSto
conditions?: Conditions): Promise<ResourceIdentifier> {
// We can potentially run into problems here if we convert a turtle document where the base IRI is required,
// since we don't know the resource IRI yet at this point.
representation = await this.convertRepresentation(container, representation);
representation = await this.convertInRepresentation(container, representation);
return this.source.addResource(container, representation, conditions);
}

public async setRepresentation(identifier: ResourceIdentifier, representation: Representation,
conditions?: Conditions): Promise<void> {
representation = await this.convertRepresentation(identifier, representation);
representation = await this.convertInRepresentation(identifier, representation);
return this.source.setRepresentation(identifier, representation, conditions);
}

Expand All @@ -79,11 +82,15 @@ export class RepresentationConvertingStore<T extends ResourceStore = ResourceSto
);
}

private async convertRepresentation(identifier: ResourceIdentifier, representation: Representation):
private async convertInRepresentation(identifier: ResourceIdentifier, representation: Representation):
Promise<Representation> {
if (!this.inPreferences || !this.inConverter || this.matchesPreferences(representation, this.inPreferences)) {
if (!this.inType) {
return representation;
}
return this.inConverter.handleSafe({ identifier, representation, preferences: this.inPreferences });
const inPreferences: RepresentationPreferences = { type: [{ value: this.inType, weight: 1 }]};
if (!inPreferences || !this.inConverter || this.matchesPreferences(representation, inPreferences)) {
return representation;
}
return this.inConverter.handleSafe({ identifier, representation, preferences: inPreferences });
}
}
9 changes: 6 additions & 3 deletions test/unit/storage/RepresentationConvertingStore.test.ts
@@ -1,6 +1,5 @@
import type { Representation } from '../../../src/ldp/representation/Representation';
import { RepresentationMetadata } from '../../../src/ldp/representation/RepresentationMetadata';
import type { RepresentationPreferences } from '../../../src/ldp/representation/RepresentationPreferences';
import type { RepresentationConverter } from '../../../src/storage/conversion/RepresentationConverter';
import { RepresentationConvertingStore } from '../../../src/storage/RepresentationConvertingStore';
import type { ResourceStore } from '../../../src/storage/ResourceStore';
Expand All @@ -11,7 +10,7 @@ describe('A RepresentationConvertingStore', (): void => {
let source: ResourceStore;
let inConverter: RepresentationConverter;
let outConverter: RepresentationConverter;
const inPreferences: RepresentationPreferences = { type: [{ value: 'text/turtle', weight: 1 }]};
const inType = 'text/turtle';
const metadata = new RepresentationMetadata({ [CONTENT_TYPE]: 'text/turtle' });
let representation: Representation;

Expand All @@ -25,7 +24,7 @@ describe('A RepresentationConvertingStore', (): void => {
inConverter = { handleSafe: jest.fn(async(): Promise<any> => 'inConvert') } as any;
outConverter = { handleSafe: jest.fn(async(): Promise<any> => 'outConvert') } as any;

store = new RepresentationConvertingStore(source, { inPreferences, inConverter, outConverter });
store = new RepresentationConvertingStore(source, { inType, inConverter, outConverter });
representation = { binary: true, data: 'data', metadata } as any;
});

Expand Down Expand Up @@ -81,6 +80,10 @@ describe('A RepresentationConvertingStore', (): void => {
await expect(store.setRepresentation(id, representation, 'conditions' as any)).resolves.toBeUndefined();
expect(inConverter.handleSafe).toHaveBeenCalledTimes(0);
expect(source.setRepresentation).toHaveBeenLastCalledWith(id, representation, 'conditions');

store = new RepresentationConvertingStore(source, {});
await expect(store.addResource(id, representation, 'conditions' as any)).resolves.toBeUndefined();
expect(source.addResource).toHaveBeenLastCalledWith(id, representation, 'conditions');
});

it('converts the data if it is required.', async(): Promise<void> => {
Expand Down

0 comments on commit dee4eef

Please sign in to comment.