Skip to content

Commit 9a1307c

Browse files
committed
feat: allow request.plainCookie options to be set via an object
1 parent 419c16c commit 9a1307c

File tree

2 files changed

+60
-4
lines changed

2 files changed

+60
-4
lines changed

src/request.ts

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import fresh from 'fresh'
1212
import typeIs from 'type-is'
1313
import accepts from 'accepts'
1414
import { isIP } from 'node:net'
15+
import is from '@sindresorhus/is'
1516
import proxyaddr from 'proxy-addr'
1617
import lodash from '@poppinss/utils/lodash'
1718
import { safeEqual } from '@poppinss/utils'
@@ -908,9 +909,23 @@ export class Request extends Macroable {
908909
* Returns value for a given key from unsigned cookies. Optional
909910
* defaultValue is returned when actual value is undefined.
910911
*/
911-
plainCookie(key: string, defaultValue?: string, encoded?: boolean): any {
912+
plainCookie(key: string, options?: { defaultValue?: string; encoded?: boolean }): any
913+
plainCookie(key: string, defaultValue?: string, encoded?: boolean): any
914+
plainCookie(
915+
key: string,
916+
defaultValueOrOptions?: string | { defaultValue?: string; encoded?: boolean },
917+
encoded?: boolean
918+
): any {
912919
this.#initiateCookieParser()
913-
return this.#cookieParser!.decode(key, encoded) || defaultValue
920+
921+
if (is.object(defaultValueOrOptions)) {
922+
return (
923+
this.#cookieParser!.decode(key, defaultValueOrOptions?.encoded) ||
924+
defaultValueOrOptions.defaultValue
925+
)
926+
}
927+
928+
return this.#cookieParser!.decode(key, encoded) || defaultValueOrOptions
914929
}
915930

916931
/**

tests/request.spec.ts

Lines changed: 43 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1255,7 +1255,7 @@ test.group('Request | Cookies', () => {
12551255
})
12561256
})
12571257

1258-
test('get value for a single unsigned cookie', async ({ assert }) => {
1258+
test('get value for a single plain cookie', async ({ assert }) => {
12591259
const server = createServer((req, res) => {
12601260
const request = new RequestFactory().merge({ req, res, encryption }).create()
12611261
res.writeHead(200, { 'content-type': 'application/json' })
@@ -1269,7 +1269,7 @@ test.group('Request | Cookies', () => {
12691269
})
12701270
})
12711271

1272-
test('use default value when actual unsigned value is missing', async ({ assert }) => {
1272+
test('use default value when actual plain cookie value is missing', async ({ assert }) => {
12731273
const server = createServer((req, res) => {
12741274
const request = new RequestFactory().merge({ req, res, encryption }).create()
12751275
res.writeHead(200, { 'content-type': 'application/json' })
@@ -1303,6 +1303,47 @@ test.group('Request | Cookies', () => {
13031303
})
13041304
})
13051305

1306+
test('get value for a single not encoded cookie using options object', async ({ assert }) => {
1307+
const server = createServer((req, res) => {
1308+
const request = new RequestFactory()
1309+
.merge({
1310+
req,
1311+
res,
1312+
encryption,
1313+
})
1314+
.create()
1315+
1316+
res.writeHead(200, { 'content-type': 'application/json' })
1317+
res.end(JSON.stringify({ name: request.plainCookie('name', { encoded: false }) }))
1318+
})
1319+
1320+
const cookies = serializer.encode('name', 'virk', { encode: false })!
1321+
const { body } = await supertest(server).get('/').set('cookie', cookies)
1322+
assert.deepEqual(body, {
1323+
name: 'virk',
1324+
})
1325+
})
1326+
1327+
test('specify plain cookie default value via options object', async ({ assert }) => {
1328+
const server = createServer((req, res) => {
1329+
const request = new RequestFactory()
1330+
.merge({
1331+
req,
1332+
res,
1333+
encryption,
1334+
})
1335+
.create()
1336+
1337+
res.writeHead(200, { 'content-type': 'application/json' })
1338+
res.end(JSON.stringify({ name: request.plainCookie('name', { defaultValue: 'virk' }) }))
1339+
})
1340+
1341+
const { body } = await supertest(server).get('/')
1342+
assert.deepEqual(body, {
1343+
name: 'virk',
1344+
})
1345+
})
1346+
13061347
test('get value for a single encrypted', async ({ assert }) => {
13071348
const server = createServer((req, res) => {
13081349
const request = new RequestFactory().merge({ req, res, encryption }).create()

0 commit comments

Comments
 (0)