@@ -2,13 +2,12 @@ import { dirname, isAbsolute, join } from 'node:path'
22import { fileURLToPath } from 'node:url'
33
44import { App } from '@tinyhttp/app'
5+ import { Eta } from 'eta'
56import { Low } from 'lowdb'
6- import sirv from 'sirv'
77import { json } from 'milliparsec'
8- import { Eta } from 'eta'
9- import { z } from 'zod'
8+ import sirv from 'sirv'
109
11- import { Data , Service } from './service.js'
10+ import { Data , isItem , Service } from './service.js'
1211
1312const __dirname = dirname ( fileURLToPath ( import . meta. url ) )
1413const isProduction = process . env [ 'NODE_ENV' ] === 'production'
@@ -20,19 +19,9 @@ export type AppOptions = {
2019
2120const eta = new Eta ( {
2221 views : join ( __dirname , '../views' ) ,
23- cache : isProduction
22+ cache : isProduction ,
2423} )
2524
26- function dataHandler ( req , res , next ) {
27- const { data } = res . locals
28- if ( data === undefined ) {
29- res . sendStatus ( 404 )
30- } else {
31- if ( req . method === 'POST' ) res . status ( 201 )
32- res . json ( data )
33- }
34- }
35-
3625export function createApp ( db : Low < Data > , options : AppOptions = { } ) {
3726 // Create service
3827 const service = new Service ( db )
@@ -45,12 +34,13 @@ export function createApp(db: Low<Data>, options: AppOptions = {}) {
4534
4635 // Static files
4736 app . use ( sirv ( join ( __dirname , '../public' ) ) )
48- options
49- . static
50- ?. map ( ( path ) => isAbsolute ( path ) ? path : join ( process . cwd ( ) , path ) )
37+ options . static
38+ ?. map ( ( path ) => ( isAbsolute ( path ) ? path : join ( process . cwd ( ) , path ) ) )
5139 . forEach ( ( dir ) => app . use ( sirv ( dir , { dev : ! isProduction } ) ) )
5240
53- app . get ( '/' , ( _req , res ) => res . send ( eta . render ( 'index.html' , { data : db . data } ) ) )
41+ app . get ( '/' , ( _req , res ) =>
42+ res . send ( eta . render ( 'index.html' , { data : db . data } ) ) ,
43+ )
5444
5545 app . get ( '/:name' , ( req , res , next ) => {
5646 const { name = '' } = req . params
@@ -66,19 +56,25 @@ export function createApp(db: Low<Data>, options: AppOptions = {}) {
6656
6757 app . post ( '/:name' , async ( req , res , next ) => {
6858 const { name = '' } = req . params
69- res . locals [ 'data' ] = await service . create ( name , req . body )
59+ if ( isItem ( req . body ) ) {
60+ res . locals [ 'data' ] = await service . create ( name , req . body )
61+ }
7062 next ( )
7163 } )
7264
7365 app . put ( '/:name/:id' , async ( req , res , next ) => {
7466 const { name = '' , id = '' } = req . params
75- res . locals [ 'data' ] = await service . update ( name , id , req . body )
67+ if ( isItem ( req . body ) ) {
68+ res . locals [ 'data' ] = await service . update ( name , id , req . body )
69+ }
7670 next ( )
7771 } )
7872
7973 app . patch ( '/:name/:id' , async ( req , res , next ) => {
8074 const { name = '' , id = '' } = req . params
81- res . locals [ 'data' ] = await service . patch ( name , id , req . body )
75+ if ( isItem ( req . body ) ) {
76+ res . locals [ 'data' ] = await service . patch ( name , id , req . body )
77+ }
8278 next ( )
8379 } )
8480
@@ -88,7 +84,15 @@ export function createApp(db: Low<Data>, options: AppOptions = {}) {
8884 next ( )
8985 } )
9086
91- app . use ( '/:name' , dataHandler )
87+ app . use ( '/:name' , ( req , res ) => {
88+ const { data } = res . locals
89+ if ( data === undefined ) {
90+ res . sendStatus ( 404 )
91+ } else {
92+ if ( req . method === 'POST' ) res . status ( 201 )
93+ res . json ( data )
94+ }
95+ } )
9296
9397 return app
9498}
0 commit comments