Skip to content

Commit 04f4755

Browse files
authored
Merge pull request #22 from d2verb/feat/logger
Add logger
2 parents 4482f1f + 7f91f56 commit 04f4755

4 files changed

Lines changed: 38 additions & 3 deletions

File tree

deno.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
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",

examples/pokedex.tsx

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff 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
});

src/server.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { dirname } from "@std/path";
55
import { renderToString } from "preact-render-to-string";
66
import { h } from "preact";
77
import { 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);

src/types.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff 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

2830
export type PeraApp<

0 commit comments

Comments
 (0)