feat: add simple body parser

This commit is contained in:
Joachim Van Herwegen
2020-06-10 11:41:15 +02:00
parent 70af46933b
commit d4f70d9c59
12 changed files with 213 additions and 1 deletions

View File

@@ -0,0 +1,52 @@
import { BodyParser } from './BodyParser';
import { HttpRequest } from '../../server/HttpRequest';
import { Quad } from 'rdf-js';
import { QuadRepresentation } from '../representation/QuadRepresentation';
import { RepresentationMetadata } from '../representation/RepresentationMetadata';
import { StreamParser } from 'n3';
import { TypedReadable } from '../../util/TypedReadable';
import { UnsupportedMediaTypeHttpError } from '../../util/errors/UnsupportedMediaTypeHttpError';
import 'jest-rdf';
export class SimpleBodyParser extends BodyParser {
private static readonly contentTypes = [
'application/n-quads',
'application/trig',
'application/n-triples',
'text/turtle',
'text/n3',
];
public async canHandle(input: HttpRequest): Promise<void> {
const contentType = input.headers['content-type'];
if (contentType && !SimpleBodyParser.contentTypes.some((type): boolean => contentType.includes(type))) {
throw new UnsupportedMediaTypeHttpError('This parser only supports RDF data.');
}
}
public async handle(input: HttpRequest): Promise<QuadRepresentation> {
const contentType = input.headers['content-type'];
if (!contentType) {
return undefined;
}
const specificType = contentType.split(';')[0];
const metadata: RepresentationMetadata = {
raw: [],
profiles: [],
contentType: specificType,
};
// StreamParser is a Readable but typings are incorrect at time of writing
const quads: TypedReadable<Quad> = input.pipe(new StreamParser()) as unknown as TypedReadable<Quad>;
return {
dataType: 'quad',
data: quads,
metadata,
};
}
}