-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from StudentOfJS/prefetch
Prefetch
- Loading branch information
Showing
15 changed files
with
277 additions
and
141 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,43 +1,23 @@ | ||
import { UnknownDataResponseType } from "../utils"; | ||
export interface FetchWorkerProps { | ||
fetchOptions?: RequestInit | undefined; | ||
maxAge?: number; | ||
middleware?: (data: UnknownDataResponseType) => UnknownDataResponseType; | ||
url: RequestInfo | URL; | ||
update?: { | ||
url: RequestInfo | URL; | ||
options?: RequestInit | undefined; | ||
}; | ||
} | ||
/** | ||
* useFetch is a React hook that can be initialized with no params. | ||
* @example const { data, error, loading, fetchWorker } = useFetch() | ||
* fetchWorker({ | ||
* url: 'https://swapi.dev/api/people/1/', | ||
* middleware: (d:Record<string, any>) => { let keys = Object.keys(d); return {[keys[0]]: d[keys[0]], [keys[1]]: d[keys[1]]}} | ||
* }); | ||
* | ||
* | ||
*/ | ||
import type { FetchWorkerProps } from "../types"; | ||
export declare function useFetch(): { | ||
data: any; | ||
loading: boolean; | ||
preload: boolean; | ||
error: undefined; | ||
update: boolean; | ||
fetchWorker: ({ url, fetchOptions, maxAge, middleware }: FetchWorkerProps) => Promise<void>; | ||
fetchWorker: ({ url, options, maxAge, middleware }: FetchWorkerProps) => Promise<void>; | ||
} | { | ||
error: any; | ||
loading: boolean; | ||
preload: boolean; | ||
data: UnknownDataResponseType; | ||
data: import("../utils").UnknownDataResponseType; | ||
update: boolean; | ||
fetchWorker: ({ url, fetchOptions, maxAge, middleware }: FetchWorkerProps) => Promise<void>; | ||
fetchWorker: ({ url, options, maxAge, middleware }: FetchWorkerProps) => Promise<void>; | ||
} | { | ||
loading: any; | ||
data: UnknownDataResponseType; | ||
data: import("../utils").UnknownDataResponseType; | ||
error?: Error | undefined; | ||
preload: boolean; | ||
update: boolean; | ||
fetchWorker: ({ url, fetchOptions, maxAge, middleware }: FetchWorkerProps) => Promise<void>; | ||
fetchWorker: ({ url, options, maxAge, middleware }: FetchWorkerProps) => Promise<void>; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
import type { FetchWorkerBaseRequestType } from "../types"; | ||
export declare function usePreFetch(prefetch?: Array<FetchWorkerBaseRequestType>): void; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
export { useFetch } from './hooks/useFetch'; | ||
export { usePreFetch } from './hooks/usePreFetch'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
export type StringAnyTuple = [string, any] | ||
export type ArrayOfStringAnyTuple = Array<StringAnyTuple> | ||
export type createArrayOfUpdatesType = (oldRecord: Record<string, any>, newRecord: Record<string, any>) => ArrayOfStringAnyTuple | ||
|
||
export type WorkerResponseType = MessageEvent<{ | ||
type: string; | ||
data?: UnknownDataResponseType | ||
}> | ||
export interface FetchWorkerBaseRequestType { | ||
options?: RequestInit | undefined | ||
maxAge?: number | ||
middleware?: (data: UnknownDataResponseType) => UnknownDataResponseType | ||
url: RequestInfo | URL | ||
} | ||
export interface FetchWorkerRequestType extends FetchWorkerBaseRequestType { | ||
middleware?: string | ||
} | ||
|
||
export interface FetchWorkerProps extends FetchWorkerBaseRequestType { | ||
/* | ||
update will be fetched after the response is received. | ||
This is useful for updating after POST PUT or DELETE, | ||
where the api doesn't return all the data and the GET | ||
endpoint is different. | ||
*/ | ||
update?: { | ||
url: RequestInfo | URL, | ||
options?: RequestInit | undefined | ||
} | ||
} | ||
export interface PollWorkerProps { | ||
url: RequestInfo | URL | ||
interval: number, | ||
options?: RequestInit | undefined | ||
maxAttempts?: number, | ||
existingData?: number, | ||
compareKeys?: string[] | ||
} | ||
|
||
export type ValueType = { timestamp: number; maxAge: number; data: any }; | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
export declare function store(): { | ||
remove: (key: IDBValidKey) => Promise<void>; | ||
getData: (key: IDBValidKey) => Promise<any>; | ||
setData: (key: IDBValidKey, value: unknown) => Promise<void>; | ||
updateData: (key: IDBValidKey, updater: (oldValue: any) => any) => Promise<void>; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.