/
githubmirror
/
json-server
Обзор
Документация
Войти
/
githubmirror
/
json-server
Код
Запросы
0
Пакеты
0
Релизы
0
Аналитика
Безопасность
v1.0.0-beta.4
src/app.ts
144 строки
4 KB
typicode
fix: patch error (#1676)
18 янв 2026, 02:14
Не верифицирован
18 янв 2026, 02:14
c3afd0f
Код
Авторство
О чём код?
import { dirname, isAbsolute, join } from 'node:path' import { fileURLToPath } from 'node:url' import { App, type Request } from '@tinyhttp/app' import { cors } from '@tinyhttp/cors' import { Eta } from 'eta' import { Low } from 'lowdb' import { json } from 'milliparsec' import sirv from 'sirv' import type { Data } from './service.ts' import { isItem, Service } from './service.ts' const __dirname = dirname(fileURLToPath(import.meta.url)) const isProduction = process.env['NODE_ENV'] === 'production' type QueryValue = Request['query'][string] | number type Query = Record<string, QueryValue> export type AppOptions = { logger?: boolean static?: string[] } const eta = new Eta({ views: join(__dirname, '../views'), cache: isProduction, }) export function createApp(db: Low<Data>, options: AppOptions = {}) { // Create service const service = new Service(db) // Create app const app = new App() // Static files app.use(sirv('public', { dev: !isProduction })) options.static ?.map((path) => (isAbsolute(path) ? path : join(process.cwd(), path))) .forEach((dir) => app.use(sirv(dir, { dev: !isProduction }))) // CORS app .use((req, res, next) => { return cors({ allowedHeaders: req.headers['access-control-request-headers'] ?.split(',') .map((h) => h.trim()), })(req, res, next) }) .options('*', cors()) // Body parser app.use(json()) app.get('/', (_req, res) => res.send(eta.render('index.html', { data: db.data }))) app.get('/:name', (req, res, next) => { const { name = '' } = req.params const query: Query = {} Object.keys(req.query).forEach((key) => { let value: QueryValue = req.query[key] if ( ['_start', '_end', '_limit', '_page', '_per_page'].includes(key) && typeof value === 'string' ) { value = parseInt(value) } if (!Number.isNaN(value)) { query[key] = value } }) res.locals['data'] = service.find(name, query) next?.() }) app.get('/:name/:id', (req, res, next) => { const { name = '', id = '' } = req.params res.locals['data'] = service.findById(name, id, req.query) next?.() }) app.post('/:name', async (req, res, next) => { const { name = '' } = req.params if (isItem(req.body)) { res.locals['data'] = await service.create(name, req.body) } next?.() }) app.put('/:name', async (req, res, next) => { const { name = '' } = req.params if (isItem(req.body)) { res.locals['data'] = await service.update(name, req.body) } next?.() }) app.put('/:name/:id', async (req, res, next) => { const { name = '', id = '' } = req.params if (isItem(req.body)) { res.locals['data'] = await service.updateById(name, id, req.body) } next?.() }) app.patch('/:name', async (req, res, next) => { const { name = '' } = req.params if (isItem(req.body)) { res.locals['data'] = await service.patch(name, req.body) } next?.() }) app.patch('/:name/:id', async (req, res, next) => { const { name = '', id = '' } = req.params if (isItem(req.body)) { res.locals['data'] = await service.patchById(name, id, req.body) } next?.() }) app.delete('/:name/:id', async (req, res, next) => { const { name = '', id = '' } = req.params res.locals['data'] = await service.destroyById(name, id, req.query['_dependent']) next?.() }) app.use('/:name', (req, res) => { const { data } = res.locals if (data === undefined) { res.sendStatus(404) } else { if (req.method === 'POST') res.status(201) res.json(data) } }) return app }