Skip to content

Commit afe6ae0

Browse files
authored
move internal exports out of entry point (#73)
* move internal exports out of entry point * smuggle in a small test
1 parent 72370c2 commit afe6ae0

10 files changed

+51
-47
lines changed

src/apiTypes.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
* Note: this file should import all other files for type discovery and declaration merging
33
*/
44
import { PatchQueryResultThunk, QueryApi, UpdateQueryResultThunk } from './buildThunks';
5-
import { AnyAction, Middleware, Reducer, ThunkDispatch } from '@reduxjs/toolkit';
5+
import { AnyAction, Middleware, Reducer, ThunkDispatch, ThunkAction } from '@reduxjs/toolkit';
66
import { PrefetchOptions } from './buildHooks';
77
import {
88
EndpointDefinitions,
@@ -12,10 +12,10 @@ import {
1212
MutationDefinition,
1313
} from './endpointDefinitions';
1414
import { CombinedState, QueryKeys, QueryStatePhantomType, RootState } from './apiState';
15-
import { InternalActions } from './index';
1615
import { UnionToIntersection } from './tsHelpers';
1716
import { TS41Hooks } from './ts41Types';
1817
import './buildSelectors';
18+
import { SliceActions } from './buildSlice';
1919

2020
type UnwrapPromise<T> = T extends PromiseLike<infer V> ? V : T;
2121
type MaybePromise<T> = T | PromiseLike<T>;
@@ -110,3 +110,7 @@ export type ApiWithInjectedEndpoints<
110110
Omit<Injections, 'endpoints'> & {
111111
endpoints: ApiDefinition['endpoints'] & Partial<UnionToIntersection<Injections[number]['endpoints']>>;
112112
};
113+
114+
export type InternalActions = SliceActions & {
115+
prefetchThunk: (endpointName: any, arg: any, options: PrefetchOptions) => ThunkAction<void, any, any, AnyAction>;
116+
};

src/buildActionMaps.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { EndpointDefinitions, QueryDefinition, MutationDefinition, QueryArgFrom
22
import type { QueryThunkArg, MutationThunkArg } from './buildThunks';
33
import { AnyAction, AsyncThunk, ThunkAction } from '@reduxjs/toolkit';
44
import { MutationSubState, QueryStatus, QuerySubState, SubscriptionOptions } from './apiState';
5-
import { InternalSerializeQueryArgs } from '.';
5+
import { InternalSerializeQueryArgs } from './defaultSerializeQueryArgs';
66
import { Api, ApiEndpointMutation, ApiEndpointQuery } from './apiTypes';
77

88
declare module './apiTypes' {

src/buildSelectors.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import {
1616
ReducerPathFrom,
1717
} from './endpointDefinitions';
1818
import type { InternalState } from './buildSlice';
19-
import { InternalSerializeQueryArgs } from '.';
19+
import { InternalSerializeQueryArgs } from './defaultSerializeQueryArgs';
2020

2121
export const skipSelector = Symbol('skip selector');
2222

src/buildThunks.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { InternalSerializeQueryArgs } from '.';
1+
import { InternalSerializeQueryArgs } from './defaultSerializeQueryArgs';
22
import { Api, ApiEndpointQuery, BaseQueryFn, BaseQueryArg, BaseQueryError } from './apiTypes';
33
import { InternalRootState, QueryKeys, QueryStatus, QuerySubstateIdentifier } from './apiState';
44
import { StartQueryActionCreatorOptions } from './buildActionMaps';

src/defaultSerializeQueryArgs.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import { QueryCacheKey } from './apiState';
2+
3+
export const defaultSerializeQueryArgs: SerializeQueryArgs<any> = ({ endpoint, queryArgs }) => {
4+
// Sort the object keys before stringifying, to prevent useQuery({ a: 1, b: 2 }) having a different cache key than useQuery({ b: 2, a: 1 })
5+
return `${endpoint}(${JSON.stringify(queryArgs, Object.keys(queryArgs || {}).sort())})`;
6+
};
7+
8+
export type SerializeQueryArgs<InternalQueryArgs> = (_: {
9+
queryArgs: any;
10+
internalQueryArgs: InternalQueryArgs;
11+
endpoint: string;
12+
}) => string;
13+
14+
export type InternalSerializeQueryArgs<InternalQueryArgs> = (_: {
15+
queryArgs: any;
16+
internalQueryArgs: InternalQueryArgs;
17+
endpoint: string;
18+
}) => QueryCacheKey;

src/index.ts

Lines changed: 17 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,30 @@
1-
import { buildThunks } from './buildThunks';
2-
import type { AnyAction, Reducer, ThunkAction } from '@reduxjs/toolkit';
3-
import { buildSlice, SliceActions } from './buildSlice';
1+
import type { AnyAction, Reducer } from '@reduxjs/toolkit';
2+
import type { CombinedState, QueryStatePhantomType } from './apiState';
3+
import { Api, BaseQueryArg, BaseQueryFn } from './apiTypes';
44
import { buildActionMaps } from './buildActionMaps';
5-
import { buildSelectors } from './buildSelectors';
6-
import { buildHooks, PrefetchOptions } from './buildHooks';
5+
import { buildHooks } from './buildHooks';
76
import { buildMiddleware } from './buildMiddleware';
7+
import { buildSelectors } from './buildSelectors';
8+
import { buildSlice } from './buildSlice';
9+
import { buildThunks } from './buildThunks';
10+
import { defaultSerializeQueryArgs, InternalSerializeQueryArgs, SerializeQueryArgs } from './defaultSerializeQueryArgs';
811
import {
9-
EndpointDefinitions,
10-
EndpointBuilder,
12+
AssertEntityTypes,
1113
DefinitionType,
12-
isQueryDefinition,
14+
EndpointBuilder,
15+
EndpointDefinitions,
1316
isMutationDefinition,
14-
AssertEntityTypes,
17+
isQueryDefinition,
1518
} from './endpointDefinitions';
16-
import type { CombinedState, QueryCacheKey, QueryStatePhantomType } from './apiState';
1719
import { assertCast } from './tsHelpers';
18-
import { Api, BaseQueryArg, BaseQueryFn } from './apiTypes';
19-
export { Api, ApiWithInjectedEndpoints, BaseQueryFn, BaseQueryEnhancer } from './apiTypes';
20-
export { fetchBaseQuery, FetchBaseQueryError } from './fetchBaseQuery';
20+
import { capitalize, IS_DEV } from './utils';
21+
export { ApiProvider } from './ApiProvider';
2122
export { QueryStatus } from './apiState';
23+
export type { Api, ApiWithInjectedEndpoints, BaseQueryEnhancer, BaseQueryFn } from './apiTypes';
24+
export { fetchBaseQuery } from './fetchBaseQuery';
25+
export type { FetchBaseQueryError } from './fetchBaseQuery';
2226
export { retry } from './retry';
2327

24-
export { ApiProvider } from './ApiProvider';
25-
26-
export type SerializeQueryArgs<InternalQueryArgs> = (_: {
27-
queryArgs: any;
28-
internalQueryArgs: InternalQueryArgs;
29-
endpoint: string;
30-
}) => string;
31-
32-
export type InternalSerializeQueryArgs<InternalQueryArgs> = (_: {
33-
queryArgs: any;
34-
internalQueryArgs: InternalQueryArgs;
35-
endpoint: string;
36-
}) => QueryCacheKey;
37-
38-
const defaultSerializeQueryArgs: SerializeQueryArgs<any> = ({ endpoint, queryArgs }) => {
39-
// Sort the object keys before stringifying, to prevent useQuery({ a: 1, b: 2 }) having a different cache key than useQuery({ b: 2, a: 1 })
40-
return `${endpoint}(${JSON.stringify(queryArgs, Object.keys(queryArgs || {}).sort())})`;
41-
};
42-
43-
const IS_DEV = () => typeof process !== 'undefined' && process.env.NODE_ENV === 'development';
44-
4528
export function createApi<
4629
BaseQuery extends BaseQueryFn,
4730
Definitions extends EndpointDefinitions,
@@ -205,11 +188,3 @@ export function createApi<
205188

206189
return api.injectEndpoints({ endpoints });
207190
}
208-
209-
export type InternalActions = SliceActions & {
210-
prefetchThunk: (endpointName: any, arg: any, options: PrefetchOptions) => ThunkAction<void, any, any, AnyAction>;
211-
};
212-
213-
function capitalize(str: string) {
214-
return str.replace(str[0], str[0].toUpperCase());
215-
}

src/utils/capitalize.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export function capitalize(str: string) {
2+
return str.replace(str[0], str[0].toUpperCase());
3+
}

src/utils/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,5 @@ export * from './joinUrls';
44
export * from './flatten';
55
export * from './shallowEqual';
66
export * from './useShallowStableValue';
7+
export * from './capitalize';
8+
export * from './isDev';

src/utils/isDev.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export const IS_DEV = () => typeof process !== 'undefined' && process.env.NODE_ENV === 'development';

test/matchers.test.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,7 @@ test('inferred types', () => {
130130
})
131131
.addMatcher(api.endpoints.querySuccess.matchFulfilled, (state, action) => {
132132
expectExactType({} as ResultType)(action.payload.result);
133+
expectExactType(0 as number)(action.payload.fulfilledTimeStamp);
133134
// @ts-expect-error
134135
console.log(action.error);
135136
expectExactType({} as ArgType)(action.meta.arg.originalArgs);

0 commit comments

Comments
 (0)