-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathcontentType.ts
More file actions
44 lines (36 loc) · 1.28 KB
/
contentType.ts
File metadata and controls
44 lines (36 loc) · 1.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.
export type ContentType = {
mediaType: string;
parameters: Record<string, string>;
}
export function parseContentType(contentTypeValue: string | undefined): ContentType | undefined {
if (!contentTypeValue) {
return undefined;
}
const [mediaType, ...args] = contentTypeValue.split(";").map((s) => s.trim().toLowerCase());
const parameters: Record<string, string> = {};
for (const param of args) {
const [key, value] = param.split("=").map((s) => s.trim().toLowerCase());
if (key && value) {
parameters[key] = value;
}
}
return { mediaType, parameters };
}
// Determine whether a content type string is a valid JSON content type.
// https://docs.microsoft.com/en-us/azure/azure-app-configuration/howto-leverage-json-content-type
export function isJsonContentType(contentType: ContentType | undefined): boolean {
const mediaType = contentType?.mediaType;
if (!mediaType) {
return false;
}
const typeParts: string[] = mediaType.split("/");
if (typeParts.length !== 2) {
return false;
}
if (typeParts[0] !== "application") {
return false;
}
return typeParts[1].split("+").includes("json");
}