-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcontent-negotiation.ts
195 lines (160 loc) · 6.77 KB
/
content-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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
// deno-lint-ignore-file
import { notAcceptable, unsupportedMediaType } from "https://ghuc.cc/worker-tools/response-creators/index.ts";
import negotiated from 'https://cdn.skypack.dev/negotiated@1.0.2';
import { Awaitable } from './utils/common-types.ts';
import { Context } from './index.ts'
const weightSortFn = <X extends { weight: number }>(a: X, b: X) => a.weight >= b.weight ? a : b;
const ACCEPT = 'Accept';
const ACCEPT_ENCODING = 'Accept-Encoding';
const ACCEPT_LANGUAGE = 'Accept-Language';
const CONTENT_TYPE = 'Content-Type';
const CONTENT_LANGUAGE = 'Content-Language';
const CONTENT_ENCODING = 'Content-Encoding';
const VARY = 'Vary';
export interface ContentType<T> {
/** The best content type _acceptable to the client_. */
type: T,
}
export interface ContentLanguage<T> {
/** The best language _acceptable to the client_. */
language: T,
}
export interface ContentEncoding<T> {
/** The best encoding _acceptable to the client_. */
encoding: T,
}
export interface Accepted<T> {
/** The request's `Content-Type` header iff acceptable to this endpoint */
accepted: T,
}
export interface AcceptedLanguage<T> {
/** The request's `Language` header if (and only if) accepted by this endpoint */
acceptedLanguage: T,
}
export interface AcceptedEncoding<T> {
/** The request's `Encoding` header if (and only if) accepted by this endpoint */
acceptedEncoding: T,
}
/**
* Performs content negotiation over the content type of the response.
* @param types The content types _provided_ by this endpoint.
*/
export function contentTypes<T extends string, TS extends readonly T[]>(
types: TS
): <X extends Context>(ax: Awaitable<X>) => Promise<X & ContentType<TS[number]>> {
return async ax => {
const ctx = await ax;
const { headers } = ctx.request;
const type = [...negotiated.mediaTypes(headers.get(ACCEPT)) as any]
.filter(t => !types || types.includes(t.type))
.reduce(weightSortFn, { weight: -1 }).type as TS[number]
if (headers.has(ACCEPT) && types && !type) throw notAcceptable();
ctx.effects.push(response => {
if (!response.headers.has(CONTENT_TYPE)) response.headers.set(CONTENT_TYPE, type)
// If the server accepts more than 1 option, we set the vary header for correct caching
if ((types?.length ?? 0) > 1) response.headers.append(VARY, ACCEPT);
return response;
})
return Object.assign(ctx, { type })
}
}
/**
* Performs content negotiation over the content language of the response.
* @param languages The languages _provided_ by this endpoint.
*/
export function contentLanguages<T extends string, TS extends readonly T[]>(
languages: TS
): <X extends Context>(ax: Awaitable<X>) => Promise<X & ContentLanguage<TS[number]>> {
return async ax => {
const ctx = await ax;
const { headers } = ctx.request;
const language = [...negotiated.languages(headers.get(ACCEPT_LANGUAGE)) as any]
.filter(l => !languages || languages.includes(l.language))
.reduce(weightSortFn, { weight: -1 }).language as TS[number]
if (headers.has(ACCEPT_LANGUAGE) && languages && !language) throw notAcceptable();
ctx.effects.push(response => {
if (!response.headers.has(CONTENT_LANGUAGE)) response.headers.set(CONTENT_LANGUAGE, language)
// If the server accepts more than 1 option, we set the vary header for correct caching
if ((languages?.length ?? 0) > 1) response.headers.append(VARY, ACCEPT_LANGUAGE);
return response;
})
return Object.assign(ctx, { language })
}
}
/**
* Performs content negotiation over the content encoding of the response.
* @param encodings The encodings _provided_ by this endpoint.
*/
export function contentEncodings<T extends string, TS extends readonly T[]>(
encodings: TS
): <X extends Context>(ax: Awaitable<X>) => Promise<X & ContentEncoding<TS[number]>> {
return async ax => {
const ctx = await ax;
const { headers } = ctx.request;
const encoding = [...negotiated.encodings(headers.get(ACCEPT_ENCODING)) as any]
.filter(e => !encodings || encodings.includes(e.encoding))
.reduce(weightSortFn, { weight: -1 }).encoding as TS[number];
// TODO: how to handle status errors in middleware??
if (headers.has(ACCEPT_ENCODING) && encodings && !encoding) throw notAcceptable();
ctx.effects!.push(response => {
if (!response.headers.has(CONTENT_ENCODING)) response.headers.set(CONTENT_ENCODING, encoding)
// If the server accepts more than 1 option, we set the vary header for correct caching
if ((encodings?.length ?? 0) > 1) response.headers.append(VARY, ACCEPT_ENCODING);
return response
})
return Object.assign(ctx, { encoding })
}
}
export {
contentTypes as provides,
contentLanguages as providesLanguages,
contentEncodings as providesEncodings,
}
/**
* Determines if a request body content type is _acceptable_ to this endpoint.
* @param types The content types _acceptable_ to this endpoint.
*/
export function accepts<T extends string, TS extends readonly T[]>(
types: TS
): <X extends Context>(ax: Awaitable<X>) => Promise<X & Accepted<TS[number]>> {
return async ax => {
const ctx = await ax;
const { headers } = ctx.request;
const accepted =
[...negotiated.mediaTypes(headers.get(CONTENT_TYPE))][0]?.type as TS[number];
if (types?.length && !types.includes(accepted)) throw unsupportedMediaType();
return Object.assign(ctx, { accepted })
}
}
/**
* Determines if a request body content language is _acceptable_ to this endpoint.
* @param languages The languages (of the request body) _acceptable_ to this endpoint.
*/
export function acceptsLanguages<T extends string, TS extends readonly T[]>(
languages: TS
): <X extends Context>(ax: Awaitable<X>) => Promise<X & AcceptedLanguage<TS[number]>> {
return async ax => {
const ctx = await ax;
const { headers } = ctx.request;
const acceptedLanguage =
[...negotiated.languages(headers.get(CONTENT_LANGUAGE)) as any][0]?.language as TS[number];
if (languages?.length && !languages.includes(acceptedLanguage)) throw notAcceptable();
return Object.assign(ctx, { acceptedLanguage })
}
}
/**
* Determines if a request body content encoding is _acceptable_ to this endpoint.
* @param encodings The body encodings _acceptable_ to this endpoint.
*/
export function acceptsEncodings<T extends string, TS extends readonly T[]>(
encodings: TS
): <X extends Context>(ax: Awaitable<X>) => Promise<X & AcceptedEncoding<TS[number]>> {
return async ax => {
const ctx = await ax;
const { headers } = ctx.request;
const acceptedEncoding =
[...negotiated.encodings(headers.get(CONTENT_ENCODING)) as any][0]?.encoding as TS[number];
if (encodings?.length && !encodings.includes(acceptedEncoding)) throw notAcceptable();
return Object.assign(ctx, { acceptedEncoding })
}
}