fast-koa-router
Advanced tools
Comparing version 1.3.2 to 1.4.1
@@ -1,1 +0,1 @@ | ||
{"processes":{"9c4ebca2-a333-421f-b93c-ce8f2deec4f5":{"parent":null,"children":[]}},"files":{"/home/runner/work/fast-koa-router/fast-koa-router/src/middleware.ts":["9c4ebca2-a333-421f-b93c-ce8f2deec4f5"],"/home/runner/work/fast-koa-router/fast-koa-router/src/router.ts":["9c4ebca2-a333-421f-b93c-ce8f2deec4f5"],"/home/runner/work/fast-koa-router/fast-koa-router/src/parse.ts":["9c4ebca2-a333-421f-b93c-ce8f2deec4f5"]},"externalIds":{}} | ||
{"processes":{"38196594-c814-45fd-919a-849df333da40":{"parent":null,"children":[]}},"files":{"/home/runner/work/fast-koa-router/fast-koa-router/src/middleware.ts":["38196594-c814-45fd-919a-849df333da40"],"/home/runner/work/fast-koa-router/fast-koa-router/src/router.ts":["38196594-c814-45fd-919a-849df333da40"],"/home/runner/work/fast-koa-router/fast-koa-router/src/parse.ts":["38196594-c814-45fd-919a-849df333da40"]},"externalIds":{}} |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.router = void 0; | ||
const router_1 = require("./router"); | ||
@@ -10,3 +11,3 @@ const makeArray = args => (Array.isArray(args) ? args : [args]); | ||
}; | ||
exports.router = routes => { | ||
const router = routes => { | ||
const router = new router_1.Router(routes); | ||
@@ -21,2 +22,3 @@ async function fastKoaRouter(ctx, next) { | ||
}; | ||
exports.router = router; | ||
function getMiddleware(router, ctx) { | ||
@@ -23,0 +25,0 @@ const policy = router.getPolicy(ctx); |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.getPathMethod = exports.addPrefixMiddleware = exports.handlePathVariables = exports.parse = void 0; | ||
const methods = new Map(); | ||
@@ -131,3 +132,3 @@ methods.set('post', true); | ||
} | ||
if (routes['/_VAR_']) { | ||
if (routes['/_VAR_'] && currentPath) { | ||
const remaining = getPathMethodRecursively(routes[`/_VAR_`], rest); | ||
@@ -134,0 +135,0 @@ const params = {}; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.Router = void 0; | ||
const parse_1 = require("./parse"); | ||
class Router { | ||
constructor(routes) { | ||
this.routes = parse_1.handlePathVariables(parse_1.addPrefixMiddleware(parse_1.parse(routes), routes.prefix)); | ||
this.routes = (0, parse_1.handlePathVariables)((0, parse_1.addPrefixMiddleware)((0, parse_1.parse)(routes), routes.prefix)); | ||
} | ||
getMiddlewareAndSetState(ctx) { | ||
const { path, method } = ctx; | ||
const { middleware, params, _matchedRoute } = parse_1.getPathMethod(this.routes, path, method.toLowerCase()); | ||
const { middleware, params, _matchedRoute } = (0, parse_1.getPathMethod)(this.routes, path, method.toLowerCase()); | ||
ctx.params = params || {}; | ||
@@ -16,3 +17,3 @@ ctx._matchedRoute = _matchedRoute; | ||
getPolicy(ctx) { | ||
return parse_1.getPathMethod(this.routes, ctx.path, 'policy').middleware; | ||
return (0, parse_1.getPathMethod)(this.routes, ctx.path, 'policy').middleware; | ||
} | ||
@@ -19,0 +20,0 @@ } |
{ | ||
"name": "fast-koa-router", | ||
"version": "1.3.2", | ||
"version": "1.4.1", | ||
"description": "", | ||
@@ -14,3 +14,3 @@ "main": "build/middleware.js", | ||
"pretest": "tslint src/*.ts", | ||
"test": "nyc mocha" | ||
"test": "TS_NODE_PROJECT='./test/tsconfig.json' nyc mocha" | ||
}, | ||
@@ -38,14 +38,15 @@ "author": "Nikos Kostoulas <nikostoulas@gmail.com>", | ||
"devDependencies": { | ||
"@koa/router": "^8.0.8", | ||
"@types/mocha": "^5.2.6", | ||
"@types/node": "^11.13.8", | ||
"koa": "^2.11.0", | ||
"mocha": "^7.0.1", | ||
"nyc": "^15.0.0", | ||
"snap-shot-it": "^7.9.2", | ||
"source-map-support": "^0.5.16", | ||
"ts-node": "^8.6.2", | ||
"tslint": "^6.0.0", | ||
"typescript": "^3.8.2" | ||
"@koa/router": "^12.0.1", | ||
"@types/mocha": "^10.0.6", | ||
"@types/node": "^20.12.7", | ||
"koa": "^2.15.3", | ||
"mocha": "^10.4.0", | ||
"nyc": "^15.1.0", | ||
"prettier": "^3.2.5", | ||
"snap-shot-it": "^7.9.10", | ||
"source-map-support": "^0.5.21", | ||
"ts-node": "^10.9.2", | ||
"tslint": "^6.1.3", | ||
"typescript": "^5.4.5" | ||
} | ||
} |
import * as snapshotIt from 'snap-shot-it'; | ||
declare global { | ||
const snapshot: typeof snapshotIt; | ||
var snapshot: typeof snapshotIt; | ||
namespace NodeJS { | ||
@@ -11,2 +11,2 @@ interface Global { | ||
global.snapshot = snapshotIt; | ||
globalThis.snapshot = snapshotIt; |
import { router } from '../src/middleware'; | ||
import * as assert from 'assert'; | ||
describe('Middleware', function() { | ||
it('route should be called and state should be changed', async function() { | ||
describe('Middleware', function () { | ||
it('route should be called and state should be changed', async function () { | ||
const ctx = { path: '/foo/bar/3', method: 'GET' }; | ||
const r = { | ||
get: { | ||
'/foo/bar/3': async function(ctx, next) { | ||
'/foo/bar/3': async function (ctx, next) { | ||
ctx.body = await 'body'; | ||
@@ -17,3 +17,3 @@ await next(); | ||
const middleware = router(r); | ||
await middleware(ctx, function() { | ||
await middleware(ctx, function () { | ||
(ctx as any).next = true; | ||
@@ -25,3 +25,3 @@ }); | ||
it('routes and policy should be called but not next', async function() { | ||
it('routes and policy should be called but not next', async function () { | ||
const ctx = { path: '/foo/bar/3', method: 'GET' }; | ||
@@ -31,7 +31,7 @@ const r = { | ||
'/foo/:id/3': [ | ||
async function(ctx, next) { | ||
async function (ctx, next) { | ||
ctx.body = await 'body'; | ||
await next(); | ||
}, | ||
async function(ctx) { | ||
async function (ctx) { | ||
ctx.state = await 'state'; | ||
@@ -42,7 +42,7 @@ } | ||
policy: { | ||
'/foo/bar/3': async function(ctx, next) { | ||
'/foo/bar/3': async function (ctx, next) { | ||
(ctx as any).policy = true; | ||
await next(); | ||
}, | ||
'/foo/bar/:id': async function(ctx, next) {} | ||
'/foo/bar/:id': async function (ctx, next) {} | ||
} | ||
@@ -52,3 +52,3 @@ }; | ||
const middleware = router(r); | ||
await middleware(ctx, function() { | ||
await middleware(ctx, function () { | ||
(ctx as any).next = true; | ||
@@ -60,7 +60,7 @@ }); | ||
it('if nothing matches next is called', async function() { | ||
it('if nothing matches next is called', async function () { | ||
const ctx = { path: '/foo/bar/not-found', method: 'GET' }; | ||
const r = { | ||
get: { | ||
'/foo/:id/3': async function(ctx) { | ||
'/foo/:id/3': async function (ctx) { | ||
ctx.body = await 'body'; | ||
@@ -70,3 +70,3 @@ } | ||
policy: { | ||
'/foo/bar/3': async function(ctx, next) { | ||
'/foo/bar/3': async function (ctx, next) { | ||
(ctx as any).policy = true; | ||
@@ -79,3 +79,3 @@ await next(); | ||
const middleware = router(r); | ||
await middleware(ctx, function() { | ||
await middleware(ctx, function () { | ||
(ctx as any).next = true; | ||
@@ -87,3 +87,3 @@ }); | ||
it('routes and policy and prefix should be called but not next', async function() { | ||
it('routes and policy and prefix should be called but not next', async function () { | ||
const ctx = { path: '/foo/bar/3', method: 'GET' }; | ||
@@ -93,7 +93,7 @@ const r = { | ||
'/foo/:id/3': [ | ||
async function(ctx, next) { | ||
async function (ctx, next) { | ||
ctx.body = await 'body'; | ||
await next(); | ||
}, | ||
async function(ctx) { | ||
async function (ctx) { | ||
ctx.state = await 'state'; | ||
@@ -104,10 +104,10 @@ } | ||
policy: { | ||
'/foo/bar/3': async function(ctx, next) { | ||
'/foo/bar/3': async function (ctx, next) { | ||
(ctx as any).policy = true; | ||
await next(); | ||
}, | ||
'/foo/bar/:id': async function(ctx, next) {} | ||
'/foo/bar/:id': async function (ctx, next) {} | ||
}, | ||
prefix: { | ||
'/': async function(ctx, next) { | ||
'/': async function (ctx, next) { | ||
ctx.prefix = true; | ||
@@ -120,3 +120,3 @@ await next(); | ||
const middleware = router(r); | ||
await middleware(ctx, function() { | ||
await middleware(ctx, function () { | ||
(ctx as any).next = true; | ||
@@ -128,7 +128,7 @@ }); | ||
it('it also exports routes', async function() { | ||
it('it also exports routes', async function () { | ||
const ctx = { path: '/foo/bar/not-found', method: 'GET' }; | ||
const r = { | ||
get: { | ||
'/foo/:id/3': async function(ctx) { | ||
'/foo/:id/3': async function (ctx) { | ||
ctx.body = await 'body'; | ||
@@ -138,3 +138,3 @@ } | ||
policy: { | ||
'/foo/bar/3': async function(ctx, next) { | ||
'/foo/bar/3': async function (ctx, next) { | ||
(ctx as any).policy = true; | ||
@@ -151,3 +151,3 @@ await next(); | ||
it('it also exports matching', async function() { | ||
it('it also exports matching', async function () { | ||
const ctx = { path: '/foo/bar/not-found', method: 'GET' }; | ||
@@ -161,3 +161,3 @@ const r = { | ||
policy: { | ||
'/foo/bar/3': async function(ctx, next) { | ||
'/foo/bar/3': async function (ctx, next) { | ||
(ctx as any).policy = true; | ||
@@ -164,0 +164,0 @@ await next(); |
@@ -37,2 +37,9 @@ import * as assert from 'assert'; | ||
it('should return no routes for policy and path without params', function () { | ||
assert.deepStrictEqual( | ||
getPathMethod(handlePathVariables(parse({ post: { '/new_path/:id': [] } })), '/new_path', 'post'), | ||
{} | ||
); | ||
}); | ||
it('should return routes with many params that does not match with another route', function () { | ||
@@ -354,3 +361,3 @@ let routes = handlePathVariables( | ||
it('should fallback to star routes', function () { | ||
it('should fallback to star routes without ', function () { | ||
let routes = handlePathVariables( | ||
@@ -357,0 +364,0 @@ addPrefixMiddleware( |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
30
1221
97910
12