refactor: Check conversion support in base class.

This commit is contained in:
Ruben Verborgh 2021-01-02 16:25:39 +01:00
parent 4faf916ece
commit da5515d50d
3 changed files with 12 additions and 10 deletions

View File

@ -6,7 +6,7 @@ import type { RepresentationPreferences } from '../../ldp/representation/Represe
import { INTERNAL_QUADS } from '../../util/ContentTypes';
import { guardStream } from '../../util/GuardedStream';
import { CONTENT_TYPE } from '../../util/UriConstants';
import { supportsConversion, matchingMediaTypes } from './ConversionUtil';
import { matchingMediaTypes } from './ConversionUtil';
import type { RepresentationConverterArgs } from './RepresentationConverter';
import { TypedRepresentationConverter } from './TypedRepresentationConverter';
@ -22,10 +22,6 @@ export class QuadToRdfConverter extends TypedRepresentationConverter {
return rdfSerializer.getContentTypesPrioritized();
}
public async canHandle(input: RepresentationConverterArgs): Promise<void> {
supportsConversion(input, [ INTERNAL_QUADS ], await rdfSerializer.getContentTypes());
}
public async handle(input: RepresentationConverterArgs): Promise<Representation> {
return this.quadsToRdf(input.representation, input.preferences);
}

View File

@ -6,7 +6,6 @@ import { INTERNAL_QUADS } from '../../util/ContentTypes';
import { BadRequestHttpError } from '../../util/errors/BadRequestHttpError';
import { pipeSafely } from '../../util/StreamUtil';
import { CONTENT_TYPE } from '../../util/UriConstants';
import { supportsConversion } from './ConversionUtil';
import type { RepresentationConverterArgs } from './RepresentationConverter';
import { TypedRepresentationConverter } from './TypedRepresentationConverter';
@ -22,10 +21,6 @@ export class RdfToQuadConverter extends TypedRepresentationConverter {
return { [INTERNAL_QUADS]: 1 };
}
public async canHandle(input: RepresentationConverterArgs): Promise<void> {
supportsConversion(input, await rdfParser.getContentTypes(), [ INTERNAL_QUADS ]);
}
public async handle(input: RepresentationConverterArgs): Promise<Representation> {
return this.rdfToQuads(input.representation, input.identifier.path);
}

View File

@ -1,4 +1,6 @@
import { supportsConversion } from './ConversionUtil';
import { RepresentationConverter } from './RepresentationConverter';
import type { RepresentationConverterArgs } from './RepresentationConverter';
/**
* A {@link RepresentationConverter} that allows requesting the supported types.
@ -17,4 +19,13 @@ export abstract class TypedRepresentationConverter extends RepresentationConvert
* @returns A promise resolving to a hash mapping content type to a priority number.
*/
public abstract getOutputTypes(): Promise<Record<string, number>>;
/**
* Verifies whether this converter supports the input.
*/
public async canHandle(args: RepresentationConverterArgs): Promise<void> {
const types = [ this.getInputTypes(), this.getOutputTypes() ];
const [ inputTypes, outputTypes ] = await Promise.all(types);
supportsConversion(args, Object.keys(inputTypes), Object.keys(outputTypes));
}
}