Skip to content
Myr

node.ts

This file

Path

src/node.ts

126 lines · 3.1 KiB · TypeScript

Last changed by

refactor(utils): better management for utils modules to avoid kitchen sink issue (#59)Minghebefore adoptionc522636

History of this file

Take it

refactor(utils): better management for utils modules to avoid kitchen sink issue (#59)Minghebefore adoptionc522636

line 1why, one step back
1import { splitPath, getPattern } from './utils/url'

Migrate to TypeScript (#21)Yusuke Wadabefore adoptione6039f6

lines 2–33why, one step back
2
3const METHOD_NAME_OF_ALL = 'ALL'45export class Result<T> {6 handler: T7 params: { [key: string]: string }8 constructor(handler: T, params: { [key: string]: string }) {9 this.handler = handler10 this.params = params11 }12}1314const noRoute = (): null => {15 return null16}1718export class Node<T> {19 method: { [key: string]: T }20 handler: T21 children: { [key: string]: Node<T> }22 middlewares: []2324 constructor(method?: string, handler?: any, children?: { [key: string]: Node<T> }) {25 this.children = children || {}26 this.method = {}27 if (method && handler) {28 this.method[method] = handler29 }30 this.middlewares = []31 }3233 insert(method: string, path: string, handler: T): Node<T> {

Setup lint to enable code styles check (#27)Minghebefore adoption7c87ac6

line 34why, one step back
34 // eslint-disable-next-line @typescript-eslint/no-this-alias

Migrate to TypeScript (#21)Yusuke Wadabefore adoptione6039f6

lines 35–50why, one step back
35 let curNode: Node<T> = this
36 const parts = splitPath(path)37 for (let i = 0, len = parts.length; i < len; i++) {38 const p: string = parts[i]39 if (Object.keys(curNode.children).includes(p)) {40 curNode = curNode.children[p]41 continue42 }43 curNode.children[p] = new Node()44 curNode = curNode.children[p]45 }46 curNode.method[method] = handler47 return curNode48 }4950 search(method: string, path: string): Result<T> {

Setup lint to enable code styles check (#27)Minghebefore adoption7c87ac6

line 51why, one step back
51 // eslint-disable-next-line @typescript-eslint/no-this-alias

Migrate to TypeScript (#21)Yusuke Wadabefore adoptione6039f6

lines 52–54why, one step back
52 let curNode: Node<T> = this
5354 const params: { [key: string]: string } = {}

Setup lint to enable code styles check (#27)Minghebefore adoption7c87ac6

line 55why, one step back
55 const parts = splitPath(path)

Migrate to TypeScript (#21)Yusuke Wadabefore adoptione6039f6

lines 56–60why, one step back
56
57 for (let i = 0, len = parts.length; i < len; i++) {58 const p: string = parts[i]5960 // '*' => match any path

feat: default route with wildcard (#52)Yusuke Wadabefore adoptionb6c3eef

lines 61–62why, one step back
61 // /api/* => default wildcard match
62 if (curNode.children['*'] && !curNode.children[p]) {

Migrate to TypeScript (#21)Yusuke Wadabefore adoptione6039f6

lines 63–126why, one step back
63 const astNode = curNode.children['*']
64 if (Object.keys(astNode.children).length === 0) {65 curNode = astNode66 break67 }68 }6970 const nextNode = curNode.children[p]7172 if (nextNode) {73 curNode = nextNode74 // '/hello/*' => match '/hello'75 if (!(i == len - 1 && nextNode.children['*'])) {76 continue77 }78 }7980 let isWildcard = false81 let isParamMatch = false82 const keys = Object.keys(curNode.children)8384 for (let j = 0, len = keys.length; j < len; j++) {85 const key: string = keys[j]8687 // Wildcard88 // '/hello/*/foo' => match /hello/bar/foo89 if (key === '*') {90 curNode = curNode.children['*']91 isWildcard = true92 break93 }94 const pattern = getPattern(key)95 // Named match96 if (pattern) {97 const match = p.match(new RegExp(pattern[1]))98 if (match) {99 const k: string = pattern[0]100 params[k] = match[1]101 curNode = curNode.children[key]102 isParamMatch = true103 break104 }105 return noRoute()106 }107 }108109 if (isWildcard && i === len - 1) {110 break111 }112113 if (isWildcard === false && isParamMatch === false) {114 return noRoute()115 }116 }117118 const handler = curNode.method[METHOD_NAME_OF_ALL] || curNode.method[method]119120 if (!handler) {121 return noRoute()122 }123124 return new Result<T>(handler, params)125 }126}