-
Notifications
You must be signed in to change notification settings - Fork 97
/
Copy pathaccept.ts
26 lines (23 loc) · 1.03 KB
/
accept.ts
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
/**
* A function for matching the 'Accept' header to an explicit MIME type.
*
* @param acceptHeader the header as specified by the caller
* @param responseContentType the content type that we expect to return
* @return true if the specified content-type is acceptable given the Accept value
*/
export const acceptMatches = (acceptHeader: string | null | undefined, responseContentType: string): boolean => {
if (acceptHeader === null || acceptHeader === undefined) {
return true;
}
// see: https://datatracker.ietf.org/doc/html/rfc7231#section-5.3.2
// we only care if anything in Accept matches a content-type we want to respond with,
// so we disregard all of the accept-params
const acceptableContentTypes = acceptHeader.split(",").map((s) => s.split(";")[0].trim());
const responsePrimaryType = responseContentType.split("/")[0];
for (const type of acceptableContentTypes) {
if (type === "*/*" || type === `${responsePrimaryType}/*` || type === responseContentType) {
return true;
}
}
return false;
};