Skip to content

Commit

Permalink
working
Browse files Browse the repository at this point in the history
  • Loading branch information
Bessonov committed Apr 16, 2024
1 parent 0f0c984 commit 79a2b56
Show file tree
Hide file tree
Showing 17 changed files with 120 additions and 72 deletions.
9 changes: 8 additions & 1 deletion .eslintrc.js → .eslintrc.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ module.exports = {
],
},
ignorePatterns: [
'.eslintrc.js',
'.eslintrc.cjs',
],
rules: {
'arrow-body-style': 'off',
Expand Down Expand Up @@ -80,6 +80,13 @@ module.exports = {
rules: {
indent: 'off', // ts has own rule: @typescript-eslint/indent
'no-useless-constructor': 'off',
'@typescript-eslint/no-unused-vars': [
'error',
{
argsIgnorePattern: '^_',
varsIgnorePattern: '^_',
},
],
},
},
],
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
node_modules/
dist/
build/
coverage/
pnpm-debug.log
3 changes: 2 additions & 1 deletion .npmignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
coverage/
.eslintignore
.eslintrc.js
.eslintrc.cjs
.git
.github
.npmignore
Expand All @@ -9,3 +9,4 @@ jest.config.js
tsconfig.json
**/__tests__/**
**/examples/**
build/
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ Router for Node.js, micro and other use cases
=============================================

[![Project is](https://img.shields.io/badge/Project%20is-fantastic-ff69b4.svg)](https://github.com/Bessonov/node-http-router)
[![Build Status](https://api.travis-ci.org/Bessonov/node-http-router.svg?branch=master)](https://travis-ci.org/Bessonov/node-http-router)
[![License](http://img.shields.io/:license-MIT-blue.svg)](https://raw.githubusercontent.com/Bessonov/node-http-router/master/LICENSE)

This router is intended to be used with native node http interface. Features:
Expand Down
16 changes: 6 additions & 10 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,11 @@
import { defaults } from 'jest-config'

// eslint-disable-next-line import/no-default-export
export default {
roots: ['<rootDir>/src'],
preset: 'ts-jest',
roots: ['<rootDir>/dist'],
testEnvironment: 'node',
testRegex: '(/__tests__/.*|(\\.|/)test)\\.tsx?$',
testPathIgnorePatterns: ['/node_modules/'],
moduleFileExtensions: [...defaults.moduleFileExtensions, 'ts', 'tsx'],
transform: {
'\\.[jt]sx?$': ['ts-jest'],
},
testRegex: '(/__tests__/.*|(\\.|/)test)\\.jsx?$',
collectCoverageFrom: ['<rootDir>/dist/**/*.js'],
// eslint-disable-next-line no-useless-escape
coveragePathIgnorePatterns: ['/dist\/examples/'],
coverageThreshold: {
global: {
statements: 100,
Expand Down
34 changes: 23 additions & 11 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,20 +25,29 @@
"type": "module",
"sideEffects": false,
"scripts": {
"test": "jest --coverage",
"test:watch": "jest --coverage --watch",
"lint": "eslint . --ext .ts,.js",
"build": "tsc",
"example-node-start": "tsc && node dist/examples/node.js",
"example-micro-start": "tsc && node dist/examples/micro.js",
"precommit": "pnpm test && pnpm run lint && pnpm run build",
"start": "pnpm run compile && ultra start:all",
"start:all": "pnpm run compile:watch || pnpm run test:watch",
"test": "pnpm run compile && NODE_OPTIONS='--experimental-vm-modules --no-warnings=ExperimentalWarning' jest --coverage",
"test:watch": "NODE_OPTIONS=--experimental-vm-modules jest --coverage --watchAll",
"lint": "eslint . --ext .ts,.js,.cjs",
"compile": "tsc --build",
"compile:watch": "tsc --build --watch",
"example-node-start": "pnpm run compile && node dist/examples/node.js",
"example-micro-start": "pnpm run compile && node dist/examples/micro.js",
"precommit": "pnpm run compile && pnpm test && pnpm run lint",
"update": "pnpm update --interactive --recursive --latest"
},
"ultra": {
"concurrent": [
"start:all"
]
},
"dependencies": {
"urlite": "3.1.0"
},
"devDependencies": {
"@bessonovs/eslint-config": "0.0.7",
"@jest/globals": "29.7.0",
"@types/express": "4.17.21",
"@types/jest": "29.5.12",
"@types/node": "20.12.7",
Expand All @@ -50,19 +59,22 @@
"eslint-plugin-jsx-a11y": "6.8.0",
"eslint-plugin-react": "7.34.1",
"jest": "29.7.0",
"jest-config": "29.7.0",
"micro": "10.0.1",
"node-mocks-http": "1.14.1",
"path-to-regexp": "6.2.2",
"ts-jest": "29.1.2",
"ts-toolbelt": "9.6.0",
"typescript": "5.4.5"
"typescript": "5.4.5",
"ultra-runner": "3.10.5"
},
"publishConfig": {
"access": "public"
},
"packageManager": "[email protected]+sha256.50783dd0fa303852de2dd1557cd4b9f07cb5b018154a6e76d0f40635d6cee019",
"pnpm": {
"updateConfig": {
"ignoreDependencies": [
"eslint"
]
},
"peerDependencyRules": {
"ignoreMissing": [
"eslint-plugin-react-hooks"
Expand Down
61 changes: 30 additions & 31 deletions src/__tests__/Router.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,7 @@ import {
compile,
pathToRegexp,
} from 'path-to-regexp'
import {
Test,
} from 'ts-toolbelt'
import {
import type {
ServerResponse,
} from 'http'
import {
Expand Down Expand Up @@ -167,60 +164,62 @@ class TestMatcherWithParams<
T extends MatchResult<void>,
D extends { test: string }
> implements Matcher<T, D> {
// eslint-disable-next-line @typescript-eslint/no-unused-vars, class-methods-use-this
match(neededForTypeScript: D): T {
// eslint-disable-next-line class-methods-use-this
match(_neededForTypeScript: D): T {
throw new Error('Method not implemented.')
}
}

it('simple route match', () => {
const routerWithoutParams = new Router()
interface TypeCheckPass {
data: unknown
match: { matched: true; result: boolean }
}
routerWithoutParams.addRoute<MatchResult<boolean>>({
matcher: new BooleanMatcher(true),
handler: params => {
Test.checks([
Test.check<typeof params, {
data: unknown
match: { matched: true; result: boolean }
}, Test.Pass>(),
])
handler: (_params: TypeCheckPass) => {
return 'test'
},
})
})

it('simple route match with custom type', () => {
const typedRouter = new Router<{ test: string }>()

interface TypeCheckPass {
data: { test: string }
match: { matched: true; result: void }
}

typedRouter.addRoute({
matcher: new TestMatcherWithParams(),
handler: params => {
Test.checks([
Test.check<typeof params, {
data: { test: string }
match: { matched: true; result: void }
}, Test.Pass>(),
])
handler: (_params: TypeCheckPass) => {
return 'myreturn'
},
})
})

it('simple non-matching router type', () => {
const typedRouter = new Router<{ test2: string }>()

interface TypeCheckPass {
data: { test2: string }
match: { matched: true; result: void }
}

interface TypeCheckFail {
data: { test: string }
match: { matched: true; result: void }
}

typedRouter.addRoute({
// @ts-expect-error
matcher: new TestMatcherWithParams(),
handler: params => {
Test.checks([
Test.check<typeof params, {
data: { test2: string }
match: { matched: true; result: void }
}, Test.Pass>(),
Test.check<typeof params, {
data: { test: string }
match: { matched: true; result: void }
}, Test.Fail>(),
])
handler: (_params: TypeCheckPass) => {
// @ts-expect-error
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const fail: TypeCheckFail = _params
return 'myreturn'
},
})
Expand Down
2 changes: 1 addition & 1 deletion src/matchers/RegExpUrlMatcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export class RegExpUrlMatcher<
R extends object,
P extends RegExpUrlMatcherInput = RegExpUrlMatcherInput
>
implements Matcher<RegExpUrlMatchResult<R>, P> {
implements Matcher<RegExpUrlMatchResult<R>, P> {
constructor(private readonly urls: RegExp[]) {
this.match = this.match.bind(this)
}
Expand Down
8 changes: 6 additions & 2 deletions src/matchers/__tests__/AndMatcher.test.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
import { createRequest } from 'node-mocks-http'
import {
createRequest,
} from 'node-mocks-http'
import {
AndMatcher,
ExactUrlPathnameMatcher,
MethodMatcher,
} from '..'
import type { ServerRequest } from '../../node/ServerRequest'
import type {
ServerRequest,
} from '../../node/ServerRequest'

it('none match', () => {
const result = new AndMatcher([
Expand Down
8 changes: 6 additions & 2 deletions src/matchers/__tests__/EndpointMatcher.test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
import { createRequest } from 'node-mocks-http'
import {
createRequest,
} from 'node-mocks-http'
import {
EndpointMatcher,
} from '..'
import type { ServerRequest } from '../../node/ServerRequest'
import type {
ServerRequest,
} from '../../node/ServerRequest'

it('not match empty', () => {
const result = new EndpointMatcher('GET', /\/test/)
Expand Down
8 changes: 6 additions & 2 deletions src/matchers/__tests__/ExactQueryMatcher.test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
import { createRequest } from 'node-mocks-http'
import {
createRequest,
} from 'node-mocks-http'
import {
ExactQueryMatcher,
} from '..'
import type { ServerRequest } from '../../node/ServerRequest'
import type {
ServerRequest,
} from '../../node/ServerRequest'

const matcher = new ExactQueryMatcher({
mustPresent: true,
Expand Down
8 changes: 6 additions & 2 deletions src/matchers/__tests__/ExactUrlPathnameMatcher.test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
import { createRequest } from 'node-mocks-http'
import {
createRequest,
} from 'node-mocks-http'
import {
ExactUrlPathnameMatcher,
} from '..'
import type { ServerRequest } from '../../node/ServerRequest'
import type {
ServerRequest,
} from '../../node/ServerRequest'

it('not match empty', () => {
const result = new ExactUrlPathnameMatcher(['/test'])
Expand Down
8 changes: 6 additions & 2 deletions src/matchers/__tests__/MethodMatcher.test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
import { createRequest } from 'node-mocks-http'
import {
createRequest,
} from 'node-mocks-http'
import {
MethodMatcher,
} from '../MethodMatcher'
import type { ServerRequest } from '../../node/ServerRequest'
import type {
ServerRequest,
} from '../../node/ServerRequest'

it('not match', () => {
const result = new MethodMatcher(['POST'])
Expand Down
8 changes: 6 additions & 2 deletions src/matchers/__tests__/OrMatcher.test.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
import { createRequest } from 'node-mocks-http'
import {
createRequest,
} from 'node-mocks-http'
import {
ExactUrlPathnameMatcher,
MethodMatcher,
OrMatcher,
} from '..'
import type { ServerRequest } from '../../node/ServerRequest'
import type {
ServerRequest,
} from '../../node/ServerRequest'

it('none match', () => {
const result = new OrMatcher([
Expand Down
8 changes: 6 additions & 2 deletions src/matchers/__tests__/RegExpUrlMatcher.test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
import { createRequest } from 'node-mocks-http'
import {
createRequest,
} from 'node-mocks-http'
import {
RegExpUrlMatcher,
} from '..'
import type { ServerRequest } from '../../node/ServerRequest'
import type {
ServerRequest,
} from '../../node/ServerRequest'

it('not match', () => {
const result = new RegExpUrlMatcher([/^\/test$/])
Expand Down
3 changes: 3 additions & 0 deletions src/middlewares/__tests__/CorsMiddleware.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ import {
createRequest,
createResponse,
} from 'node-mocks-http'
import {
jest,
} from '@jest/globals'
import {
CorsMiddleware,
} from '../CorsMiddleware'
Expand Down
6 changes: 4 additions & 2 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,11 @@
"resolveJsonModule": true,
"sourceMap": true,
"noUncheckedIndexedAccess": true,
"noImplicitOverride": true,
"noImplicitOverride": true,
"verbatimModuleSyntax": true,
"rootDir": "./src",
"outDir": "./dist"
"outDir": "./dist",
"incremental": true,
"tsBuildInfoFile": "./build/.tsbuildinfo"
}
}

0 comments on commit 79a2b56

Please sign in to comment.