Skip to content

Commit ddf37ed

Browse files
committed
Accept URL parameter in getCookies and setCookie
This is a version of PR #261 that is compatible with our v5 TypeScript code.
1 parent aa1ee67 commit ddf37ed

File tree

2 files changed

+46
-27
lines changed

2 files changed

+46
-27
lines changed

lib/__tests__/cookieJar.spec.ts

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ import { CookieJar } from '../cookie/cookieJar'
3434
import type { SerializedCookieJar } from '../cookie/constants'
3535
import { MemoryCookieStore } from '../memstore'
3636
import { Store } from '../store'
37-
import { ParameterError } from '../validators'
3837

3938
// ported from:
4039
// - test/api_test.js (cookie jar tests)
@@ -1173,7 +1172,7 @@ it('should fix issue #145 - missing 2nd url parameter', () => {
11731172
expect(
11741173
// @ts-expect-error test case explicitly violates the expected function signature
11751174
() => cookieJar.setCookie('x=y; Domain=example.com; Path=/'),
1176-
).toThrow(ParameterError)
1175+
).toThrowError('`url` argument is invalid')
11771176
})
11781177

11791178
it('should fix issue #197 - CookieJar().setCookie throws an error when empty cookie is passed', async () => {
@@ -1241,6 +1240,21 @@ it('should fix issue #154 - Expiry should not be affected by creation date', asy
12411240
expect(updatedCookies[0]?.expiryTime()).toBe(now + 60 * 1000 + 1000)
12421241
})
12431242

1243+
it('should fix issue #261 - URL objects should be accepted in setCookie', async () => {
1244+
const jar = new CookieJar()
1245+
const url = new URL('https://example.com')
1246+
await jar.setCookie('foo=bar; Max-Age=60;', url)
1247+
const cookies = await jar.getCookies(url)
1248+
expect(cookies).toEqual([
1249+
expect.objectContaining({
1250+
key: 'foo',
1251+
value: 'bar',
1252+
path: '/',
1253+
domain: 'example.com',
1254+
}),
1255+
])
1256+
})
1257+
12441258
// special use domains under a sub-domain
12451259
describe.each(['local', 'example', 'invalid', 'localhost', 'test'])(
12461260
'when special use domain is dev.%s',

lib/cookie/cookieJar.ts

Lines changed: 30 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,15 @@ import urlParse from 'url-parse'
22

33
import { getPublicSuffix } from '../getPublicSuffix'
44
import * as validators from '../validators'
5+
import { ParameterError } from '../validators'
56
import { Store } from '../store'
67
import { MemoryCookieStore } from '../memstore'
78
import { pathMatch } from '../pathMatch'
89
import { Cookie } from './cookie'
910
import {
1011
Callback,
11-
ErrorCallback,
1212
createPromiseCallback,
13+
ErrorCallback,
1314
inOperator,
1415
safeToString,
1516
} from '../utils'
@@ -68,20 +69,18 @@ type CreateCookieJarOptions = {
6869
const SAME_SITE_CONTEXT_VAL_ERR =
6970
'Invalid sameSiteContext option for getCookies(); expected one of "strict", "lax", or "none"'
7071

71-
function getCookieContext(url: string | URL) {
72-
if (url instanceof URL && 'query' in url) {
72+
function getCookieContext(url: unknown) {
73+
if (url instanceof URL) {
7374
return url
74-
}
75-
76-
if (typeof url === 'string') {
75+
} else if (typeof url === 'string') {
7776
try {
7877
return urlParse(decodeURI(url))
7978
} catch {
8079
return urlParse(url)
8180
}
81+
} else {
82+
throw new ParameterError('`url` argument is invalid')
8283
}
83-
84-
throw new Error('`url` argument is invalid')
8584
}
8685

8786
function checkSameSiteContext(value: string) {
@@ -197,29 +196,29 @@ export class CookieJar {
197196
// return `undefined` when `ignoreError` is true. But would that be excessive overloading?
198197
setCookie(
199198
cookie: string | Cookie,
200-
url: string,
199+
url: string | URL,
201200
callback: Callback<Cookie | undefined>,
202201
): void
203202
setCookie(
204203
cookie: string | Cookie,
205-
url: string,
204+
url: string | URL,
206205
options: SetCookieOptions,
207206
callback: Callback<Cookie | undefined>,
208207
): void
209208
setCookie(
210209
cookie: string | Cookie,
211-
url: string,
210+
url: string | URL,
212211
options?: SetCookieOptions,
213212
): Promise<Cookie | undefined>
214213
setCookie(
215214
cookie: string | Cookie,
216-
url: string,
215+
url: string | URL,
217216
options: SetCookieOptions | Callback<Cookie | undefined>,
218217
callback?: Callback<Cookie | undefined>,
219218
): unknown
220219
setCookie(
221220
cookie: string | Cookie,
222-
url: string,
221+
url: string | URL,
223222
options?: SetCookieOptions | Callback<Cookie | undefined>,
224223
callback?: Callback<Cookie | undefined>,
225224
): unknown {
@@ -230,18 +229,22 @@ export class CookieJar {
230229
const promiseCallback = createPromiseCallback(callback)
231230
const cb = promiseCallback.callback
232231

233-
validators.validate(
234-
validators.isNonEmptyString(url),
235-
callback,
236-
safeToString(options),
237-
)
232+
if (typeof url === 'string') {
233+
validators.validate(
234+
validators.isNonEmptyString(url),
235+
callback,
236+
safeToString(options),
237+
)
238+
}
239+
240+
const context = getCookieContext(url)
241+
238242
let err
239243

240244
if (typeof url === 'function') {
241245
return promiseCallback.reject(new Error('No URL was specified'))
242246
}
243247

244-
const context = getCookieContext(url)
245248
if (typeof options === 'function') {
246249
options = defaultSetCookieOptions
247250
}
@@ -498,21 +501,21 @@ export class CookieJar {
498501
// RFC6365 S5.4
499502
getCookies(url: string, callback: Callback<Cookie[]>): void
500503
getCookies(
501-
url: string,
504+
url: string | URL,
502505
options: GetCookiesOptions | undefined,
503506
callback: Callback<Cookie[]>,
504507
): void
505508
getCookies(
506-
url: string,
509+
url: string | URL,
507510
options?: GetCookiesOptions | undefined,
508511
): Promise<Cookie[]>
509512
getCookies(
510-
url: string,
513+
url: string | URL,
511514
options: GetCookiesOptions | undefined | Callback<Cookie[]>,
512515
callback?: Callback<Cookie[]>,
513516
): unknown
514517
getCookies(
515-
url: string,
518+
url: string | URL,
516519
options?: GetCookiesOptions | Callback<Cookie[]>,
517520
callback?: Callback<Cookie[]>,
518521
): unknown {
@@ -525,10 +528,12 @@ export class CookieJar {
525528
const promiseCallback = createPromiseCallback(callback)
526529
const cb = promiseCallback.callback
527530

528-
validators.validate(validators.isNonEmptyString(url), cb, url)
531+
if (typeof url === 'string') {
532+
validators.validate(validators.isNonEmptyString(url), cb, url)
533+
}
534+
const context = getCookieContext(url)
529535
validators.validate(validators.isObject(options), cb, safeToString(options))
530536
validators.validate(typeof cb === 'function', cb)
531-
const context = getCookieContext(url)
532537

533538
const host = canonicalDomain(context.hostname)
534539
const path = context.pathname || '/'

0 commit comments

Comments
 (0)