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

[TypeScript] Fix some strict null checks errors in core #9644

Merged
merged 2 commits into from
Feb 6, 2024
Merged
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
35 changes: 28 additions & 7 deletions packages/ra-core/src/auth/useGetIdentity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,8 @@ import { useQuery, UseQueryOptions, QueryObserverResult } from 'react-query';
import useAuthProvider from './useAuthProvider';
import { UserIdentity } from '../types';

const defaultIdentity = {
const defaultIdentity: UserIdentity = {
id: '',
fullName: null,
};
const defaultQueryParams = {
staleTime: 5 * 60 * 1000,
Expand Down Expand Up @@ -48,9 +47,18 @@ export const useGetIdentity = (

const result = useQuery(
['auth', 'getIdentity'],
authProvider
? () => authProvider.getIdentity()
: async () => defaultIdentity,
async () => {
if (
authProvider &&
typeof authProvider.getIdentity === 'function'
) {
const identity = await authProvider.getIdentity();
return identity || defaultIdentity;
} else {
return defaultIdentity;
}
},

{
enabled: typeof authProvider?.getIdentity === 'function',
...queryParams,
Expand All @@ -64,32 +72,45 @@ export const useGetIdentity = (
? { isLoading: true }
: result.error
? { error: result.error, isLoading: false }
: {
: result.isSuccess
? {
data: result.data,
identity: result.data,
refetch: result.refetch,
isLoading: false,
},
}
: { isLoading: false },

[result]
);
};

export type UseGetIdentityResult =
// idle result
| {
isLoading: false;
data?: undefined;
identity?: undefined;
error?: undefined;
refetch?: undefined;
}
// loading result
| {
isLoading: true;
data?: undefined;
identity?: undefined;
error?: undefined;
refetch?: undefined;
}
// error result
| {
isLoading: false;
data?: undefined;
identity?: undefined;
error: Error;
refetch?: undefined;
}
// success result
| {
isLoading: false;
data: UserIdentity;
Expand Down
9 changes: 6 additions & 3 deletions packages/ra-core/src/auth/useHandleAuthCallback.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { useQuery, UseQueryOptions } from 'react-query';
import { useLocation } from 'react-router';
import { useRedirect } from '../routing';
import { AuthProvider, AuthRedirectResult } from '../types';
import { AuthRedirectResult } from '../types';
import useAuthProvider from './useAuthProvider';

/**
Expand All @@ -12,7 +12,7 @@ import useAuthProvider from './useAuthProvider';
* @returns An object containing { isLoading, data, error, refetch }.
*/
export const useHandleAuthCallback = (
options?: UseQueryOptions<ReturnType<AuthProvider['handleCallback']>>
options?: UseQueryOptions<AuthRedirectResult | void | any>
) => {
const authProvider = useAuthProvider();
const redirect = useRedirect();
Expand All @@ -24,7 +24,10 @@ export const useHandleAuthCallback = (

return useQuery(
['auth', 'handleCallback'],
() => authProvider.handleCallback(),
() =>
authProvider && typeof authProvider.handleCallback === 'function'
? authProvider.handleCallback()
: Promise.resolve(),
{
retry: false,
onSuccess: data => {
Expand Down
5 changes: 3 additions & 2 deletions packages/ra-core/src/core/useResourceDefinitions.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { ResourceDefinitions } from './ResourceDefinitionContext';
import type { ResourceOptions } from '../types';
import type { ResourceDefinitions } from './ResourceDefinitionContext';
import { useResourceDefinitionContext } from './useResourceDefinitionContext';

/**
Expand All @@ -20,6 +21,6 @@ import { useResourceDefinitionContext } from './useResourceDefinitionContext';
* // }
*/
export const useResourceDefinitions = <
OptionsType = any
OptionsType extends ResourceOptions = any
>(): ResourceDefinitions<OptionsType> =>
useResourceDefinitionContext().definitions;
2 changes: 1 addition & 1 deletion packages/ra-core/src/dataProvider/fetch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ const isValidObject = value => {
return !isArray && !isBuffer && isObject && hasKeys;
};

export const flattenObject = (value, path = []) => {
export const flattenObject = (value: any, path: string[] = []) => {
if (isValidObject(value)) {
return Object.assign(
{},
Expand Down
6 changes: 5 additions & 1 deletion packages/ra-core/src/form/useUnique.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,11 @@ export const useUnique = (options?: UseUniqueOptions) => {
}
);

if (total > 0 && !data.some(r => r.id === record?.id)) {
if (
typeof total !== 'undefined' &&
total > 0 &&
!data.some(r => r.id === record?.id)
) {
return {
message,
args: {
Expand Down
Loading