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:
parent
8cd3f7d2e5
commit
0bd73115cc
@ -1,7 +1,7 @@
|
|||||||
import type { Representation } from '../../ldp/representation/Representation';
|
import type { Representation } from '../../ldp/representation/Representation';
|
||||||
import type { ValuePreferences } from '../../ldp/representation/RepresentationPreferences';
|
import type { ValuePreferences } from '../../ldp/representation/RepresentationPreferences';
|
||||||
import { getLoggerFor } from '../../logging/LogUtil';
|
import { getLoggerFor } from '../../logging/LogUtil';
|
||||||
import { supportsConversion, matchesMediaType } from './ConversionUtil';
|
import { matchesMediaType } from './ConversionUtil';
|
||||||
import type { RepresentationConverterArgs } from './RepresentationConverter';
|
import type { RepresentationConverterArgs } from './RepresentationConverter';
|
||||||
import { TypedRepresentationConverter } from './TypedRepresentationConverter';
|
import { TypedRepresentationConverter } from './TypedRepresentationConverter';
|
||||||
|
|
||||||
@ -43,12 +43,6 @@ export class ChainedConverter extends TypedRepresentationConverter {
|
|||||||
return this.last.getOutputTypes();
|
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> {
|
public async handle(input: RepresentationConverterArgs): Promise<Representation> {
|
||||||
const args = { ...input };
|
const args = { ...input };
|
||||||
for (let i = 0; i < this.converters.length - 1; ++i) {
|
for (let i = 0; i < this.converters.length - 1; ++i) {
|
||||||
|
@ -1,9 +1,7 @@
|
|||||||
import type { ValuePreferences } from '../../ldp/representation/RepresentationPreferences';
|
import type { ValuePreferences } from '../../ldp/representation/RepresentationPreferences';
|
||||||
import { INTERNAL_ALL } from '../../util/ContentTypes';
|
import { INTERNAL_ALL } from '../../util/ContentTypes';
|
||||||
import { BadRequestHttpError } from '../../util/errors/BadRequestHttpError';
|
|
||||||
import { InternalServerError } from '../../util/errors/InternalServerError';
|
import { InternalServerError } from '../../util/errors/InternalServerError';
|
||||||
import { NotImplementedHttpError } from '../../util/errors/NotImplementedHttpError';
|
import { NotImplementedHttpError } from '../../util/errors/NotImplementedHttpError';
|
||||||
import type { RepresentationConverterArgs } from './RepresentationConverter';
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Filters media types based on the given preferences.
|
* 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 input type is supported by the parser.
|
||||||
* - Checks if the parser can produce one of the preferred output types.
|
* - Checks if the parser can produce one of the preferred output types.
|
||||||
* Throws an error with details if conversion is not possible.
|
* Throws an error with details if conversion is not possible.
|
||||||
* @param request - Incoming arguments.
|
* @param inputType - Actual input type.
|
||||||
* @param supportedIn - Media types that can be parsed by the converter.
|
* @param outputTypes - Acceptable output types.
|
||||||
* @param supportedOut - Media types that can be produced by the converter.
|
* @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,
|
export const supportsMediaTypeConversion = (
|
||||||
supportedOut: ValuePreferences): void => {
|
inputType = 'unknown', outputTypes: ValuePreferences = {},
|
||||||
const inType = request.representation.metadata.contentType;
|
convertorIn: ValuePreferences = {}, convertorOut: ValuePreferences = {},
|
||||||
if (!inType) {
|
): void => {
|
||||||
throw new BadRequestHttpError('No content type indicated on request.');
|
if (!Object.keys(convertorIn).some((type): boolean => matchesMediaType(inputType, type)) ||
|
||||||
}
|
matchingMediaTypes(outputTypes, convertorOut).length === 0) {
|
||||||
if (!Object.keys(supportedIn).some((type): boolean => matchesMediaType(inType, type)) ||
|
|
||||||
matchingMediaTypes(request.preferences.type, supportedOut).length === 0) {
|
|
||||||
throw new NotImplementedHttpError(
|
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 type { ValuePreferences } from '../../ldp/representation/RepresentationPreferences';
|
||||||
import { supportsConversion } from './ConversionUtil';
|
import { supportsMediaTypeConversion } from './ConversionUtil';
|
||||||
import { RepresentationConverter } from './RepresentationConverter';
|
import { RepresentationConverter } from './RepresentationConverter';
|
||||||
import type { RepresentationConverterArgs } from './RepresentationConverter';
|
import type { RepresentationConverterArgs } from './RepresentationConverter';
|
||||||
|
|
||||||
@ -22,7 +22,8 @@ export abstract class TypedRepresentationConverter extends RepresentationConvert
|
|||||||
*/
|
*/
|
||||||
public async canHandle(args: RepresentationConverterArgs): Promise<void> {
|
public async canHandle(args: RepresentationConverterArgs): Promise<void> {
|
||||||
const types = [ this.getInputTypes(), this.getOutputTypes() ];
|
const types = [ this.getInputTypes(), this.getOutputTypes() ];
|
||||||
|
const { contentType } = args.representation.metadata;
|
||||||
const [ inputTypes, outputTypes ] = await Promise.all(types);
|
const [ inputTypes, outputTypes ] = await Promise.all(types);
|
||||||
supportsConversion(args, inputTypes, outputTypes);
|
supportsMediaTypeConversion(contentType, args.preferences.type, inputTypes, outputTypes);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,63 +1,34 @@
|
|||||||
import type { Representation } from '../../../../src/ldp/representation/Representation';
|
import type { ValuePreferences } from '../../../../src/ldp/representation/RepresentationPreferences';
|
||||||
import { RepresentationMetadata } from '../../../../src/ldp/representation/RepresentationMetadata';
|
|
||||||
import type {
|
|
||||||
ValuePreferences,
|
|
||||||
RepresentationPreferences,
|
|
||||||
} from '../../../../src/ldp/representation/RepresentationPreferences';
|
|
||||||
import type { ResourceIdentifier } from '../../../../src/ldp/representation/ResourceIdentifier';
|
|
||||||
import {
|
import {
|
||||||
matchesMediaType,
|
matchesMediaType,
|
||||||
matchingMediaTypes,
|
matchingMediaTypes,
|
||||||
supportsConversion,
|
supportsMediaTypeConversion,
|
||||||
} from '../../../../src/storage/conversion/ConversionUtil';
|
} from '../../../../src/storage/conversion/ConversionUtil';
|
||||||
import { InternalServerError } from '../../../../src/util/errors/InternalServerError';
|
import { InternalServerError } from '../../../../src/util/errors/InternalServerError';
|
||||||
|
|
||||||
describe('ConversionUtil', (): void => {
|
describe('ConversionUtil', (): void => {
|
||||||
const identifier: ResourceIdentifier = { path: 'path' };
|
describe('supportsMediaTypeConversion', (): void => {
|
||||||
let representation: Representation;
|
it('requires preferences.', async(): Promise<void> => {
|
||||||
let metadata: RepresentationMetadata;
|
expect((): any => supportsMediaTypeConversion()).toThrow();
|
||||||
|
});
|
||||||
|
|
||||||
beforeEach(async(): Promise<void> => {
|
|
||||||
metadata = new RepresentationMetadata();
|
|
||||||
representation = { metadata } as Representation;
|
|
||||||
});
|
|
||||||
|
|
||||||
describe('#supportsConversion', (): void => {
|
|
||||||
it('requires an input type.', async(): Promise<void> => {
|
it('requires an input type.', async(): Promise<void> => {
|
||||||
const preferences: RepresentationPreferences = {};
|
expect((): any => supportsMediaTypeConversion(undefined, { 'b/x': 1 }, { 'a/x': 1 }, { 'a/x': 1 }))
|
||||||
expect((): any => supportsConversion({ identifier, representation, preferences },
|
.toThrow('Cannot convert from unknown to b/x, only from a/x to a/x.');
|
||||||
{ 'a/x': 1 },
|
|
||||||
{ 'a/x': 1 }))
|
|
||||||
.toThrow('No content type indicated on request.');
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it('requires a matching input type.', async(): Promise<void> => {
|
it('requires a matching input type.', async(): Promise<void> => {
|
||||||
metadata.contentType = 'a/x';
|
expect((): any => supportsMediaTypeConversion('a/x', { 'b/x': 1 }, { 'c/x': 1 }, { 'a/x': 1 }))
|
||||||
const preferences: RepresentationPreferences =
|
.toThrow('Cannot convert from a/x to b/x, only from c/x to a/x.');
|
||||||
{ type: { 'b/x': 1 }};
|
|
||||||
expect((): any => supportsConversion({ identifier, representation, preferences },
|
|
||||||
{ 'c/x': 1 },
|
|
||||||
{ 'a/x': 1 }))
|
|
||||||
.toThrow('Can only convert from c/x to a/x.');
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it('requires a matching output type.', async(): Promise<void> => {
|
it('requires a matching output type.', async(): Promise<void> => {
|
||||||
metadata.contentType = 'a/x';
|
expect((): any => supportsMediaTypeConversion('a/x', { 'b/x': 1 }, { 'a/x': 1 }, { 'c/x': 1 }))
|
||||||
const preferences: RepresentationPreferences =
|
.toThrow('Cannot convert from a/x to b/x, only from a/x to c/x.');
|
||||||
{ type: { 'b/x': 1 }};
|
|
||||||
expect((): any => supportsConversion({ identifier, representation, preferences },
|
|
||||||
{ 'a/x': 1 },
|
|
||||||
{ 'c/x': 1 }))
|
|
||||||
.toThrow('Can only convert from a/x to c/x.');
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it('succeeds with a valid input and output type.', async(): Promise<void> => {
|
it('succeeds with a valid input and output type.', async(): Promise<void> => {
|
||||||
metadata.contentType = 'a/x';
|
expect(supportsMediaTypeConversion('a/x', { 'b/x': 1 }, { 'a/x': 1 }, { 'b/x': 1 }))
|
||||||
const preferences: RepresentationPreferences =
|
|
||||||
{ type: { 'b/x': 1 }};
|
|
||||||
expect(supportsConversion({ identifier, representation, preferences },
|
|
||||||
{ 'a/x': 1 },
|
|
||||||
{ 'b/x': 1 }))
|
|
||||||
.toBeUndefined();
|
.toBeUndefined();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
Loading…
x
Reference in New Issue
Block a user