Skip to content

Commit

Permalink
docs: Docs adjustments
Browse files Browse the repository at this point in the history
  • Loading branch information
prc5 committed Dec 19, 2024
1 parent 82dd8bd commit 2c44009
Show file tree
Hide file tree
Showing 16 changed files with 30 additions and 53 deletions.
1 change: 1 addition & 0 deletions documentation/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
"typescript": "~5.2.2"
},
"resolutions": {
"wrap-ansi": "7.0.0",
"string-width": "4.2.3",
"strip-ansi": "6.0.1"
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ describe("Request [ Setters ]", () => {
});
it("should allow for setting cache time", async () => {
expect(request.staleTime).toBe(Time.MIN * 5);
const updatedRequest = request.setCacheTime(1000);
const updatedRequest = request.setStaleTime(1000);
expect(updatedRequest.staleTime).toBe(1000);
});
it("should allow for setting queued", async () => {
Expand Down Expand Up @@ -126,7 +126,7 @@ describe("Request [ Setters ]", () => {
});
it("should allow for setting cacheTime", async () => {
expect(request.cacheTime).toBe(Time.MIN * 5);
const updatedRequest = request.setGarbageCollection(Time.MIN);
const updatedRequest = request.setCacheTime(Time.MIN);
expect(updatedRequest.cacheTime).toBe(Time.MIN);
});
it("should allow for setting data mapper", async () => {
Expand Down
4 changes: 2 additions & 2 deletions packages/core/src/request/request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ export class Request<
return this.clone({ retryTime });
};

public setGarbageCollection = (
public setCacheTime = (
cacheTime: RequestOptionsType<
Endpoint,
ExtractAdapterOptionsType<ExtractClientAdapterType<Client>>,
Expand All @@ -248,7 +248,7 @@ export class Request<
return this.clone({ cache });
};

public setCacheTime = (
public setStaleTime = (
staleTime: RequestOptionsType<
Endpoint,
ExtractAdapterOptionsType<ExtractClientAdapterType<Client>>,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ describe("useFetch [ Base ]", () => {
});
});

const view = renderUseFetch(request.setCacheTime(10));
const view = renderUseFetch(request.setStaleTime(10));

await testCacheState({ data: null, error: null, status: null, success: true, extra: xhrExtra }, view);
});
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
/* eslint-disable @typescript-eslint/naming-convention */
import { useMemo, useRef } from "react";
import { useRef } from "react";
import { useDidUpdate, useForceUpdate } from "@better-hooks/lifecycle";
import {
ExtractErrorType,
CacheValueType,
ExtractResponseType,
RequestInstance,
ExtractAdapterType,
HydrateDataType,
ExtractAdapterExtraType,
} from "@hyper-fetch/core";

Expand All @@ -19,42 +18,28 @@ import {
UseTrackedStateReturn,
} from "./use-tracked-state.types";
import { getInitialState, getValidCacheData, isStaleCacheData } from "./use-tracked-state.utils";
import { useProvider } from "provider";

/**
*
* @param request
* @param initialData
* @param initialResponse
* @param dispatcher
* @param dependencies
* @internal
*/
export const useTrackedState = <T extends RequestInstance>({
request,
dispatcher,
initialData,
initialResponse,
deepCompare,
dependencyTracking,
}: UseTrackedStateProps<T>): UseTrackedStateReturn<T> => {
const { client, cacheKey, queryKey, staleTime, __responseMapper } = request;
const { cache, requestManager } = client;

const forceUpdate = useForceUpdate();
const { hydrationData } = useProvider();

const { hydrationResponse } = useMemo(() => {
const hydrationItem = hydrationData?.find((item) => item.cacheKey === cacheKey) as HydrateDataType<
ExtractResponseType<T>,
ExtractErrorType<T>,
ExtractAdapterType<T>
>;

return {
hydrationResponse: hydrationItem?.response,
};
}, [cacheKey, hydrationData]);

const state = useRef<UseTrackedStateType<T>>(getInitialState(initialData || hydrationResponse, dispatcher, request));
const state = useRef<UseTrackedStateType<T>>(getInitialState(initialResponse, dispatcher, request));
const renderKeys = useRef<Array<keyof UseTrackedStateType<T>>>([]);
const isProcessingData = useRef("");

Expand Down Expand Up @@ -93,9 +78,9 @@ export const useTrackedState = <T extends RequestInstance>({

// Get cache state
const cacheData = cache.get<ExtractResponseType<T>, ExtractErrorType<T>, ExtractAdapterType<T>>(cacheKey);
const cacheState = getValidCacheData<T>(request, initialData || hydrationResponse, cacheData);
const cacheState = getValidCacheData<T>(request, initialResponse, cacheData);

const hasInitialState = isEqual(initialData?.data || hydrationResponse?.data, state.current.data);
const hasInitialState = isEqual(initialResponse?.data, state.current.data);
const hasState = !!(state.current.data || state.current.error) && !hasInitialState;
const shouldLoadInitialCache = !hasState && !!state.current.data;
const shouldRemovePreviousData = hasState && !state.current.data;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import { isEqual } from "utils";
export type UseTrackedStateProps<T extends RequestInstance> = {
request: T;
logger: LoggerType;
initialData: NullableType<Partial<ExtractAdapterResolvedType<T>>>;
initialResponse: NullableType<Partial<ExtractAdapterResolvedType<T>>>;
dispatcher: Dispatcher;
dependencyTracking: boolean;
deepCompare: boolean | typeof isEqual;
Expand Down
2 changes: 1 addition & 1 deletion packages/react/src/hooks/use-cache/use-cache.constants.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export const useCacheDefaultOptions = {
dependencyTracking: true,
initialData: null,
initialResponse: null,
deepCompare: true,
};
4 changes: 2 additions & 2 deletions packages/react/src/hooks/use-cache/use-cache.hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export const useCache = <T extends RequestInstance>(

// Build the configuration options
const { config: globalConfig } = useProvider();
const { dependencyTracking, initialData, deepCompare } = {
const { dependencyTracking, initialResponse, deepCompare } = {
...useCacheDefaultOptions,
...globalConfig.useCacheConfig,
...options,
Expand All @@ -32,7 +32,7 @@ export const useCache = <T extends RequestInstance>(
logger,
request,
dispatcher,
initialData,
initialResponse,
deepCompare,
dependencyTracking,
});
Expand Down
2 changes: 1 addition & 1 deletion packages/react/src/hooks/use-cache/use-cache.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export type UseCacheOptionsType<T extends RequestInstance> = {
/**
* If cache is empty we can use placeholder data.
*/
initialData?: CacheValueType<ExtractResponseType<T>, ExtractErrorType<T>>["data"] | null;
initialResponse?: CacheValueType<ExtractResponseType<T>, ExtractErrorType<T>>["data"] | null;
/**
* Deep comparison function for hook to check for equality in incoming data, to limit rerenders.
*/
Expand Down
6 changes: 3 additions & 3 deletions packages/react/src/hooks/use-fetch/use-fetch.constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,16 @@ import { Time, RequestInstance, RequiredKeys } from "@hyper-fetch/core";

import { UseFetchOptionsType } from "hooks/use-fetch";

type DefaultOptionsType = RequiredKeys<Omit<UseFetchOptionsType<RequestInstance>, "initialData">> & {
initialData: null;
type DefaultOptionsType = RequiredKeys<Omit<UseFetchOptionsType<RequestInstance>, "initialResponse">> & {
initialResponse: null;
};

export const useFetchDefaultOptions: DefaultOptionsType = {
dependencies: [],
disabled: false,
dependencyTracking: true,
revalidate: true,
initialData: null,
initialResponse: null,
refresh: false,
refreshTime: Time.HOUR,
refetchBlurred: true,
Expand Down
4 changes: 2 additions & 2 deletions packages/react/src/hooks/use-fetch/use-fetch.hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export const useFetch = <R extends RequestInstance>(
disabled = useFetchDefaultOptions.disabled,
dependencyTracking = useFetchDefaultOptions.dependencyTracking,
revalidate = useFetchDefaultOptions.revalidate,
initialData = useFetchDefaultOptions.initialData,
initialResponse = useFetchDefaultOptions.initialResponse,
refresh = useFetchDefaultOptions.refresh,
refreshTime = useFetchDefaultOptions.refreshTime,
refetchBlurred = useFetchDefaultOptions.refetchBlurred,
Expand Down Expand Up @@ -73,7 +73,7 @@ export const useFetch = <R extends RequestInstance>(
logger,
request,
dispatcher,
initialData,
initialResponse,
deepCompare,
dependencyTracking,
});
Expand Down
2 changes: 1 addition & 1 deletion packages/react/src/hooks/use-fetch/use-fetch.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ export type UseFetchOptionsType<R extends RequestInstance> = {
/**
* If cache is empty we can use placeholder data.
*/
initialData?: NullableType<Partial<ExtractAdapterResolvedType<UseFetchRequest<R>>>>;
initialResponse?: NullableType<Partial<ExtractAdapterResolvedType<UseFetchRequest<R>>>>;
/**
* Enable/disable refresh data
*/
Expand Down
6 changes: 3 additions & 3 deletions packages/react/src/hooks/use-submit/use-submit.constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ import { RequestInstance, RequiredKeys } from "@hyper-fetch/core";

import { UseSubmitOptionsType } from "./use-submit.types";

type DefaultOptionsType = RequiredKeys<Omit<UseSubmitOptionsType<RequestInstance>, "initialData">> & {
initialData: null;
type DefaultOptionsType = RequiredKeys<Omit<UseSubmitOptionsType<RequestInstance>, "initialResponse">> & {
initialResponse: null;
};

export const useSubmitDefaultOptions: DefaultOptionsType = {
disabled: false,
dependencyTracking: true,
initialData: null,
initialResponse: null,
bounce: false,
bounceType: "debounce",
bounceTime: 400,
Expand Down
4 changes: 2 additions & 2 deletions packages/react/src/hooks/use-submit/use-submit.hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ export const useSubmit = <RequestType extends RequestInstance>(
const {
disabled,
dependencyTracking = true,
initialData,
initialResponse,
bounce,
bounceType,
bounceTime,
Expand Down Expand Up @@ -82,7 +82,7 @@ export const useSubmit = <RequestType extends RequestInstance>(
logger,
request,
dispatcher,
initialData,
initialResponse,
deepCompare,
dependencyTracking,
});
Expand Down
2 changes: 1 addition & 1 deletion packages/react/src/hooks/use-submit/use-submit.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export type UseSubmitOptionsType<T extends RequestInstance> = {
/**
* If cache is empty we can use placeholder data.
*/
initialData?: NullableType<Partial<ExtractAdapterResolvedType<T>>>;
initialResponse?: NullableType<Partial<ExtractAdapterResolvedType<T>>>;
/**
* Enable/disable debouncing for often changing keys or refreshing, to limit requests to server.
*/
Expand Down
11 changes: 1 addition & 10 deletions packages/react/src/provider/provider.types.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React from "react";
import { ClientInstance, RequestInstance, HydrateDataType, HydrationOptions } from "@hyper-fetch/core";
import { ClientInstance, RequestInstance } from "@hyper-fetch/core";

import { UseSubmitOptionsType } from "hooks/use-submit";
import { UseCacheOptionsType } from "hooks/use-cache";
Expand Down Expand Up @@ -32,18 +32,9 @@ export type ProviderProps<Client extends ClientInstance = ClientInstance> = {
* Configuration to set for available hooks
*/
config?: ProviderOptionsType;
/**
* Fallbacks to hydrate for particular requests
*/
hydrationData?: HydrateDataType[];
/**
* Hydration configuration
*/
hydrationConfig?: HydrationOptions;
};

export type ProviderValueType = {
config: ProviderOptionsType;
setConfig: (newConfig: ProviderOptionsType) => void;
hydrationData?: HydrateDataType[];
};

0 comments on commit 2c44009

Please sign in to comment.