feat: add hello middleware (#1)

* feat: add hello middleware

* add changeset
pull/2/head
Yusuke Wada 2022-10-16 18:14:21 +09:00 committed by GitHub
parent f7d6fb0d95
commit 5cd86dd5d7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 102 additions and 0 deletions

View File

@ -0,0 +1,5 @@
---
'@honojs/hello': patch
---
the repository has been moved

View File

@ -0,0 +1,26 @@
# Hello middleware for Hono
An example project of the third-party middleware for [Hono](https://github.com/honojs/hono).
This middleware add `X-Message` header to the Response.
## Usage
```ts
import { hello } from '@honojs/hello'
import { Hono } from 'hono'
const app = new Hono()
app.use('*', hello('Hello!! Hono!!'))
app.get('/', (c) => c.text('foo'))
export default app
```
## Author
Yusuke Wada <https://github.com/yusukebe>
## License
MIT

View File

@ -0,0 +1 @@
module.exports = require('../../jest.config.js')

View File

@ -0,0 +1,26 @@
{
"name": "@honojs/hello",
"version": "0.0.6",
"description": "An example of third-party middleware for Hono",
"main": "dist/index.js",
"types": "dist/index.d.ts",
"files": [
"dist"
],
"scripts": {
"test": "jest",
"build": "rimraf dist && tsc",
"prerelease": "yarn build && yarn test",
"release": "yarn publish"
},
"license": "MIT",
"private": false,
"repository": {
"type": "git",
"url": "https://github.com/honojs/middleware.git"
},
"homepage": "https://github.com/honojs/middleware",
"dependencies": {
"hono": "^2.2.5"
}
}

View File

@ -0,0 +1,26 @@
import { Hono } from 'hono'
import { hello } from '../src'
describe('Hello middleware', () => {
const app = new Hono()
app.use('/hello/*', hello())
app.get('/hello/foo', (c) => c.text('foo'))
app.use('/x/*', hello('X'))
app.get('/x/foo', (c) => c.text('foo'))
it('Should be hello message', async () => {
const res = await app.request('http://localhost/hello/foo')
expect(res).not.toBeNull()
expect(res.status).toBe(200)
expect(res.headers.get('X-Message')).toBe('Hello')
})
it('Should be X', async () => {
const res = await app.request('http://localhost/x/foo')
expect(res).not.toBeNull()
expect(res.status).toBe(200)
expect(res.headers.get('X-Message')).toBe('X')
})
})

View File

@ -0,0 +1,8 @@
import type { Handler } from 'hono'
export const hello = (message: string = 'Hello'): Handler => {
return async (c, next) => {
await next()
c.res.headers.append('X-Message', message)
}
}

View File

@ -0,0 +1,10 @@
{
"extends": "../../tsconfig.json",
"compilerOptions": {
"rootDir": "./src",
"outDir": "./dist",
},
"include": [
"src/**/*.ts"
],
}