Skip to content

Commit

Permalink
feat(main: api): add middleware for POST, PATCH & PUT
Browse files Browse the repository at this point in the history
  • Loading branch information
antonreshetov committed Mar 29, 2022
1 parent 80f7fc5 commit df26660
Showing 1 changed file with 17 additions and 1 deletion.
18 changes: 17 additions & 1 deletion src/main/services/api/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import * as jsonServer from 'json-server'
import { store } from '../../store'
import type { Server } from 'http'
import type { Socket } from 'net'
import { nanoid } from 'nanoid'
interface ServerWithDestroy extends Server {
destroy: Function
}
Expand Down Expand Up @@ -32,6 +33,9 @@ export const createApiServer = () => {
const middlewares = jsonServer.defaults()
const router = jsonServer.router(db)

app.use(jsonServer.bodyParser)
app.use(middlewares)

app.get('/restart', (req, res) => {
server.destroy()
createApiServer()
Expand All @@ -40,7 +44,19 @@ export const createApiServer = () => {
res.status(200)
})

app.use(middlewares)
app.use((req, res, next) => {
if (req.method === 'POST') {
req.body.id = nanoid(8)
req.body.createdAt = Date.now().valueOf()
req.body.updatedAt = Date.now().valueOf()
}

if (req.method === 'PATCH' || req.method === 'PUT') {
req.body.updatedAt = Date.now().valueOf()
}
next()
})

app.use(router)

const server = app.listen(process.env.VITE_APP_API_PORT, () => {
Expand Down

0 comments on commit df26660

Please sign in to comment.