@urql/exchange-request-policy
Version:
An exchange for operation request-policy upgrading in urql
53 lines (50 loc) • 2.06 kB
TypeScript
import { Exchange, Operation } from '@urql/core';
/** Input parameters for the {@link requestPolicyExchange}. */
interface Options {
/** Predicate allowing you to selectively not upgrade `Operation`s.
*
* @remarks
* When `shouldUpgrade` is set, it may be used to selectively return a boolean
* per `Operation`. This allows certain `Operation`s to not be upgraded to a
* `cache-and-network` policy, when `false` is returned.
*
* By default, all `Operation`s are subject to be upgraded.
* operation to "cache-and-network".
*/
shouldUpgrade?: (op: Operation) => boolean;
/** The time-to-live (TTL) for which a request policy won't be upgraded.
*
* @remarks
* The `ttl` defines the time frame in which the `Operation` won't be updated
* with a `cache-and-network` request policy. If an `Operation` is sent again
* and the `ttl` time period has expired, the policy is upgraded.
*
* @defaultValue `300_000` - 5min
*/
ttl?: number;
}
/** Exchange factory that upgrades request policies to `cache-and-network` for queries outside of a defined `ttl`.
*
* @param options - An {@link Options} configuration object.
* @returns the created request-policy {@link Exchange}.
*
* @remarks
* The `requestPolicyExchange` upgrades query operations based on {@link Options.ttl}.
* The `ttl` defines a timeframe outside of which a query's request policy is set to
* `cache-and-network` to refetch it in the background.
*
* You may define a {@link Options.shouldUpgrade} function to selectively ignore some
* operations by returning `false` there.
*
* @example
* ```ts
* requestPolicyExchange({
* // Upgrade when we haven't seen this operation for 1 second
* ttl: 1000,
* // and only upgrade operations that query the `todos` field.
* shouldUpgrade: op => op.kind === 'query' && op.query.definitions[0].name?.value === 'todos'
* });
* ```
*/
declare const requestPolicyExchange: (options: Options) => Exchange;
export { requestPolicyExchange };