@podium/client
Version:
Client for fetching podium component fragments over HTTP.
120 lines (119 loc) • 4.93 kB
TypeScript
/**
* @typedef {import('./resource.js').default} PodiumClientResource
* @typedef {import('./resource.js').PodiumClientResourceOptions} PodiumClientResourceOptions
* @typedef {import('./response.js').default} PodiumClientResponse
* @typedef {import('./http-outgoing.js').PodiumRedirect} PodiumRedirect
* @typedef {import('@podium/schemas').PodletManifestSchema} PodletManifest
*/
/**
* @typedef {object} PodiumClientOptions
* @property {string} name
* @property {import('abslog').AbstractLoggerOptions} [logger]
* @property {number} [retries=4]
* @property {number} [timeout=1000] In milliseconds
* @property {number} [maxAge=Infinity]
* @property {boolean} [rejectUnauthorized=true]
* @property {number} [resolveThreshold]
* @property {number} [resolveMax]
* @property {import('http').Agent} [httpAgent]
* @property {import('https').Agent} [httpsAgent]
*/
/**
* @typedef {object} RegisterOptions
* @property {string} name A unique name for the podlet
* @property {string} uri URL to the podlet's `manifest.json`
* @property {number} [retries=4] Number of retries before serving fallback
* @property {number} [timeout=1000] In milliseconds, the amount of time to wait before serving fallback.
* @property {boolean} [throwable=false] Set to `true` and surround `fetch` in `try/catch` to serve different content in case podlet is unavailable. Will not server fallback content.
* @property {boolean} [redirectable=false] Set to `true` to allow podlet to respond with a redirect. You need to look for the redirect response from the podlet and return a redirect response to the browser yourself.
* @property {import('./resource.js').RequestFilterOptions} [excludeBy] Used by `fetch` to conditionally skip fetching the podlet content based on values on the request.
* @property {import('./resource.js').RequestFilterOptions} [includeBy] Used by `fetch` to conditionally skip fetching the podlet content based on values on the request.
*/
export default class PodiumClient extends EventEmitter<[never]> {
/**
* @constructor
* @param {PodiumClientOptions} options
*/
constructor(options?: PodiumClientOptions);
get registry(): any;
get metrics(): Metrics;
get state(): "instantiated" | "stable" | "initializing" | "unhealthy" | "unstable";
/**
* Register a podlet so you can fetch its contents later with {@link PodiumClientResource.fetch}.
*
* @param {RegisterOptions} options
* @returns {PodiumClientResource}
*
* @example
* ```js
* const headerPodlet = layout.client.register({
* name: 'header',
* uri: 'http://header/manifest.json',
* });
* ```
*/
register(options: RegisterOptions): PodiumClientResource;
dump(): any;
load(dump: any): any;
/**
* Refreshes the cached podlet manifest for all {@link register}ed podlets.
*/
refreshManifests(): Promise<void>;
#private;
}
export type PodiumClientResource = import("./resource.js").default;
export type PodiumClientResourceOptions = import("./resource.js").PodiumClientResourceOptions;
export type PodiumClientResponse = import("./response.js").default;
export type PodiumRedirect = import("./http-outgoing.js").PodiumRedirect;
export type PodletManifest = import("@podium/schemas").PodletManifestSchema;
export type PodiumClientOptions = {
name: string;
logger?: import("abslog").AbstractLoggerOptions;
retries?: number;
/**
* In milliseconds
*/
timeout?: number;
maxAge?: number;
rejectUnauthorized?: boolean;
resolveThreshold?: number;
resolveMax?: number;
httpAgent?: import("http").Agent;
httpsAgent?: import("https").Agent;
};
export type RegisterOptions = {
/**
* A unique name for the podlet
*/
name: string;
/**
* URL to the podlet's `manifest.json`
*/
uri: string;
/**
* Number of retries before serving fallback
*/
retries?: number;
/**
* In milliseconds, the amount of time to wait before serving fallback.
*/
timeout?: number;
/**
* Set to `true` and surround `fetch` in `try/catch` to serve different content in case podlet is unavailable. Will not server fallback content.
*/
throwable?: boolean;
/**
* Set to `true` to allow podlet to respond with a redirect. You need to look for the redirect response from the podlet and return a redirect response to the browser yourself.
*/
redirectable?: boolean;
/**
* Used by `fetch` to conditionally skip fetching the podlet content based on values on the request.
*/
excludeBy?: import("./resource.js").RequestFilterOptions;
/**
* Used by `fetch` to conditionally skip fetching the podlet content based on values on the request.
*/
includeBy?: import("./resource.js").RequestFilterOptions;
};
import EventEmitter from 'events';
import Metrics from '@metrics/client';