Skip to content

Commit

Permalink
[TypeScript] Fix data provider packages export non-strict types
Browse files Browse the repository at this point in the history
  • Loading branch information
fzaninotto committed Jul 2, 2024
1 parent 69e5d9a commit ccc4aa1
Show file tree
Hide file tree
Showing 14 changed files with 69 additions and 44 deletions.
4 changes: 2 additions & 2 deletions packages/ra-core/src/core/CoreAdminUI.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -152,8 +152,8 @@ export interface CoreAdminUIProps {
resetErrorBoundary,
}: {
errorInfo?: ErrorInfo;
error?: Error;
resetErrorBoundary?: (args) => void;
error: Error;
resetErrorBoundary: (args) => void;
}) => ReactElement;

/**
Expand Down
1 change: 1 addition & 0 deletions packages/ra-data-fakerest/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { DataProvider } from 'ra-core';

/* eslint-disable no-console */
function log(type, resource, params, response) {
// @ts-ignore
if (console.group) {
// Better logging in Chrome
console.groupCollapsed(type, resource, JSON.stringify(params));
Expand Down
3 changes: 2 additions & 1 deletion packages/ra-data-fakerest/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
"compilerOptions": {
"outDir": "lib",
"rootDir": "src",
"allowJs": false
"allowJs": false,
"strictNullChecks": true
},
"exclude": ["**/*.spec.ts", "**/*.spec.tsx", "**/*.spec.js"],
"include": ["src"]
Expand Down
2 changes: 1 addition & 1 deletion packages/ra-data-graphql/src/buildApolloClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
InMemoryCache,
} from '@apollo/client';

export default (options: Partial<ApolloClientOptions<unknown>>) => {
export default (options?: Partial<ApolloClientOptions<unknown>>) => {
if (!options) {
return new ApolloClient({
cache: new InMemoryCache().restore({}),
Expand Down
6 changes: 5 additions & 1 deletion packages/ra-data-graphql/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,11 @@ export const defaultOptions = {
};

const getOptions = (
options: GetQueryOptions | GetMutationOptions | GetWatchQueryOptions,
options:
| GetQueryOptions
| GetMutationOptions
| GetWatchQueryOptions
| undefined,
raFetchMethod: string,
resource: string
) => {
Expand Down
27 changes: 12 additions & 15 deletions packages/ra-data-graphql/src/introspection.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {
getIntrospectionQuery,
IntrospectionField,
IntrospectionObjectType,
IntrospectionQuery,
IntrospectionSchema,
Expand Down Expand Up @@ -51,21 +52,20 @@ export type IntrospectionResult = {

const fetchSchema = (
client: ApolloClient<unknown>
): Promise<IntrospectionSchema> => {
return client
): Promise<IntrospectionSchema> =>
client
.query<IntrospectionQuery>({
fetchPolicy: 'network-only',
query: gql`
${getIntrospectionQuery()}
`,
})
.then(({ data: { __schema } }) => __schema);
};

const getQueriesFromSchema = (
schema: IntrospectionSchema
): IntrospectionObjectType[] => {
return schema.types.reduce((acc, type) => {
): IntrospectionField[] =>
schema.types.reduce((acc, type) => {
if (
type.name !== schema.queryType?.name &&
type.name !== schema.mutationType?.name &&
Expand All @@ -76,19 +76,17 @@ const getQueriesFromSchema = (

return [...acc, ...((type as IntrospectionObjectType).fields || [])];
}, []);
};

const getTypesFromSchema = (schema: IntrospectionSchema) => {
return schema.types.filter(
const getTypesFromSchema = (schema: IntrospectionSchema) =>
schema.types.filter(
type =>
type.name !== (schema.queryType && schema.queryType.name) &&
type.name !== (schema.mutationType && schema.mutationType.name)
);
};

const getResources = (
types: IntrospectionType[],
queries: IntrospectionObjectType[],
queries: IntrospectionField[],
options: IntrospectionOptions
): IntrospectedResource[] => {
const filteredResources = types.filter(type =>
Expand All @@ -101,7 +99,7 @@ const getResources = (

const isResource = (
type: IntrospectionType,
queries: IntrospectionObjectType[],
queries: IntrospectionField[],
options: IntrospectionOptions
) => {
if (isResourceIncluded(type, options)) return true;
Expand Down Expand Up @@ -150,10 +148,10 @@ export const isResourceExcluded = (

const buildResource = (
type: IntrospectionObjectType,
queries: IntrospectionObjectType[],
queries: IntrospectionField[],
options: IntrospectionOptions
): IntrospectedResource => {
return ALL_TYPES.reduce(
): IntrospectedResource =>
ALL_TYPES.reduce(
(acc, raFetchMethod) => {
const query = queries.find(
({ name }) =>
Expand All @@ -170,4 +168,3 @@ const buildResource = (
},
{ type }
);
};
3 changes: 2 additions & 1 deletion packages/ra-data-graphql/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
"compilerOptions": {
"outDir": "lib",
"rootDir": "src",
"allowJs": false
"allowJs": false,
"strictNullChecks": true
},
"exclude": [
"**/*.spec.ts",
Expand Down
39 changes: 27 additions & 12 deletions packages/ra-data-json-server/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,17 @@ import { fetchUtils, DataProvider } from 'ra-core';
*/
export default (apiUrl, httpClient = fetchUtils.fetchJson): DataProvider => ({
getList: (resource, params) => {
const { page, perPage } = params.pagination;
const { field, order } = params.sort;
const { page, perPage } = params.pagination || {};
const { field, order } = params.sort || {};
const query = {
...fetchUtils.flattenObject(params.filter),
_sort: field,
_order: order,
_start: (page - 1) * perPage,
_end: page * perPage,
_start:
page != null && perPage != null
? (page - 1) * perPage
: undefined,
_end: page != null && perPage != null ? page * perPage : undefined,
};
const url = `${apiUrl}/${resource}?${stringify(query)}`;

Expand All @@ -53,12 +56,18 @@ export default (apiUrl, httpClient = fetchUtils.fetchJson): DataProvider => ({
'The X-Total-Count header is missing in the HTTP Response. The jsonServer Data Provider expects responses for lists of resources to contain this header with the total number of results to build the pagination. If you are using CORS, did you declare X-Total-Count in the Access-Control-Expose-Headers header?'
);
}
const totalString = headers
.get('x-total-count')!
.split('/')
.pop();
if (totalString == null) {
throw new Error(
'The X-Total-Count header is invalid in the HTTP Response.'
);
}
return {
data: json,
total: parseInt(
headers.get('x-total-count').split('/').pop(),
10
),
total: parseInt(totalString, 10),
};
}
);
Expand Down Expand Up @@ -101,12 +110,18 @@ export default (apiUrl, httpClient = fetchUtils.fetchJson): DataProvider => ({
'The X-Total-Count header is missing in the HTTP Response. The jsonServer Data Provider expects responses for lists of resources to contain this header with the total number of results to build the pagination. If you are using CORS, did you declare X-Total-Count in the Access-Control-Expose-Headers header?'
);
}
const totalString = headers
.get('x-total-count')!
.split('/')
.pop();
if (totalString == null) {
throw new Error(
'The X-Total-Count header is invalid in the HTTP Response.'
);
}
return {
data: json,
total: parseInt(
headers.get('x-total-count').split('/').pop(),
10
),
total: parseInt(totalString, 10),
};
}
);
Expand Down
3 changes: 2 additions & 1 deletion packages/ra-data-json-server/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
"compilerOptions": {
"outDir": "lib",
"rootDir": "src",
"allowJs": false
"allowJs": false,
"strictNullChecks": true
},
"exclude": ["**/*.spec.ts", "**/*.spec.tsx", "**/*.spec.js"],
"include": ["src"]
Expand Down
3 changes: 2 additions & 1 deletion packages/ra-data-localforage/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
"rootDir": "src",
"declaration": true,
"declarationMap": true,
"allowJs": false
"allowJs": false,
"strictNullChecks": true,
},
"exclude": ["**/*.spec.ts", "**/*.spec.tsx", "**/*.spec.js"],
"include": ["src"]
Expand Down
2 changes: 1 addition & 1 deletion packages/ra-data-localstorage/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ export default (params?: LocalStorageDataProviderParams): DataProvider => {

window?.addEventListener('storage', event => {
if (event.key === localStorageKey) {
const newData = JSON.parse(event.newValue);
const newData = event.newValue ? JSON.parse(event.newValue) : {};
data = newData;
baseDataProvider = fakeRestProvider(
newData,
Expand Down
3 changes: 2 additions & 1 deletion packages/ra-data-localstorage/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
"compilerOptions": {
"outDir": "lib",
"rootDir": "src",
"allowJs": false
"allowJs": false,
"strictNullChecks": true
},
"exclude": ["**/*.spec.ts", "**/*.spec.tsx", "**/*.spec.js"],
"include": ["src"]
Expand Down
14 changes: 8 additions & 6 deletions packages/ra-data-simple-rest/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ export default (
countHeader: string = 'Content-Range'
): DataProvider => ({
getList: (resource, params) => {
const { page, perPage } = params.pagination;
const { field, order } = params.sort;
const { page, perPage } = params.pagination || { page: 1, perPage: 10 };
const { field, order } = params.sort || { field: 'id', order: 'ASC' };

const rangeStart = (page - 1) * perPage;
const rangeEnd = page * perPage - 1;
Expand Down Expand Up @@ -73,10 +73,11 @@ export default (
total:
countHeader === 'Content-Range'
? parseInt(
headers.get('content-range').split('/').pop(),
headers.get('content-range')!.split('/').pop() ||
'',
10
)
: parseInt(headers.get(countHeader.toLowerCase())),
: parseInt(headers.get(countHeader.toLowerCase())!),
};
});
},
Expand Down Expand Up @@ -136,10 +137,11 @@ export default (
total:
countHeader === 'Content-Range'
? parseInt(
headers.get('content-range').split('/').pop(),
headers.get('content-range')!.split('/').pop() ||
'',
10
)
: parseInt(headers.get(countHeader.toLowerCase())),
: parseInt(headers.get(countHeader.toLowerCase())!),
};
});
},
Expand Down
3 changes: 2 additions & 1 deletion packages/ra-data-simple-rest/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
"compilerOptions": {
"outDir": "lib",
"rootDir": "src",
"allowJs": false
"allowJs": false,
"strictNullChecks": true
},
"exclude": ["**/*.spec.ts", "**/*.spec.tsx", "**/*.spec.js"],
"include": ["src"]
Expand Down

0 comments on commit ccc4aa1

Please sign in to comment.