From df946beff50b8190c67d1af1f91a79d505cad44a Mon Sep 17 00:00:00 2001 From: lnkarma Date: Sat, 17 Aug 2024 20:14:41 +0000 Subject: [PATCH] fix: respect the allParamsOptional config option in React query initialData overloads --- packages/core/src/getters/params.ts | 10 ++++++++++ packages/core/src/getters/props.ts | 8 ++++++++ packages/core/src/types.ts | 2 ++ packages/query/src/index.ts | 20 +++----------------- 4 files changed, 23 insertions(+), 17 deletions(-) diff --git a/packages/core/src/getters/params.ts b/packages/core/src/getters/params.ts index b04fb08fe..99249b2fe 100644 --- a/packages/core/src/getters/params.ts +++ b/packages/core/src/getters/params.ts @@ -71,6 +71,7 @@ export const getParams = ({ return { name, definition: `${name}${!required ? '?' : ''}: unknown`, + definedDefinition: `${name}:${!required ? 'undefined' : ''} | unknown`, implementation: `${name}${!required ? '?' : ''}: unknown`, default: false, required, @@ -107,9 +108,18 @@ export const getParams = ({ : `: ${paramType} = ${stringify(resolvedValue.originalSchema!.default)}` // FIXME: in Vue if we have `version: MaybeRef = 1` and we don't pass version, the unref(version) will be `undefined` and not `1`, so we need to handle default value somewhere in implementation and not in the definition }`; + const definedDefinition = `${name}: ${ + !required && + !resolvedValue.originalSchema!.default && + !output.allParamsOptional + ? 'undefined | ' + : '' + }${paramType}`; + return { name, definition, + definedDefinition, implementation, default: resolvedValue.originalSchema!.default, required, diff --git a/packages/core/src/getters/props.ts b/packages/core/src/getters/props.ts index 6060952e1..bcf73b70f 100644 --- a/packages/core/src/getters/props.ts +++ b/packages/core/src/getters/props.ts @@ -26,6 +26,7 @@ export const getProps = ({ const bodyProp = { name: body.implementation, definition: `${body.implementation}${body.isOptional ? '?' : ''}: ${body.definition}`, + definedDefinition: `${body.implementation}: ${body.isOptional ? 'undefined' : ''} | ${body.definition}`, implementation: `${body.implementation}${body.isOptional ? '?' : ''}: ${body.definition}`, default: false, required: !body.isOptional, @@ -37,6 +38,9 @@ export const getProps = ({ definition: `params${queryParams?.isOptional ? '?' : ''}: ${ queryParams?.schema.name }`, + definedDefinition: `params: ${queryParams?.isOptional ? 'undefined' : ''} | ${ + queryParams?.schema.name + }`, implementation: `params${queryParams?.isOptional ? '?' : ''}: ${ queryParams?.schema.name }`, @@ -52,6 +56,9 @@ export const getProps = ({ definition: `headers${headers?.isOptional ? '?' : ''}: ${ headers?.schema.name }`, + definedDefinition: `headers: ${headers?.isOptional ? 'undefined' : ''} | ${ + headers?.schema.name + }`, implementation: `headers${headers?.isOptional ? '?' : ''}: ${ headers?.schema.name }`, @@ -90,6 +97,7 @@ export const getProps = ({ type: GetterPropType.NAMED_PATH_PARAMS, name, definition: `${name}: ${parameterTypeName}`, + definedDefinition: `${name}: ${parameterTypeName}`, implementation, default: false, destructured, diff --git a/packages/core/src/types.ts b/packages/core/src/types.ts index 8f2c3d464..11c2ebe25 100644 --- a/packages/core/src/types.ts +++ b/packages/core/src/types.ts @@ -882,6 +882,7 @@ export type GetterParameters = { export type GetterParam = { name: string; definition: string; + definedDefinition: string; implementation: string; default: boolean; required: boolean; @@ -914,6 +915,7 @@ export const GetterPropType = { type GetterPropBase = { name: string; definition: string; + definedDefinition: string; implementation: string; default: boolean; required: boolean; diff --git a/packages/query/src/index.ts b/packages/query/src/index.ts index 2b8f37829..c9ce3fb12 100644 --- a/packages/query/src/index.ts +++ b/packages/query/src/index.ts @@ -724,24 +724,10 @@ const generateQueryImplementation = ({ }) => { const queryPropDefinitions = toObjectString(props, 'definition'); const definedInitialDataQueryPropsDefinitions = toObjectString( - props.map((prop) => { - const regex = new RegExp(`^${prop.name}\\s*\\?:`); - - if (!regex.test(prop.definition)) { - return prop; - } - - const definitionWithUndefined = prop.definition.replace( - regex, - `${prop.name}: undefined | `, - ); - return { - ...prop, - definition: definitionWithUndefined, - }; - }), - 'definition', + props, + 'definedDefinition', ); + const queryProps = toObjectString(props, 'implementation'); const hasInfiniteQueryParam = queryParam && queryParams?.schema.name;