Skip to content

Commit 8099453

Browse files
committed
🔧 fix: cookie
1 parent 5bd3d7e commit 8099453

File tree

5 files changed

+64
-25
lines changed

5 files changed

+64
-25
lines changed

example/a.ts

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,25 @@
11
import { Elysia } from '../src'
2-
import z from 'zod'
3-
import * as v from 'valibot'
2+
import { z } from 'zod'
43

5-
new Elysia()
6-
.guard({
7-
schema: 'standalone',
8-
body: z.object({
9-
id: z.coerce.number()
10-
})
11-
})
12-
.get('/user/:id', ({ body }) => body, {
13-
body: v.object({
14-
name: v.literal('lilith')
4+
const app = new Elysia().get(
5+
'test',
6+
({ cookie: { test } }) => {
7+
console.log(test.value)
8+
9+
return typeof test
10+
},
11+
{
12+
cookie: z.object({ test: z.coerce.number() })
13+
}
14+
)
15+
16+
const value = await app
17+
.handle(
18+
new Request('http://localhost:3000/test', {
19+
headers: {
20+
cookie: 'test=123'
21+
}
1522
})
16-
})
23+
)
24+
.then((x) => x.headers.toJSON())
25+
.then(console.log)

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "elysia",
33
"description": "Ergonomic Framework for Human",
4-
"version": "1.4.4",
4+
"version": "1.4.5-beta.0",
55
"author": {
66
"name": "saltyAom",
77
"url": "https://github.com/SaltyAom",

src/compose.ts

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1504,7 +1504,7 @@ export const composeHandler = ({
15041504
)
15051505

15061506
fnLiteral +=
1507-
`const cookieValue={}\n` +
1507+
`let cookieValue={}\n` +
15081508
`for(const [key,value] of Object.entries(c.cookie))` +
15091509
`cookieValue[key]=value.value\n`
15101510

@@ -1513,25 +1513,29 @@ export const composeHandler = ({
15131513

15141514
if (validator.cookie.provider === 'standard') {
15151515
fnLiteral +=
1516-
`let vac=validator.cookie.Check(c.body)\n` +
1516+
`let vac=validator.cookie.Check(cookieValue)\n` +
15171517
`if(vac instanceof Promise)vac=await vac\n` +
15181518
`if(vac.issues){` +
15191519
validation.validate('cookie', undefined, 'vac.issues') +
1520-
'}else{c.body=vac.value}\n'
1520+
'}else{cookieValue=vac.value}\n'
1521+
1522+
fnLiteral +=
1523+
`for(const k of Object.keys(cookieValue))` +
1524+
`c.cookie[k].innerValue=cookieValue[k]\n`
15211525
} else if (validator.body?.schema?.noValidate !== true) {
15221526
fnLiteral +=
15231527
`if(validator.cookie.Check(cookieValue)===false){` +
15241528
validation.validate('cookie', 'cookieValue') +
15251529
'}'
1526-
}
15271530

1528-
// if (validator.cookie.hasTransform)
1529-
// fnLiteral += coerceTransformDecodeError(
1530-
// `for(const [key,value] of Object.entries(validator.cookie.Decode(cookieValue))){` +
1531-
// `c.cookie[key].value=value` +
1532-
// `}`,
1533-
// 'cookie'
1534-
// )
1531+
if (validator.cookie.hasTransform)
1532+
fnLiteral += coerceTransformDecodeError(
1533+
`for(const [key,value] of Object.entries(validator.cookie.Decode(cookieValue))){` +
1534+
`c.cookie[key].value=value` +
1535+
`}`,
1536+
'cookie'
1537+
)
1538+
}
15351539

15361540
if (validator.cookie.isOptional) fnLiteral += `}`
15371541
}

src/cookies.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,10 @@ export class Cookie<T> implements ElysiaCookie {
260260
return this
261261
}
262262

263+
private set innerValue(value: T) {
264+
this.value = value
265+
}
266+
263267
remove() {
264268
if (this.value === undefined) return
265269

test/standard-schema/validate.test.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -299,4 +299,26 @@ describe('Standard Schema Validate', () => {
299299
expect(responses[4]).toEqual(422)
300300
expect(responses[5]).toEqual(422)
301301
})
302+
303+
it('handle cookie', async () => {
304+
const app = new Elysia().get(
305+
'test',
306+
({ cookie: { test } }) => typeof test.value,
307+
{
308+
cookie: z.object({ test: z.coerce.number() })
309+
}
310+
)
311+
312+
const value = await app
313+
.handle(
314+
new Request('http://localhost:3000/test', {
315+
headers: {
316+
cookie: 'test=123'
317+
}
318+
})
319+
)
320+
.then((x) => x.text())
321+
322+
expect(value).toBe('number')
323+
})
302324
})

0 commit comments

Comments
 (0)