refactor: Streamline RepresentationMetadata interface

This commit is contained in:
Joachim Van Herwegen
2020-09-08 09:43:30 +02:00
parent 76319ba360
commit 8d3979372b
36 changed files with 416 additions and 230 deletions

View File

@@ -1,7 +1,7 @@
import { Representation } from '../../ldp/representation/Representation';
import { RepresentationMetadata } from '../../ldp/representation/RepresentationMetadata';
import { RepresentationPreferences } from '../../ldp/representation/RepresentationPreferences';
import { CONTENT_TYPE } from '../../util/MetadataTypes';
import { MA_CONTENT_TYPE } from '../../util/MetadataTypes';
import { matchingMediaType } from '../../util/Util';
import { RepresentationConverterArgs } from './RepresentationConverter';
import { TypedRepresentationConverter } from './TypedRepresentationConverter';
@@ -53,8 +53,7 @@ export class ChainedConverter extends TypedRepresentationConverter {
const idx = this.converters.length - 1;
const lastChain = await this.getMatchingType(this.converters[idx - 1], this.converters[idx]);
const oldMeta = input.representation.metadata;
const metadata = new RepresentationMetadata(oldMeta.identifier, oldMeta.quads());
metadata.set(CONTENT_TYPE, lastChain);
const metadata = new RepresentationMetadata(oldMeta, { [MA_CONTENT_TYPE]: lastChain });
const representation: Representation = { ...input.representation, metadata };
await this.last.canHandle({ ...input, representation });
}

View File

@@ -1,7 +1,6 @@
import { RepresentationPreference } from '../../ldp/representation/RepresentationPreference';
import { RepresentationPreferences } from '../../ldp/representation/RepresentationPreferences';
import { UnsupportedHttpError } from '../../util/errors/UnsupportedHttpError';
import { CONTENT_TYPE } from '../../util/MetadataTypes';
import { matchingMediaType } from '../../util/Util';
import { RepresentationConverterArgs } from './RepresentationConverter';
@@ -35,11 +34,11 @@ RepresentationPreference[] => {
*/
export const checkRequest = (request: RepresentationConverterArgs, supportedIn: string[], supportedOut: string[]):
void => {
const inType = request.representation.metadata.get(CONTENT_TYPE);
const inType = request.representation.metadata.contentType;
if (!inType) {
throw new UnsupportedHttpError('Input type required for conversion.');
}
if (!supportedIn.some((type): boolean => matchingMediaType(inType.value, type))) {
if (!supportedIn.some((type): boolean => matchingMediaType(inType, type))) {
throw new UnsupportedHttpError(`Can only convert from ${supportedIn} to ${supportedOut}.`);
}
if (matchingTypes(request.preferences, supportedOut).length <= 0) {

View File

@@ -4,7 +4,7 @@ import { Representation } from '../../ldp/representation/Representation';
import { RepresentationMetadata } from '../../ldp/representation/RepresentationMetadata';
import { RepresentationPreferences } from '../../ldp/representation/RepresentationPreferences';
import { INTERNAL_QUADS } from '../../util/ContentTypes';
import { CONTENT_TYPE } from '../../util/MetadataTypes';
import { MA_CONTENT_TYPE } from '../../util/MetadataTypes';
import { checkRequest, matchingTypes } from './ConversionUtil';
import { RepresentationConverterArgs } from './RepresentationConverter';
import { TypedRepresentationConverter } from './TypedRepresentationConverter';
@@ -31,8 +31,7 @@ export class QuadToRdfConverter extends TypedRepresentationConverter {
private async quadsToRdf(quads: Representation, preferences: RepresentationPreferences): Promise<Representation> {
const contentType = matchingTypes(preferences, await rdfSerializer.getContentTypes())[0].value;
const metadata = new RepresentationMetadata(quads.metadata.identifier, quads.metadata.quads());
metadata.set(CONTENT_TYPE, contentType);
const metadata = new RepresentationMetadata(quads.metadata, { [MA_CONTENT_TYPE]: contentType });
return {
binary: true,
data: rdfSerializer.serialize(quads.data, { contentType }) as Readable,

View File

@@ -2,7 +2,7 @@ import { StreamWriter } from 'n3';
import { Representation } from '../../ldp/representation/Representation';
import { RepresentationMetadata } from '../../ldp/representation/RepresentationMetadata';
import { INTERNAL_QUADS, TEXT_TURTLE } from '../../util/ContentTypes';
import { CONTENT_TYPE } from '../../util/MetadataTypes';
import { MA_CONTENT_TYPE } from '../../util/MetadataTypes';
import { checkRequest } from './ConversionUtil';
import { RepresentationConverter, RepresentationConverterArgs } from './RepresentationConverter';
@@ -19,8 +19,7 @@ export class QuadToTurtleConverter extends RepresentationConverter {
}
private quadsToTurtle(quads: Representation): Representation {
const metadata = new RepresentationMetadata(quads.metadata.identifier, quads.metadata.quads());
metadata.set(CONTENT_TYPE, TEXT_TURTLE);
const metadata = new RepresentationMetadata(quads.metadata, { [MA_CONTENT_TYPE]: TEXT_TURTLE });
return {
binary: true,
data: quads.data.pipe(new StreamWriter({ format: TEXT_TURTLE })),

View File

@@ -3,7 +3,7 @@ import rdfParser from 'rdf-parse';
import { Representation } from '../../ldp/representation/Representation';
import { RepresentationMetadata } from '../../ldp/representation/RepresentationMetadata';
import { INTERNAL_QUADS } from '../../util/ContentTypes';
import { CONTENT_TYPE } from '../../util/MetadataTypes';
import { MA_CONTENT_TYPE } from '../../util/MetadataTypes';
import { pipeStreamsAndErrors } from '../../util/Util';
import { checkRequest } from './ConversionUtil';
import { RepresentationConverterArgs } from './RepresentationConverter';
@@ -30,10 +30,9 @@ export class RdfToQuadConverter extends TypedRepresentationConverter {
}
private rdfToQuads(representation: Representation, baseIRI: string): Representation {
const metadata = new RepresentationMetadata(representation.metadata.identifier, representation.metadata.quads());
metadata.set(CONTENT_TYPE, INTERNAL_QUADS);
const metadata = new RepresentationMetadata(representation.metadata, { [MA_CONTENT_TYPE]: INTERNAL_QUADS });
const rawQuads = rdfParser.parse(representation.data, {
contentType: representation.metadata.get(CONTENT_TYPE)!.value,
contentType: representation.metadata.contentType!,
baseIRI,
});

View File

@@ -4,7 +4,7 @@ import { Representation } from '../../ldp/representation/Representation';
import { RepresentationMetadata } from '../../ldp/representation/RepresentationMetadata';
import { TEXT_TURTLE, INTERNAL_QUADS } from '../../util/ContentTypes';
import { UnsupportedHttpError } from '../../util/errors/UnsupportedHttpError';
import { CONTENT_TYPE } from '../../util/MetadataTypes';
import { MA_CONTENT_TYPE } from '../../util/MetadataTypes';
import { checkRequest } from './ConversionUtil';
import { RepresentationConverter, RepresentationConverterArgs } from './RepresentationConverter';
@@ -21,8 +21,7 @@ export class TurtleToQuadConverter extends RepresentationConverter {
}
private turtleToQuads(turtle: Representation, baseIRI: string): Representation {
const metadata = new RepresentationMetadata(turtle.metadata.identifier, turtle.metadata.quads());
metadata.set(CONTENT_TYPE, INTERNAL_QUADS);
const metadata = new RepresentationMetadata(turtle.metadata, { [MA_CONTENT_TYPE]: INTERNAL_QUADS });
// Catch parsing errors and emit correct error
// Node 10 requires both writableObjectMode and readableObjectMode