-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
150 additions
and
11 deletions.
There are no files selected for viewing
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,3 +1,3 @@ | ||
module.exports.get = (req, res) => { | ||
res.send({ userId: req.params.id, likes: ['a', 'b', 'c' ]}) | ||
res.send({ userId: req.params.id, likes: ['a', 'b', 'c' ] }) | ||
} |
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,14 @@ | ||
const Koa = require('koa') | ||
const soda = require('soda') | ||
const app = new Koa() | ||
|
||
async function startup() { | ||
const sodaRouter = await soda.withKoaRouter('./routes') | ||
app | ||
.use(sodaRouter.routes()) | ||
.use(sodaRouter.allowedMethods()) | ||
|
||
app.listen(5555) | ||
console.log('Server listening on :5555') | ||
} | ||
startup() |
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,17 @@ | ||
{ | ||
"name": "soda-example-koa", | ||
"version": "0.0.0", | ||
"description": "", | ||
"main": "index.js", | ||
"scripts": { | ||
"test": "echo \"Error: no test specified\" && exit 1" | ||
}, | ||
"author": "", | ||
"license": "MIT", | ||
"repository": "tmarshall/soda", | ||
"dependencies": { | ||
"@koa/router": "^12.0.0", | ||
"koa": "^2.13.4", | ||
"soda": "latest" | ||
} | ||
} |
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,6 @@ | ||
module.exports.get = (ctx, next) => { | ||
ctx.body = { | ||
success: true, | ||
page: 'root', | ||
} | ||
} |
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,5 @@ | ||
module.exports.get = (ctx, next) => { | ||
ctx.body = { | ||
userId: parseInt(ctx.params.id, 10), | ||
} | ||
} |
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,3 @@ | ||
module.exports.get = (ctx, next) => { | ||
ctx.body = { userId: ctx.params.id, likeId: ctx.params.likeId } | ||
} |
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,3 @@ | ||
module.exports.get = (ctx, next) => { | ||
ctx.body = { userId: ctx.params.id, likes: ['a', 'b', 'c' ] } | ||
} |
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,14 @@ | ||
const mockQuery = () => new Promise((resolve) => resolve([{ | ||
id: 1, | ||
name: 'Tim', | ||
}, { | ||
id: 2, | ||
name: 'Cosmo' | ||
}])) | ||
|
||
module.exports.get = async (ctx, next) => { | ||
const users = await mockQuery() | ||
ctx.body = { | ||
users, | ||
} | ||
} |
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,56 @@ | ||
import type { DefineRoute, RouteVerbKey } from './walkRoutes' | ||
|
||
import walkRoutes from './walkRoutes' | ||
import walkMiddleware from './walkMiddleware' | ||
|
||
interface RouteDefinition { | ||
verb: RouteVerbKey | ||
path: string | ||
func: Function | ||
} | ||
|
||
const defineRoute: DefineRoute<RouteDefinition> = ({ verb, routePath, func }) => { | ||
const pathParamCheck = new RegExp(`/\\[(?<paramName>[a-zA-Z_$][a-zA-Z0-9_$]*)\\](?=/|$)`, 'g') | ||
|
||
if (!pathParamCheck.test(routePath)) { | ||
return { | ||
verb, | ||
path: routePath, | ||
func, | ||
} | ||
} | ||
|
||
const parts = routePath.split(pathParamCheck) | ||
let resultExpressPath = '' | ||
|
||
for (let i = 0; i < parts.length; i++) { | ||
if (i % 2 === 0) { | ||
resultExpressPath += parts[i] | ||
continue | ||
} | ||
|
||
const paramName = parts[i] | ||
resultExpressPath += `/:${paramName}` | ||
} | ||
|
||
return { | ||
verb, | ||
path: resultExpressPath, | ||
func, | ||
} | ||
} | ||
|
||
export default async function withKoaRouter(routesDirpath?: string, middlewareDirpath?: string) { | ||
const Router = require('@koa/router') | ||
|
||
const middleware = await walkMiddleware(middlewareDirpath) | ||
const routes = await walkRoutes<RouteDefinition>(routesDirpath, middleware, defineRoute) | ||
|
||
const koaRouter = new Router() | ||
|
||
for (let routeDef of routes) { | ||
koaRouter[routeDef.verb](routeDef.path, routeDef.func) | ||
} | ||
|
||
return koaRouter | ||
} |