Skip to main content

Class: SearchNativeFetchStrategy<T, P>

@headstartwp/core.SearchNativeFetchStrategy

The SearchNativeFetchStrategy is used to fetch search results for a given search query Uses the native WordPress search endpoint.

Note that custom post types and custom taxonomies should be defined in headless.config.js

This strategy supports extracting endpoint params from url E.g:

  • /page/2/ maps to { page: 2 }
  • /searched-term/page/2 maps to { search: 'searched-term', page: 2 }

See

getParamsFromURL to learn about url param mapping

Type parameters

NameType
Textends PostSearchEntity | TermSearchEntity = PostSearchEntity | TermSearchEntity
Pextends SearchParams = SearchParams

Hierarchy

Constructors

constructor

new SearchNativeFetchStrategy<T, P>(baseURL?, defaultParams?)

The strategy constructor

Type parameters

NameType
Textends PostSearchEntity | TermSearchEntity = PostSearchEntity | TermSearchEntity
Pextends SearchParams = SearchParams

Parameters

NameTypeDescription
baseURL?stringThe 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

AbstractFetchStrategy.baseURL

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/SearchNativeFetchStrategy.ts:75


path

path: string = ''

Defined in

packages/core/src/data/strategies/SearchNativeFetchStrategy.ts:73

Methods

buildEndpointURL

buildEndpointURL(params): string

Builds the endpoint url for the search endpoint

Parameters

NameTypeDescription
paramsPartial<P>The params for the request

Returns

string

Overrides

AbstractFetchStrategy.buildEndpointURL

Defined in

packages/core/src/data/strategies/SearchNativeFetchStrategy.ts:106


fetcher

fetcher(url, params, options?): Promise<{ isCached?: boolean ; pageInfo: PageInfo ; queriedObject: QueriedObject ; result: T[] }>

The fetcher function is overridden to disable throwing if not found

If a search request returns not found we do not want to redirect to a 404 page, instead the user should be informed that no posts were found

Parameters

NameTypeDescription
urlstringThe url to parse
paramsPartial<P>The params to build the endpoint with
optionsPartial<FetchOptions>FetchOptions

Returns

Promise<{ isCached?: boolean ; pageInfo: PageInfo ; queriedObject: QueriedObject ; result: T[] }>

Overrides

AbstractFetchStrategy.fetcher

Defined in

packages/core/src/data/strategies/SearchNativeFetchStrategy.ts:125


filterData

filterData(data, filterOptions?): FetchResponse<T[]>

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

Parameters

NameTypeDescription
dataFetchResponse<T[]>The data to filter
filterOptions?FilterDataOptions<T[]>Filter options

Returns

FetchResponse<T[]>

The filtered data

Inherited from

AbstractFetchStrategy.filterData

Defined in

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


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

NameTypeDescription
paramsPartial<P>The endpoint params

Returns

Promise<FetchResponse<T[]>>

Inherited from

AbstractFetchStrategy.get

Defined in

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


getAuthHeader

getAuthHeader(options?): string

Parameters

NameType
optionsPartial<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

NameTypeDescription
paramsPartial<P>The request params

Returns

Object

The cache key object

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

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/SearchNativeFetchStrategy.ts:77


getDefaultParams

getDefaultParams(): Partial<P>

Returns

Partial<P>

Overrides

AbstractFetchStrategy.getDefaultParams

Defined in

packages/core/src/data/strategies/SearchNativeFetchStrategy.ts:81


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, params?): Partial<P>

This strategy automatically extracts the search term and and pagination params from the URL

Parameters

NameTypeDescription
pathstringThe URL path to extract params from
paramsPartial<P>The params passed to the strategy

Returns

Partial<P>

Overrides

AbstractFetchStrategy.getParamsFromURL

Defined in

packages/core/src/data/strategies/SearchNativeFetchStrategy.ts:91


getPreviewAuthHeader

getPreviewAuthHeader(options?): string

Parameters

NameType
optionsPartial<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

NameType
optionsPartial<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

NameType
responseFetchResponse<T[]>
paramsPartial<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

NameTypeDescription
pathstringThe page name
nonUrlParamsPartial<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

NameTypeDescription
dataFetchResponse<T[]>The fetch response data
paramsPartial<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[]>

Parameters

NameType
responseFetchResponse<T[]>
paramsPartial<P>

Returns

FetchResponse<T[]>

Inherited from

AbstractFetchStrategy.prepareResponse

Defined in

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


setBaseURL

setBaseURL(url?): void

Parameters

NameTypeDefault value
urlundefined | 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

NameTypeDescription
endpointstringThe endpoint to fetch

Returns

void

Inherited from

AbstractFetchStrategy.setEndpoint

Defined in

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