This repository has been archived by the owner on Jan 18, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(router): add initial working router (#57)
fix(router): add initial working router
- Loading branch information
Showing
9 changed files
with
122 additions
and
31 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import { METHODS } from 'http'; | ||
import join from 'url-join'; | ||
import camelCase from 'lodash.camelcase'; | ||
|
||
interface RouterRouteType { | ||
method: string; | ||
path: string; | ||
handler: string; | ||
} | ||
|
||
export default (): any => { | ||
// closures | ||
const _routes: RouterRouteType[] = []; | ||
|
||
const getRouterObj = (namespace: string): any => { | ||
const obj: any = { | ||
namespace(name: string, handler: (route: any) => {}): void { | ||
handler(getRouterObj(join(namespace, name))); | ||
}, | ||
}; | ||
|
||
METHODS.forEach((method: string): void => { | ||
const name = camelCase(method); | ||
obj[name] = (rawPath: string | string[], handler: string): void => { | ||
let path: any = rawPath; | ||
if (typeof rawPath === 'string') { | ||
path = [path]; | ||
} | ||
path = path.map((r: string): string => join('/', namespace, r)); | ||
_routes.push({ | ||
method, | ||
path, | ||
handler: join('/', handler), | ||
}); | ||
}; | ||
}); | ||
return obj; | ||
}; | ||
|
||
return { | ||
routes: _routes, | ||
route: getRouterObj(''), | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,24 @@ | ||
import { join } from 'path'; | ||
import { existsSync } from 'fs'; | ||
import glob from './glob'; | ||
|
||
export default (routesPath: string): string[] => { | ||
export default (routesPath: string): any[] => { | ||
const routesFilePath = join(routesPath, '../', 'routes.js'); | ||
if (existsSync(routesFilePath)) { | ||
const routerRoutes = require(routesFilePath) || []; // eslint-disable-line | ||
const mappedRoutes = routerRoutes.map((r: any): any => ({ | ||
...r, | ||
handler: join(routesPath, r.handler), | ||
})); | ||
return mappedRoutes; | ||
} | ||
const routes: string[] = []; | ||
|
||
const addRoutes = (path: any): number => { | ||
const files: string[] = glob(join('/', path), '**/*.{js,ts}'); | ||
return routes.push(...files); | ||
}; | ||
|
||
addRoutes(routesPath); | ||
|
||
return routes; | ||
return routes.map((s: string): any => ({ handler: s })); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters