Router.d.ts 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. import { RouteHandler, RouteHandlerCallbackOptions, RouteMatchCallbackOptions } from 'workbox-core/types.js';
  2. import { HTTPMethod } from './utils/constants.js';
  3. import { Route } from './Route.js';
  4. import './_version.js';
  5. /**
  6. * The Router can be used to process a `FetchEvent` using one or more
  7. * {@link workbox-routing.Route}, responding with a `Response` 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 workbox-routing
  21. */
  22. declare class Router {
  23. private readonly _routes;
  24. private readonly _defaultHandlerMap;
  25. private _catchHandler?;
  26. /**
  27. * Initializes a new Router.
  28. */
  29. constructor();
  30. /**
  31. * @return {Map<string, Array<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.
  70. * @param {ExtendableEvent} options.event The event that triggered the
  71. * request.
  72. * @return {Promise<Response>|undefined} A promise is returned if a
  73. * registered route can handle the request. If there is no matching
  74. * route and there's no `defaultHandler`, `undefined` is returned.
  75. */
  76. handleRequest({ request, event, }: {
  77. request: Request;
  78. event: ExtendableEvent;
  79. }): Promise<Response> | undefined;
  80. /**
  81. * Checks a request and URL (and optionally an event) against the list of
  82. * registered routes, and if there's a match, returns the corresponding
  83. * route along with any params generated by the match.
  84. *
  85. * @param {Object} options
  86. * @param {URL} options.url
  87. * @param {boolean} options.sameOrigin The result of comparing `url.origin`
  88. * against the current origin.
  89. * @param {Request} options.request The request to match.
  90. * @param {Event} options.event The corresponding event.
  91. * @return {Object} An object with `route` and `params` properties.
  92. * They are populated if a matching route was found or `undefined`
  93. * otherwise.
  94. */
  95. findMatchingRoute({ url, sameOrigin, request, event, }: RouteMatchCallbackOptions): {
  96. route?: Route;
  97. params?: RouteHandlerCallbackOptions['params'];
  98. };
  99. /**
  100. * Define a default `handler` that's called when no routes explicitly
  101. * match the incoming request.
  102. *
  103. * Each HTTP method ('GET', 'POST', etc.) gets its own default handler.
  104. *
  105. * Without a default handler, unmatched requests will go against the
  106. * network as if there were no service worker present.
  107. *
  108. * @param {workbox-routing~handlerCallback} handler A callback
  109. * function that returns a Promise resulting in a Response.
  110. * @param {string} [method='GET'] The HTTP method to associate with this
  111. * default handler. Each method has its own default.
  112. */
  113. setDefaultHandler(handler: RouteHandler, method?: HTTPMethod): void;
  114. /**
  115. * If a Route throws an error while handling a request, this `handler`
  116. * will be called and given a chance to provide a response.
  117. *
  118. * @param {workbox-routing~handlerCallback} handler A callback
  119. * function that returns a Promise resulting in a Response.
  120. */
  121. setCatchHandler(handler: RouteHandler): void;
  122. /**
  123. * Registers a route with the router.
  124. *
  125. * @param {workbox-routing.Route} route The route to register.
  126. */
  127. registerRoute(route: Route): void;
  128. /**
  129. * Unregisters a route with the router.
  130. *
  131. * @param {workbox-routing.Route} route The route to unregister.
  132. */
  133. unregisterRoute(route: Route): void;
  134. }
  135. export { Router };