Skip to main content

Class: SinglePostFetchStrategy<T, P>

Defined in: packages/core/src/data/strategies/SinglePostFetchStrategy.ts:80

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

Extends

Type Parameters

T

T extends PostEntity = PostEntity

P

P extends PostParams = PostParams

Constructors

Constructor

new SinglePostFetchStrategy<T, P>(baseURL?, defaultParams?): SinglePostFetchStrategy<T, P>

Defined in: packages/core/src/data/strategies/AbstractFetchStrategy.ts:169

The strategy constructor

Parameters

baseURL?

string

The base URL of the API

defaultParams?

Partial<P>

(optional) list of default params

Returns

SinglePostFetchStrategy<T, P>

Inherited from

AbstractFetchStrategy.constructor

Properties

baseURL

baseURL: string = ''

Defined in: packages/core/src/data/strategies/AbstractFetchStrategy.ts:155

The base URL where the API is located

Inherited from

AbstractFetchStrategy.baseURL


defaultParams

defaultParams: Partial<Params> = {}

Defined in: packages/core/src/data/strategies/AbstractFetchStrategy.ts:145

The Default Params

Inherited from

AbstractFetchStrategy.defaultParams


endpoint

endpoint: string = ''

Defined in: packages/core/src/data/strategies/AbstractFetchStrategy.ts:150

Holds the current endpoint for the strategy

Inherited from

AbstractFetchStrategy.endpoint


locale

locale: string = ''

Defined in: packages/core/src/data/strategies/SinglePostFetchStrategy.ts:90


optimizeYoastPayload

optimizeYoastPayload: boolean = false

Defined in: packages/core/src/data/strategies/SinglePostFetchStrategy.ts:94


path

path: string = ''

Defined in: packages/core/src/data/strategies/SinglePostFetchStrategy.ts:88


postType

postType: string = 'post'

Defined in: packages/core/src/data/strategies/SinglePostFetchStrategy.ts:84


revision?

optional revision: PostEntity

Defined in: packages/core/src/data/strategies/SinglePostFetchStrategy.ts:86


shouldCheckCurrentPathAgainstPostLink: boolean = true

Defined in: packages/core/src/data/strategies/SinglePostFetchStrategy.ts:92

Methods

buildEndpointURL()

buildEndpointURL(params): string

Defined in: packages/core/src/data/strategies/SinglePostFetchStrategy.ts:132

Handlers post types, revisions and fetching by id

Parameters

params

P

The params to build the endpoint url

Returns

string

Overrides

AbstractFetchStrategy.buildEndpointURL


fetcher()

fetcher(url, params, options): Promise<FetchResponse<T>>

Defined in: packages/core/src/data/strategies/SinglePostFetchStrategy.ts:295

Handles fetching by multiple post types, authToken and revisions

Parameters

url

string

The url to fetch

params

P

The params to build the endpoint url

options

Partial<FetchOptions> = {}

FetchOptions

Returns

Promise<FetchResponse<T>>

Overrides

AbstractFetchStrategy.fetcher


filterData()

filterData(data, filterOptions?): any

Defined in: packages/core/src/data/strategies/SinglePostFetchStrategy.ts:366

Filters the data returned from the API by excluding fields that are not needed in order to reduce payload size.

Parameters

data

FetchResponse<PostEntity>

The data to filter

filterOptions?

FilterDataOptions<PostEntity>

Filter options

Returns

any

The filtered data

Overrides

AbstractFetchStrategy.filterData


get()

get(params): Promise<FetchResponse<T>>

Defined in: packages/core/src/data/strategies/AbstractFetchStrategy.ts:477

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

params

Partial<Params> = {}

The endpoint params

Returns

Promise<FetchResponse<T>>

Inherited from

AbstractFetchStrategy.get


getAuthHeader()

getAuthHeader(options): string

Defined in: packages/core/src/data/strategies/AbstractFetchStrategy.ts:282

Parameters

options

Partial<FetchOptions> = {}

Returns

string

Inherited from

AbstractFetchStrategy.getAuthHeader


getCacheKey()

getCacheKey(params): object

Defined in: packages/core/src/data/strategies/AbstractFetchStrategy.ts:442

Returns the cache key with both the endpoint and the sourceUrl to distinguish between multiple sites

Parameters

params

Partial<Params>

The request params

Returns

object

The cache key object

args

args: Partial<P> & object

Type declaration
sourceUrl

sourceUrl: string

url

url: string

Inherited from

AbstractFetchStrategy.getCacheKey


getDefaultEndpoint()

getDefaultEndpoint(): string

Defined in: packages/core/src/data/strategies/SinglePostFetchStrategy.ts:96

A method that must be implemented by concrete implementations which returns the default endpoint for the strategy

Returns

string

Overrides

AbstractFetchStrategy.getDefaultEndpoint


getDefaultParams()

getDefaultParams(): Partial<P>

Defined in: packages/core/src/data/strategies/SinglePostFetchStrategy.ts:100

Returns

Partial<P>

Overrides

AbstractFetchStrategy.getDefaultParams


getEndpoint()

getEndpoint(): string

Defined in: packages/core/src/data/strategies/AbstractFetchStrategy.ts:200

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


getParamsFromURL()

getParamsFromURL(path, nonUrlParams): Partial<P>

Defined in: packages/core/src/data/strategies/SinglePostFetchStrategy.ts:105

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

path

string

The Path name

nonUrlParams

Partial<P> = {}

The non-url params

Returns

Partial<P>

params extracted from the URL

Overrides

AbstractFetchStrategy.getParamsFromURL


getPostThatMatchesCurrentPath()

getPostThatMatchesCurrentPath(result, params): undefined | T

Defined in: packages/core/src/data/strategies/SinglePostFetchStrategy.ts:171

Returns only the post that matches the current path

Parameters

result

T[]

params

Partial<P>

Returns

undefined | T


getPreviewAuthHeader()

getPreviewAuthHeader(options): string

Defined in: packages/core/src/data/strategies/AbstractFetchStrategy.ts:273

Parameters

options

Partial<FetchOptions> = {}

Returns

string

Inherited from

AbstractFetchStrategy.getPreviewAuthHeader


getPreviewHeaderName()

getPreviewHeaderName(options): "X-HeadstartWP-Authorization" | "Authorization"

Defined in: packages/core/src/data/strategies/AbstractFetchStrategy.ts:267

Parameters

options

Partial<FetchOptions> = {}

Returns

"X-HeadstartWP-Authorization" | "Authorization"

Inherited from

AbstractFetchStrategy.getPreviewHeaderName


getQueriedObject()

getQueriedObject(response, params): object

Defined in: packages/core/src/data/strategies/AbstractFetchStrategy.ts:400

Parameters

response

FetchResponse<T[]>

params

Partial<Params>

Returns

object

Inherited from

AbstractFetchStrategy.getQueriedObject


isMainQuery()

isMainQuery(path, nonUrlParams): boolean

Defined in: packages/core/src/data/strategies/AbstractFetchStrategy.ts:231

Checks if this is the main query for a page

Parameters

path

string

The page name

nonUrlParams

Partial<Params>

The non-url params

Returns

boolean

Inherited from

AbstractFetchStrategy.isMainQuery


normalizeForCache()

normalizeForCache(data, params): NormalizedDataForCache<T, P>

Defined in: packages/core/src/data/strategies/AbstractFetchStrategy.ts:452

Normalize data for cache.

Parameters

data

FetchResponse<T>

The fetch response data

params

Partial<Params>

The request params

Returns

NormalizedDataForCache<T, P>

Inherited from

AbstractFetchStrategy.normalizeForCache


prepareResponse()

prepareResponse(response, params): FetchResponse<T>

Defined in: packages/core/src/data/strategies/SinglePostFetchStrategy.ts:225

Prepares the post response

Parameters

response

FetchResponse<T | T[]>

params

Partial<P>

Returns

FetchResponse<T>

Overrides

AbstractFetchStrategy.prepareResponse


setBaseURL()

setBaseURL(url): void

Defined in: packages/core/src/data/strategies/AbstractFetchStrategy.ts:190

Parameters

url

undefined | string

Returns

void

Inherited from

AbstractFetchStrategy.setBaseURL


setEndpoint()

setEndpoint(endpoint): void

Defined in: packages/core/src/data/strategies/AbstractFetchStrategy.ts:186

The strategy can switch endpoints at runtime if needed.

E.g: The actual endpoint for a post depends on its post_type

Parameters

endpoint

string

The endpoint to fetch

Returns

void

Inherited from

AbstractFetchStrategy.setEndpoint