From: Eduardo San Martin Morote Date: Tue, 14 Jun 2022 09:37:44 +0000 (+0200) Subject: refactor(types): remove old types X-Git-Tag: v4.1.0~23 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=541146da14aaedffc4117edbea0a638704e90516;p=thirdparty%2Fvuejs%2Frouter.git refactor(types): remove old types --- diff --git a/packages/router/src/types/paths.ts b/packages/router/src/types/paths.ts index 25fe6cb0..7b97120b 100644 --- a/packages/router/src/types/paths.ts +++ b/packages/router/src/types/paths.ts @@ -1,4 +1,4 @@ -import { RouteParams, RouteParamsRaw, RouteParamValueRaw } from '.' +import { RouteParamValueRaw } from '.' /** * Extract an object of params given a path like `/users/:id`. @@ -71,20 +71,16 @@ export type _ExtractParamsPath< _ExtractParamsPath : {} -type _PathParam

= - | `${string}:${P}` - | `${string}:${P}${_ParamModifier}${Rest}` - -type b = '/' extends _PathParam ? P : never -type c = '/home' extends _PathParam ? P : never -type d = '/user/:id' extends _PathParam ? [P, Rest] : never -type e = '/user/:id+' extends _PathParam ? P : never - +/** + * Given a path, extracts the possible params or {} when there are no params. + * + * @internal + */ export type _ExtractParamsOfPath< P extends string, isRaw extends boolean > = P extends `${string}:${infer HasParam}` - ? _ParamName extends _ParamExtractResult< + ? _ExtractParamName extends _ParamExtractResult< infer ParamName, infer Rest > @@ -123,61 +119,79 @@ type test1 = ? [P, Rest] : never -type _ParamName_OLD

= - P extends `${_AlphaNumeric}${infer Rest}` - ? P extends `${infer C}${Rest}` - ? // Keep extracting other alphanumeric chars - `${C}${_ParamName_OLD}` - : never // ERR - : // add the rest to the end after a % which is invalid in a path so it can be used as a delimiter - ` % ${P}` - +/** + * Helper type to infer a param name extraction result + * @internal + */ interface _ParamExtractResult

{ param: P rest: Rest } -type _ParamName< +/** + * Extracts the param name from a path. Requires to strip off the starting `:` + * + * @example + * ```ts + * _ExtractParamName<'id/:b'> // 'id' + * ``` + * + * @internal + */ +type _ExtractParamName< Tail extends string, Head extends string = '' > = Tail extends `${_AlphaNumeric}${infer Rest}` ? Tail extends `${infer C}${Rest}` ? // Keep extracting other alphanumeric chars - _ParamName + _ExtractParamName : never // ERR : // add the rest to the end after a % which is invalid in a path so it can be used as a delimiter _ParamExtractResult -type p1 = _ParamName<'id'> -type p2 = _ParamName<'abc+/dos'> -type p3 = _ParamName<'abc/:dos)'> +type p1 = _ExtractParamName<'id'> +type p2 = _ExtractParamName<'abc+/dos'> +type p3 = _ExtractParamName<'abc/:dos)'> /** * We consider a what comes after a param, e.g. For `/:id(\\d+)+/edit`, it would be `(\\d+)+/edit`. This should output - * everything after the regex while handling escaped `)`: `+/edit`. + * everything after the regex while handling escaped `)`: `+/edit`. Note this type should be used with a string that + * starts with `(` as it will remove everything until the closing parenthesis `)`. + * + * @internal */ export type _StripRegex = // do we have an escaped closing parenthesis? - S extends `${infer A}\\)${infer Rest}` + S extends `(${infer A}\\)${infer Rest}` ? // the actual regexp finished before, A has no escaped ) A extends `${string})${infer Rest2}` - ? // get the modifier if there is one + ? // Rebuild the rest `${Rest2}\\)${Rest}` // job done - : _RemoveUntilClosingPar // we keep removing + : // NOTE: expensive type when there are multiple custom regex. It's a good idea to avoid multiple custom regexs in paths. Only use custom regexs when necessary or cast the string type: `path: /:id(...)/rest` as '/:id/rest' + _RemoveUntilClosingPar // we keep removing : // simple case with no escaping - S extends `${string})${infer Rest}` + S extends `(${string})${infer Rest}` ? // extract the modifier if there is one Rest : // nothing to remove S +const a = '/:id(\\d+)+/edit/:more(.*)' as '/:id+/edit/:more' + type r1 = _StripRegex<'(\\d+)+/edit/:other(.*)*'> type r3 = _StripRegex<'(.*)*'> type r4 = _StripRegex<'?/rest'> type r5 = _StripRegex<'*'> type r6 = _StripRegex<'-other-stuff'> type r7 = _StripRegex<'/edit'> +type r8 = _StripRegex<'?/rest/:other(.*)'> +type r9 = _StripRegex<'?/rest/:other(.*)/more/:b(.*)'> +/** + * Helper type to infer a modifier extraction result. + * + * @internal + */ export interface _ModifierExtracTResult< M extends _ParamModifier | '', Rest extends string @@ -186,6 +200,12 @@ export interface _ModifierExtracTResult< rest: Rest } +/** + * Extracts the modifier and the rest of the path. This is meant to be used with everything after the param name, e.g., + * given a path of `/:paths(.+)+/end`, it should be given `+/end` and will split the path into `+` and `/end`. + * + * @internal + */ export type _ExtractModifier

= P extends `${_ParamModifier}${infer Rest}` ? P extends `${infer M}${Rest}` @@ -204,31 +224,6 @@ type m3 = _ExtractModifier<'edit'> type m4 = _ExtractModifier<'+'> type m5 = _ExtractModifier<'+/edit'> -export type _StripModifierAndRegex_OLD = - // do we have an escaped closing parenthesis? - S extends `${infer A}\\)${infer Rest}` - ? // the actual regexp finished before, A has no escaped ) - A extends `${string})${infer Rest2}` - ? // get the modifier if there is one - Rest2 extends `${_ParamModifier}${infer Rest3}` - ? Rest2 extends `${infer M}${Rest3}` - ? { mod: M; rest: `${Rest3}\\)${Rest}` } - : never - : // No modifier - { mod: ''; rest: `${Rest2}\\)${Rest}` } // job done - : _RemoveUntilClosingPar // we keep removing - : // simple case with no escaping - S extends `${string})${infer Rest}` - ? // extract the modifier if there is one - Rest extends `${_ParamModifier}${infer Rest2}` - ? Rest extends `${infer M}${Rest2}` - ? { mod: M; rest: Rest2 } - : never - : // no modifier - { mod: ''; rest: Rest } - : // nothing to remove - { mod: ''; rest: S } - /** * Gets the possible type of a param based on its modifier M. * @@ -373,119 +368,6 @@ export type _ExtractFirstParamName = ? never : S -/** - * Join an array of param values for repeated params - * - * @internal - */ -type _JoinParams = V extends - | null - | undefined - ? '' - : V extends readonly [infer A, ...infer Rest] - ? A extends string - ? `${A}${Rest extends readonly [any, ...any[]] - ? `/${_JoinParams}` - : ''}` - : never - : '' - -/** - * Transform a param value to a string. - * - * @internal - */ -export type _ParamToString = V extends null | undefined | readonly string[] - ? _JoinParams - : V extends null | undefined | readonly never[] | readonly [] - ? '' - : V extends string - ? V - : never - -/** - * Possible values for a Modifier. - * - * @internal - */ -type _PossibleModifierValue = - | string - | readonly string[] - | null - | undefined - | readonly never[] - -/** - * Recursively builds a path from a param based path with curly braces (e.g. `\{id\}`). - * - * @internal - */ -export type _BuildPath< - P extends string, - PO extends ParamsFromPath -> = P extends `${infer A}{${infer PP}}${infer Rest}` - ? PP extends `${infer N}${_ParamModifier}` - ? PO extends Record - ? PO[N] extends readonly [] | readonly never[] | null | undefined - ? `${A}${Rest extends `/${infer Rest2}` ? _BuildPath : ''}` - : `${A}${_ParamToString}${_BuildPath}` - : `${A}${Rest extends `/${infer Rest2}` ? _BuildPath : ''}` - : `${A}${PO extends Record - ? _ParamToString - : ''}${_BuildPath}` - : P - -/** - * Builds a path string type from a path definition and an object of params. - * - * @example - * ```ts - * type url = PathFromParams<'/users/:id', { id: 'posva' }> -> '/users/posva' - * ``` - */ -export type PathFromParams< - P extends string, - PO extends ParamsFromPath

= ParamsFromPath

-> = string extends P ? string : _BuildPath<_RemoveRegexpFromParam

, PO> - -/** - * A param in a url like `/users/:id`. - */ -export interface PathParserParamKey< - N extends string = string, - M extends _ParamModifier | '' = _ParamModifier | '' -> { - name: N - repeatable: M extends '+' | '*' ? true : false - optional: M extends '?' | '*' ? true : false -} - -/** - * Extracts the params of a path. - * - * @internal - */ -export type _ExtractPathParamKeys

= - P extends `${string}{${infer PP}}${infer Rest}` - ? [ - PP extends `${infer N}${_ParamModifier}` - ? PP extends `${N}${infer M}` - ? M extends _ParamModifier - ? PathParserParamKey - : never - : never - : PathParserParamKey, - ..._ExtractPathParamKeys - ] - : [] - -/** - * Extract the param keys (name and modifiers) tuple of a path. - */ -export type ParamKeysFromPath

= string extends P - ? readonly PathParserParamKey[] // Generic version - : _ExtractPathParamKeys<_RemoveRegexpFromParam

> - /** * Joins a prefix and a path putting a `/` between them when necessary *