Skip to content

feat(DataSource): add the transformError data-source function #24

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

Merged
merged 1 commit into from
Mar 25, 2025

Conversation

ArturAbdullin
Copy link
Contributor

When the transformError is set, the fetch errors will be transformed into data without changing the state to error.

@@ -6,11 +6,14 @@ export type DataSourceTag = string;
declare const errorHintSymbol: unique symbol;
declare const stateHintSymbol: unique symbol;

export type NonNullableUnknown = NonNullable<unknown>;
Copy link
Collaborator

Choose a reason for hiding this comment

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

suggestion(blocking): Let's move it to utils.ts

* When set, the `fetch` errors will be transformed into data without changing the state to error.
* @returns NonNullable
*/
transformError?: (reason: unknown) => TResponseErrorTransform;
Copy link
Collaborator

Choose a reason for hiding this comment

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

suggestion(non-blocking): reason -> error

export interface DataSource<
TContext,
TParams,
TRequest,
TResponse,
TResponseErrorTransform extends NonNullableUnknown,
Copy link
Collaborator

Choose a reason for hiding this comment

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

issue(blocking): Move it after TError and add default like TResponseErrorTransform extends NonNullableUnknown = NonNullableUnknown to avoid breaking changes

Copy link
Contributor Author

@ArturAbdullin ArturAbdullin Mar 6, 2025

Choose a reason for hiding this comment

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

Required type parameters may not follow optional type parameters, so I will move it to the end.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I resolved an undefined and null return values of the transformError, so the TErrorResponse does not extend NonNullableUnknown and can be anything at this point.

export interface DataSource<
TContext,
TParams,
TRequest,
TResponse,
TResponseErrorTransform extends NonNullableUnknown,
Copy link
Collaborator

Choose a reason for hiding this comment

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

suggestion(non-blocking): TResponseErrorTransform -> TErrorResponse

* When set, the `fetch` errors will be transformed into data without changing the state to error.
* @returns NonNullable
*/
transformError?: (reason: unknown) => TResponseErrorTransform;
Copy link
Collaborator

Choose a reason for hiding this comment

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

question(blocking): Can we use TError here instead of unknown?

infer TData,
infer _TError,
infer _TOptions,
infer _TState,
infer _TFetchContext
>
? ActualData<TData, TResponse>
? ActualData<TResponse, TResponseErrorTransform, TData>
: never;

export type DataSourceError<TDataSource> =
Copy link
Collaborator

Choose a reason for hiding this comment

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

suggestion(blocking): Add DataSourceErrorResponse for inferring like DataSourceError

unknown extends TResponseErrorTransform ? TResponse : TResponse | TResponseErrorTransform;

export type ActualData<TResponse, TResponseErrorTransform, TData> = unknown extends TData
? unknown extends TResponseErrorTransform
Copy link
Collaborator

Choose a reason for hiding this comment

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

suggestion(blocking): Looks like you can use ActualResponse here

QueryDataSourceContext,
TParams,
TRequest,
TResponse,
TResponseErrorTransform,
TData,
TError,
InfiniteQueryObserverExtendedOptions<
TResponse,
Copy link
Collaborator

Choose a reason for hiding this comment

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

issue(blocking): You have to wrap TResponse using ActualResponse in the TOptions and TState types

TError,
TErrorResponse,
QueryObserverExtendedOptions<
TResponse,
Copy link
Collaborator

Choose a reason for hiding this comment

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

issue(blocking): Wrap it using ActualResponse

TError,
TErrorResponse,
InfiniteQueryObserverExtendedOptions<
TResponse,
Copy link
Collaborator

Choose a reason for hiding this comment

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

issue(blocking): Wrap it using ActualResponse

} catch (error) {
if (!transformError) throw error;

return formatNullableValue(transformError(error));
Copy link
Collaborator

Choose a reason for hiding this comment

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

suggestion(blocking): Use formatNullableValue for fetch result too

? (data) => ({...data, pages: data.pages.map(transformResponse)})
: undefined,
select:
transformResponse || transformError
Copy link
Collaborator

Choose a reason for hiding this comment

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

suggestion(blocking): Use innertTransform without condition

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Oh, missed it =\ Thanks!

DakEnviy
DakEnviy previously approved these changes Mar 11, 2025
Copy link
Collaborator

@DakEnviy DakEnviy left a comment

Choose a reason for hiding this comment

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

❤️

refactor: review issues

feat: resolve an undefined and null error response values

docs(skipContext): add the JSDoc

fix(react-query/impl): review issues
@ArturAbdullin ArturAbdullin merged commit 16c1775 into main Mar 25, 2025
3 checks passed
@ArturAbdullin ArturAbdullin deleted the transform-error branch March 25, 2025 15:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants