Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
82 changes: 24 additions & 58 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,97 +8,63 @@ module.exports = {
'@typescript-eslint',
],
extends: [
'airbnb',
'airbnb-base',
'plugin:@typescript-eslint/recommended',
'@bessonovs/eslint-config',
'@bessonovs/eslint-config/typescript',
],
parser: '@typescript-eslint/parser',
rules: {
'arrow-body-style': 'off',
'lines-between-class-members': 'off',
'implicit-arrow-linebreak': 'off',
'no-console': 'error',
'no-trailing-spaces': 'error',
'no-restricted-syntax': 'off',
'indent': [
'error',
'tab',
],
'object-curly-newline': [
'error',
{
ImportDeclaration: {
minProperties: 2,
multiline: true,
}
}
],
semi: [
'error',
'never',
],
quotes: [
'error',
'single',
{
avoidEscape: true,
allowTemplateLiterals: true
}
],
'comma-dangle': [
'error',
'always-multiline'
],
'no-tabs': [
'error',
{
'allowIndentationTabs': true
}
],
'sort-imports': [
'error',
{
ignoreDeclarationSort: true,
}
],
'@typescript-eslint/explicit-function-return-type': 'error',
'@typescript-eslint/explicit-member-accessibility': 'off',
'@typescript-eslint/no-angle-bracket-type-assertion': 'off',
'@typescript-eslint/no-object-literal-type-assertion': 'off',
'@typescript-eslint/no-explicit-any': 'error',
'@typescript-eslint/no-extra-parens': 'error',
'@typescript-eslint/member-delimiter-style': [
'error',
{
multiline: {
delimiter: 'none'
}
}
},
},
],
'@typescript-eslint/indent': [
'error',
'tab'
'tab',
],
'import/prefer-default-export': 'off',
'import/no-default-export': 'error',
'import/no-unresolved': 'off',
'import/no-useless-path-segments': 'error',
'import/no-cycle': 'error',
// doesn't work for central devDependencies
'import/no-extraneous-dependencies': 'off',
'import/extensions': [
'error',
'ignorePackages',
{
ts: 'never',
tsx: 'never',
},
],
},
settings: {
'import/resolver': {
node: {
extensions: ['.js', '.jsx', '.ts', '.tsx', '.json'],
},
},
},
overrides: [
{
files: ['**/*.js'],
rules: {
'@typescript-eslint/no-var-requires': 'off',
'@typescript-eslint/explicit-function-return-type': 'off'
'@typescript-eslint/explicit-function-return-type': 'off',
},
},
{
files: ['**/__tests__/**'],
rules: {
'@typescript-eslint/explicit-function-return-type': 'off',
}
},
},
{
files: ['**/*.ts', '**/*.d.ts'],
Expand All @@ -107,5 +73,5 @@ module.exports = {
'no-useless-constructor': 'off',
},
},
]
],
}
33 changes: 17 additions & 16 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
"http",
"server"
],
"version": "0.0.4",
"version": "0.0.5",
"author": "Anton Bessonov",
"license": "MIT",
"repository": "bessonov/node-http-router",
Expand All @@ -34,25 +34,26 @@
},
"dependencies": {
"fast-url-parser": "1.1.3",
"ts-toolbelt": "4.8.19"
"ts-toolbelt": "6.7.7"
},
"devDependencies": {
"@types/express": "4.17.1",
"@types/jest": "24.0.19",
"@types/node": "12.11.1",
"@typescript-eslint/eslint-plugin": "2.4.0",
"@typescript-eslint/parser": "2.4.0",
"eslint": "6.5.1",
"eslint-config-airbnb": "18.0.1",
"eslint-plugin-import": "2.18.2",
"@bessonovs/eslint-config": "0.0.6",
"@types/express": "4.17.6",
"@types/jest": "25.2.2",
"@types/node": "14.0.1",
"@typescript-eslint/eslint-plugin": "2.33.0",
"@typescript-eslint/parser": "2.33.0",
"eslint": "7.0.0",
"eslint-config-airbnb": "18.1.0",
"eslint-plugin-import": "2.20.2",
"eslint-plugin-jsx-a11y": "6.2.3",
"eslint-plugin-react": "7.16.0",
"jest": "24.9.0",
"eslint-plugin-react": "7.20.0",
"jest": "26.0.1",
"micro": "9.3.5-canary.3",
"node-mocks-http": "1.8.0",
"path-to-regexp": "3.1.0",
"ts-jest": "24.1.0",
"typescript": "3.6.4"
"node-mocks-http": "1.8.1",
"path-to-regexp": "6.1.0",
"ts-jest": "26.0.0",
"typescript": "3.9.2"
},
"publishConfig": {
"access": "public"
Expand Down
13 changes: 8 additions & 5 deletions src/__tests__/router.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,17 @@ import {
import {
createRequest, createResponse,
} from 'node-mocks-http'
import pathToRegexp from 'path-to-regexp'
import {
compile, pathToRegexp,
} from 'path-to-regexp'
import {
MatchedHandler, Router,
} from '../router'
import {
AndMatcher, ExactUrlPathnameMatcher, MethodMatcher,
AndMatcher, EndpointMatcher, ExactUrlPathnameMatcher,
MethodMatcher,
} from '../matchers'
import { EndpointMatcher } from '../matchers/EndpointMatcher'


let router: Router

Expand Down Expand Up @@ -58,7 +61,7 @@ it('match POST /test route', () => {
const handler = (
req: IncomingMessage,
res: ServerResponse,
match: {and: [{method: string}, {pathname: string}]}
match: {and: [{method: string}, {pathname: string}]},
) => {
const [{ method }, { pathname }] = match.and
return `matched ${method} ${pathname}`
Expand Down Expand Up @@ -94,7 +97,7 @@ it('match POST /group/123 endpoint', () => {
// define an endpoint
const endpoint = ((pattern: string) => ({
pattern: pathToRegexp(pattern),
path: pathToRegexp.compile<{groupId: number}>(pattern),
path: compile<{groupId: number}>(pattern),
}))('/group/:groupId')

router.addRoute({
Expand Down
5 changes: 3 additions & 2 deletions src/examples/micro.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import http from 'http'
import micro, { sendError } from 'micro'
import { Router } from '../router'
import { EndpointMatcher } from '../matchers/EndpointMatcher'
import { ExactUrlPathnameMatcher } from '../matchers'
import {
EndpointMatcher, ExactUrlPathnameMatcher,
} from '../matchers'

/*

Expand Down
5 changes: 3 additions & 2 deletions src/examples/node.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import http from 'http'
import { Router } from '../router'
import { EndpointMatcher } from '../matchers/EndpointMatcher'
import { ExactUrlPathnameMatcher } from '../matchers'
import {
EndpointMatcher, ExactUrlPathnameMatcher,
} from '../matchers'

/*

Expand Down
5 changes: 2 additions & 3 deletions src/matchers/AndMatcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@ import {
IncomingMessage, ServerResponse,
} from 'http'
import {
MatchResult, Matched, isMatched,
} from './MatchResult'
import { Matcher } from './Matcher'
MatchResult, Matched, Matcher, isMatched,
} from '.'

export type AndMatcherResult<MR1 extends MatchResult, MR2 extends MatchResult> = MatchResult<{
and: [Matched<MR1>, Matched<MR2>]
Expand Down
2 changes: 1 addition & 1 deletion src/matchers/ExactUrlPathnameMatcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ implements Matcher<ExactUrlPathnameMatchResult<U>> {
/* istanbul ignore else */
if (req.url !== undefined) {
const { pathname } = Url.parse(req.url)
if (pathname !== undefined && this.urls.indexOf(pathname) >= 0) {
if (pathname !== null && this.urls.indexOf(pathname) >= 0) {
return {
matched: true,
pathname,
Expand Down
2 changes: 2 additions & 0 deletions src/matchers/Matcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import {
} from 'http'
import { MatchResult } from './MatchResult'

export type ExtractMatchResult<M> = M extends Matcher<infer MR> ? MR : never

export interface Matcher<T extends MatchResult = MatchResult> {
match(req: IncomingMessage, res: ServerResponse): T
}
12 changes: 6 additions & 6 deletions src/matchers/index.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
export { OrMatcher, OrMatcherResult } from './OrMatcher'
export { AndMatcher, AndMatcherResult } from './AndMatcher'
export { EndpointMatcher } from './EndpointMatcher'
export { ExactQueryMatcher } from './ExactQueryMatcher'
export { ExactUrlPathnameMatcher, ExactUrlPathnameMatchResult } from './ExactUrlPathnameMatcher'
export { MatchResult, isMatched, Matched } from './MatchResult'
export { Matcher } from './Matcher'
export { MethodMatcher, MethodMatchResult, Method } from './MethodMatcher'
export type { ExtractMatchResult, Matcher } from './Matcher'
export { isMatched, Matched, MatchResult } from './MatchResult'
export { Method, MethodMatcher, MethodMatchResult } from './MethodMatcher'
export { OrMatcher, OrMatcherResult } from './OrMatcher'
export { RegExpUrlMatcher, RegExpUrlMatchResult } from './RegExpUrlMatcher'
export { ExactQueryMatcher } from './ExactQueryMatcher'
export { EndpointMatcher } from './EndpointMatcher'
2 changes: 1 addition & 1 deletion src/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
MatchResult, Matched, Matcher, isMatched,
} from './matchers'

type Handler<MR extends MatchResult> = (
export type Handler<MR extends MatchResult> = (
req: IncomingMessage,
res: ServerResponse,
matchResult: Matched<MR>) => any // eslint-disable-line @typescript-eslint/no-explicit-any
Expand Down