node.ts
This file
Path
src/node.ts
125 lines · 3.1 KiB · TypeScript
Last changed by
Setup lint to enable code styles check (#27)Minghebefore adoption7c87ac6
History of this fileTake it
Migrate to TypeScript (#21)Yusuke Wadabefore adoptione6039f6
lines 1–33why, one step back1import { splitPath, getPattern } from './util'23const 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 back34 // eslint-disable-next-line @typescript-eslint/no-this-aliasMigrate to TypeScript (#21)Yusuke Wadabefore adoptione6039f6
lines 35–50why, one step back35 let curNode: Node<T> = this36 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 back51 // eslint-disable-next-line @typescript-eslint/no-this-aliasMigrate to TypeScript (#21)Yusuke Wadabefore adoptione6039f6
lines 52–54why, one step back52 let curNode: Node<T> = this5354 const params: { [key: string]: string } = {}Setup lint to enable code styles check (#27)Minghebefore adoption7c87ac6
line 55why, one step back55 const parts = splitPath(path)Migrate to TypeScript (#21)Yusuke Wadabefore adoptione6039f6
lines 56–125why, one step back5657 for (let i = 0, len = parts.length; i < len; i++) {58 const p: string = parts[i]5960 // '*' => match any path61 if (curNode.children['*']) {62 const astNode = curNode.children['*']63 if (Object.keys(astNode.children).length === 0) {64 curNode = astNode65 break66 }67 }6869 const nextNode = curNode.children[p]7071 if (nextNode) {72 curNode = nextNode73 // '/hello/*' => match '/hello'74 if (!(i == len - 1 && nextNode.children['*'])) {75 continue76 }77 }7879 let isWildcard = false80 let isParamMatch = false81 const keys = Object.keys(curNode.children)8283 for (let j = 0, len = keys.length; j < len; j++) {84 const key: string = keys[j]8586 // Wildcard87 // '/hello/*/foo' => match /hello/bar/foo88 if (key === '*') {89 curNode = curNode.children['*']90 isWildcard = true91 break92 }93 const pattern = getPattern(key)94 // Named match95 if (pattern) {96 const match = p.match(new RegExp(pattern[1]))97 if (match) {98 const k: string = pattern[0]99 params[k] = match[1]100 curNode = curNode.children[key]101 isParamMatch = true102 break103 }104 return noRoute()105 }106 }107108 if (isWildcard && i === len - 1) {109 break110 }111112 if (isWildcard === false && isParamMatch === false) {113 return noRoute()114 }115 }116117 const handler = curNode.method[METHOD_NAME_OF_ALL] || curNode.method[method]118119 if (!handler) {120 return noRoute()121 }122123 return new Result<T>(handler, params)124 }125}