mirror of
https://github.com/CommunitySolidServer/CommunitySolidServer.git
synced 2024-10-03 14:55:10 +00:00
refactor: Simplify supportsMediaTypeConversion arguments.
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
import type { Representation } from '../../ldp/representation/Representation';
|
||||
import type { ValuePreferences } from '../../ldp/representation/RepresentationPreferences';
|
||||
import { getLoggerFor } from '../../logging/LogUtil';
|
||||
import { supportsConversion, matchesMediaType } from './ConversionUtil';
|
||||
import { matchesMediaType } from './ConversionUtil';
|
||||
import type { RepresentationConverterArgs } from './RepresentationConverter';
|
||||
import { TypedRepresentationConverter } from './TypedRepresentationConverter';
|
||||
|
||||
@@ -43,12 +43,6 @@ export class ChainedConverter extends TypedRepresentationConverter {
|
||||
return this.last.getOutputTypes();
|
||||
}
|
||||
|
||||
public async canHandle(input: RepresentationConverterArgs): Promise<void> {
|
||||
// We assume a chain can be constructed, otherwise there would be a configuration issue
|
||||
// So we only check if the input can be parsed and the preferred type can be written
|
||||
supportsConversion(input, await this.first.getInputTypes(), await this.last.getOutputTypes());
|
||||
}
|
||||
|
||||
public async handle(input: RepresentationConverterArgs): Promise<Representation> {
|
||||
const args = { ...input };
|
||||
for (let i = 0; i < this.converters.length - 1; ++i) {
|
||||
|
||||
@@ -1,9 +1,7 @@
|
||||
import type { ValuePreferences } from '../../ldp/representation/RepresentationPreferences';
|
||||
import { INTERNAL_ALL } from '../../util/ContentTypes';
|
||||
import { BadRequestHttpError } from '../../util/errors/BadRequestHttpError';
|
||||
import { InternalServerError } from '../../util/errors/InternalServerError';
|
||||
import { NotImplementedHttpError } from '../../util/errors/NotImplementedHttpError';
|
||||
import type { RepresentationConverterArgs } from './RepresentationConverter';
|
||||
|
||||
/**
|
||||
* Filters media types based on the given preferences.
|
||||
@@ -91,20 +89,20 @@ export const matchesMediaType = (mediaA: string, mediaB: string): boolean => {
|
||||
* - Checks if the input type is supported by the parser.
|
||||
* - Checks if the parser can produce one of the preferred output types.
|
||||
* Throws an error with details if conversion is not possible.
|
||||
* @param request - Incoming arguments.
|
||||
* @param supportedIn - Media types that can be parsed by the converter.
|
||||
* @param supportedOut - Media types that can be produced by the converter.
|
||||
* @param inputType - Actual input type.
|
||||
* @param outputTypes - Acceptable output types.
|
||||
* @param convertorIn - Media types that can be parsed by the converter.
|
||||
* @param convertorOut - Media types that can be produced by the converter.
|
||||
*/
|
||||
export const supportsConversion = (request: RepresentationConverterArgs, supportedIn: ValuePreferences,
|
||||
supportedOut: ValuePreferences): void => {
|
||||
const inType = request.representation.metadata.contentType;
|
||||
if (!inType) {
|
||||
throw new BadRequestHttpError('No content type indicated on request.');
|
||||
}
|
||||
if (!Object.keys(supportedIn).some((type): boolean => matchesMediaType(inType, type)) ||
|
||||
matchingMediaTypes(request.preferences.type, supportedOut).length === 0) {
|
||||
export const supportsMediaTypeConversion = (
|
||||
inputType = 'unknown', outputTypes: ValuePreferences = {},
|
||||
convertorIn: ValuePreferences = {}, convertorOut: ValuePreferences = {},
|
||||
): void => {
|
||||
if (!Object.keys(convertorIn).some((type): boolean => matchesMediaType(inputType, type)) ||
|
||||
matchingMediaTypes(outputTypes, convertorOut).length === 0) {
|
||||
throw new NotImplementedHttpError(
|
||||
`Can only convert from ${Object.keys(supportedIn)} to ${Object.keys(supportedOut)}.`,
|
||||
`Cannot convert from ${inputType} to ${Object.keys(outputTypes)
|
||||
}, only from ${Object.keys(convertorIn)} to ${Object.keys(convertorOut)}.`,
|
||||
);
|
||||
}
|
||||
};
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import type { ValuePreferences } from '../../ldp/representation/RepresentationPreferences';
|
||||
import { supportsConversion } from './ConversionUtil';
|
||||
import { supportsMediaTypeConversion } from './ConversionUtil';
|
||||
import { RepresentationConverter } from './RepresentationConverter';
|
||||
import type { RepresentationConverterArgs } from './RepresentationConverter';
|
||||
|
||||
@@ -22,7 +22,8 @@ export abstract class TypedRepresentationConverter extends RepresentationConvert
|
||||
*/
|
||||
public async canHandle(args: RepresentationConverterArgs): Promise<void> {
|
||||
const types = [ this.getInputTypes(), this.getOutputTypes() ];
|
||||
const { contentType } = args.representation.metadata;
|
||||
const [ inputTypes, outputTypes ] = await Promise.all(types);
|
||||
supportsConversion(args, inputTypes, outputTypes);
|
||||
supportsMediaTypeConversion(contentType, args.preferences.type, inputTypes, outputTypes);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user