Router.d.ts 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. import { Route } from './Route.js';
  2. import { HTTPMethod } from './utils/constants.js';
  3. import { Handler, HandlerCallbackOptions } from './_types.js';
  4. import './_version.js';
  5. /**
  6. * The Router can be used to process a FetchEvent through one or more
  7. * [Routes]{@link module:workbox-routing.Route} responding with a Request if
  8. * a matching route exists.
  9. *
  10. * If no route matches a given a request, the Router will use a "default"
  11. * handler if one is defined.
  12. *
  13. * Should the matching Route throw an error, the Router will use a "catch"
  14. * handler if one is defined to gracefully deal with issues and respond with a
  15. * Request.
  16. *
  17. * If a request matches multiple routes, the **earliest** registered route will
  18. * be used to respond to the request.
  19. *
  20. * @memberof module:workbox-routing
  21. */
  22. declare class Router {
  23. private readonly _routes;
  24. private _defaultHandler?;
  25. private _catchHandler?;
  26. /**
  27. * Initializes a new Router.
  28. */
  29. constructor();
  30. /**
  31. * @return {Map<string, Array<module:workbox-routing.Route>>} routes A `Map` of HTTP
  32. * method name ('GET', etc.) to an array of all the corresponding `Route`
  33. * instances that are registered.
  34. */
  35. get routes(): Map<HTTPMethod, Route[]>;
  36. /**
  37. * Adds a fetch event listener to respond to events when a route matches
  38. * the event's request.
  39. */
  40. addFetchListener(): void;
  41. /**
  42. * Adds a message event listener for URLs to cache from the window.
  43. * This is useful to cache resources loaded on the page prior to when the
  44. * service worker started controlling it.
  45. *
  46. * The format of the message data sent from the window should be as follows.
  47. * Where the `urlsToCache` array may consist of URL strings or an array of
  48. * URL string + `requestInit` object (the same as you'd pass to `fetch()`).
  49. *
  50. * ```
  51. * {
  52. * type: 'CACHE_URLS',
  53. * payload: {
  54. * urlsToCache: [
  55. * './script1.js',
  56. * './script2.js',
  57. * ['./script3.js', {mode: 'no-cors'}],
  58. * ],
  59. * },
  60. * }
  61. * ```
  62. */
  63. addCacheListener(): void;
  64. /**
  65. * Apply the routing rules to a FetchEvent object to get a Response from an
  66. * appropriate Route's handler.
  67. *
  68. * @param {Object} options
  69. * @param {Request} options.request The request to handle (this is usually
  70. * from a fetch event, but it does not have to be).
  71. * @param {FetchEvent} [options.event] The event that triggered the request,
  72. * if applicable.
  73. * @return {Promise<Response>|undefined} A promise is returned if a
  74. * registered route can handle the request. If there is no matching
  75. * route and there's no `defaultHandler`, `undefined` is returned.
  76. */
  77. handleRequest({ request, event }: {
  78. request: Request;
  79. event?: ExtendableEvent;
  80. }): Promise<Response> | undefined;
  81. /**
  82. * Checks a request and URL (and optionally an event) against the list of
  83. * registered routes, and if there's a match, returns the corresponding
  84. * route along with any params generated by the match.
  85. *
  86. * @param {Object} options
  87. * @param {URL} options.url
  88. * @param {Request} options.request The request to match.
  89. * @param {Event} [options.event] The corresponding event (unless N/A).
  90. * @return {Object} An object with `route` and `params` properties.
  91. * They are populated if a matching route was found or `undefined`
  92. * otherwise.
  93. */
  94. findMatchingRoute({ url, request, event }: {
  95. url: URL;
  96. request: Request;
  97. event?: ExtendableEvent;
  98. }): {
  99. route?: Route;
  100. params?: HandlerCallbackOptions['params'];
  101. };
  102. /**
  103. * Define a default `handler` that's called when no routes explicitly
  104. * match the incoming request.
  105. *
  106. * Without a default handler, unmatched requests will go against the
  107. * network as if there were no service worker present.
  108. *
  109. * @param {module:workbox-routing~handlerCallback} handler A callback
  110. * function that returns a Promise resulting in a Response.
  111. */
  112. setDefaultHandler(handler: Handler): void;
  113. /**
  114. * If a Route throws an error while handling a request, this `handler`
  115. * will be called and given a chance to provide a response.
  116. *
  117. * @param {module:workbox-routing~handlerCallback} handler A callback
  118. * function that returns a Promise resulting in a Response.
  119. */
  120. setCatchHandler(handler: Handler): void;
  121. /**
  122. * Registers a route with the router.
  123. *
  124. * @param {module:workbox-routing.Route} route The route to register.
  125. */
  126. registerRoute(route: Route): void;
  127. /**
  128. * Unregisters a route with the router.
  129. *
  130. * @param {module:workbox-routing.Route} route The route to unregister.
  131. */
  132. unregisterRoute(route: Route): void;
  133. }
  134. export { Router };