Strategy.d.ts 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. import { HandlerCallbackOptions, RouteHandlerObject, WorkboxPlugin } from 'workbox-core/types.js';
  2. import { StrategyHandler } from './StrategyHandler.js';
  3. import './_version.js';
  4. export interface StrategyOptions {
  5. cacheName?: string;
  6. plugins?: WorkboxPlugin[];
  7. fetchOptions?: RequestInit;
  8. matchOptions?: CacheQueryOptions;
  9. }
  10. /**
  11. * An abstract base class that all other strategy classes must extend from:
  12. *
  13. * @memberof workbox-strategies
  14. */
  15. declare abstract class Strategy implements RouteHandlerObject {
  16. cacheName: string;
  17. plugins: WorkboxPlugin[];
  18. fetchOptions?: RequestInit;
  19. matchOptions?: CacheQueryOptions;
  20. protected abstract _handle(request: Request, handler: StrategyHandler): Promise<Response | undefined>;
  21. /**
  22. * Creates a new instance of the strategy and sets all documented option
  23. * properties as public instance properties.
  24. *
  25. * Note: if a custom strategy class extends the base Strategy class and does
  26. * not need more than these properties, it does not need to define its own
  27. * constructor.
  28. *
  29. * @param {Object} [options]
  30. * @param {string} [options.cacheName] Cache name to store and retrieve
  31. * requests. Defaults to the cache names provided by
  32. * {@link workbox-core.cacheNames}.
  33. * @param {Array<Object>} [options.plugins] [Plugins]{@link https://developers.google.com/web/tools/workbox/guides/using-plugins}
  34. * to use in conjunction with this caching strategy.
  35. * @param {Object} [options.fetchOptions] Values passed along to the
  36. * [`init`](https://developer.mozilla.org/en-US/docs/Web/API/WindowOrWorkerGlobalScope/fetch#Parameters)
  37. * of [non-navigation](https://github.com/GoogleChrome/workbox/issues/1796)
  38. * `fetch()` requests made by this strategy.
  39. * @param {Object} [options.matchOptions] The
  40. * [`CacheQueryOptions`]{@link https://w3c.github.io/ServiceWorker/#dictdef-cachequeryoptions}
  41. * for any `cache.match()` or `cache.put()` calls made by this strategy.
  42. */
  43. constructor(options?: StrategyOptions);
  44. /**
  45. * Perform a request strategy and returns a `Promise` that will resolve with
  46. * a `Response`, invoking all relevant plugin callbacks.
  47. *
  48. * When a strategy instance is registered with a Workbox
  49. * {@link workbox-routing.Route}, this method is automatically
  50. * called when the route matches.
  51. *
  52. * Alternatively, this method can be used in a standalone `FetchEvent`
  53. * listener by passing it to `event.respondWith()`.
  54. *
  55. * @param {FetchEvent|Object} options A `FetchEvent` or an object with the
  56. * properties listed below.
  57. * @param {Request|string} options.request A request to run this strategy for.
  58. * @param {ExtendableEvent} options.event The event associated with the
  59. * request.
  60. * @param {URL} [options.url]
  61. * @param {*} [options.params]
  62. */
  63. handle(options: FetchEvent | HandlerCallbackOptions): Promise<Response>;
  64. /**
  65. * Similar to {@link workbox-strategies.Strategy~handle}, but
  66. * instead of just returning a `Promise` that resolves to a `Response` it
  67. * it will return an tuple of `[response, done]` promises, where the former
  68. * (`response`) is equivalent to what `handle()` returns, and the latter is a
  69. * Promise that will resolve once any promises that were added to
  70. * `event.waitUntil()` as part of performing the strategy have completed.
  71. *
  72. * You can await the `done` promise to ensure any extra work performed by
  73. * the strategy (usually caching responses) completes successfully.
  74. *
  75. * @param {FetchEvent|Object} options A `FetchEvent` or an object with the
  76. * properties listed below.
  77. * @param {Request|string} options.request A request to run this strategy for.
  78. * @param {ExtendableEvent} options.event The event associated with the
  79. * request.
  80. * @param {URL} [options.url]
  81. * @param {*} [options.params]
  82. * @return {Array<Promise>} A tuple of [response, done]
  83. * promises that can be used to determine when the response resolves as
  84. * well as when the handler has completed all its work.
  85. */
  86. handleAll(options: FetchEvent | HandlerCallbackOptions): [Promise<Response>, Promise<void>];
  87. _getResponse(handler: StrategyHandler, request: Request, event: ExtendableEvent): Promise<Response>;
  88. _awaitComplete(responseDone: Promise<Response>, handler: StrategyHandler, request: Request, event: ExtendableEvent): Promise<void>;
  89. }
  90. export { Strategy };
  91. /**
  92. * Classes extending the `Strategy` based class should implement this method,
  93. * and leverage the {@link workbox-strategies.StrategyHandler}
  94. * arg to perform all fetching and cache logic, which will ensure all relevant
  95. * cache, cache options, fetch options and plugins are used (per the current
  96. * strategy instance).
  97. *
  98. * @name _handle
  99. * @instance
  100. * @abstract
  101. * @function
  102. * @param {Request} request
  103. * @param {workbox-strategies.StrategyHandler} handler
  104. * @return {Promise<Response>}
  105. *
  106. * @memberof workbox-strategies.Strategy
  107. */