Initial commit

pull/34/head
Yusuke Wada 2022-07-23 23:56:20 +09:00
commit 1e06bb22df
15 changed files with 3013 additions and 0 deletions

27
.github/workflows/ci.yml vendored 100644
View File

@ -0,0 +1,27 @@
name: ci
on:
push:
branches: [main]
pull_request:
branches: ['*']
jobs:
ci:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: actions/setup-node@v2
with:
node-version: 16.x
- run: yarn install --frozen-lockfile
- run: yarn build
- run: yarn test
deno:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- uses: denoland/setup-deno@v1
with:
deno-version: v1.x
- run: deno test deno_test

7
.gitignore vendored 100644
View File

@ -0,0 +1,7 @@
dist
node_modules
.yarn/*
# for debug or playing
sandbox

41
README.md 100644
View File

@ -0,0 +1,41 @@
# 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
```
## Deno
```ts
import { serve } from 'https://deno.land/std/http/server.ts'
import { hello } from 'https://deno.land/x/hono_hello/mod.ts'
import { Hono } from 'https://deno.land/x/hono/mod.ts'
const app = new Hono()
app.use('*', hello('Hello!! Hono!!'))
app.get('/', (c) => c.text('foo'))
serve(app.fetch)
```
## Author
Yusuke Wada <https://github.com/yusukebe>
## License
MIT

View File

@ -0,0 +1,41 @@
# 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
```
## Deno
```ts
import { serve } from 'https://deno.land/std/http/server.ts'
import { hello } from 'https://deno.land/x/hono_hello/mod.ts'
import { Hono } from 'https://deno.land/x/hono/mod.ts'
const app = new Hono()
app.use('*', hello('Hello!! Hono!!'))
app.get('/', (c) => c.text('foo'))
serve(app.fetch)
```
## Author
Yusuke Wada <https://github.com/yusukebe>
## License
MIT

View File

@ -0,0 +1,8 @@
import { Context, Next, Handler } from 'https://raw.githubusercontent.com/honojs/hono/v2.0.2/deno_dist/mod.ts'
export const hello = (message: string = 'Hello'): Handler => {
return async (c: Context, next: Next) => {
await next()
c.res.headers.append('X-Message', message)
}
}

1
deno_dist/mod.ts 100644
View File

@ -0,0 +1 @@
export * from "./index.ts";

View File

@ -0,0 +1,3 @@
{
"deno.enable": true
}

View File

@ -0,0 +1,2 @@
export { assert, assertEquals } from 'https://deno.land/std@0.148.0/testing/asserts.ts'
export { Hono } from 'https://deno.land/x/hono@v2.0.2/mod.ts'

View File

@ -0,0 +1,15 @@
import { hello } from '../deno_dist/mod.ts'
import { assertEquals, Hono } from './deps.ts'
// Test just only minimal patterns.
// Because others are tested well in Cloudflare Workers environment already.
Deno.test('Hello Middleware', async () => {
const app = new Hono()
app.use('/hello/*', hello())
app.get('/hello/foo', (c) => c.text('foo'))
let res = await app.request('http://localhost/hello/foo')
assertEquals(res.status, 200)
assertEquals(res.headers.get('X-Message'), 'Hello')
})

7
jest.config.js 100644
View File

@ -0,0 +1,7 @@
module.exports = {
testMatch: ['**/test/**/*.+(ts|tsx|js)', '**/src/**/(*.)+(spec|test).+(ts|tsx|js)'],
transform: {
'^.+\\.(ts|tsx)$': 'ts-jest',
},
testEnvironment: 'miniflare',
}

44
package.json 100644
View File

@ -0,0 +1,44 @@
{
"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",
"test:deno": "deno test deno_test",
"test:all": "yarn test && yarn test:deno",
"denoify": "rimraf deno_dist && denoify",
"build": "rimraf dist && tsc",
"prerelease": "yarn denoify && yarn build && yarn test:all",
"release": "yarn publish"
},
"license": "MIT",
"repository": {
"type": "git",
"url": "https://github.com/honojs/middleware-template.git"
},
"homepage": "https://github.com/honojs/middleware-template",
"author": "Yusuke Wada <yusuke@kamawada.com> (https://github.com/yusukebe)",
"publishConfig": {
"registry": "https://registry.npmjs.org",
"access": "public"
},
"dependencies": {
"hono": "^2.0.2"
},
"devDependencies": {
"@cloudflare/workers-types": "^3.14.0",
"@types/jest": "^28.1.4",
"denoify": "^0.11.1",
"jest": "^28.1.2",
"jest-environment-miniflare": "^2.6.0",
"prettier": "^2.7.1",
"rimraf": "^3.0.2",
"ts-jest": "^28.0.5",
"typescript": "^4.7.4"
}
}

8
src/index.ts 100644
View File

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

26
test/index.test.ts 100644
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')
})
})

26
tsconfig.json 100644
View File

@ -0,0 +1,26 @@
{
"compilerOptions": {
"target": "es2020",
"module": "commonjs",
"declaration": true,
"moduleResolution": "Node",
"esModuleInterop": true,
"forceConsistentCasingInFileNames": true,
"strict": true,
"skipLibCheck": true,
"strictPropertyInitialization": true,
"strictNullChecks": true,
"noUnusedLocals": true,
"noUnusedParameters": true,
"types": [
"jest",
"node",
"@cloudflare/workers-types"
],
"rootDir": "./src",
"outDir": "./dist",
},
"include": [
"src/**/*.ts"
],
}

2757
yarn.lock 100644

File diff suppressed because it is too large Load Diff