File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 11{
22 "name" : " @d2verb/pera" ,
3- "version" : " 0.0.13 " ,
3+ "version" : " 0.0.14 " ,
44 "license" : " MIT" ,
55 "tasks" : {
66 "test" : " deno test -A --coverage=coverage" ,
Original file line number Diff line number Diff line change @@ -71,7 +71,7 @@ export function App() {
7171
7272 try {
7373 const response = await fetch (
74- `https://pokeapi.co/ api/v2 /pokemon/${ name . toLowerCase ( ) } ` ,
74+ `/_pera/ api/pokemon/${ name . toLowerCase ( ) } ` ,
7575 ) ;
7676
7777 if ( ! response . ok ) {
@@ -371,5 +371,32 @@ await serve(App, {
371371 port : 8080 ,
372372 title : "Pokédex - pera Sample" ,
373373 moduleUrl : import . meta. url ,
374- props : { } ,
374+ logging : true ,
375+ api : ( app ) => {
376+ // GET /api/pokemon/:name - Fetch Pokemon data from PokeAPI
377+ app . get ( "/pokemon/:name" , async ( c ) => {
378+ const name = c . req . param ( "name" ) ;
379+
380+ try {
381+ const response = await fetch (
382+ `https://pokeapi.co/api/v2/pokemon/${ name . toLowerCase ( ) } ` ,
383+ ) ;
384+
385+ if ( ! response . ok ) {
386+ if ( response . status === 404 ) {
387+ return c . json ( { error : `Pokémon "${ name } " not found` } , 404 ) ;
388+ }
389+ return c . json (
390+ { error : `Failed to fetch Pokémon data: ${ response . status } ` } ,
391+ response . status ,
392+ ) ;
393+ }
394+
395+ const pokemonData = await response . json ( ) ;
396+ return c . json ( pokemonData ) ;
397+ } catch {
398+ return c . json ( { error : "An unexpected error occurred" } , 500 ) ;
399+ }
400+ } ) ;
401+ } ,
375402} ) ;
Original file line number Diff line number Diff line change @@ -5,6 +5,7 @@ import { dirname } from "@std/path";
55import { renderToString } from "preact-render-to-string" ;
66import { h } from "preact" ;
77import { Hono } from "@hono/hono" ;
8+ import { logger } from "@hono/hono/logger" ;
89
910/**
1011 * The implementation of the serve() function.
@@ -23,9 +24,14 @@ export async function serveImpl<
2324 const rootId = opts . rootId ?? "root" ;
2425 const appFile = filePathFromModuleUrl ( opts . moduleUrl ) ;
2526 const hmr = opts . hmr ?? true ;
27+ const logging = opts . logging ?? false ;
2628
2729 const app = new Hono ( ) ;
2830
31+ if ( logging ) {
32+ app . use ( logger ( ) ) ;
33+ }
34+
2935 app . get ( "/_pera/app.js" , async ( ) => {
3036 try {
3137 const origCode = await Deno . readTextFile ( appFile ) ;
Original file line number Diff line number Diff line change @@ -23,6 +23,8 @@ export type PeraOptions<
2323 api ?: ( app : Hono ) => void | Promise < void > ;
2424 /** The signal to abort the server. (Default: undefined) */
2525 signal ?: AbortSignal ;
26+ /** Whether to enable logging. (Default: false) */
27+ logging ?: boolean ;
2628} ;
2729
2830export type PeraApp <
You can’t perform that action at this time.
0 commit comments