Skip to main content

Class: AbstractFetchStrategy<E, Params, R>

@headstartwp/core.AbstractFetchStrategy

Abstract class that lays out a strategy for fetching data

All Fetch Strategies should implement this class and it allows to share logic for fetching data both on the front-end and on the back-end.

Type parameters

NameTypeDescription
EEThe type of entity that is fetched (e.g PostEntity, TermEntity etc)
Paramsextends EndpointParamsThe type of the params that are passed to the endpoint
RE-

Hierarchy

Constructors

constructor

new AbstractFetchStrategy<E, Params, R>(baseURL?, defaultParams?)

The strategy constructor

Type parameters

NameType
EE
Paramsextends EndpointParams
RE

Parameters

NameTypeDescription
baseURL?stringThe base URL of the API
defaultParams?Partial<Params>(optional) list of default params

Defined in

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

Properties

baseURL

baseURL: string = ''

The base URL where the API is located

Defined in

packages/core/src/data/strategies/AbstractFetchStrategy.ts:131


defaultParams

defaultParams: Partial<Params> = {}

The Default Params

Defined in

packages/core/src/data/strategies/AbstractFetchStrategy.ts:121


endpoint

endpoint: string = ''

Holds the current endpoint for the strategy

Defined in

packages/core/src/data/strategies/AbstractFetchStrategy.ts:126

Methods

buildEndpointURL

buildEndpointURL(params): string

Builds the final endpoint URL based on the passed parameters

Parameters

NameTypeDescription
paramsPartial<Params>The params to add to the request

Returns

string

The endpoint URL.

Defined in

packages/core/src/data/strategies/AbstractFetchStrategy.ts:222


fetcher

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

The default fetcher function

The default fetcher function handles authentication headers and errors from the API.

A custom strategy can override this function to run additional logic before or after the fetch call

Parameters

NameTypeDescription
urlstringThe URL to fetch
paramsPartial<Params>The request params
optionsPartial<FetchOptions>The fetcher options

Returns

Promise<FetchResponse<R>>

Defined in

packages/core/src/data/strategies/AbstractFetchStrategy.ts:292


filterData

filterData(data, filterOptions?): FetchResponse<R>

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

Parameters

NameTypeDescription
dataFetchResponse<R>The data to filter
filterOptions?FilterDataOptions<R>Filter options

Returns

FetchResponse<R>

The filtered data

Defined in

packages/core/src/data/strategies/AbstractFetchStrategy.ts:379


get

get(params?): Promise<FetchResponse<R>>

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

NameTypeDescription
paramsPartial<Params>The endpoint params

Returns

Promise<FetchResponse<R>>

Defined in

packages/core/src/data/strategies/AbstractFetchStrategy.ts:444


getAuthHeader

getAuthHeader(options?): string

Parameters

NameType
optionsPartial<FetchOptions>

Returns

string

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

NameTypeDescription
paramsPartial<Params>The request params

Returns

Object

The cache key object

NameType
argsPartial<Params> & { sourceUrl: string }
urlstring

Defined in

packages/core/src/data/strategies/AbstractFetchStrategy.ts:409


getDefaultEndpoint

Abstract getDefaultEndpoint(): string

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

Returns

string

Defined in

packages/core/src/data/strategies/AbstractFetchStrategy.ts:137


getDefaultParams

getDefaultParams(): Partial<Params>

Returns

Partial<Params>

Defined in

packages/core/src/data/strategies/AbstractFetchStrategy.ts:184


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

Defined in

packages/core/src/data/strategies/AbstractFetchStrategy.ts:176


getParamsFromURL

Abstract getParamsFromURL(path, nonUrlParams): Partial<Params>

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

NameTypeDescription
pathstringThe Path name
nonUrlParamsPartial<Params>The non-url params

Returns

Partial<Params>

params extracted from the URL

Defined in

packages/core/src/data/strategies/AbstractFetchStrategy.ts:199


getPreviewAuthHeader

getPreviewAuthHeader(options?): string

Parameters

NameType
optionsPartial<FetchOptions>

Returns

string

Defined in

packages/core/src/data/strategies/AbstractFetchStrategy.ts:249


getPreviewHeaderName

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

Parameters

NameType
optionsPartial<FetchOptions>

Returns

"X-HeadstartWP-Authorization" | "Authorization"

Defined in

packages/core/src/data/strategies/AbstractFetchStrategy.ts:243


getQueriedObject

getQueriedObject(response, params): Object

Parameters

NameType
responseFetchResponse<E>
paramsPartial<Params>

Returns

Object

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

NameTypeDescription
pathstringThe page name
nonUrlParamsPartial<Params>The non-url params

Returns

boolean

Defined in

packages/core/src/data/strategies/AbstractFetchStrategy.ts:207


normalizeForCache

normalizeForCache(data, params): NormalizedDataForCache<R, Params>

Normalize data for cache.

Parameters

NameTypeDescription
dataFetchResponse<R>The fetch response data
paramsPartial<Params>The request params

Returns

NormalizedDataForCache<R, Params>

Defined in

packages/core/src/data/strategies/AbstractFetchStrategy.ts:419


prepareResponse

prepareResponse(response, params): FetchResponse<R>

Parameters

NameType
responseFetchResponse<E>
paramsPartial<Params>

Returns

FetchResponse<R>

Defined in

packages/core/src/data/strategies/AbstractFetchStrategy.ts:235


setBaseURL

setBaseURL(url?): void

Parameters

NameTypeDefault value
urlundefined | string''

Returns

void

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

NameTypeDescription
endpointstringThe endpoint to fetch

Returns

void

Defined in

packages/core/src/data/strategies/AbstractFetchStrategy.ts:162