Skip to content

Commit 6c75e47

Browse files
committed
implement nextjs api example
1 parent eb550ad commit 6c75e47

File tree

16 files changed

+94
-449
lines changed

16 files changed

+94
-449
lines changed

.vscode/extensions.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"recommendations": ["humao.rest-client"]
3+
}

example/.eslintrc.json

Lines changed: 0 additions & 3 deletions
This file was deleted.

example/package.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,24 @@
44
"private": true,
55
"scripts": {
66
"dev": "next dev",
7+
"debug": "NODE_OPTIONS='--inspect' next dev",
78
"build": "next build",
89
"start": "next start",
910
"lint": "next lint"
1011
},
1112
"dependencies": {
13+
"@koa/router": "^10.1.1",
14+
"koa": "^2.13.4",
15+
"koa-body": "^5.0.0",
1216
"next": "12.1.6",
17+
"nextjs-koa-api": "workspace:^0.0.0",
18+
"on-finished": "^2.4.1",
1319
"react": "18.1.0",
1420
"react-dom": "18.1.0"
1521
},
1622
"devDependencies": {
23+
"@types/koa": "^2.13.4",
24+
"@types/koa__router": "^8.0.11",
1725
"@types/node": "^17.0.30",
1826
"@types/react": "18.0.8",
1927
"@types/react-dom": "18.0.3",

example/pages/_app.tsx

Lines changed: 0 additions & 8 deletions
This file was deleted.

example/pages/api/hello.ts

Lines changed: 0 additions & 13 deletions
This file was deleted.

example/pages/index.tsx

Lines changed: 0 additions & 72 deletions
This file was deleted.

example/request.http

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
1-
POST http://localhost:3000/api/hello
2-
Content-Type: application/json
1+
GET http://localhost:3000/api/
32

4-
{
5-
"name":"ivan"
6-
}
3+
### trigger error
4+
GET http://localhost:3000/api/throw
75

86
###
9-
GET http://localhost:3000/api/instance
7+
POST http://localhost:3000/api/hello
108
Content-Type: application/json
119

1210
{
13-
"name":"ivan-a"
11+
"name":"Luigi"
1412
}
13+
14+
### nested router
15+
GET http://localhost:3000/api/nested/hello
16+
Content-Type: application/json

example/src/koa-test.ts

Lines changed: 0 additions & 70 deletions
This file was deleted.

example/src/pages/api/[...hello].ts

Lines changed: 0 additions & 66 deletions
This file was deleted.
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import { KoaApi, withKoaApi } from 'nextjs-koa-api'
2+
3+
const api = new KoaApi({
4+
router: {
5+
prefix: '/api'
6+
}
7+
})
8+
9+
api.use(async (ctx, next) => {
10+
try {
11+
await next()
12+
} catch (err: any) {
13+
//catch all errors
14+
err as Error
15+
ctx.status = err.statusCode || err.status || 500
16+
ctx.body = {
17+
message: err.message
18+
}
19+
}
20+
})
21+
22+
api.router
23+
.get('/hello', (ctx) => {
24+
ctx.body = {
25+
method: 'GET',
26+
route: '/hello'
27+
}
28+
})
29+
.post('/hello', (ctx) => {
30+
ctx.body = ctx.request.body
31+
})
32+
.get('/throw', () => {
33+
throw new Error('my error')
34+
})
35+
36+
// create nested router
37+
const subRouter = api.createNewRouter().get('/', (ctx) => {
38+
ctx.body = `nested params: ${JSON.stringify(ctx.params)}`
39+
})
40+
41+
//mount nested router
42+
//https://github.com/koajs/router/blob/master/API.md#nested-routers
43+
api.router.use('/nested/:data', subRouter.routes(), subRouter.allowedMethods())
44+
45+
export default withKoaApi(api)

0 commit comments

Comments
 (0)