Skip to content

Commit

Permalink
refactor: fix configure config type
Browse files Browse the repository at this point in the history
  • Loading branch information
pyphilia committed Nov 1, 2023
1 parent 108a172 commit 266aad0
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 9 deletions.
28 changes: 21 additions & 7 deletions src/queryClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,24 @@ const retry = (failureCount: number, error: Error): boolean => {
return false;
};

export default (config: Partial<QueryClientConfig>) => {
export type ConfigureQueryClientConfig = Partial<
Pick<
QueryClientConfig,
| 'API_HOST'
| 'DOMAIN'
| 'SHOW_NOTIFICATIONS'
| 'onConfigAxios'
| 'enableWebsocket'
| 'WS_HOST'
| 'notifier'
>
> &
Pick<QueryClientConfig, 'defaultQueryOptions'>;

export default (config: ConfigureQueryClientConfig) => {
const baseConfig = {
API_HOST: config?.API_HOST || 'http://localhost:3000',
SHOW_NOTIFICATIONS: config?.SHOW_NOTIFICATIONS || false,
API_HOST: config.API_HOST || 'http://localhost:3000',
SHOW_NOTIFICATIONS: config.SHOW_NOTIFICATIONS || false,
DOMAIN: config.DOMAIN ?? getHostname(),
};

Expand All @@ -67,10 +81,10 @@ export default (config: Partial<QueryClientConfig>) => {
axios,
// derive WS_HOST from API_HOST if needed
WS_HOST:
config?.WS_HOST || `${baseConfig.API_HOST.replace('http', 'ws')}/ws`,
config.WS_HOST || `${baseConfig.API_HOST.replace('http', 'ws')}/ws`,
// whether websocket support should be enabled
enableWebsocket: config?.enableWebsocket || false,
notifier: config?.notifier,
enableWebsocket: config.enableWebsocket || false,
notifier: config.notifier,
// default hooks & mutation config
defaultQueryOptions: {
retry,
Expand All @@ -81,7 +95,7 @@ export default (config: Partial<QueryClientConfig>) => {
refetchOnWindowFocus: false,
notifyOnChangeProps: 'tracked',
isDataEqual,
...config?.defaultQueryOptions,
...config.defaultQueryOptions,
},
};

Expand Down
4 changes: 2 additions & 2 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ export type QueryClientConfig = {
onConfigAxios?: (axios: AxiosInstance) => void;
defaultQueryOptions: {
// time until data in cache considered stale if cache not invalidated
staleTime: number;
staleTime?: number;
// time before cache labeled as inactive to be garbage collected
cacheTime: number;
cacheTime?: number;
retry?:
| number
| boolean
Expand Down

0 comments on commit 266aad0

Please sign in to comment.