feat(http)!: cookie decorators, setCookie, and string | string[] response headers (CARD-14, #113)
Reading a cookie meant parsing the Cookie header by hand, and a response
could not carry more than one Set-Cookie: response header values were
Record<string, string>, and the fetch adapters would have stringified an
array into one broken "a,b" cookie anyway.
- @cookie('name') / @cookies() sit next to @header/@headers as a new
'cookies' arg source. The header is parsed on first use per request and
cached on the context, so a route without a cookie argument never parses
it. RFC 6265: first value wins, %-sequences decoded, malformed pairs
skipped (never a 400), undecodable values kept raw. Several cookie lines
are joined with "; ". The map has no prototype. @needs('cookies') reads
the same map, and 'cookies' joins the seed-key allow-list.
- setCookie(name, value, options) serialises one Set-Cookie value with
HttpOnly, SameSite=Lax, Path=/ and Secure by default (D10), and throws,
naming the field, on anything that could inject a header or that browsers
reject (sameSite none / partitioned without secure).
- Response header values widen to string | string[] (D11) across
TransformerFn, ErrorResponse, HttpError(Like).headers, ResponseShape and
the neutral outcome. Node writes a set-cookie array as one line per
element and joins every other array with ", " before writeHead; the
fetch adapters build a Headers and append each set-cookie element.
mergeInjectedHeaders keeps arrays by reference and joins a vary array
before merging it.
- OpenAPI maps a cookie argument to an in: cookie parameter.
BREAKING CHANGE: code that reads a header value from TransformerFn,
ErrorResponse, HttpError or ResponseShape as a string must handle string[].
Signed-off-by: Diego Resendez <diego.resendez@zero-oneit.com>
This commit is contained in:
24 files changed
+781
-34
No files matched your search
+11
-4
@@ -26,7 +26,7 @@ export interface OpenApiDoc {
|
||||
|
||||
type Parameter = {
|
||||
name: string;
|
||||
in: 'path' | 'query' | 'header';
|
||||
in: 'path' | 'query' | 'header' | 'cookie';
|
||||
required: boolean;
|
||||
schema: { type: 'string'; pattern?: string };
|
||||
};
|
||||
@@ -57,13 +57,20 @@ function pathParams(pattern: string): Parameter[] {
|
||||
});
|
||||
}
|
||||
|
||||
/** Query and header parameters declared by the handler's `@query` / `@headers` / `@header` argument decorators. */
|
||||
/** Where each argument source sits in an OpenAPI parameter list; sources absent here are not parameters. */
|
||||
const ARG_LOCATION: Partial<Record<ArgSpec['source'], Parameter['in']>> = {
|
||||
query: 'query',
|
||||
headers: 'header',
|
||||
cookies: 'cookie',
|
||||
};
|
||||
|
||||
/** Query, header and cookie parameters declared by `@query` / `@headers` / `@header` / `@cookies` / `@cookie`. */
|
||||
function argParams(args: ArgSpec[]): Parameter[] {
|
||||
const params: Parameter[] = [];
|
||||
|
||||
for (const arg of args) {
|
||||
if (arg.source !== 'query' && arg.source !== 'headers') continue;
|
||||
const location = arg.source === 'query' ? 'query' : 'header';
|
||||
const location = ARG_LOCATION[arg.source];
|
||||
if (!location) continue;
|
||||
const names = arg.keys ?? (arg.key ? [arg.key] : []);
|
||||
for (const name of names) params.push({ name, in: location, required: false, schema: { type: 'string' } });
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user