-
Notifications
You must be signed in to change notification settings - Fork 576
/
negotiation.ts
91 lines (85 loc) · 3.21 KB
/
negotiation.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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
// Copyright 2018-2022 the Deno authors. All rights reserved. MIT license.
/**
* Contains the functions {@linkcode accepts}, {@linkcode acceptsEncodings}, and
* {@linkcode acceptsLanguages} to provide content negotiation capabilities.
*
* @module
*/
import { preferredEncodings } from "./_negotiation/encoding.ts";
import { preferredLanguages } from "./_negotiation/language.ts";
import { preferredMediaTypes } from "./_negotiation/media_type.ts";
export type Request = {
headers: {
get(key: string): string | null;
};
};
/** Returns an array of media types accepted by the request, in order of
* preference. If there are no media types supplied in the request, then any
* media type selector will be returned. */
export function accepts(request: Request): string[];
/** For a given set of media types, return the best match accepted in the
* request. If no media type matches, then the function returns `undefined`. */
export function accepts(
request: Request,
...types: string[]
): string | undefined;
export function accepts(
request: Request,
...types: string[]
): string | string[] | undefined {
const accept = request.headers.get("accept");
return types.length
? accept ? preferredMediaTypes(accept, types)[0] : types[0]
: accept
? preferredMediaTypes(accept)
: ["*/*"];
}
/** Returns an array of content encodings accepted by the request, in order of
* preference. If there are no encoding supplied in the request, then `["*"]`
* is returned, implying any encoding is accepted. */
export function acceptsEncodings(request: Request): string[];
/** For a given set of content encodings, return the best match accepted in the
* request. If no content encodings match, then the function returns
* `undefined`.
*
* **NOTE:** You should always supply `identity` as one of the encodings
* to ensure that there is a match when the `Accept-Encoding` header is part
* of the request. */
export function acceptsEncodings(
request: Request,
...encodings: string[]
): string | undefined;
export function acceptsEncodings(
request: Request,
...encodings: string[]
): string | string[] | undefined {
const acceptEncoding = request.headers.get("accept-encoding");
return encodings.length
? acceptEncoding
? preferredEncodings(acceptEncoding, encodings)[0]
: encodings[0]
: acceptEncoding
? preferredEncodings(acceptEncoding)
: ["*"];
}
/** Returns an array of languages accepted by the request, in order of
* preference. If there are no languages supplied in the request, then `["*"]`
* is returned, imply any language is accepted. */
export function acceptsLanguages(request: Request): string[];
/** For a given set of languages, return the best match accepted in the request.
* If no languages match, then the function returns `undefined`. */
export function acceptsLanguages(
request: Request,
...langs: string[]
): string | undefined;
export function acceptsLanguages(
request: Request,
...langs: string[]
): string | string[] | undefined {
const acceptLanguage = request.headers.get("accept-language");
return langs.length
? acceptLanguage ? preferredLanguages(acceptLanguage, langs)[0] : langs[0]
: acceptLanguage
? preferredLanguages(acceptLanguage)
: ["*"];
}