From f3c8b4c13e29c0139df50942acf854d2dbe6e23b Mon Sep 17 00:00:00 2001 From: Evan Sutherland Date: Sun, 17 Dec 2023 18:43:05 -0600 Subject: [PATCH] updated types on params utility --- src/types/routeMethods.ts | 2 +- src/utilities/params.ts | 11 ++++++----- 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/src/types/routeMethods.ts b/src/types/routeMethods.ts index a571982b..e70c80f2 100644 --- a/src/types/routeMethods.ts +++ b/src/types/routeMethods.ts @@ -124,7 +124,7 @@ type ExtractPathParamType< ? ExtractParamType : string -type ExtractParamType = TParam extends ParamGetSet +export type ExtractParamType = TParam extends ParamGetSet ? Type : TParam extends ParamGetter ? ReturnType diff --git a/src/utilities/params.ts b/src/utilities/params.ts index 599b2b94..e1a41156 100644 --- a/src/utilities/params.ts +++ b/src/utilities/params.ts @@ -1,4 +1,4 @@ -import { Param, ParamGetSet, ParamExtras, isParamGetSet, isParamGetter } from '@/types' +import { Param, ParamGetSet, ParamExtras, isParamGetSet, isParamGetter, ExtractParamType } from '@/types' import { InvalidRouteParamValueError } from '@/types/invalidRouteParamValueError' const extras: ParamExtras = { @@ -7,7 +7,7 @@ const extras: ParamExtras = { }, } -const booleanParam: ParamGetSet = { +const booleanParam: ParamGetSet = { get: (value, { invalid }) => { if (value === 'true') { return true @@ -28,7 +28,7 @@ const booleanParam: ParamGetSet = { }, } -const numberParam: ParamGetSet = { +const numberParam: ParamGetSet = { get: (value, { invalid }) => { // Number('') === 0 if (value.length === 0) { @@ -45,14 +45,15 @@ const numberParam: ParamGetSet = { }, set: (value, { invalid }) => { if (typeof value !== 'number') { - invalid() + throw invalid() } return value.toString() }, } -export function getParamValue(value: string, param: Param): unknown { +export function getParamValue(value: string, param: T): ExtractParamType +export function getParamValue(value: string, param: T): unknown { if (param === Boolean) { return booleanParam.get(value, extras) }