import { PageInfo, QueriedObject } from '../types/index.js'; /** * The base interface for defining endpoint parameters * * Strategies should define type with the actually supported EndPointParams */ export interface EndpointParams { /** * The _embed param returned associated entities in the response * * It's recommended to avoid additional requests to fetch data */ _embed?: boolean; /** * The _fields parameter is used to return only the specified fields in the response */ _fields?: string[]; /** * The polylang ?lang parameter. * * This is only used if the polylang integration is enabled */ lang?: string; /** * The custom parameter to optimize the Yoast payload. * * This is only used if the YoastSEO integration is enabled */ optimizeYoastPayload?: boolean; [k: string]: unknown; } /** * The type of the fetch response */ export interface FetchResponse { isCached?: boolean; /** * Contains the actual data returned from the API */ result: T; /** * Contains pagination information */ pageInfo: PageInfo; /** * Queried Object information */ queriedObject: QueriedObject; } type NextJSFetchOptions = { revalidate?: false | 0 | number; tags?: string[]; }; /** * The options supported by the default fetcher method */ export interface FetchOptions { /** * Whether to thrown an exception if data is not found. */ throwIfNotFound: boolean; /** * The authentication token to use for the request. */ bearerToken?: string; /** * The preview token to use for the request. * * These are tokens issued by the HeadstartWP plugin and is used to authenticate previews */ previewToken?: string; /** * Flag to enable using the alternative authorization header. * * This can be useful if you have separate authentication on your project. */ alternativePreviewAuthorizationHeader?: boolean; /** * Whether to burst cache by appending a timestamp to the query */ burstCache?: boolean; /** * Cache option */ cache?: 'no-store' | 'force-cache'; /** * Headers to sent to fetch */ headers?: Record; next?: NextJSFetchOptions; } export interface FilterDataOptions { /** * If method is 'ALLOW' then only the fields specified in the filter will be returned. * If method is 'REMOVE' then the fields specified in the filter will be removed. */ method: 'ALLOW' | 'REMOVE'; fields: (keyof T)[]; } export type NormalizedDataForCache = { key: { url: string; args: Partial

; }; data: FetchResponse; additionalCacheObjects?: NormalizedDataForCache[]; }; /** * 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. * * @template E The type of entity that is fetched (e.g PostEntity, TermEntity etc) * @template Params The type of the params that are passed to the endpoint * * @category Data Fetching */ export declare abstract class AbstractFetchStrategy { /** * The Default Params */ defaultParams: Partial; /** * Holds the current endpoint for the strategy */ endpoint: string; /** * The base URL where the API is located */ baseURL: string; /** * A method that must be implemented by concrete implementations which returns the default endpoint * for the strategy */ abstract getDefaultEndpoint(): string; /** * The strategy constructor * * @param baseURL The base URL of the API * @param defaultParams (optional) list of default params */ constructor(baseURL?: string, defaultParams?: Partial); /** * The strategy can switch endpoints at runtime if needed. * * E.g: The actual endpoint for a post depends on its post_type * * @param endpoint The endpoint to fetch */ setEndpoint(endpoint: string): void; setBaseURL(url?: string | undefined): void; /** * Returns the endpoint of the strategy. If no endpoint has been set at runtime, * returns the default endpoint * * @returns The current endpoint for the strategy */ getEndpoint(): string; getDefaultParams(): Partial; /** * 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. * * @param path The Path name * @param nonUrlParams The non-url params * * @returns params extracted from the URL */ abstract getParamsFromURL(path: string, nonUrlParams: Partial): Partial; /** * Checks if this is the main query for a page * * @param path The page name * @param nonUrlParams The non-url params */ isMainQuery(path: string, nonUrlParams: Partial): boolean; /** * Builds the final endpoint URL based on the passed parameters * * @param params The params to add to the request * * @returns The endpoint URL. */ buildEndpointURL(params: Partial): string; prepareResponse(response: FetchResponse, params: Partial): FetchResponse; getPreviewHeaderName(options?: Partial): "X-HeadstartWP-Authorization" | "Authorization"; getPreviewAuthHeader(options?: Partial): string; getAuthHeader(options?: Partial): string; /** * 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 * * @param url The URL to fetch * @param params The request params * * @param options The fetcher options */ fetcher(url: string, params: Partial, options?: Partial): Promise>; getQueriedObject(response: FetchResponse, params: Partial): {}; /** * Filters the data returned from the API by excluding fields that are not needed in order to reduce * payload size. * * @param data The data to filter * @param filterOptions Filter options * @returns The filtered data */ filterData(data: FetchResponse, filterOptions?: FilterDataOptions): FetchResponse; /** * Returns the cache key with both the endpoint and the sourceUrl to distinguish between multiple sites * * @param params The request params * * @returns The cache key object */ getCacheKey(params: Partial): { url: string; args: Partial & { sourceUrl: string; }; }; /** * Normalize data for cache. * * @param data The fetch response data * @param params The request params */ normalizeForCache(data: FetchResponse, params: Partial): NormalizedDataForCache; /** * This is a simple wrapper to quickly fetch data from the API given a set of params * * ## Usage * * ```tsx * import { PostsArchiveFetchStrategy } from '@headstartwp/core'; * * new PostsArchiveFetchStrategy('http://my-wp-url.com').get({perPage: 10}); * ``` * * @param params The endpoint params * * @returns */ get(params?: Partial): Promise>; } export {};