fix: Let Representations always have a body

This is relevant when the request has a content-type
but no data.
This commit is contained in:
Joachim Van Herwegen
2021-10-11 15:21:40 +02:00
parent f1ef2ced03
commit 5613ff9e71
36 changed files with 95 additions and 63 deletions

View File

@@ -24,6 +24,11 @@ export class BasicRepresentation implements Representation {
public readonly metadata: RepresentationMetadata;
public readonly binary: boolean;
/**
* An empty Representation
*/
public constructor();
/**
* @param data - The representation data
* @param metadata - The representation metadata
@@ -86,13 +91,15 @@ export class BasicRepresentation implements Representation {
);
public constructor(
data: Readable | any[] | string,
metadata: RepresentationMetadata | MetadataRecord | MetadataIdentifier | string,
data?: Readable | any[] | string,
metadata?: RepresentationMetadata | MetadataRecord | MetadataIdentifier | string,
metadataRest?: MetadataRecord | string | boolean,
binary?: boolean,
) {
if (typeof data === 'string' || Array.isArray(data)) {
data = guardedStreamFrom(data);
} else if (!data) {
data = guardedStreamFrom([]);
}
this.data = guardStream(data);
@@ -110,4 +117,11 @@ export class BasicRepresentation implements Representation {
}
this.binary = binary;
}
/**
* Data should only be interpreted if there is a content type.
*/
public get isEmpty(): boolean {
return !this.metadata.contentType;
}
}

View File

@@ -19,4 +19,10 @@ export interface Representation {
* (as opposed to complex objects).
*/
binary: boolean;
/**
* Whether the data stream is empty.
* This being true does not imply that the data stream has a length of more than 0,
* only that it is a possibility and should be read to be sure.
*/
isEmpty: boolean;
}