fix(types): add `Handler<string>` (#134)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Fix #133
authored by Yusuke Wada · committed by GitHub · parent db2382e · Browse files · 2 files·+15 added−7 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
2 files+15 added−7 removed
- other:+15 added−7 removed
Provenance
- Commit
5813596- Tree
bf5db09- Author
Yusuke Wada <yusuke@kamawada.com>- Committer
GitHub <noreply@github.com>- Parent
db2382e
2 files changed+15 added−7 removed
modified:examples/blog/src/controller.ts+7 added-7 removed
1123445678891011121313141516173 unmodified lines21222324252526272829303132333434353637382 unmodified lines414243444545464748494 unmodified linesimport type { Context, Handler } from 'hono'import type { Context } from 'hono'import * as Model from './model'
export const root: Handler = (c: Context) => {export const root = (c: Context) => { return c.json({ message: 'Hello' })}
export const list: Handler = async (c: Context) => {export const list = async (c: Context) => { const posts = await Model.getPosts() return c.json({ posts: posts, ok: true })}
export const create: Handler = async (c: Context) => {export const create = async (c: Context) => { let newPost: Model.Post | undefined if (c.req.parsedBody) { const param: Model.Param = c.req.parsedBody newPost = await Model.createPost(param)3 unmodified lines } return c.json({ post: newPost, ok: true }, 201)}
export const show: Handler = async (c: Context) => {export const show = async (c: Context<'id'>) => { const id = c.req.param('id') const post = await Model.getPost(id) if (!post) { return c.json({ error: 'Not Found', ok: false }, 404) } return c.json({ post: post, ok: true })}
export const update: Handler = async (c: Context) => {export const update = async (c: Context<'id'>) => { const id = c.req.param('id') if (!(await Model.getPost(id))) { // 204 No Content return c.json({ ok: false }, 204)2 unmodified lines const success = await Model.updatePost(id, param) return c.json({ ok: success })}
export const destroy: Handler = async (c: Context) => {export const destroy = async (c: Context<'id'>) => { const id = c.req.param('id') if (!(await Model.getPost(id))) { // 204 No Content return c.json({ ok: false }, 204)4 unmodified linesmodified:src/hono.ts+8 added
63 unmodified lines64 }65 66 /* HTTP METHODS */67 get<Path extends string>(path: Path, handler: Handler<ParamKeys<Path>>): Hono68+ get(path: string, handler: Handler<string>): Hono69 get(path: string, handler: Handler): Hono {70 return this.addRoute('get', path, handler)71 }72 73 post<Path extends string>(path: Path, handler: Handler<ParamKeys<Path>>): Hono74+ post(path: string, handler: Handler<string>): Hono75 post(path: string, handler: Handler): Hono {76 return this.addRoute('post', path, handler)77 }78 79 put<Path extends string>(path: Path, handler: Handler<ParamKeys<Path>>): Hono80+ put(path: string, handler: Handler<string>): Hono81 put(path: string, handler: Handler): Hono {82 return this.addRoute('put', path, handler)83 }84 85 head<Path extends string>(path: Path, handler: Handler<ParamKeys<Path>>): Hono86+ head(path: string, handler: Handler<string>): Hono87 head(path: string, handler: Handler): Hono {88 return this.addRoute('head', path, handler)89 }90 91 delete<Path extends string>(path: Path, handler: Handler<ParamKeys<Path>>): Hono92+ delete(path: string, handler: Handler<string>): Hono93 delete(path: string, handler: Handler): Hono {94 return this.addRoute('delete', path, handler)95 }96 97 options<Path extends string>(path: Path, handler: Handler<ParamKeys<Path>>): Hono98+ options(path: string, handler: Handler<string>): Hono99 options(path: string, handler: Handler): Hono {100 return this.addRoute('options', path, handler)101 }102 103 patch<Path extends string>(path: Path, handler: Handler<ParamKeys<Path>>): Hono104+ patch(path: string, handler: Handler<string>): Hono105 patch(path: string, handler: Handler): Hono {106 return this.addRoute('patch', path, handler)107 }108 109 /* Any methods */110 all<Path extends string>(path: Path, handler: Handler<ParamKeys<Path>>): Hono111+ all(path: string, handler: Handler<string>): Hono112 all(path: string, handler: Handler): Hono {113 return this.addRoute('all', path, handler)114 }115 110 unmodified lines