-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
5df7f07
commit e7dc174
Showing
15 changed files
with
179 additions
and
34 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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 @@ | ||
export * from './dist/mjs/rsc'; |
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 @@ | ||
// eslint-disable-next-line import/extensions | ||
module.exports = require('./dist/cjs/rsc'); |
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 @@ | ||
export * from './useFetch'; | ||
export * from './useFetchPost'; |
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,10 @@ | ||
import { FetchOptions } from '../../data'; | ||
|
||
export interface HookResponse { | ||
data?: {}; | ||
isMainQuery: boolean; | ||
} | ||
|
||
export interface FetchHookOptions { | ||
fetchStrategyOptions?: Partial<FetchOptions>; | ||
} |
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,63 @@ | ||
import deepmerge from 'deepmerge'; | ||
import type { EndpointParams } from '../../data'; | ||
import { AbstractFetchStrategy } from '../../data'; | ||
|
||
import type { FetchHookOptions } from './types'; | ||
import { getHeadstartWPConfig, log, LOGTYPE } from '../../utils'; | ||
|
||
export interface useFetchOptions {} | ||
|
||
/** | ||
* The use Fetch Hook is the foundation for most hooks in the headless framework. It is a wrapper around | ||
* `useSWR` and provides a consistent API for fetching data from the API. It requires a fetch strategy which implements | ||
* the actual data fetching logic | ||
* | ||
* @param params The list of params to pass to the fetch strategy. It overrides the ones in the URL. | ||
* @param fetchStrategy The fetch strategy. | ||
* @param options The options to pass to the swr hook. | ||
* @param path The path of the url to get url params from. | ||
* | ||
* @category Data Fetching Hooks | ||
* | ||
*/ | ||
export async function useFetch<E, Params extends EndpointParams, R = E>( | ||
params: Params | {}, | ||
fetchStrategy: AbstractFetchStrategy<E, Params, R>, | ||
options: FetchHookOptions = {}, | ||
path = '', | ||
) { | ||
const { sourceUrl, debug } = getHeadstartWPConfig(); | ||
|
||
fetchStrategy.setBaseURL(sourceUrl); | ||
|
||
const defaultParams = fetchStrategy.getDefaultParams(); | ||
const urlParams = fetchStrategy.getParamsFromURL(path, params); | ||
const isMainQuery = fetchStrategy.isMainQuery(path, params); | ||
|
||
const finalParams = deepmerge.all([defaultParams, urlParams, params]) as Partial<Params>; | ||
|
||
const { fetchStrategyOptions } = options; | ||
|
||
const endpointUrl = fetchStrategy.buildEndpointURL(finalParams); | ||
const key = fetchStrategy.getCacheKey(finalParams); | ||
|
||
if (debug?.devMode) { | ||
log(LOGTYPE.INFO, `[useFetch] key for ${key.url}`, key); | ||
} | ||
|
||
const fetchData = await fetchStrategy.fetcher(endpointUrl, finalParams, fetchStrategyOptions); | ||
|
||
const { data /* additionalCacheObjects */ } = fetchStrategy.normalizeForCache( | ||
fetchData, | ||
finalParams, | ||
); | ||
|
||
// mutate additiional cache objects | ||
/* if (additionalCacheObjects) { | ||
additionalCacheObjects.forEach(({ key, data }) => { | ||
// mutate(key, data); | ||
}); | ||
} */ | ||
|
||
return { data, params: finalParams, isMainQuery }; | ||
} |
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,65 @@ | ||
import { useFetch } from './useFetch'; | ||
import type { FetchHookOptions, HookResponse } from './types'; | ||
import { | ||
FetchResponse, | ||
getPostAuthor, | ||
getPostTerms, | ||
PostEntity, | ||
PostParams, | ||
SinglePostFetchStrategy, | ||
} from '../../data'; | ||
import { getWPUrl } from '../../utils'; | ||
|
||
export interface usePostResponse<T extends PostEntity = PostEntity> extends HookResponse { | ||
data: { | ||
post: T; | ||
pageInfo: FetchResponse<T>['pageInfo']; | ||
queriedObject: FetchResponse<T>['queriedObject']; | ||
}; | ||
} | ||
|
||
/** | ||
* The useFetchPost hook. Returns a single post entity | ||
* | ||
* See {@link usePost} for usage instructions. | ||
* | ||
* @param params The list of params to pass to the fetch strategy. It overrides the ones in the URL. | ||
* @param options The options to pass to the swr hook. | ||
* @param path The path of the url to get url params from. | ||
* | ||
* @module useFetchPost | ||
* @category Data Fetching Hooks | ||
*/ | ||
export async function useFetchPost< | ||
T extends PostEntity = PostEntity, | ||
P extends PostParams = PostParams, | ||
>(params: P | {} = {}, options: FetchHookOptions = {}, path = ''): Promise<usePostResponse<T>> { | ||
const { data, isMainQuery } = await useFetch<T[], P, T>( | ||
params, | ||
useFetchPost.fetcher<T, P>(), | ||
options, | ||
path, | ||
); | ||
|
||
const post = { | ||
...data.result, | ||
author: getPostAuthor(data.result), | ||
terms: getPostTerms(data.result), | ||
}; | ||
|
||
return { | ||
data: { post, pageInfo: data.pageInfo, queriedObject: data.queriedObject }, | ||
isMainQuery, | ||
}; | ||
} | ||
|
||
/** | ||
* @internal | ||
*/ | ||
// eslint-disable-next-line no-redeclare | ||
export namespace useFetchPost { | ||
export const fetcher = <T extends PostEntity = PostEntity, P extends PostParams = PostParams>( | ||
sourceUrl?: string, | ||
defaultParams?: P, | ||
) => new SinglePostFetchStrategy<T, P>(sourceUrl ?? getWPUrl(), defaultParams); | ||
} |
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 * from './components/BlocksRenderer'; | ||
export * from './hooks'; |
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 @@ | ||
NEXT_PUBLIC_HEADLESS_WP_URL=https://js1.10up.com | ||
NEXT_PUBLIC_HOST_URL=https://js1.10up.com |
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,10 @@ | ||
/** | ||
* Headless Config | ||
* | ||
* @type {import('@headstartwp/core').HeadlessConfig} | ||
*/ | ||
module.exports = { | ||
sourceUrl: process.env.NEXT_PUBLIC_HEADLESS_WP_URL, | ||
|
||
useWordPressPlugin: true, | ||
}; |
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 @@ | ||
const { withHeadstartWPConfig } = require('@headstartwp/next/config'); | ||
|
||
/** @type {import('next').NextConfig} */ | ||
const nextConfig = {}; | ||
|
||
module.exports = withHeadstartWPConfig(nextConfig); |
This file was deleted.
Oops, something went wrong.
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