From a3484f45251f9f44305c2b4a4a15bb89229f71c8 Mon Sep 17 00:00:00 2001 From: Nikolai Petukhov Date: Sat, 28 Sep 2024 12:51:59 +0300 Subject: [PATCH] backend init --- bro.config.js | 1 + src/app.tsx | 6 ++++++ stubs/api/books/book-list.json | 6 ++++++ stubs/api/books/book.json | 5 +++++ stubs/api/books/index.js | 36 ++++++++++++++++++++++++++++++++++ stubs/api/index.js | 5 +++++ stubs/api/middlewares/delay.js | 5 +++++ 7 files changed, 64 insertions(+) create mode 100644 stubs/api/books/book-list.json create mode 100644 stubs/api/books/book.json create mode 100644 stubs/api/books/index.js create mode 100644 stubs/api/middlewares/delay.js diff --git a/bro.config.js b/bro.config.js index ad91b69..66e3715 100644 --- a/bro.config.js +++ b/bro.config.js @@ -23,5 +23,6 @@ module.exports = { }, config: { "enterfront.api": "/api", + // paste stand URL to config }, }; diff --git a/src/app.tsx b/src/app.tsx index a84f877..e0758d2 100644 --- a/src/app.tsx +++ b/src/app.tsx @@ -3,8 +3,14 @@ import { BrowserRouter } from 'react-router-dom'; import { Dashboard } from './dashboard'; +import { getConfigValue } from "@brojs/cli"; + import './index.css' +const BASE_API_URL = getConfigValue("enterfront.api"); + +// fetch(`${BASE_API_URL}/books/list`) + const App = () => { return( diff --git a/stubs/api/books/book-list.json b/stubs/api/books/book-list.json new file mode 100644 index 0000000..1f6d483 --- /dev/null +++ b/stubs/api/books/book-list.json @@ -0,0 +1,6 @@ +{ + "content": { + + }, + "totalElement": 0 +} \ No newline at end of file diff --git a/stubs/api/books/book.json b/stubs/api/books/book.json new file mode 100644 index 0000000..4c24b18 --- /dev/null +++ b/stubs/api/books/book.json @@ -0,0 +1,5 @@ +{ + "id": "1", + "name": "Book name", + "description": "Interesting book description" +} \ No newline at end of file diff --git a/stubs/api/books/index.js b/stubs/api/books/index.js new file mode 100644 index 0000000..254ad06 --- /dev/null +++ b/stubs/api/books/index.js @@ -0,0 +1,36 @@ +const booksRouter = require('express').Router(); + +module.exports = booksRouter; + +const books = [] + +booksRouter.get('/list', (req, res) => { + res.send(require('./book-list.json')) +}) + +booksRouter.post('/', (req, res) => { + // body() can be used because of dev server + console.log(req.body) + books.push({ + name: req.body.name, + }) + + res.send({ + status: 200 + }) +}) + + +booksRouter.get('/:id', (req, res) => { + console.log(req.params); + + res.send(require('./book.json')); + + // res.status(404).send() +}) + +booksRouter.delete('/:id', (req, res) => { + res.status(201).send({ + status: 'ok' + }) +}) diff --git a/stubs/api/index.js b/stubs/api/index.js index d2cd565..2dd59fa 100644 --- a/stubs/api/index.js +++ b/stubs/api/index.js @@ -1,3 +1,8 @@ +const booksRouter = require("./books"); const router = require('express').Router(); +const delay = require('./middlewares/delay'); module.exports = router; + +// router.use(delay(300)); +router.use('/books', delay, booksRouter); diff --git a/stubs/api/middlewares/delay.js b/stubs/api/middlewares/delay.js new file mode 100644 index 0000000..789a7b6 --- /dev/null +++ b/stubs/api/middlewares/delay.js @@ -0,0 +1,5 @@ +const delay = (ms = 1000) => (req, res, next) => { + setTimeout(next, ms) +} + +module.exports = delay