Skip to content
This repository was archived by the owner on Jul 31, 2025. It is now read-only.

Commit 5b544a9

Browse files
author
v1rtl
committed
add req.parsedBody and fix failing tests
1 parent 17fe28b commit 5b544a9

File tree

4 files changed

+22
-51
lines changed

4 files changed

+22
-51
lines changed

examples/mongodb/server.ts

Lines changed: 7 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,11 @@
1-
import { App, RHandler } from '../../mod.ts'
2-
import { Request } from '../../request.ts'
1+
import { App } from '../../mod.ts'
32
import { MongoClient, Bson } from 'https://deno.land/x/mongo@v0.21.2/mod.ts'
43
import * as dotenv from 'https://deno.land/x/tiny_env@1.0.0/mod.ts'
4+
import { json } from 'https://deno.land/x/parsec/mod.ts'
55

66
dotenv.load()
77

8-
interface Req extends Request {
9-
bodyResult: Record<string, unknown>
10-
}
11-
12-
const app = new App<unknown, Req>()
8+
const app = new App()
139
const port = parseInt(Deno.env.get('PORT') || '') || 3000
1410

1511
// connect to mongodb
@@ -32,27 +28,12 @@ app.get('/notes', async (_, res, next) => {
3228
}
3329
})
3430

35-
const bodyParser: RHandler<Req> = async (req, _res, next) => {
36-
const buf = await Deno.readAll(req.body)
37-
38-
const dec = new TextDecoder()
39-
40-
const body = dec.decode(buf)
41-
42-
try {
43-
const result = JSON.parse(body)
44-
req.bodyResult = result
45-
} catch (e) {
46-
next(e)
47-
}
48-
next()
49-
}
50-
app.use(bodyParser)
31+
app.use(json)
5132

5233
// add new note
5334
app.post('/notes', async (req, res, next) => {
5435
try {
55-
const { title, desc } = req.bodyResult
36+
const { title, desc } = req.parsedBody!
5637
await coll.insertOne({ title, desc })
5738

5839
res.send(`Note with title of "${title}" has been added`)
@@ -64,7 +45,7 @@ app.post('/notes', async (req, res, next) => {
6445
// delete note
6546
app.delete('/notes', async (req, res, next) => {
6647
try {
67-
const { id } = req.bodyResult
48+
const { id } = req.parsedBody!
6849
await coll.deleteOne({ _id: new Bson.ObjectId(id) })
6950

7051
res.send(`Note with id of ${id} has been deleted`)
@@ -76,7 +57,7 @@ app.delete('/notes', async (req, res, next) => {
7657
// update existing note
7758
app.put('/notes', async (req, res, next) => {
7859
try {
79-
const { title, desc, id } = req.bodyResult
60+
const { title, desc, id } = req.parsedBody!
8061
await coll.updateOne({ _id: new Bson.ObjectId(id) }, { $set: { title, desc } })
8162
res.send(`Note with title of ${title} has been updated`)
8263
} catch (err) {

examples/postgresql/server.ts

Lines changed: 5 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,8 @@
1-
import { App, RHandler, Request } from '../../mod.ts'
1+
import { App } from '../../mod.ts'
22
import { Client } from 'https://deno.land/x/postgres@v0.8.0/mod.ts'
3+
import { json } from 'https://deno.land/x/parsec/mod.ts'
34

4-
interface Req extends Request {
5-
bodyResult: Record<string, unknown>
6-
}
7-
8-
const app = new App<unknown, Req>()
5+
const app = new App()
96
const port = parseInt(Deno.env.get('PORT') || '', 10) || 3000
107

118
const client = new Client({
@@ -16,29 +13,13 @@ const client = new Client({
1613
})
1714
await client.connect()
1815

19-
const bodyParser: RHandler<Req> = async (req, _, next) => {
20-
const buf = await Deno.readAll(req.body)
21-
22-
const dec = new TextDecoder()
23-
24-
const body = dec.decode(buf)
25-
26-
try {
27-
const result = JSON.parse(body)
28-
req.bodyResult = result
29-
} catch (e) {
30-
next(e)
31-
}
32-
next()
33-
}
34-
3516
app.get('/notes', async (_, res) => {
3617
const query = await client.queryObject('SELECT * FROM NOTES', { fields: ['id', 'name'] })
3718
res.send(query.rows)
3819
})
3920

40-
app.use(bodyParser).post('/notes', async (req, res) => {
41-
const { title, desc } = req.bodyResult
21+
app.use(json).post('/notes', async (req, res) => {
22+
const { title, desc } = req.parsedBody!
4223

4324
const query = await client.queryObject(`INSERT INTO users(title, desc) VALUES (${title}, ${desc});`)
4425

request.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,4 +35,6 @@ export interface Request extends ServerRequest, tinyhttp.Request {
3535
connection: {
3636
remoteAddress: string
3737
}
38+
39+
parsedBody?: Record<string, unknown>
3840
}

tests/core/app.test.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,14 +51,21 @@ describe('App constructor', () => {
5151

5252
describe('Template engines', () => {
5353
it('Works with eta out of the box', async () => {
54-
const app = new App<EtaConfig>()
54+
const app = new App<EtaConfig>({
55+
onError: (err) => console.log(err)
56+
})
57+
58+
const pwd = Deno.cwd()
59+
60+
Deno.chdir(path.resolve(pwd, 'tests/fixtures'))
5561

5662
app.engine('eta', eta)
5763

5864
app.use((_, res) => {
5965
res.render('index.eta', {
6066
name: 'Eta'
6167
})
68+
Deno.chdir(pwd)
6269
})
6370

6471
const fetch = BindToSuperDeno(app)

0 commit comments

Comments
 (0)