-
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
…n route match
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import { Param, ParamGetter } from '@/types/params' | ||
import { getParamValue } from '@/utilities' | ||
|
||
export function optional<T extends Param>(param: T): ParamGetter<T | undefined> { | ||
return (value: string | undefined) => { | ||
if (value === undefined) { | ||
return undefined | ||
} | ||
|
||
return getParamValue(value, param) | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,9 @@ | ||
import { Param } from '@/types/params' | ||
import { Path } from '@/utilities' | ||
|
||
export type RouteFlat = { | ||
name: string, | ||
path: string, | ||
path: string | Path, | ||
regex: RegExp, | ||
params: Record<string, Param[]>, | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,6 @@ | ||
export * from './createRouter' | ||
export * from './flattenRoutes' | ||
export * from './paramValidation' | ||
export * from './params' | ||
export * from './path' | ||
export * from './random' |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import { describe, expect, test } from 'vitest' | ||
import { RouteFlat } from '@/types' | ||
import { generateRouteRegexPattern, routeParamsAreValid } from '@/utilities' | ||
|
||
describe('routeParamsAreValid', () => { | ||
test('given route without params, always return true', () => { | ||
const path = '/no-params' | ||
const route: RouteFlat = { | ||
name: 'no-params', | ||
path: '/no-params', | ||
regex: generateRouteRegexPattern('/no-params'), | ||
params: {}, | ||
} | ||
|
||
const response = routeParamsAreValid(path, route) | ||
|
||
expect(response).toBe(true) | ||
}) | ||
|
||
test('given route with params, always return true', () => { | ||
const path = '/ABC' | ||
const route: RouteFlat = { | ||
name: 'simple-params', | ||
path: '/:simple', | ||
regex: generateRouteRegexPattern('/:simple'), | ||
params: { | ||
simple: [String], | ||
}, | ||
} | ||
|
||
const response = routeParamsAreValid(path, route) | ||
|
||
expect(response).toBe(true) | ||
}) | ||
}) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import { ExtractParamsFromPath, Route, RouteFlat, Routes } from '@/types' | ||
Check failure on line 1 in src/utilities/paramValidation.ts GitHub Actions / Type Validation
Check failure on line 1 in src/utilities/paramValidation.ts GitHub Actions / Type Validation
Check failure on line 1 in src/utilities/paramValidation.ts GitHub Actions / Type Validation
Check failure on line 1 in src/utilities/paramValidation.ts GitHub Actions / Type Tests
|
||
|
||
export function routeParamsAreValid(path: string, route: RouteFlat): boolean { | ||
Check failure on line 3 in src/utilities/paramValidation.ts GitHub Actions / Type Validation
Check failure on line 3 in src/utilities/paramValidation.ts GitHub Actions / Type Validation
Check failure on line 3 in src/utilities/paramValidation.ts GitHub Actions / Type Tests
|
||
// each param | ||
// get string value from string path (param) | ||
// run that through utilities.getParamValue (if non-simple param) | ||
// if it throws exception, consider not swallowing | ||
|
||
throw 'not implemented' | ||
} |