Skip to content

Commit 267c71f

Browse files
committed
fix(response): correctly deep-merge options
don't allow `undefined` to overwrite a non-nullish value
1 parent 847f605 commit 267c71f

File tree

3 files changed

+25
-12
lines changed

3 files changed

+25
-12
lines changed

packages/cookie/src/index.ts

+10-10
Original file line numberDiff line numberDiff line change
@@ -55,16 +55,16 @@ export function parse(
5555
return obj
5656
}
5757

58-
export type SerializeOptions = Partial<{
59-
encode: (str: string) => string
60-
maxAge: number
61-
domain: string
62-
path: string
63-
httpOnly: boolean
64-
secure: boolean
65-
sameSite: boolean | "Strict" | "strict" | "Lax" | "lax" | "None" | "none" | string
66-
expires: Date
67-
}>
58+
export type SerializeOptions = {
59+
encode?: ((str: string) => string) | null | undefined
60+
maxAge?: number | null | undefined
61+
domain?: string | null | undefined
62+
path?: string | null | undefined
63+
httpOnly?: boolean | null | undefined
64+
secure?: boolean | null | undefined
65+
sameSite?: boolean | "Strict" | "strict" | "Lax" | "lax" | "None" | "none" | string | null | undefined
66+
expires?: Date | null | undefined
67+
}
6868

6969
export function serialize(name: string, val: string, options: SerializeOptions = {}): string {
7070
options.encode ??= encodeURIComponent

packages/response/src/cookie.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,9 @@ import type { HasIncomingHeaders, HasOutgoingHeaders, HasReq } from "./types"
44

55
type SetCookieResponse = HasOutgoingHeaders & HasReq<HasIncomingHeaders>
66
export type SetCookieOptions = cookie.SerializeOptions & {
7-
sign?: ((cookieValue: string) => string) | undefined
7+
sign?: ((cookieValue: string) => string) | undefined | null
88
}
9+
910
export function setCookie(res: SetCookieResponse, name, value: string, options: SetCookieOptions = {}): void {
1011
if (options.maxAge != null) {
1112
options.expires = new Date(Date.now() + options.maxAge)

packages/response/src/prototype.ts

+13-1
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,19 @@ export class Response<Req extends Request<unknown> = Request<unknown>> extends S
119119

120120
cookie(name: string, value: string, options?: SetCookieOptions): this {
121121
if (options == null) options = this.appSettings?.setCookieOptions
122-
else options = Object.assign({}, this.appSettings?.setCookieOptions, options)
122+
else {
123+
options = [this.appSettings?.setCookieOptions, options].reduce<SetCookieOptions>(
124+
(optionsAccumulator, optionsSource) => {
125+
if (optionsSource == null) return optionsAccumulator
126+
for (const [key, value] of Object.entries(optionsSource)) {
127+
if (value === undefined) continue
128+
optionsAccumulator[key] = value
129+
}
130+
return optionsAccumulator
131+
},
132+
{},
133+
)
134+
}
123135
setCookie(this, name, value, options)
124136
return this
125137
}

0 commit comments

Comments
 (0)