From 9e9ee7861bebfe68990171799537d3664ac1a66e Mon Sep 17 00:00:00 2001 From: Ali Mihandoost Date: Sun, 29 Jan 2023 16:36:05 +0330 Subject: [PATCH] fix(route): RouteContextBase type --- core/router/src/core.ts | 68 ++++++++++++++++++++--------------------- core/router/src/type.ts | 25 ++++++++++++--- 2 files changed, 55 insertions(+), 38 deletions(-) diff --git a/core/router/src/core.ts b/core/router/src/core.ts index 7e1a30a53..d5b9fbd75 100644 --- a/core/router/src/core.ts +++ b/core/router/src/core.ts @@ -2,7 +2,7 @@ import {createLogger, globalAlwatr} from '@alwatr/logger'; import {isNumber} from '@alwatr/math'; import {contextConsumer} from '@alwatr/signal'; -import {RouteContext, RoutesConfig} from './type.js'; +import {RouteContext, RouteContextBase, RoutesConfig} from './type.js'; import type {ParamValueType, QueryParameters} from '@alwatr/type'; @@ -15,6 +15,8 @@ export const logger = createLogger('alwatr/router'); export const routeContextConsumer = contextConsumer.bind('route-context'); +const documentBaseUrl = document.querySelector('base')?.href || '/'; + /** * The result of calling the current route's render() callback base on routesConfig. * @@ -78,35 +80,6 @@ export const routerOutlet = (routesConfig: RoutesConfig): unknown => { } }; -// ---- - -/** - * Sanitize string value to valid parameters types. - */ -export function sanitizeValue(value?: string | null): ParamValueType { - if (value == null) { - return null; - } - // else - value = value.trim(); - if (value === '') { - return value; - } - // else - const lowerValue = value.toLocaleLowerCase(); - if (lowerValue === 'true' || lowerValue === 'false') { - return lowerValue === 'true'; - } - // else - if (isNumber(value)) { - return +value; - } - // else - return value; -} - -const documentBaseUrl = document.querySelector('base')?.href || '/'; - /** * Make anchor valid href from RouteContext format. * @@ -116,7 +89,7 @@ const documentBaseUrl = document.querySelector('base')?.href || '/'; * * ``` */ -export function url(route: Partial>): string { +export const url = (route: Partial): string => { logger.logMethodArgs('url', {route}); let href = ''; @@ -135,16 +108,43 @@ export function url(route: Partial { + if (value == null) { + return null; + } + // else + value = value.trim(); + if (value === '') { + return value; + } + // else + const lowerValue = value.toLocaleLowerCase(); + if (lowerValue === 'true' || lowerValue === 'false') { + return lowerValue === 'true'; + } + // else + if (isNumber(value)) { + return +value; + } + // else + return value; +}; /** * Make query string from QueryParameters object. */ -export function toQueryParamString(parameterList?: QueryParameters): string { +export const toQueryParamString = (parameterList?: QueryParameters): string => { if (parameterList == null) return ''; const list: Array = []; for (const key of Object.keys(parameterList)) { list.push(`${key}=${String(parameterList[key])}`); } return '?' + list.join('&'); -} +}; diff --git a/core/router/src/type.ts b/core/router/src/type.ts index 6ffbbe9cf..0b15fb9b0 100644 --- a/core/router/src/type.ts +++ b/core/router/src/type.ts @@ -1,5 +1,25 @@ import {QueryParameters} from '@alwatr/type'; +/** + * Route context base type. + * + * Sample: + * + * ```js + * // http://example.com:8080/product/100/book?cart=1&color=white#description + * { + * sectionList: [product, 100, book], + * queryParamList: {cart: 1, color: 'white'}, + * hash: '#description', + * } + * ``` + */ +export type RouteContextBase = { + sectionList: Array; + queryParamList: QueryParameters; + hash: string; +} + /** * Global route context type. * @@ -19,16 +39,13 @@ import {QueryParameters} from '@alwatr/type'; * } * ``` */ -export type RouteContext = { +export type RouteContext = RouteContextBase & { href: string; pathname: string; hostname: string; port: string; origin: string; protocol: 'http' | 'https'; - sectionList: Array; - queryParamList: QueryParameters; - hash: string; } // @TODO: description