From e970536220617e54c77f5bac7e5fe930248aff1a Mon Sep 17 00:00:00 2001 From: Yusuke Wada Date: Fri, 21 Apr 2023 23:43:30 +0900 Subject: [PATCH] feat: add "medley-router" (#82) * feat: add "medley-router" * setup * add changeset --- .changeset/breezy-clocks-end.md | 5 ++++ .github/workflows/ci-medley-router.yml | 25 ++++++++++++++++ package.json | 3 +- packages/medley-router/README.md | 22 +++++++++++++++ packages/medley-router/jest.config.js | 1 + packages/medley-router/package.json | 36 ++++++++++++++++++++++++ packages/medley-router/src/index.test.ts | 14 +++++++++ packages/medley-router/src/index.ts | 1 + packages/medley-router/src/router.ts | 31 ++++++++++++++++++++ packages/medley-router/tsconfig.json | 10 +++++++ yarn.lock | 22 +++++++++++---- 11 files changed, 164 insertions(+), 6 deletions(-) create mode 100644 .changeset/breezy-clocks-end.md create mode 100644 .github/workflows/ci-medley-router.yml create mode 100644 packages/medley-router/README.md create mode 100644 packages/medley-router/jest.config.js create mode 100644 packages/medley-router/package.json create mode 100644 packages/medley-router/src/index.test.ts create mode 100644 packages/medley-router/src/index.ts create mode 100644 packages/medley-router/src/router.ts create mode 100644 packages/medley-router/tsconfig.json diff --git a/.changeset/breezy-clocks-end.md b/.changeset/breezy-clocks-end.md new file mode 100644 index 00000000..5ff8f6e1 --- /dev/null +++ b/.changeset/breezy-clocks-end.md @@ -0,0 +1,5 @@ +--- +'@hono/medley-router': patch +--- + +initial release diff --git a/.github/workflows/ci-medley-router.yml b/.github/workflows/ci-medley-router.yml new file mode 100644 index 00000000..d2ea3e5c --- /dev/null +++ b/.github/workflows/ci-medley-router.yml @@ -0,0 +1,25 @@ +name: ci-medley-router +on: + push: + branches: [main] + paths: + - 'packages/medley-router/**' + pull_request: + branches: ['*'] + paths: + - 'packages/medley-router/**' + +jobs: + ci: + runs-on: ubuntu-latest + defaults: + run: + working-directory: ./packages/medley-router + steps: + - uses: actions/checkout@v2 + - uses: actions/setup-node@v2 + with: + node-version: 18.x + - run: yarn install --frozen-lockfile + - run: yarn build + - run: yarn test diff --git a/package.json b/package.json index 064972fd..961cefb4 100644 --- a/package.json +++ b/package.json @@ -14,6 +14,7 @@ "build:firebase-auth": "yarn workspace @hono/firebase-auth build", "build:trpc-server": "yarn workspace @hono/trpc-server build", "build:typebox-validator": "yarn workspace @hono/typebox-validator build", + "build:medley-router": "yarn workspace @hono/medley-router build", "build": "run-p build:*" }, "license": "MIT", @@ -45,4 +46,4 @@ "ts-jest": "^29.0.5", "typescript": "^4.7.4" } -} +} \ No newline at end of file diff --git a/packages/medley-router/README.md b/packages/medley-router/README.md new file mode 100644 index 00000000..0c10a3bb --- /dev/null +++ b/packages/medley-router/README.md @@ -0,0 +1,22 @@ +# Router using @medley/router + +Just a PoC. + +## Usage + +```ts +import { Hono } from 'hono' +import { MedleyRouter } from '@hono/medley-router' + +const app = new Hono({ router: new MedleyRouter() }) + +app.get('/', (c) => c.text('Hello')) +``` + +## Authors + +Yusuke Wada + +## License + +MIT diff --git a/packages/medley-router/jest.config.js b/packages/medley-router/jest.config.js new file mode 100644 index 00000000..f697d831 --- /dev/null +++ b/packages/medley-router/jest.config.js @@ -0,0 +1 @@ +module.exports = require('../../jest.config.js') diff --git a/packages/medley-router/package.json b/packages/medley-router/package.json new file mode 100644 index 00000000..76827ba8 --- /dev/null +++ b/packages/medley-router/package.json @@ -0,0 +1,36 @@ +{ + "name": "@hono/medley-router", + "version": "0.0.0", + "description": "Router using @medley/router", + "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, + "publishConfig": { + "registry": "https://registry.npmjs.org", + "access": "public" + }, + "repository": { + "type": "git", + "url": "https://github.com/honojs/middleware.git" + }, + "homepage": "https://github.com/honojs/middleware", + "peerDependencies": { + "hono": "*" + }, + "devDependencies": { + "hono": "^3.1.6" + }, + "dependencies": { + "@medley/router": "^0.2.1" + } +} \ No newline at end of file diff --git a/packages/medley-router/src/index.test.ts b/packages/medley-router/src/index.test.ts new file mode 100644 index 00000000..98486c49 --- /dev/null +++ b/packages/medley-router/src/index.test.ts @@ -0,0 +1,14 @@ +import { Hono } from 'hono' +import { MedleyRouter } from '../src' + +describe('Basic', () => { + const app = new Hono({ router: new MedleyRouter() }) + + app.get('/', (c) => c.text('Hello')) + + it('Should return a 200 response', async () => { + const res = await app.request('/') + expect(res).not.toBeNull() + expect(res.status).toBe(200) + }) +}) diff --git a/packages/medley-router/src/index.ts b/packages/medley-router/src/index.ts new file mode 100644 index 00000000..d63d1dde --- /dev/null +++ b/packages/medley-router/src/index.ts @@ -0,0 +1 @@ +export { MedleyRouter } from './router' diff --git a/packages/medley-router/src/router.ts b/packages/medley-router/src/router.ts new file mode 100644 index 00000000..803b4bf2 --- /dev/null +++ b/packages/medley-router/src/router.ts @@ -0,0 +1,31 @@ +// eslint-disable-next-line @typescript-eslint/ban-ts-comment +// @ts-ignore +import OriginalRouter from '@medley/router' +// Should be exported from `hono/router` +import type { Result, Router } from 'hono/dist/types/router' + +export class MedleyRouter implements Router { + router: any + + constructor() { + this.router = new OriginalRouter() + } + + add(method: string, path: string, handler: T) { + const store = this.router.register(path) + store[method] = handler + } + + match(method: string, path: string): Result | null { + const route = this.router.find(path) + + if (route) { + return { + handlers: [route['store'][method]], + params: route['params'], + } + } + + return null + } +} diff --git a/packages/medley-router/tsconfig.json b/packages/medley-router/tsconfig.json new file mode 100644 index 00000000..acfcd843 --- /dev/null +++ b/packages/medley-router/tsconfig.json @@ -0,0 +1,10 @@ +{ + "extends": "../../tsconfig.json", + "compilerOptions": { + "rootDir": "./src", + "outDir": "./dist", + }, + "include": [ + "src/**/*.ts" + ], +} \ No newline at end of file diff --git a/yarn.lock b/yarn.lock index 7687d6ea..268592d2 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1196,6 +1196,13 @@ unist-util-visit "^4.0.0" vfile "^5.0.0" +"@medley/router@^0.2.1": + version "0.2.1" + resolved "https://registry.yarnpkg.com/@medley/router/-/router-0.2.1.tgz#d8b8aad3fb1de710e9bd35e9e249545dc65945d0" + integrity sha512-mdvS1spIxmZoUbTdYmWknHtwm72WwrGNoQCDd4RTvcXJ9G6XThxeC3g+cpOf6Fw6vIERHt50pYiJpsk5XTJQ5w== + dependencies: + object-treeify "^1.1.20" + "@miniflare/cache@2.12.2": version "2.12.2" resolved "https://registry.yarnpkg.com/@miniflare/cache/-/cache-2.12.2.tgz#402cbf67c9fc717b26c06516a166100d4b020870" @@ -5369,16 +5376,16 @@ hono@3.1.5: resolved "https://registry.yarnpkg.com/hono/-/hono-3.1.5.tgz#a1c5314bb1cf0fd8b72bd2b6b6698eee16fbc520" integrity sha512-ypFLhNYoXXtep4I9zJt3VpB5/Ze3p9BLU4dpnAp7fxHOmSg8lu/Wwjs5sTJnb2GwVdfjbt9KFB9alA4Zt/P0jw== -hono@^2.7.2: - version "2.7.8" - resolved "https://registry.yarnpkg.com/hono/-/hono-2.7.8.tgz#5f6916c7f6838fe1f909f6046b30e6a0900f3128" - integrity sha512-LXLXw6LilE16QO0siFBDiNzmaRP6ca5ZyF0gDWcaiUqJJtE/d4lV/Hpst2O33AmJB5n0DQa5w53gZLUVf7uXNg== - hono@^3.0.0, hono@^3.0.2, hono@^3.0.3, hono@^3.1.0, hono@^3.1.2: version "3.1.2" resolved "https://registry.yarnpkg.com/hono/-/hono-3.1.2.tgz#896231b8940c201212bb3d440bebce637e68be26" integrity sha512-keNMGSlBX2VbwD5gF10Xu0zuUm9mTy1HWctIhuom8FJEJY6aKo1Bb/vQXTEjBupKDG7MJi2aG05YMo01GjkMQA== +hono@^3.1.6: + version "3.1.6" + resolved "https://registry.yarnpkg.com/hono/-/hono-3.1.6.tgz#7a7ca700fff69553a8c2a7a97d544f49bb8b1bd3" + integrity sha512-ugC7YbuyATZChp+SxfstiavvTCqENjvSiTiDAkbAOP20pJ58N4kXcbDi93SwZrvMFA2VVx1veMYHrsvUY9hcVA== + hosted-git-info@^2.1.4: version "2.8.9" resolved "https://registry.yarnpkg.com/hosted-git-info/-/hosted-git-info-2.8.9.tgz#dffc0bf9a21c02209090f2aa69429e1414daf3f9" @@ -8499,6 +8506,11 @@ object-keys@^1.1.1: resolved "https://registry.yarnpkg.com/object-keys/-/object-keys-1.1.1.tgz#1c47f272df277f3b1daf061677d9c82e2322c60e" integrity sha512-NuAESUOUMrlIXOfHKzD6bpPu3tYt3xvjNdRIQ+FeT0lNb4K8WR70CaDxhuNguS2XG+GjkyMwOzsN5ZktImfhLA== +object-treeify@^1.1.20: + version "1.1.33" + resolved "https://registry.yarnpkg.com/object-treeify/-/object-treeify-1.1.33.tgz#f06fece986830a3cba78ddd32d4c11d1f76cdf40" + integrity sha512-EFVjAYfzWqWsBMRHPMAXLCDIJnpMhdWAqR7xG6M6a2cs6PMFpl/+Z20w9zDW4vkxOFfddegBKq9Rehd0bxWE7A== + object.assign@^4.1.4: version "4.1.4" resolved "https://registry.yarnpkg.com/object.assign/-/object.assign-4.1.4.tgz#9673c7c7c351ab8c4d0b516f4343ebf4dfb7799f"