pluto-http-client
Version:
HTTP client for NodeJS. Inspired in the Java JAX-RS spec so you can expect excellence, versatility and extensibility.
87 lines (86 loc) • 3.35 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.MediaType = void 0;
const http_header_reader_1 = require("../utils/http-header-reader");
class MediaType {
constructor(type, subtype, parameters) {
this._type = type;
this._subtype = subtype;
this._parameters = parameters;
}
equals(other) {
if (other instanceof MediaType) {
return this.type === other.type && this.subtype === other.subtype;
}
return false;
}
get type() {
return this._type;
}
get subtype() {
return this._subtype;
}
isWildcardType() {
return this.type === "*";
}
isWildcardSubtype() {
return this.subtype === "*";
}
isCompatible(other) {
if (!other) {
return false;
}
else {
return (this.type.toLowerCase() == other.type.toLowerCase()
|| this.isWildcardType()
|| other.isWildcardType())
&& (this.subtype.toLowerCase() === other.subtype.toLowerCase()
|| this.isWildcardSubtype()
|| other.isWildcardSubtype());
}
}
toString() {
const buffer = [`${this.type}/${this.subtype}`];
if (this._parameters) {
for (const [k, v] of this._parameters.entries()) {
buffer.push(";", k, "=");
http_header_reader_1.HttpHeaderReader.appendQuotedIfNonToken(buffer, v);
}
}
return buffer.join("");
}
static fromString(header) {
if (!header) {
throw new Error("Mediatype is null");
}
const reader = new http_header_reader_1.HttpHeaderReader(header);
reader.hasNext();
const type = reader.nextToken();
reader.nextSeparator("/");
const subtype = reader.nextToken();
let params;
if (reader.hasNext()) {
params = http_header_reader_1.HttpHeaderReader.readParameters(reader);
}
if (!type || !subtype) {
throw new Error("Header with media type or subtype missing");
}
return new MediaType(type, subtype, params);
}
}
exports.MediaType = MediaType;
MediaType.WILDCARD_TYPE = new MediaType("*", "*");
MediaType.ANY_TEXT_TYPE = new MediaType("text", "*");
MediaType.APPLICATION_XML_TYPE = new MediaType("application", "xml");
MediaType.APPLICATION_ATOM_XML_TYPE = new MediaType("application", "atom+xml");
MediaType.APPLICATION_XHTML_XML_TYPE = new MediaType("application", "xhtml+xml");
MediaType.APPLICATION_SVG_XML_TYPE = new MediaType("application", "svg+xml");
MediaType.APPLICATION_JSON_TYPE = new MediaType("application", "json");
MediaType.APPLICATION_FORM_URLENCODED_TYPE = new MediaType("application", "x-www-form-urlencoded");
MediaType.MULTIPART_FORM_DATA_TYPE = new MediaType("multipart", "form-data");
MediaType.APPLICATION_OCTET_STREAM_TYPE = new MediaType("application", "octet-stream");
MediaType.TEXT_PLAIN_TYPE = new MediaType("text", "plain");
MediaType.TEXT_XML_TYPE = new MediaType("text", "xml");
MediaType.TEXT_HTML_TYPE = new MediaType("text", "html");
MediaType.SERVER_SENT_EVENTS_TYPE = new MediaType("text", "event-stream");
MediaType.APPLICATION_JSON_PATCH_JSON_TYPE = new MediaType("application", "json-patch+json");