perks/codemodel-v3/media-types.ts

105 строки
3.3 KiB
TypeScript
Исходник Обычный вид История

2018-11-06 22:03:52 +03:00
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
// Media Type is: type "/" [tree "."] subtype ["+" suffix] * [";" parameter]
const json = 'json';
const xml = 'xml';
const application = 'application';
const text = 'text';
const multipart = 'multipart';
const formData = 'form-data';
export enum KnownMediaType {
Json = 'application/json',
Xml = 'application/xml',
Stream = 'application/octet-stream',
Multipart = 'multipart/form-data',
Text = 'text/plain',
2019-05-06 18:59:30 +03:00
PSObject = '-PSObject-',
Hashtable = '-Hashtable-',
2018-11-06 22:03:52 +03:00
None = '-none-',
QueryParameter = '-query-parameter-',
UriParameter = '-uri-parameter-',
Header = '-header-',
Cookie = '-cookie-',
}
2019-08-28 22:11:40 +03:00
export function parseMediaType(mediaType: string) {
if (mediaType) {
const parsed = /(application|audio|font|example|image|message|model|multipart|text|video|x-(?:[0-9A-Za-z!#$%&'*+.^_`|~-]+))\/([0-9A-Za-z!#$%&'*.^_`|~-]+)\s*(?:\+([0-9A-Za-z!#$%&'*.^_`|~-]+))?\s*(?:;.\s*(\S*))?/g.exec(mediaType);
if (parsed) {
return {
type: parsed[1],
subtype: parsed[2],
suffix: parsed[3],
parameter: parsed[4]
};
}
}
return undefined;
}
2018-11-06 22:03:52 +03:00
export function knownMediaType(mediaType: string) {
const mt = parseMediaType(mediaType);
if (mt) {
if ((mt.subtype === json || mt.suffix === json) && (mt.type === application || mt.type === text)) {
return KnownMediaType.Json;
}
if ((mt.subtype === xml || mt.suffix === xml) && (mt.type === application || mt.type === text)) {
return KnownMediaType.Xml;
}
if (mt.subtype === 'audio' || mt.subtype === 'image' || mt.subtype === 'video' || mt.subtype === 'octet-stream') {
return KnownMediaType.Stream;
}
if (mt.type === 'multipart' && mt.subtype === 'form-data') {
return KnownMediaType.Multipart;
}
}
// pseudo-media types for figuring out how to de/serialize from from/to other types.
switch (mediaType) {
case 'header':
return KnownMediaType.Header;
case 'cookie':
return KnownMediaType.Cookie;
case 'urlencoding':
return KnownMediaType.Cookie;
}
return KnownMediaType.None;
}
export function normalizeMediaType(contentType: string) {
if (contentType) {
const mt = parseMediaType(contentType);
if (mt) {
return mt.suffix ?
`${mt.type}/${mt.subtype}+${mt.suffix}` :
`${mt.type}/${mt.subtype}`;
}
}
return undefined;
}
export function isMediaTypeJson(mediaType: string): boolean {
const mt = parseMediaType(mediaType);
return mt ? (mt.subtype === json || mt.suffix === json) && (mt.type === application || mt.type === text) : false;
}
export function isMediaTypeXml(mediaType: string): boolean {
const mt = parseMediaType(mediaType);
return mt ? (mt.subtype === xml || mt.suffix === xml) && (mt.type === application || mt.type === text) : false;
}
export function isMediaTypeMultipartFormData(mediaType: string): boolean {
const mt = parseMediaType(mediaType);
return mt ? mt.type === multipart && mt.subtype === formData : false;
}