Initial commit

pull/31/head
Yusuke Wada 2022-08-04 08:54:30 +09:00
commit fafa2c6bba
17 changed files with 4326 additions and 0 deletions

60
.eslintrc.js 100644
View File

@ -0,0 +1,60 @@
const { defineConfig } = require('eslint-define-config')
module.exports = defineConfig({
root: true,
extends: [
'eslint:recommended',
'plugin:node/recommended',
'plugin:@typescript-eslint/recommended',
'prettier',
],
parser: '@typescript-eslint/parser',
parserOptions: {
sourceType: 'module',
ecmaVersion: 2021,
},
plugins: ['@typescript-eslint', 'import'],
globals: {
fetch: false,
Response: false,
Request: false,
addEventListener: false,
},
rules: {
quotes: ['error', 'single'],
semi: ['error', 'never'],
'no-debugger': ['error'],
'no-empty': ['warn', { allowEmptyCatch: true }],
'no-process-exit': 'off',
'no-useless-escape': 'off',
'prefer-const': [
'warn',
{
destructuring: 'all',
},
],
'@typescript-eslint/ban-types': [
'error',
{
types: {
Function: false,
},
},
],
'sort-imports': 0,
'import/order': [2, { alphabetize: { order: 'asc' } }],
'node/no-missing-import': 'off',
'node/no-missing-require': 'off',
'node/no-deprecated-api': 'off',
'node/no-unpublished-import': 'off',
'node/no-unpublished-require': 'off',
'node/no-unsupported-features/es-syntax': 'off',
'@typescript-eslint/no-empty-function': ['error', { allow: ['arrowFunctions'] }],
'@typescript-eslint/no-empty-interface': 'off',
'@typescript-eslint/no-inferrable-types': 'off',
'@typescript-eslint/no-var-requires': 'off',
'@typescript-eslint/consistent-type-imports': ['error', { prefer: 'type-imports' }],
},
})

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

9
.gitignore vendored 100644
View File

@ -0,0 +1,9 @@
dist
node_modules
.yarn/*
yarn-error.log
*.tgz
# for debug or playing
sandbox

9
.prettierrc 100644
View File

@ -0,0 +1,9 @@
{
"printWidth": 100,
"trailingComma": "es5",
"tabWidth": 2,
"semi": false,
"singleQuote": true,
"jsxSingleQuote": true,
"endOfLine": "lf"
}

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 type { Handler } from 'https://raw.githubusercontent.com/honojs/hono/v2.0.6/deno_dist/mod.ts'
export const hello = (message: string = 'Hello'): Handler => {
return async (c, 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.6/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',
}

60
package.json 100644
View File

@ -0,0 +1,60 @@
{
"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"
},
"denoify": {
"port": {
"hono": "honojs/hono"
}
},
"license": "MIT",
"private": false,
"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.6"
},
"devDependencies": {
"@cloudflare/workers-types": "^3.14.0",
"@types/jest": "^28.1.4",
"@typescript-eslint/eslint-plugin": "^5.32.0",
"@typescript-eslint/parser": "^5.32.0",
"denoify": "^0.11.1",
"eslint": "^8.21.0",
"eslint-config-prettier": "^8.5.0",
"eslint-define-config": "^1.6.0",
"eslint-import-resolver-typescript": "^3.4.0",
"eslint-plugin-eslint-comments": "^3.2.0",
"eslint-plugin-flowtype": "^8.0.3",
"eslint-plugin-import": "^2.26.0",
"eslint-plugin-node": "^11.1.0",
"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 type { Handler } from 'hono'
export const hello = (message: string = 'Hello'): Handler => {
return async (c, 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"
],
}

3983
yarn.lock 100644

File diff suppressed because it is too large Load Diff