-
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.
add rewrite rule test, fix startup problem
- Loading branch information
Showing
5 changed files
with
95 additions
and
25 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 |
---|---|---|
|
@@ -11,4 +11,5 @@ export { extname } from 'https://deno.land/[email protected]/path/mod.ts'; | |
export { brightGreen } from 'https://deno.land/[email protected]/fmt/colors.ts'; | ||
export type { Evt } from 'https://deno.land/x/evt/mod.ts'; | ||
import sample from 'https://dev.jspm.io/lodash-es/sample'; | ||
export { sample }; | ||
import isEqual from 'https://dev.jspm.io/lodash-es/isEqual'; | ||
export { sample, isEqual }; |
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,32 @@ | ||
import { JsonDB } from './server.ts'; | ||
|
||
export const findPathInDb = ( | ||
db: JsonDB, | ||
routePaths: string[], | ||
rewriteRules?: Record<string, string[]> | ||
) => { | ||
const matchedRewriteKey = | ||
rewriteRules && | ||
Object.keys(rewriteRules).find( | ||
(rewriteKey) => rewriteKey === routePaths.join('/') | ||
); | ||
const appliedRoutePaths = matchedRewriteKey | ||
? rewriteRules![matchedRewriteKey] | ||
: routePaths; | ||
return appliedRoutePaths.reduce((subDB, routePart) => { | ||
if (routePart == null || routePart === '') { | ||
return subDB; | ||
} | ||
const id = Number(routePart); | ||
if (Array.isArray(subDB) && id !== NaN) { | ||
return (subDB as { id: number }[]).find( | ||
(item) => item.id === id | ||
) as Record<string, unknown>; | ||
} | ||
if (routePart && routePart in subDB) { | ||
return subDB[routePart] as Record<string, unknown>; | ||
} | ||
console.error(`${routePart} not found in ${JSON.stringify(subDB)}!`); | ||
return subDB; | ||
}, db); | ||
}; |
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,39 @@ | ||
import { assertEquals } from 'https://deno.land/[email protected]/testing/asserts.ts'; | ||
import { findPathInDb } from './findPathInDb.ts'; | ||
|
||
const { test } = Deno; | ||
|
||
test({ | ||
name: 'match simple path', | ||
fn: () => { | ||
const db = { test: { levelTwo: '1' } }; | ||
|
||
const matched = findPathInDb(db, ['test', 'levelTwo']); | ||
|
||
assertEquals(matched, '1'); | ||
}, | ||
}); | ||
|
||
test({ | ||
name: 'match ids', | ||
fn: () => { | ||
const db = { test: { id: 3, levelTwo: '1' } }; | ||
|
||
const matched = findPathInDb(db, ['test', '3']); | ||
|
||
assertEquals(matched, { id: 3, levelTwo: '1' }); | ||
}, | ||
}); | ||
|
||
test({ | ||
name: 'match simple rewritten path', | ||
fn: () => { | ||
const db = { test: { levelTwo: '1' } }; | ||
|
||
const matched = findPathInDb(db, ['yolo'], { | ||
yolo: ['test', 'levelTwo'], | ||
}); | ||
|
||
assertEquals(matched, '1'); | ||
}, | ||
}); |
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