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

Commit dfca747

Browse files
author
v1rtl
committed
add Body generic to Request, add GraphQL example
1 parent f2bc9f1 commit dfca747

File tree

8 files changed

+82
-17
lines changed

8 files changed

+82
-17
lines changed

.github/workflows/main.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ jobs:
1616
- uses: denolib/setup-deno@v2
1717
with:
1818
deno-version: v1.8
19+
- name: Cache Dependencies
20+
run: deno cache deps.ts
1921
- name: Create tests
2022
run: deno test -A --no-check --location http://127.0.1 --unstable --coverage=coverage ./tests -q
2123
- name: Create coverage report

egg.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"entry": "./app.ts",
55
"description": "0-legacy, tiny & fast web framework as a replacement of Express",
66
"homepage": "https://github.com/talentlessguy/tinyhttp-deno",
7-
"version": "0.0.23",
7+
"version": "0.0.25",
88
"files": [
99
"./*.ts",
1010
"./utils/*.ts",

examples/graphql/README.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# tinyhttp GraphQL example
2+
3+
Simple GraphQL server example with tinyhttp and [gql](https://github.com/deno-libs/gql).
4+
5+
## Run
6+
7+
```sh
8+
deno run --allow-read --allow-net server.ts
9+
```
10+
11+
Then in another terminal:
12+
13+
```sh
14+
$ curl -X POST localhost:3000/graphql -d '{ "query": "{ hello }" }'
15+
{
16+
"data": {
17+
"hello": "Hello World!"
18+
}
19+
}
20+
```

examples/graphql/server.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import { App } from '../../mod.ts'
2+
import { GraphQLHTTP } from 'https://deno.land/x/gql/mod.ts'
3+
import { GraphQLSchema, GraphQLString, GraphQLObjectType } from 'https://deno.land/x/graphql_deno@v15.0.0/mod.ts'
4+
5+
const schema = new GraphQLSchema({
6+
query: new GraphQLObjectType({
7+
name: 'Query',
8+
fields: {
9+
hello: {
10+
type: GraphQLString,
11+
resolve() {
12+
return 'Hello World!'
13+
}
14+
}
15+
}
16+
})
17+
})
18+
19+
const app = new App()
20+
21+
app.post('/graphql', GraphQLHTTP({ schema }))
22+
23+
app.listen(3000, () => console.log(`☁ Started on http://localhost:3000`))

request.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { ServerRequest } from 'https://deno.land/std@0.88.0/http/server.ts'
33
import { App } from './app.ts'
44
import { QueryParams, Ranges, Protocol, AcceptsReturns, Middleware } from './types.ts'
55

6-
export interface Request extends ServerRequest, tinyhttp.Request {
6+
export interface Request<Body = Record<string, unknown>> extends ServerRequest, tinyhttp.Request {
77
path: string
88
originalUrl: string
99
query: QueryParams
@@ -36,5 +36,5 @@ export interface Request extends ServerRequest, tinyhttp.Request {
3636
remoteAddress: string
3737
}
3838

39-
parsedBody?: Record<string, unknown>
39+
parsedBody?: Body
4040
}

tests/modules/send.test.ts

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -51,28 +51,43 @@ describe('send(body)', () => {
5151
// @ts-ignore
5252
await fetch.get('/').expect(null)
5353
})
54-
/* it('should remove some headers for 204 status', async () => {
54+
/* it('should remove some headers for 204 status', async () => {
5555
const { fetch } = InitAppAndTest((req, res) => {
5656
res.status = 204
5757
5858
send(req, res)('Hello World')
5959
})
6060
61-
await fetch.get('/').expect('Content-Length', '').expect('Content-Type', '').expect('Transfer-Encoding', '')
61+
await fetch
62+
.get('/')
63+
.expect('Content-Length', '')
64+
.expect('Content-Type', '')
65+
.expect('Transfer-Encoding', '')
66+
.expect('')
6267
})
6368
it('should remove some headers for 304 status', async () => {
64-
const { fetch } = InitAppAndTest((req, res) => {
65-
res.status = 304
66-
67-
send(req, res)('Hello World')
68-
})
69+
const { fetch } = InitAppAndTest(
70+
(req, res) => {
71+
res.status = 304
72+
res.headers?.delete('Content-Type')
73+
send(req, res)('Hello World')
74+
},
75+
'/',
76+
{},
77+
'get'
78+
)
6979
70-
await fetch.get('/').expect('Content-Length', '').expect('Content-Type', '').expect('Transfer-Encoding', '')
71-
})
72-
it("should set Content-Type to application/octet-stream for buffers if the header hasn't been set before", async () => {
80+
await fetch
81+
.get('/')
82+
.expect('Content-Length', '')
83+
.expect('Content-Type', '')
84+
.expect('Transfer-Encoding', '')
85+
.expect('')
86+
}) */
87+
/* it("should set Content-Type to application/octet-stream for buffers if the header hasn't been set before", async () => {
7388
const { fetch } = InitAppAndTest((req, res) => send(req, res)(new TextEncoder().encode('Hello World')).end())
7489
75-
await fetch.get('/').set('Content-Type', '').expect('Content-Type', 'application/octet-stream')
90+
await fetch.get('/').expect('Content-Type', 'application/octet-stream')
7691
}) */
7792
})
7893

tests/util.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,15 @@ export const BindToSuperDeno = <Req extends Request, Res extends Response>(app:
1616
return fetch
1717
}
1818

19-
export const InitAppAndTest = (handler: Handler, route = '/', settings: AppConstructor<Request, Response> = {}) => {
19+
export const InitAppAndTest = (
20+
handler: Handler,
21+
route = '/',
22+
settings: AppConstructor<Request, Response> = {},
23+
method: 'get' | 'post' | 'use' = 'use'
24+
) => {
2025
const app = new App<unknown, Request, Response>(settings)
2126

22-
app.use(route, handler)
27+
app[method](route, handler)
2328

2429
return { fetch: BindToSuperDeno(app), app }
2530
}

types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ interface Range {
1717
end: number
1818
}
1919

20-
export type Handler = RHandler<Request, Response>
20+
export type Handler<Req = Request> = RHandler<Req, Response>
2121

2222
export type {
2323
QueryParams,

0 commit comments

Comments
 (0)