-
Notifications
You must be signed in to change notification settings - Fork 8
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
1 parent
b9c9282
commit f899954
Showing
8 changed files
with
106 additions
and
3 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
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,42 @@ | ||
import { match } from '../match' | ||
import { SKIP_SYMBOL } from '../run' | ||
|
||
const CONTEXT = { | ||
request: { | ||
path: '/users/1111', | ||
}, | ||
} | ||
|
||
describe('Match function test', () => { | ||
it('should return skip symbol if not matched', () => { | ||
const payloadGetter = match( | ||
['/user/me'], | ||
(ctx: typeof CONTEXT) => ctx.request.path, | ||
)(() => { | ||
return 1 | ||
}) | ||
// @ts-expect-error | ||
expect(payloadGetter(CONTEXT)).toBe(SKIP_SYMBOL) | ||
}) | ||
|
||
it('should return skip symbol if request factory return falsy value', () => { | ||
const payloadGetter = match( | ||
['/user/me'], | ||
(_ctx: typeof CONTEXT) => '', | ||
)(() => { | ||
return 1 | ||
}) | ||
// @ts-expect-error | ||
expect(payloadGetter(CONTEXT)).toBe(SKIP_SYMBOL) | ||
}) | ||
|
||
it('should into matched router', () => { | ||
const payloadGetter = match( | ||
['/users/:id'], | ||
(ctx: typeof CONTEXT) => ctx.request.path, | ||
)(() => { | ||
return 1 | ||
}) | ||
expect(payloadGetter(CONTEXT, SKIP_SYMBOL)).toBe(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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
export { runSSREffects as emitSSREffects, ModuleMeta } from './run' | ||
export { match } from './match' |
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,15 @@ | ||
import { match as matchPath } from 'path-to-regexp' | ||
|
||
import { SKIP_SYMBOL } from './run' | ||
|
||
export function match<Ctx>(routers: string[], pathFactory: (ctx: Ctx) => string) { | ||
return <T>(payloadGetter: (ctx: Ctx, skip: symbol) => T | Promise<T>) => { | ||
return function payloadGetterWithMatch(ctx: Ctx, skip: symbol) { | ||
const requestPath = pathFactory(ctx) | ||
if (requestPath && routers.some((router) => matchPath(router)(requestPath))) { | ||
return payloadGetter(ctx, skip) | ||
} | ||
return SKIP_SYMBOL | ||
} | ||
} | ||
} |
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