Skip to content

Commit

Permalink
move internal exports out of entry point (#73)
Browse files Browse the repository at this point in the history
* move internal exports out of entry point
* smuggle in a small test
  • Loading branch information
phryneas committed Dec 1, 2020
1 parent 72370c2 commit afe6ae0
Show file tree
Hide file tree
Showing 10 changed files with 51 additions and 47 deletions.
8 changes: 6 additions & 2 deletions src/apiTypes.ts
Expand Up @@ -2,7 +2,7 @@
* Note: this file should import all other files for type discovery and declaration merging
*/
import { PatchQueryResultThunk, QueryApi, UpdateQueryResultThunk } from './buildThunks';
import { AnyAction, Middleware, Reducer, ThunkDispatch } from '@reduxjs/toolkit';
import { AnyAction, Middleware, Reducer, ThunkDispatch, ThunkAction } from '@reduxjs/toolkit';
import { PrefetchOptions } from './buildHooks';
import {
EndpointDefinitions,
Expand All @@ -12,10 +12,10 @@ import {
MutationDefinition,
} from './endpointDefinitions';
import { CombinedState, QueryKeys, QueryStatePhantomType, RootState } from './apiState';
import { InternalActions } from './index';
import { UnionToIntersection } from './tsHelpers';
import { TS41Hooks } from './ts41Types';
import './buildSelectors';
import { SliceActions } from './buildSlice';

type UnwrapPromise<T> = T extends PromiseLike<infer V> ? V : T;
type MaybePromise<T> = T | PromiseLike<T>;
Expand Down Expand Up @@ -110,3 +110,7 @@ export type ApiWithInjectedEndpoints<
Omit<Injections, 'endpoints'> & {
endpoints: ApiDefinition['endpoints'] & Partial<UnionToIntersection<Injections[number]['endpoints']>>;
};

export type InternalActions = SliceActions & {
prefetchThunk: (endpointName: any, arg: any, options: PrefetchOptions) => ThunkAction<void, any, any, AnyAction>;
};
2 changes: 1 addition & 1 deletion src/buildActionMaps.ts
Expand Up @@ -2,7 +2,7 @@ import { EndpointDefinitions, QueryDefinition, MutationDefinition, QueryArgFrom
import type { QueryThunkArg, MutationThunkArg } from './buildThunks';
import { AnyAction, AsyncThunk, ThunkAction } from '@reduxjs/toolkit';
import { MutationSubState, QueryStatus, QuerySubState, SubscriptionOptions } from './apiState';
import { InternalSerializeQueryArgs } from '.';
import { InternalSerializeQueryArgs } from './defaultSerializeQueryArgs';
import { Api, ApiEndpointMutation, ApiEndpointQuery } from './apiTypes';

declare module './apiTypes' {
Expand Down
2 changes: 1 addition & 1 deletion src/buildSelectors.ts
Expand Up @@ -16,7 +16,7 @@ import {
ReducerPathFrom,
} from './endpointDefinitions';
import type { InternalState } from './buildSlice';
import { InternalSerializeQueryArgs } from '.';
import { InternalSerializeQueryArgs } from './defaultSerializeQueryArgs';

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

Expand Down
2 changes: 1 addition & 1 deletion src/buildThunks.ts
@@ -1,4 +1,4 @@
import { InternalSerializeQueryArgs } from '.';
import { InternalSerializeQueryArgs } from './defaultSerializeQueryArgs';
import { Api, ApiEndpointQuery, BaseQueryFn, BaseQueryArg, BaseQueryError } from './apiTypes';
import { InternalRootState, QueryKeys, QueryStatus, QuerySubstateIdentifier } from './apiState';
import { StartQueryActionCreatorOptions } from './buildActionMaps';
Expand Down
18 changes: 18 additions & 0 deletions src/defaultSerializeQueryArgs.ts
@@ -0,0 +1,18 @@
import { QueryCacheKey } from './apiState';

export const defaultSerializeQueryArgs: SerializeQueryArgs<any> = ({ endpoint, queryArgs }) => {
// Sort the object keys before stringifying, to prevent useQuery({ a: 1, b: 2 }) having a different cache key than useQuery({ b: 2, a: 1 })
return `${endpoint}(${JSON.stringify(queryArgs, Object.keys(queryArgs || {}).sort())})`;
};

export type SerializeQueryArgs<InternalQueryArgs> = (_: {
queryArgs: any;
internalQueryArgs: InternalQueryArgs;
endpoint: string;
}) => string;

export type InternalSerializeQueryArgs<InternalQueryArgs> = (_: {
queryArgs: any;
internalQueryArgs: InternalQueryArgs;
endpoint: string;
}) => QueryCacheKey;
59 changes: 17 additions & 42 deletions src/index.ts
@@ -1,47 +1,30 @@
import { buildThunks } from './buildThunks';
import type { AnyAction, Reducer, ThunkAction } from '@reduxjs/toolkit';
import { buildSlice, SliceActions } from './buildSlice';
import type { AnyAction, Reducer } from '@reduxjs/toolkit';
import type { CombinedState, QueryStatePhantomType } from './apiState';
import { Api, BaseQueryArg, BaseQueryFn } from './apiTypes';
import { buildActionMaps } from './buildActionMaps';
import { buildSelectors } from './buildSelectors';
import { buildHooks, PrefetchOptions } from './buildHooks';
import { buildHooks } from './buildHooks';
import { buildMiddleware } from './buildMiddleware';
import { buildSelectors } from './buildSelectors';
import { buildSlice } from './buildSlice';
import { buildThunks } from './buildThunks';
import { defaultSerializeQueryArgs, InternalSerializeQueryArgs, SerializeQueryArgs } from './defaultSerializeQueryArgs';
import {
EndpointDefinitions,
EndpointBuilder,
AssertEntityTypes,
DefinitionType,
isQueryDefinition,
EndpointBuilder,
EndpointDefinitions,
isMutationDefinition,
AssertEntityTypes,
isQueryDefinition,
} from './endpointDefinitions';
import type { CombinedState, QueryCacheKey, QueryStatePhantomType } from './apiState';
import { assertCast } from './tsHelpers';
import { Api, BaseQueryArg, BaseQueryFn } from './apiTypes';
export { Api, ApiWithInjectedEndpoints, BaseQueryFn, BaseQueryEnhancer } from './apiTypes';
export { fetchBaseQuery, FetchBaseQueryError } from './fetchBaseQuery';
import { capitalize, IS_DEV } from './utils';
export { ApiProvider } from './ApiProvider';
export { QueryStatus } from './apiState';
export type { Api, ApiWithInjectedEndpoints, BaseQueryEnhancer, BaseQueryFn } from './apiTypes';
export { fetchBaseQuery } from './fetchBaseQuery';
export type { FetchBaseQueryError } from './fetchBaseQuery';
export { retry } from './retry';

export { ApiProvider } from './ApiProvider';

export type SerializeQueryArgs<InternalQueryArgs> = (_: {
queryArgs: any;
internalQueryArgs: InternalQueryArgs;
endpoint: string;
}) => string;

export type InternalSerializeQueryArgs<InternalQueryArgs> = (_: {
queryArgs: any;
internalQueryArgs: InternalQueryArgs;
endpoint: string;
}) => QueryCacheKey;

const defaultSerializeQueryArgs: SerializeQueryArgs<any> = ({ endpoint, queryArgs }) => {
// Sort the object keys before stringifying, to prevent useQuery({ a: 1, b: 2 }) having a different cache key than useQuery({ b: 2, a: 1 })
return `${endpoint}(${JSON.stringify(queryArgs, Object.keys(queryArgs || {}).sort())})`;
};

const IS_DEV = () => typeof process !== 'undefined' && process.env.NODE_ENV === 'development';

export function createApi<
BaseQuery extends BaseQueryFn,
Definitions extends EndpointDefinitions,
Expand Down Expand Up @@ -205,11 +188,3 @@ export function createApi<

return api.injectEndpoints({ endpoints });
}

export type InternalActions = SliceActions & {
prefetchThunk: (endpointName: any, arg: any, options: PrefetchOptions) => ThunkAction<void, any, any, AnyAction>;
};

function capitalize(str: string) {
return str.replace(str[0], str[0].toUpperCase());
}
3 changes: 3 additions & 0 deletions src/utils/capitalize.ts
@@ -0,0 +1,3 @@
export function capitalize(str: string) {
return str.replace(str[0], str[0].toUpperCase());
}
2 changes: 2 additions & 0 deletions src/utils/index.ts
Expand Up @@ -4,3 +4,5 @@ export * from './joinUrls';
export * from './flatten';
export * from './shallowEqual';
export * from './useShallowStableValue';
export * from './capitalize';
export * from './isDev';
1 change: 1 addition & 0 deletions src/utils/isDev.ts
@@ -0,0 +1 @@
export const IS_DEV = () => typeof process !== 'undefined' && process.env.NODE_ENV === 'development';
1 change: 1 addition & 0 deletions test/matchers.test.tsx
Expand Up @@ -130,6 +130,7 @@ test('inferred types', () => {
})
.addMatcher(api.endpoints.querySuccess.matchFulfilled, (state, action) => {
expectExactType({} as ResultType)(action.payload.result);
expectExactType(0 as number)(action.payload.fulfilledTimeStamp);
// @ts-expect-error
console.log(action.error);
expectExactType({} as ArgType)(action.meta.arg.originalArgs);
Expand Down

0 comments on commit afe6ae0

Please sign in to comment.