Class: SinglePostFetchStrategy<T, P>
@headstartwp/core.SinglePostFetchStrategy
The SinglePostFetchStrategy is used to fetch a single post entity from any post type.
Note that custom post types should be defined in headless.config.js
This strategy supports extracting endpoint params from url E.g:
/post-name
maps to{ slug: 'post-name'}
/2021/10/20/post-name
maps to{ year: 2021, month: 10, day: 20, slug: 'post-name }
/2021/
maps to{ year: 2021, slug: 'post-name' }
See
getParamsFromURL to learn about url param mapping
Type parameters
Name | Type |
---|---|
T | extends PostEntity = PostEntity |
P | extends PostParams = PostParams |
Hierarchy
AbstractFetchStrategy
<T
[],P
,T
>↳
SinglePostFetchStrategy
Constructors
constructor
• new SinglePostFetchStrategy<T
, P
>(baseURL?
, defaultParams?
)
The strategy constructor
Type parameters
Name | Type |
---|---|
T | extends PostEntity = PostEntity |
P | extends PostParams = PostParams |
Parameters
Name | Type | Description |
---|---|---|
baseURL? | string | The base URL of the API |
defaultParams? | Partial <P > | (optional) list of default params |
Inherited from
AbstractFetchStrategy.constructor
Defined in
packages/core/src/data/strategies/AbstractFetchStrategy.ts:145
Properties
baseURL
• baseURL: string
= ''
The base URL where the API is located
Inherited from
Defined in
packages/core/src/data/strategies/AbstractFetchStrategy.ts:131
defaultParams
• defaultParams: Partial
<P
> = {}
The Default Params
Inherited from
AbstractFetchStrategy.defaultParams
Defined in
packages/core/src/data/strategies/AbstractFetchStrategy.ts:121
endpoint
• endpoint: string
= ''
Holds the current endpoint for the strategy
Inherited from
AbstractFetchStrategy.endpoint
Defined in
packages/core/src/data/strategies/AbstractFetchStrategy.ts:126
locale
• locale: string
= ''
Defined in
packages/core/src/data/strategies/SinglePostFetchStrategy.ts:89
path
• path: string
= ''
Defined in
packages/core/src/data/strategies/SinglePostFetchStrategy.ts:87
postType
• postType: string
= 'post'
Defined in
packages/core/src/data/strategies/SinglePostFetchStrategy.ts:83
revision
• Optional
revision: PostEntity
Defined in
packages/core/src/data/strategies/SinglePostFetchStrategy.ts:85
shouldCheckCurrentPathAgainstPostLink
• shouldCheckCurrentPathAgainstPostLink: boolean
= true
Defined in
packages/core/src/data/strategies/SinglePostFetchStrategy.ts:91
Methods
buildEndpointURL
▸ buildEndpointURL(params
): string
Handlers post types, revisions and fetching by id
Parameters
Name | Type | Description |
---|---|---|
params | P | The params to build the endpoint url |
Returns
string
Overrides
AbstractFetchStrategy.buildEndpointURL
Defined in
packages/core/src/data/strategies/SinglePostFetchStrategy.ts:127
fetcher
▸ fetcher(url
, params
, options?
): Promise
<FetchResponse
<T
>>
Handles fetching by multiple post types, authToken and revisions
Parameters
Name | Type | Description |
---|---|---|
url | string | The url to fetch |
params | P | The params to build the endpoint url |
options | Partial <FetchOptions > | FetchOptions |
Returns
Promise
<FetchResponse
<T
>>
Overrides
Defined in
packages/core/src/data/strategies/SinglePostFetchStrategy.ts:275
filterData
▸ filterData(data
, filterOptions?
): any
Filters the data returned from the API by excluding fields that are not needed in order to reduce payload size.
Parameters
Name | Type | Description |
---|---|---|
data | FetchResponse <PostEntity > | The data to filter |
filterOptions? | FilterDataOptions <PostEntity > | Filter options |
Returns
any
The filtered data
Overrides
AbstractFetchStrategy.filterData
Defined in
packages/core/src/data/strategies/SinglePostFetchStrategy.ts:338
get
▸ get(params?
): Promise
<FetchResponse
<T
>>
This is a simple wrapper to quickly fetch data from the API given a set of params
Usage
import { PostsArchiveFetchStrategy } from '@headstartwp/core';
new PostsArchiveFetchStrategy('http://my-wp-url.com').get({perPage: 10});
Parameters
Name | Type | Description |
---|---|---|
params | Partial <P > | The endpoint params |
Returns
Promise
<FetchResponse
<T
>>
Inherited from
Defined in
packages/core/src/data/strategies/AbstractFetchStrategy.ts:444
getAuthHeader
▸ getAuthHeader(options?
): string
Parameters
Name | Type |
---|---|
options | Partial <FetchOptions > |
Returns
string
Inherited from
AbstractFetchStrategy.getAuthHeader
Defined in
packages/core/src/data/strategies/AbstractFetchStrategy.ts:258
getCacheKey
▸ getCacheKey(params
): Object
Returns the cache key with both the endpoint and the sourceUrl to distinguish between multiple sites
Parameters
Name | Type | Description |
---|---|---|
params | Partial <P > | The request params |
Returns
Object
The cache key object
Name | Type |
---|---|
args | Partial <P > & { sourceUrl : string } |
url | string |
Inherited from
AbstractFetchStrategy.getCacheKey
Defined in
packages/core/src/data/strategies/AbstractFetchStrategy.ts:409
getDefaultEndpoint
▸ getDefaultEndpoint(): string
A method that must be implemented by concrete implementations which returns the default endpoint for the strategy
Returns
string
Overrides
AbstractFetchStrategy.getDefaultEndpoint
Defined in
packages/core/src/data/strategies/SinglePostFetchStrategy.ts:93
getDefaultParams
▸ getDefaultParams(): Partial
<P
>
Returns
Partial
<P
>
Overrides
AbstractFetchStrategy.getDefaultParams
Defined in
packages/core/src/data/strategies/SinglePostFetchStrategy.ts:97
getEndpoint
▸ getEndpoint(): string
Returns the endpoint of the strategy. If no endpoint has been set at runtime, returns the default endpoint
Returns
string
The current endpoint for the strategy
Inherited from
AbstractFetchStrategy.getEndpoint
Defined in
packages/core/src/data/strategies/AbstractFetchStrategy.ts:176
getParamsFromURL
▸ getParamsFromURL(path
, nonUrlParams?
): Partial
<P
>
Returns the supported params from the URL if present.
These params are passed to buildEndpointURL
. If the strategy does not support
mapping url params to endpoint params, it should return an empty object.
Parameters
Name | Type | Description |
---|---|---|
path | string | The Path name |
nonUrlParams | Partial <P > | The non-url params |
Returns
Partial
<P
>
params extracted from the URL
Overrides
AbstractFetchStrategy.getParamsFromURL
Defined in
packages/core/src/data/strategies/SinglePostFetchStrategy.ts:102
getPostThatMatchesCurrentPath
▸ getPostThatMatchesCurrentPath(result
, params
): undefined
| T
Returns only the post that matches the current path
Parameters
Name | Type |
---|---|
result | T [] |
params | Partial <P > |
Returns
undefined
| T
Defined in
packages/core/src/data/strategies/SinglePostFetchStrategy.ts:166
getPreviewAuthHeader
▸ getPreviewAuthHeader(options?
): string
Parameters
Name | Type |
---|---|
options | Partial <FetchOptions > |
Returns
string
Inherited from
AbstractFetchStrategy.getPreviewAuthHeader
Defined in
packages/core/src/data/strategies/AbstractFetchStrategy.ts:249
getPreviewHeaderName
▸ getPreviewHeaderName(options?
): "X-HeadstartWP-Authorization"
| "Authorization"
Parameters
Name | Type |
---|---|
options | Partial <FetchOptions > |
Returns
"X-HeadstartWP-Authorization"
| "Authorization"
Inherited from
AbstractFetchStrategy.getPreviewHeaderName
Defined in
packages/core/src/data/strategies/AbstractFetchStrategy.ts:243
getQueriedObject
▸ getQueriedObject(response
, params
): Object
Parameters
Name | Type |
---|---|
response | FetchResponse <T []> |
params | Partial <P > |
Returns
Object
Inherited from
AbstractFetchStrategy.getQueriedObject
Defined in
packages/core/src/data/strategies/AbstractFetchStrategy.ts:367
isMainQuery
▸ isMainQuery(path
, nonUrlParams
): boolean
Checks if this is the main query for a page
Parameters
Name | Type | Description |
---|---|---|
path | string | The page name |
nonUrlParams | Partial <P > | The non-url params |
Returns
boolean
Inherited from
AbstractFetchStrategy.isMainQuery
Defined in
packages/core/src/data/strategies/AbstractFetchStrategy.ts:207
normalizeForCache
▸ normalizeForCache(data
, params
): NormalizedDataForCache
<T
, P
>
Normalize data for cache.
Parameters
Name | Type | Description |
---|---|---|
data | FetchResponse <T > | The fetch response data |
params | Partial <P > | The request params |
Returns
NormalizedDataForCache
<T
, P
>
Inherited from
AbstractFetchStrategy.normalizeForCache
Defined in
packages/core/src/data/strategies/AbstractFetchStrategy.ts:419
prepareResponse
▸ prepareResponse(response
, params
): FetchResponse
<T
>
Prepares the post response
Parameters
Name | Type |
---|---|
response | FetchResponse <T | T []> |
params | Partial <P > |
Returns
Overrides
AbstractFetchStrategy.prepareResponse
Defined in
packages/core/src/data/strategies/SinglePostFetchStrategy.ts:205
setBaseURL
▸ setBaseURL(url?
): void
Parameters
Name | Type | Default value |
---|---|---|
url | undefined | string | '' |
Returns
void
Inherited from
AbstractFetchStrategy.setBaseURL
Defined in
packages/core/src/data/strategies/AbstractFetchStrategy.ts:166
setEndpoint
▸ setEndpoint(endpoint
): void
The strategy can switch endpoints at runtime if needed.
E.g: The actual endpoint for a post depends on its post_type
Parameters
Name | Type | Description |
---|---|---|
endpoint | string | The endpoint to fetch |
Returns
void
Inherited from
AbstractFetchStrategy.setEndpoint
Defined in
packages/core/src/data/strategies/AbstractFetchStrategy.ts:162