Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: respect the allParamsOptional config option in React query initi… #1583

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions packages/core/src/getters/params.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -107,9 +108,18 @@ export const getParams = ({
: `: ${paramType} = ${stringify(resolvedValue.originalSchema!.default)}` // FIXME: in Vue if we have `version: MaybeRef<number | undefined | null> = 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,
Expand Down
8 changes: 8 additions & 0 deletions packages/core/src/getters/props.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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}`,
Copy link
Collaborator

@anymaniax anymaniax Aug 22, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

would be better not modifying the core for that. You could do the same think directly in the react query client no?

implementation: `${body.implementation}${body.isOptional ? '?' : ''}: ${body.definition}`,
default: false,
required: !body.isOptional,
Expand All @@ -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
}`,
Expand All @@ -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
}`,
Expand Down Expand Up @@ -90,6 +97,7 @@ export const getProps = ({
type: GetterPropType.NAMED_PATH_PARAMS,
name,
definition: `${name}: ${parameterTypeName}`,
definedDefinition: `${name}: ${parameterTypeName}`,
implementation,
default: false,
destructured,
Expand Down
2 changes: 2 additions & 0 deletions packages/core/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -882,6 +882,7 @@ export type GetterParameters = {
export type GetterParam = {
name: string;
definition: string;
definedDefinition: string;
implementation: string;
default: boolean;
required: boolean;
Expand Down Expand Up @@ -914,6 +915,7 @@ export const GetterPropType = {
type GetterPropBase = {
name: string;
definition: string;
definedDefinition: string;
implementation: string;
default: boolean;
required: boolean;
Expand Down
20 changes: 3 additions & 17 deletions packages/query/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down