serwist 9.0.0-preview.24
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +1 -0
- package/dist/NavigationRoute.d.ts +57 -0
- package/dist/NavigationRoute.d.ts.map +1 -0
- package/dist/PrecacheRoute.d.ts +17 -0
- package/dist/PrecacheRoute.d.ts.map +1 -0
- package/dist/PrecacheStrategy.d.ts +66 -0
- package/dist/PrecacheStrategy.d.ts.map +1 -0
- package/dist/RegExpRoute.d.ts +24 -0
- package/dist/RegExpRoute.d.ts.map +1 -0
- package/dist/Route.d.ts +33 -0
- package/dist/Route.d.ts.map +1 -0
- package/dist/Serwist.d.ts +331 -0
- package/dist/Serwist.d.ts.map +1 -0
- package/dist/cacheNames.d.ts +20 -0
- package/dist/cacheNames.d.ts.map +1 -0
- package/dist/chunks/NetworkOnly.js +599 -0
- package/dist/chunks/PrecacheFallbackPlugin.js +634 -0
- package/dist/chunks/Serwist.js +1034 -0
- package/dist/chunks/registerQuotaErrorCallback.js +17 -0
- package/dist/chunks/resultingClientExists.js +32 -0
- package/dist/chunks/timeout.js +400 -0
- package/dist/chunks/waitUntil.js +24 -0
- package/dist/cleanupOutdatedCaches.d.ts +6 -0
- package/dist/cleanupOutdatedCaches.d.ts.map +1 -0
- package/dist/clientsClaim.d.ts +6 -0
- package/dist/clientsClaim.d.ts.map +1 -0
- package/dist/constants.d.ts +15 -0
- package/dist/constants.d.ts.map +1 -0
- package/dist/copyResponse.d.ts +20 -0
- package/dist/copyResponse.d.ts.map +1 -0
- package/dist/disableDevLogs.d.ts +7 -0
- package/dist/disableDevLogs.d.ts.map +1 -0
- package/dist/index.d.ts +21 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.internal.d.ts +16 -0
- package/dist/index.internal.d.ts.map +1 -0
- package/dist/index.internal.js +24 -0
- package/dist/index.js +27 -0
- package/dist/index.legacy.d.ts +32 -0
- package/dist/index.legacy.d.ts.map +1 -0
- package/dist/index.legacy.js +640 -0
- package/dist/index.plugins.d.ts +41 -0
- package/dist/index.plugins.d.ts.map +1 -0
- package/dist/index.plugins.js +669 -0
- package/dist/index.strategies.d.ts +22 -0
- package/dist/index.strategies.d.ts.map +1 -0
- package/dist/index.strategies.js +144 -0
- package/dist/legacy/PrecacheController.d.ts +146 -0
- package/dist/legacy/PrecacheController.d.ts.map +1 -0
- package/dist/legacy/PrecacheFallbackPlugin.d.ts +62 -0
- package/dist/legacy/PrecacheFallbackPlugin.d.ts.map +1 -0
- package/dist/legacy/PrecacheRoute.d.ts +19 -0
- package/dist/legacy/PrecacheRoute.d.ts.map +1 -0
- package/dist/legacy/Router.d.ts +151 -0
- package/dist/legacy/Router.d.ts.map +1 -0
- package/dist/legacy/addPlugins.d.ts +9 -0
- package/dist/legacy/addPlugins.d.ts.map +1 -0
- package/dist/legacy/addRoute.d.ts +16 -0
- package/dist/legacy/addRoute.d.ts.map +1 -0
- package/dist/legacy/createHandlerBoundToURL.d.ts +18 -0
- package/dist/legacy/createHandlerBoundToURL.d.ts.map +1 -0
- package/dist/legacy/fallbacks.d.ts +59 -0
- package/dist/legacy/fallbacks.d.ts.map +1 -0
- package/dist/legacy/getCacheKeyForURL.d.ts +20 -0
- package/dist/legacy/getCacheKeyForURL.d.ts.map +1 -0
- package/dist/legacy/handlePrecaching.d.ts +54 -0
- package/dist/legacy/handlePrecaching.d.ts.map +1 -0
- package/dist/legacy/installSerwist.d.ts +15 -0
- package/dist/legacy/installSerwist.d.ts.map +1 -0
- package/dist/legacy/matchPrecache.d.ts +15 -0
- package/dist/legacy/matchPrecache.d.ts.map +1 -0
- package/dist/legacy/precache.d.ts +20 -0
- package/dist/legacy/precache.d.ts.map +1 -0
- package/dist/legacy/precacheAndRoute.d.ts +15 -0
- package/dist/legacy/precacheAndRoute.d.ts.map +1 -0
- package/dist/legacy/registerRoute.d.ts +16 -0
- package/dist/legacy/registerRoute.d.ts.map +1 -0
- package/dist/legacy/registerRuntimeCaching.d.ts +11 -0
- package/dist/legacy/registerRuntimeCaching.d.ts.map +1 -0
- package/dist/legacy/setCatchHandler.d.ts +10 -0
- package/dist/legacy/setCatchHandler.d.ts.map +1 -0
- package/dist/legacy/setDefaultHandler.d.ts +13 -0
- package/dist/legacy/setDefaultHandler.d.ts.map +1 -0
- package/dist/legacy/singletonPrecacheController.d.ts +34 -0
- package/dist/legacy/singletonPrecacheController.d.ts.map +1 -0
- package/dist/legacy/singletonRouter.d.ts +41 -0
- package/dist/legacy/singletonRouter.d.ts.map +1 -0
- package/dist/legacy/unregisterRoute.d.ts +9 -0
- package/dist/legacy/unregisterRoute.d.ts.map +1 -0
- package/dist/legacy/utils/PrecacheCacheKeyPlugin.d.ts +16 -0
- package/dist/legacy/utils/PrecacheCacheKeyPlugin.d.ts.map +1 -0
- package/dist/legacy/utils/getCacheKeyForURL.d.ts +14 -0
- package/dist/legacy/utils/getCacheKeyForURL.d.ts.map +1 -0
- package/dist/models/messages/messageGenerator.d.ts +4 -0
- package/dist/models/messages/messageGenerator.d.ts.map +1 -0
- package/dist/models/messages/messages.d.ts +44 -0
- package/dist/models/messages/messages.d.ts.map +1 -0
- package/dist/models/pluginEvents.d.ts +10 -0
- package/dist/models/pluginEvents.d.ts.map +1 -0
- package/dist/models/quotaErrorCallbacks.d.ts +3 -0
- package/dist/models/quotaErrorCallbacks.d.ts.map +1 -0
- package/dist/navigationPreload.d.ts +24 -0
- package/dist/navigationPreload.d.ts.map +1 -0
- package/dist/parseRoute.d.ts +16 -0
- package/dist/parseRoute.d.ts.map +1 -0
- package/dist/plugins/backgroundSync/BackgroundSyncPlugin.d.ts +23 -0
- package/dist/plugins/backgroundSync/BackgroundSyncPlugin.d.ts.map +1 -0
- package/dist/plugins/backgroundSync/Queue.d.ts +166 -0
- package/dist/plugins/backgroundSync/Queue.d.ts.map +1 -0
- package/dist/plugins/backgroundSync/QueueDb.d.ts +90 -0
- package/dist/plugins/backgroundSync/QueueDb.d.ts.map +1 -0
- package/dist/plugins/backgroundSync/QueueStore.d.ts +75 -0
- package/dist/plugins/backgroundSync/QueueStore.d.ts.map +1 -0
- package/dist/plugins/backgroundSync/StorableRequest.d.ts +51 -0
- package/dist/plugins/backgroundSync/StorableRequest.d.ts.map +1 -0
- package/dist/plugins/broadcastUpdate/BroadcastCacheUpdate.d.ts +45 -0
- package/dist/plugins/broadcastUpdate/BroadcastCacheUpdate.d.ts.map +1 -0
- package/dist/plugins/broadcastUpdate/BroadcastUpdatePlugin.d.ts +27 -0
- package/dist/plugins/broadcastUpdate/BroadcastUpdatePlugin.d.ts.map +1 -0
- package/dist/plugins/broadcastUpdate/constants.d.ts +5 -0
- package/dist/plugins/broadcastUpdate/constants.d.ts.map +1 -0
- package/dist/plugins/broadcastUpdate/responsesAreSame.d.ts +11 -0
- package/dist/plugins/broadcastUpdate/responsesAreSame.d.ts.map +1 -0
- package/dist/plugins/broadcastUpdate/types.d.ts +34 -0
- package/dist/plugins/broadcastUpdate/types.d.ts.map +1 -0
- package/dist/plugins/cacheableResponse/CacheableResponse.d.ts +40 -0
- package/dist/plugins/cacheableResponse/CacheableResponse.d.ts.map +1 -0
- package/dist/plugins/cacheableResponse/CacheableResponsePlugin.d.ts +27 -0
- package/dist/plugins/cacheableResponse/CacheableResponsePlugin.d.ts.map +1 -0
- package/dist/plugins/expiration/CacheExpiration.d.ts +66 -0
- package/dist/plugins/expiration/CacheExpiration.d.ts.map +1 -0
- package/dist/plugins/expiration/ExpirationPlugin.d.ts +116 -0
- package/dist/plugins/expiration/ExpirationPlugin.d.ts.map +1 -0
- package/dist/plugins/expiration/models/CacheTimestampsModel.d.ts +73 -0
- package/dist/plugins/expiration/models/CacheTimestampsModel.d.ts.map +1 -0
- package/dist/plugins/googleAnalytics/constants.d.ts +10 -0
- package/dist/plugins/googleAnalytics/constants.d.ts.map +1 -0
- package/dist/plugins/googleAnalytics/initialize.d.ts +30 -0
- package/dist/plugins/googleAnalytics/initialize.d.ts.map +1 -0
- package/dist/plugins/precaching/PrecacheFallbackPlugin.d.ts +53 -0
- package/dist/plugins/precaching/PrecacheFallbackPlugin.d.ts.map +1 -0
- package/dist/plugins/rangeRequests/RangeRequestsPlugin.d.ts +19 -0
- package/dist/plugins/rangeRequests/RangeRequestsPlugin.d.ts.map +1 -0
- package/dist/plugins/rangeRequests/createPartialResponse.d.ts +18 -0
- package/dist/plugins/rangeRequests/createPartialResponse.d.ts.map +1 -0
- package/dist/plugins/rangeRequests/utils/calculateEffectiveBoundaries.d.ts +14 -0
- package/dist/plugins/rangeRequests/utils/calculateEffectiveBoundaries.d.ts.map +1 -0
- package/dist/plugins/rangeRequests/utils/parseRangeHeader.d.ts +12 -0
- package/dist/plugins/rangeRequests/utils/parseRangeHeader.d.ts.map +1 -0
- package/dist/registerQuotaErrorCallback.d.ts +8 -0
- package/dist/registerQuotaErrorCallback.d.ts.map +1 -0
- package/dist/setCacheNameDetails.d.ts +9 -0
- package/dist/setCacheNameDetails.d.ts.map +1 -0
- package/dist/strategies/CacheFirst.d.ts +23 -0
- package/dist/strategies/CacheFirst.d.ts.map +1 -0
- package/dist/strategies/CacheOnly.d.ts +20 -0
- package/dist/strategies/CacheOnly.d.ts.map +1 -0
- package/dist/strategies/NetworkFirst.d.ts +61 -0
- package/dist/strategies/NetworkFirst.d.ts.map +1 -0
- package/dist/strategies/NetworkOnly.d.ts +32 -0
- package/dist/strategies/NetworkOnly.d.ts.map +1 -0
- package/dist/strategies/StaleWhileRevalidate.d.ts +35 -0
- package/dist/strategies/StaleWhileRevalidate.d.ts.map +1 -0
- package/dist/strategies/Strategy.d.ts +83 -0
- package/dist/strategies/Strategy.d.ts.map +1 -0
- package/dist/strategies/StrategyHandler.d.ts +189 -0
- package/dist/strategies/StrategyHandler.d.ts.map +1 -0
- package/dist/strategies/plugins/cacheOkAndOpaquePlugin.d.ts +3 -0
- package/dist/strategies/plugins/cacheOkAndOpaquePlugin.d.ts.map +1 -0
- package/dist/strategies/utils/messages.d.ts +5 -0
- package/dist/strategies/utils/messages.d.ts.map +1 -0
- package/dist/types.d.ts +317 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/utils/Deferred.d.ts +19 -0
- package/dist/utils/Deferred.d.ts.map +1 -0
- package/dist/utils/PrecacheCacheKeyPlugin.d.ts +16 -0
- package/dist/utils/PrecacheCacheKeyPlugin.d.ts.map +1 -0
- package/dist/utils/PrecacheInstallReportPlugin.d.ts +14 -0
- package/dist/utils/PrecacheInstallReportPlugin.d.ts.map +1 -0
- package/dist/utils/SerwistError.d.ts +24 -0
- package/dist/utils/SerwistError.d.ts.map +1 -0
- package/dist/utils/assert.d.ts +11 -0
- package/dist/utils/assert.d.ts.map +1 -0
- package/dist/utils/cacheMatchIgnoreParams.d.ts +15 -0
- package/dist/utils/cacheMatchIgnoreParams.d.ts.map +1 -0
- package/dist/utils/cacheNames.d.ts +40 -0
- package/dist/utils/cacheNames.d.ts.map +1 -0
- package/dist/utils/canConstructReadableStream.d.ts +12 -0
- package/dist/utils/canConstructReadableStream.d.ts.map +1 -0
- package/dist/utils/canConstructResponseFromBodyStream.d.ts +11 -0
- package/dist/utils/canConstructResponseFromBodyStream.d.ts.map +1 -0
- package/dist/utils/createCacheKey.d.ts +16 -0
- package/dist/utils/createCacheKey.d.ts.map +1 -0
- package/dist/utils/deleteOutdatedCaches.d.ts +18 -0
- package/dist/utils/deleteOutdatedCaches.d.ts.map +1 -0
- package/dist/utils/dontWaitFor.d.ts +7 -0
- package/dist/utils/dontWaitFor.d.ts.map +1 -0
- package/dist/utils/executeQuotaErrorCallbacks.d.ts +8 -0
- package/dist/utils/executeQuotaErrorCallbacks.d.ts.map +1 -0
- package/dist/utils/generateURLVariations.d.ts +12 -0
- package/dist/utils/generateURLVariations.d.ts.map +1 -0
- package/dist/utils/getFriendlyURL.d.ts +3 -0
- package/dist/utils/getFriendlyURL.d.ts.map +1 -0
- package/dist/utils/logger.d.ts +24 -0
- package/dist/utils/logger.d.ts.map +1 -0
- package/dist/utils/normalizeHandler.d.ts +10 -0
- package/dist/utils/normalizeHandler.d.ts.map +1 -0
- package/dist/utils/pluginUtils.d.ts +5 -0
- package/dist/utils/pluginUtils.d.ts.map +1 -0
- package/dist/utils/printCleanupDetails.d.ts +6 -0
- package/dist/utils/printCleanupDetails.d.ts.map +1 -0
- package/dist/utils/printInstallDetails.d.ts +7 -0
- package/dist/utils/printInstallDetails.d.ts.map +1 -0
- package/dist/utils/removeIgnoredSearchParams.d.ts +12 -0
- package/dist/utils/removeIgnoredSearchParams.d.ts.map +1 -0
- package/dist/utils/resultingClientExists.d.ts +12 -0
- package/dist/utils/resultingClientExists.d.ts.map +1 -0
- package/dist/utils/timeout.d.ts +10 -0
- package/dist/utils/timeout.d.ts.map +1 -0
- package/dist/utils/waitUntil.d.ts +11 -0
- package/dist/utils/waitUntil.d.ts.map +1 -0
- package/dist/utils/welcome.d.ts +2 -0
- package/dist/utils/welcome.d.ts.map +1 -0
- package/package.json +85 -0
- package/src/NavigationRoute.ts +119 -0
- package/src/PrecacheRoute.ts +46 -0
- package/src/PrecacheStrategy.ts +239 -0
- package/src/RegExpRoute.ts +74 -0
- package/src/Route.ts +67 -0
- package/src/Serwist.ts +920 -0
- package/src/cacheNames.ts +39 -0
- package/src/cleanupOutdatedCaches.ts +32 -0
- package/src/clientsClaim.ts +18 -0
- package/src/constants.ts +24 -0
- package/src/copyResponse.ts +60 -0
- package/src/disableDevLogs.ts +10 -0
- package/src/index.internal.ts +33 -0
- package/src/index.legacy.ts +66 -0
- package/src/index.plugins.ts +95 -0
- package/src/index.strategies.ts +26 -0
- package/src/index.ts +39 -0
- package/src/legacy/PrecacheController.ts +337 -0
- package/src/legacy/PrecacheFallbackPlugin.ts +93 -0
- package/src/legacy/PrecacheRoute.ts +48 -0
- package/src/legacy/Router.ts +484 -0
- package/src/legacy/addPlugins.ts +21 -0
- package/src/legacy/addRoute.ts +29 -0
- package/src/legacy/createHandlerBoundToURL.ts +30 -0
- package/src/legacy/fallbacks.ts +94 -0
- package/src/legacy/getCacheKeyForURL.ts +32 -0
- package/src/legacy/handlePrecaching.ts +86 -0
- package/src/legacy/installSerwist.ts +19 -0
- package/src/legacy/matchPrecache.ts +26 -0
- package/src/legacy/precache.ts +31 -0
- package/src/legacy/precacheAndRoute.ts +28 -0
- package/src/legacy/registerRoute.ts +27 -0
- package/src/legacy/registerRuntimeCaching.ts +17 -0
- package/src/legacy/setCatchHandler.ts +21 -0
- package/src/legacy/setDefaultHandler.ts +24 -0
- package/src/legacy/singletonPrecacheController.ts +53 -0
- package/src/legacy/singletonRouter.ts +70 -0
- package/src/legacy/unregisterRoute.ts +12 -0
- package/src/legacy/utils/PrecacheCacheKeyPlugin.ts +33 -0
- package/src/legacy/utils/getCacheKeyForURL.ts +36 -0
- package/src/models/messages/messageGenerator.ts +29 -0
- package/src/models/messages/messages.ts +233 -0
- package/src/models/pluginEvents.ts +17 -0
- package/src/models/quotaErrorCallbacks.ts +13 -0
- package/src/navigationPreload.ts +68 -0
- package/src/parseRoute.ts +78 -0
- package/src/plugins/backgroundSync/BackgroundSyncPlugin.ts +38 -0
- package/src/plugins/backgroundSync/Queue.ts +440 -0
- package/src/plugins/backgroundSync/QueueDb.ts +176 -0
- package/src/plugins/backgroundSync/QueueStore.ts +160 -0
- package/src/plugins/backgroundSync/StorableRequest.ts +142 -0
- package/src/plugins/broadcastUpdate/BroadcastCacheUpdate.ts +161 -0
- package/src/plugins/broadcastUpdate/BroadcastUpdatePlugin.ts +42 -0
- package/src/plugins/broadcastUpdate/constants.ts +12 -0
- package/src/plugins/broadcastUpdate/responsesAreSame.ts +49 -0
- package/src/plugins/broadcastUpdate/types.ts +37 -0
- package/src/plugins/cacheableResponse/CacheableResponse.ts +144 -0
- package/src/plugins/cacheableResponse/CacheableResponsePlugin.ts +45 -0
- package/src/plugins/expiration/CacheExpiration.ts +193 -0
- package/src/plugins/expiration/ExpirationPlugin.ts +300 -0
- package/src/plugins/expiration/models/CacheTimestampsModel.ts +184 -0
- package/src/plugins/googleAnalytics/constants.ts +22 -0
- package/src/plugins/googleAnalytics/initialize.ts +209 -0
- package/src/plugins/precaching/PrecacheFallbackPlugin.ts +83 -0
- package/src/plugins/rangeRequests/RangeRequestsPlugin.ts +38 -0
- package/src/plugins/rangeRequests/createPartialResponse.ts +93 -0
- package/src/plugins/rangeRequests/utils/calculateEffectiveBoundaries.ts +59 -0
- package/src/plugins/rangeRequests/utils/parseRangeHeader.ts +55 -0
- package/src/registerQuotaErrorCallback.ts +34 -0
- package/src/setCacheNameDetails.ts +53 -0
- package/src/strategies/CacheFirst.ts +88 -0
- package/src/strategies/CacheOnly.ts +59 -0
- package/src/strategies/NetworkFirst.ts +229 -0
- package/src/strategies/NetworkOnly.ts +98 -0
- package/src/strategies/StaleWhileRevalidate.ts +110 -0
- package/src/strategies/Strategy.ts +204 -0
- package/src/strategies/StrategyHandler.ts +554 -0
- package/src/strategies/plugins/cacheOkAndOpaquePlugin.ts +26 -0
- package/src/strategies/utils/messages.ts +21 -0
- package/src/types.ts +358 -0
- package/src/utils/Deferred.ts +33 -0
- package/src/utils/PrecacheCacheKeyPlugin.ts +33 -0
- package/src/utils/PrecacheInstallReportPlugin.ts +47 -0
- package/src/utils/SerwistError.ts +41 -0
- package/src/utils/assert.ts +89 -0
- package/src/utils/cacheMatchIgnoreParams.ts +54 -0
- package/src/utils/cacheNames.ts +87 -0
- package/src/utils/canConstructReadableStream.ts +34 -0
- package/src/utils/canConstructResponseFromBodyStream.ts +37 -0
- package/src/utils/createCacheKey.ts +68 -0
- package/src/utils/deleteOutdatedCaches.ts +40 -0
- package/src/utils/dontWaitFor.ts +16 -0
- package/src/utils/executeQuotaErrorCallbacks.ts +33 -0
- package/src/utils/generateURLVariations.ts +55 -0
- package/src/utils/getFriendlyURL.ts +16 -0
- package/src/utils/logger.ts +95 -0
- package/src/utils/normalizeHandler.ts +40 -0
- package/src/utils/pluginUtils.ts +15 -0
- package/src/utils/printCleanupDetails.ts +38 -0
- package/src/utils/printInstallDetails.ts +53 -0
- package/src/utils/removeIgnoredSearchParams.ts +29 -0
- package/src/utils/resultingClientExists.ts +58 -0
- package/src/utils/timeout.ts +19 -0
- package/src/utils/waitUntil.ts +21 -0
- package/src/utils/welcome.ts +19 -0
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
import { NavigationRoute } from "../NavigationRoute.js";
|
|
2
|
+
import { cleanupOutdatedCaches as cleanupOutdatedCachesImpl } from "../cleanupOutdatedCaches.js";
|
|
3
|
+
import type { PrecacheEntry, PrecacheRouteOptions } from "../types.js";
|
|
4
|
+
import type { PrecacheController } from "./PrecacheController.js";
|
|
5
|
+
import { PrecacheRoute } from "./PrecacheRoute.js";
|
|
6
|
+
import type { Router } from "./Router.js";
|
|
7
|
+
import { createHandlerBoundToURL } from "./createHandlerBoundToURL.js";
|
|
8
|
+
import { getSingletonPrecacheController } from "./singletonPrecacheController.js";
|
|
9
|
+
import { getSingletonRouter } from "./singletonRouter.js";
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* @deprecated
|
|
13
|
+
*/
|
|
14
|
+
export interface HandlePrecachingOptions {
|
|
15
|
+
/**
|
|
16
|
+
* An optional `PrecacheController` instance. If not provided, the singleton
|
|
17
|
+
* `PrecacheController` will be used.
|
|
18
|
+
*/
|
|
19
|
+
precacheController?: PrecacheController;
|
|
20
|
+
/**
|
|
21
|
+
* An optional `Router` instance. If not provided, the singleton `Router`
|
|
22
|
+
* will be used.
|
|
23
|
+
*/
|
|
24
|
+
router?: Router;
|
|
25
|
+
/**
|
|
26
|
+
* A list of URLs that should be cached.
|
|
27
|
+
*/
|
|
28
|
+
precacheEntries?: (PrecacheEntry | string)[];
|
|
29
|
+
/**
|
|
30
|
+
* Options to customize how Serwist precaches the URLs.
|
|
31
|
+
*/
|
|
32
|
+
precacheOptions?: PrecacheRouteOptions;
|
|
33
|
+
/**
|
|
34
|
+
* Whether outdated caches should be removed.
|
|
35
|
+
*
|
|
36
|
+
* @default false
|
|
37
|
+
*/
|
|
38
|
+
cleanupOutdatedCaches?: boolean;
|
|
39
|
+
/**
|
|
40
|
+
* An URL that should point to a HTML file with which navigation requests for URLs that aren't
|
|
41
|
+
* precached will be fulfilled.
|
|
42
|
+
*/
|
|
43
|
+
navigateFallback?: string;
|
|
44
|
+
/**
|
|
45
|
+
* URLs that should be allowed to use the `navigateFallback` handler.
|
|
46
|
+
*/
|
|
47
|
+
navigateFallbackAllowlist?: RegExp[];
|
|
48
|
+
/**
|
|
49
|
+
* URLs that should not be allowed to use the `navigateFallback` handler. This takes precedence
|
|
50
|
+
* over `navigateFallbackAllowlist`.
|
|
51
|
+
*/
|
|
52
|
+
navigateFallbackDenylist?: RegExp[];
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* Handles a list of precache entries and cleans up outdated caches.
|
|
57
|
+
*
|
|
58
|
+
* @param options
|
|
59
|
+
* @deprecated
|
|
60
|
+
*/
|
|
61
|
+
export const handlePrecaching = ({
|
|
62
|
+
precacheController = getSingletonPrecacheController(),
|
|
63
|
+
router = getSingletonRouter(),
|
|
64
|
+
precacheEntries,
|
|
65
|
+
precacheOptions,
|
|
66
|
+
cleanupOutdatedCaches = false,
|
|
67
|
+
navigateFallback,
|
|
68
|
+
navigateFallbackAllowlist,
|
|
69
|
+
navigateFallbackDenylist,
|
|
70
|
+
}: HandlePrecachingOptions): void => {
|
|
71
|
+
if (!!precacheEntries && precacheEntries.length > 0) {
|
|
72
|
+
precacheController.precache(precacheEntries);
|
|
73
|
+
router.registerRoute(new PrecacheRoute(precacheController, precacheOptions));
|
|
74
|
+
|
|
75
|
+
if (cleanupOutdatedCaches) cleanupOutdatedCachesImpl();
|
|
76
|
+
|
|
77
|
+
if (navigateFallback) {
|
|
78
|
+
router.registerRoute(
|
|
79
|
+
new NavigationRoute(createHandlerBoundToURL(navigateFallback), {
|
|
80
|
+
allowlist: navigateFallbackAllowlist,
|
|
81
|
+
denylist: navigateFallbackDenylist,
|
|
82
|
+
}),
|
|
83
|
+
);
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
};
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { Serwist, type SerwistOptions } from "../Serwist.js";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Options for `installSerwist`.
|
|
5
|
+
*
|
|
6
|
+
* @deprecated
|
|
7
|
+
*/
|
|
8
|
+
export type InstallSerwistOptions = SerwistOptions;
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Abstracts away the core APIs of Serwist.
|
|
12
|
+
*
|
|
13
|
+
* @param options - `installSerwist` options.
|
|
14
|
+
* @deprecated
|
|
15
|
+
*/
|
|
16
|
+
export const installSerwist = (options: InstallSerwistOptions): void => {
|
|
17
|
+
const serwist = new Serwist(options);
|
|
18
|
+
serwist.addEventListeners();
|
|
19
|
+
};
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
/*
|
|
2
|
+
Copyright 2019 Google LLC
|
|
3
|
+
|
|
4
|
+
Use of this source code is governed by an MIT-style
|
|
5
|
+
license that can be found in the LICENSE file or at
|
|
6
|
+
https://opensource.org/licenses/MIT.
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
import { getSingletonPrecacheController } from "./singletonPrecacheController.js";
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Helper function that calls `PrecacheController#matchPrecache`
|
|
13
|
+
* on the default `PrecacheController` instance.
|
|
14
|
+
*
|
|
15
|
+
* If you are creating your own `PrecacheController`, then call
|
|
16
|
+
* `PrecacheController#matchPrecache` on that instance,
|
|
17
|
+
* instead of using this function.
|
|
18
|
+
*
|
|
19
|
+
* @param request The key (without revisioning parameters)
|
|
20
|
+
* to look up in the precache.
|
|
21
|
+
* @returns
|
|
22
|
+
* @deprecated
|
|
23
|
+
*/
|
|
24
|
+
export const matchPrecache = (request: string | Request): Promise<Response | undefined> => {
|
|
25
|
+
return getSingletonPrecacheController().matchPrecache(request);
|
|
26
|
+
};
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
/*
|
|
2
|
+
Copyright 2019 Google LLC
|
|
3
|
+
|
|
4
|
+
Use of this source code is governed by an MIT-style
|
|
5
|
+
license that can be found in the LICENSE file or at
|
|
6
|
+
https://opensource.org/licenses/MIT.
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
import type { PrecacheEntry } from "../types.js";
|
|
10
|
+
import { getSingletonPrecacheController } from "./singletonPrecacheController.js";
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* Adds items to the precache list, removing any duplicates and
|
|
14
|
+
* stores the files in the precache cache when the service
|
|
15
|
+
* worker installs.
|
|
16
|
+
*
|
|
17
|
+
* This method can be called multiple times.
|
|
18
|
+
*
|
|
19
|
+
* Please note: This method **will not** serve any of the cached files for you.
|
|
20
|
+
* It only precaches files. To respond to a network request you call
|
|
21
|
+
* `serwist/legacy.addRoute`.
|
|
22
|
+
*
|
|
23
|
+
* If you have a single array of files to precache, you can just call
|
|
24
|
+
* `serwist/legacy.precacheAndRoute`.
|
|
25
|
+
*
|
|
26
|
+
* @param entries Array of entries to precache.
|
|
27
|
+
* @deprecated
|
|
28
|
+
*/
|
|
29
|
+
export const precache = (entries: (PrecacheEntry | string)[]): void => {
|
|
30
|
+
getSingletonPrecacheController().precache(entries);
|
|
31
|
+
};
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
/*
|
|
2
|
+
Copyright 2019 Google LLC
|
|
3
|
+
|
|
4
|
+
Use of this source code is governed by an MIT-style
|
|
5
|
+
license that can be found in the LICENSE file or at
|
|
6
|
+
https://opensource.org/licenses/MIT.
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
import type { PrecacheEntry, PrecacheRouteOptions } from "../types.js";
|
|
10
|
+
import { addRoute } from "./addRoute.js";
|
|
11
|
+
import { precache } from "./precache.js";
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* This method will add entries to the precache list and add a route to
|
|
15
|
+
* respond to fetch events.
|
|
16
|
+
*
|
|
17
|
+
* This is a convenience method that will call
|
|
18
|
+
* `serwist/legacy.precache` and
|
|
19
|
+
* `serwist/legacy.addRoute` in a single call.
|
|
20
|
+
*
|
|
21
|
+
* @param entries Array of entries to precache.
|
|
22
|
+
* @param options See the `serwist.PrecacheRouteOptions` options.
|
|
23
|
+
* @deprecated
|
|
24
|
+
*/
|
|
25
|
+
export const precacheAndRoute = (entries: (PrecacheEntry | string)[], options?: PrecacheRouteOptions): void => {
|
|
26
|
+
precache(entries);
|
|
27
|
+
addRoute(options);
|
|
28
|
+
};
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
/*
|
|
2
|
+
Copyright 2019 Google LLC
|
|
3
|
+
|
|
4
|
+
Use of this source code is governed by an MIT-style
|
|
5
|
+
license that can be found in the LICENSE file or at
|
|
6
|
+
https://opensource.org/licenses/MIT.
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
import type { RouteHandler, RouteMatchCallback } from "../types.js";
|
|
10
|
+
import type { Route } from "../Route.js";
|
|
11
|
+
import type { HTTPMethod } from "../constants.js";
|
|
12
|
+
import { getSingletonRouter } from "./singletonRouter.js";
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Registers a `RegExp`, string, or function with a caching
|
|
16
|
+
* strategy to a singleton `Router` instance.
|
|
17
|
+
*
|
|
18
|
+
* @param capture If the capture param is a `Route`, all other arguments will be ignored.
|
|
19
|
+
* @param handler A callback function that returns a `Promise` resulting in a `Response`.
|
|
20
|
+
* This parameter is required if `capture` is not a `Route` object.
|
|
21
|
+
* @param method The HTTP method to match the `Route` against. Defaults to `'GET'`.
|
|
22
|
+
* @returns The generated `Route`, which can then be provided to `unregisterRoute` if needed.
|
|
23
|
+
* @deprecated
|
|
24
|
+
*/
|
|
25
|
+
export const registerRoute = (capture: RegExp | string | RouteMatchCallback | Route, handler?: RouteHandler, method?: HTTPMethod): Route => {
|
|
26
|
+
return getSingletonRouter().registerCapture(capture, handler, method);
|
|
27
|
+
};
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { registerRoute } from "./registerRoute.js";
|
|
2
|
+
|
|
3
|
+
import type { RuntimeCaching } from "../types.js";
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Registers caching strategies to a singleton Router instance. It is a simple
|
|
7
|
+
* syntatic sugar for `serwist/legacy.registerRoute`.
|
|
8
|
+
*
|
|
9
|
+
* @param runtimeCachingList
|
|
10
|
+
* @returns
|
|
11
|
+
* @deprecated
|
|
12
|
+
*/
|
|
13
|
+
export const registerRuntimeCaching = (...runtimeCachingList: RuntimeCaching[]): void => {
|
|
14
|
+
for (const entry of runtimeCachingList) {
|
|
15
|
+
registerRoute(entry.matcher, entry.handler, entry.method);
|
|
16
|
+
}
|
|
17
|
+
};
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
/*
|
|
2
|
+
Copyright 2019 Google LLC
|
|
3
|
+
|
|
4
|
+
Use of this source code is governed by an MIT-style
|
|
5
|
+
license that can be found in the LICENSE file or at
|
|
6
|
+
https://opensource.org/licenses/MIT.
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
import type { RouteHandler } from "../types.js";
|
|
10
|
+
import { getSingletonRouter } from "./singletonRouter.js";
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* If a `Route` throws an error while handling a request, this `handler`
|
|
14
|
+
* will be called and given a chance to provide a response.
|
|
15
|
+
*
|
|
16
|
+
* @param handler A callback function that returns a Promise resulting in a Response.
|
|
17
|
+
* @deprecated
|
|
18
|
+
*/
|
|
19
|
+
export const setCatchHandler = (handler: RouteHandler): void => {
|
|
20
|
+
getSingletonRouter().setCatchHandler(handler);
|
|
21
|
+
};
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
/*
|
|
2
|
+
Copyright 2019 Google LLC
|
|
3
|
+
|
|
4
|
+
Use of this source code is governed by an MIT-style
|
|
5
|
+
license that can be found in the LICENSE file or at
|
|
6
|
+
https://opensource.org/licenses/MIT.
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
import type { RouteHandler } from "../types.js";
|
|
10
|
+
import { getSingletonRouter } from "./singletonRouter.js";
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* Defines a default `handler` that's called when no routes explicitly
|
|
14
|
+
* match the incoming request.
|
|
15
|
+
*
|
|
16
|
+
* Without a default `handler`, unmatched requests will go against the
|
|
17
|
+
* network as if there were no service worker present.
|
|
18
|
+
*
|
|
19
|
+
* @param handler A callback function that returns a Promise resulting in a Response.
|
|
20
|
+
* @deprecated
|
|
21
|
+
*/
|
|
22
|
+
export const setDefaultHandler = (handler: RouteHandler): void => {
|
|
23
|
+
getSingletonRouter().setDefaultHandler(handler);
|
|
24
|
+
};
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
/*
|
|
2
|
+
Copyright 2019 Google LLC
|
|
3
|
+
|
|
4
|
+
Use of this source code is governed by an MIT-style
|
|
5
|
+
license that can be found in the LICENSE file or at
|
|
6
|
+
https://opensource.org/licenses/MIT.
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
import { PrecacheController } from "./PrecacheController.js";
|
|
10
|
+
|
|
11
|
+
let defaultPrecacheController: PrecacheController | undefined = undefined;
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Creates a new, singleton `PrecacheController` if one does not exist. If one does
|
|
15
|
+
* already exist, that instance is returned. This instance is used by Serwist's
|
|
16
|
+
* `PrecacheController`-dependent functions and classes unless you provide a different
|
|
17
|
+
* `Router` to them.
|
|
18
|
+
*
|
|
19
|
+
* @returns The singleton `PrecacheController`.
|
|
20
|
+
* @deprecated
|
|
21
|
+
*/
|
|
22
|
+
export const getSingletonPrecacheController = (): PrecacheController => {
|
|
23
|
+
if (!defaultPrecacheController) {
|
|
24
|
+
defaultPrecacheController = new PrecacheController();
|
|
25
|
+
}
|
|
26
|
+
return defaultPrecacheController;
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* Changes the singleton `PrecacheController` to a different instance. This is meant for when you do not
|
|
31
|
+
* want to pass your own `PrecacheController` to every one of Serwist's `PrecacheController`-dependent
|
|
32
|
+
* functions and classes.
|
|
33
|
+
*
|
|
34
|
+
* It is highly recommended that you call this before anything else, if you plan on doing so.
|
|
35
|
+
*
|
|
36
|
+
* @example
|
|
37
|
+
* ```js
|
|
38
|
+
* import { PrecacheController, setSingletonPrecacheController } from "serwist/legacy";
|
|
39
|
+
*
|
|
40
|
+
* const controller = new PrecacheController();
|
|
41
|
+
*
|
|
42
|
+
* setSingletonPrecacheController(controller);
|
|
43
|
+
*
|
|
44
|
+
* // Do something with your controller...
|
|
45
|
+
* ```
|
|
46
|
+
* @param router
|
|
47
|
+
* @returns The new singleton `PrecacheController`.
|
|
48
|
+
* @deprecated
|
|
49
|
+
*/
|
|
50
|
+
export const setSingletonPrecacheController = (precacheController: PrecacheController): PrecacheController => {
|
|
51
|
+
defaultPrecacheController = precacheController;
|
|
52
|
+
return defaultPrecacheController;
|
|
53
|
+
};
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
/*
|
|
2
|
+
Copyright 2019 Google LLC
|
|
3
|
+
|
|
4
|
+
Use of this source code is governed by an MIT-style
|
|
5
|
+
license that can be found in the LICENSE file or at
|
|
6
|
+
https://opensource.org/licenses/MIT.
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
import { Router } from "./Router.js";
|
|
10
|
+
|
|
11
|
+
let defaultRouter: Router | undefined = undefined;
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Creates a new, singleton `Router` if one does not exist. If one does
|
|
15
|
+
* already exist, that instance is returned. This instance is used by
|
|
16
|
+
* Serwist's `Router`-dependent functions and classes unless you provide
|
|
17
|
+
* a different `Router` to them.
|
|
18
|
+
*
|
|
19
|
+
* @returns The singleton `Router`.
|
|
20
|
+
* @deprecated
|
|
21
|
+
*/
|
|
22
|
+
export const getSingletonRouter = (): Router => {
|
|
23
|
+
if (!defaultRouter) {
|
|
24
|
+
defaultRouter = new Router();
|
|
25
|
+
|
|
26
|
+
// The helpers that use the default Router assume these listeners exist.
|
|
27
|
+
defaultRouter.addFetchListener();
|
|
28
|
+
defaultRouter.addCacheListener();
|
|
29
|
+
}
|
|
30
|
+
return defaultRouter;
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* Changes the singleton `Router` to a different instance. This is meant for when you do not
|
|
35
|
+
* want to pass your own `Router` to every one of Serwist's `Router`-dependent functions and classes.
|
|
36
|
+
* If this or `getSingletonRouter` has been called before, it removes the listeners of the
|
|
37
|
+
* previous singleton `Router`. It also adds those of the new one, so you need not do that yourself.
|
|
38
|
+
*
|
|
39
|
+
* It is highly recommended that you call this before anything else, if you plan on doing so.
|
|
40
|
+
*
|
|
41
|
+
* @example
|
|
42
|
+
* ```js
|
|
43
|
+
* import { Router, setSingletonRouter } from "serwist/legacy";
|
|
44
|
+
*
|
|
45
|
+
* const router = new Router();
|
|
46
|
+
*
|
|
47
|
+
* setSingletonRouter(router);
|
|
48
|
+
*
|
|
49
|
+
* router.registerRoute(
|
|
50
|
+
* new Route(
|
|
51
|
+
* /\/api\/.*\/*.json/,
|
|
52
|
+
* new NetworkOnly(),
|
|
53
|
+
* "POST",
|
|
54
|
+
* ),
|
|
55
|
+
* );
|
|
56
|
+
* ```
|
|
57
|
+
* @param router
|
|
58
|
+
* @returns The new singleton `Router`.
|
|
59
|
+
* @deprecated
|
|
60
|
+
*/
|
|
61
|
+
export const setSingletonRouter = (router: Router): Router => {
|
|
62
|
+
if (defaultRouter) {
|
|
63
|
+
defaultRouter.removeFetchListener();
|
|
64
|
+
defaultRouter.removeCacheListener();
|
|
65
|
+
}
|
|
66
|
+
defaultRouter = router;
|
|
67
|
+
defaultRouter.addFetchListener();
|
|
68
|
+
defaultRouter.addCacheListener();
|
|
69
|
+
return defaultRouter;
|
|
70
|
+
};
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import type { Route } from "../Route.js";
|
|
2
|
+
import { getSingletonRouter } from "./singletonRouter.js";
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Unregisters a route from the singleton `Router` instance.
|
|
6
|
+
*
|
|
7
|
+
* @param route The route to unregister.
|
|
8
|
+
* @deprecated
|
|
9
|
+
*/
|
|
10
|
+
export const unregisterRoute = (route: Route): void => {
|
|
11
|
+
getSingletonRouter().unregisterRoute(route);
|
|
12
|
+
};
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
/*
|
|
2
|
+
Copyright 2020 Google LLC
|
|
3
|
+
|
|
4
|
+
Use of this source code is governed by an MIT-style
|
|
5
|
+
license that can be found in the LICENSE file or at
|
|
6
|
+
https://opensource.org/licenses/MIT.
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
import type { SerwistPlugin, SerwistPluginCallbackParam } from "../../types.js";
|
|
10
|
+
import type { PrecacheController } from "../PrecacheController.js";
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* A plugin, designed to be used with PrecacheController, to translate URLs into
|
|
14
|
+
* the corresponding cache key, based on the current revision info.
|
|
15
|
+
*
|
|
16
|
+
* @private
|
|
17
|
+
*/
|
|
18
|
+
export class PrecacheCacheKeyPlugin implements SerwistPlugin {
|
|
19
|
+
private readonly _precacheController: PrecacheController;
|
|
20
|
+
|
|
21
|
+
constructor({ precacheController }: { precacheController: PrecacheController }) {
|
|
22
|
+
this._precacheController = precacheController;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
cacheKeyWillBeUsed: SerwistPlugin["cacheKeyWillBeUsed"] = async ({ request, params }: SerwistPluginCallbackParam["cacheKeyWillBeUsed"]) => {
|
|
26
|
+
// Params is type any, can't change right now.
|
|
27
|
+
/* eslint-disable */
|
|
28
|
+
const cacheKey = params?.cacheKey || this._precacheController.getCacheKeyForURL(request.url);
|
|
29
|
+
/* eslint-enable */
|
|
30
|
+
|
|
31
|
+
return cacheKey ? new Request(cacheKey, { headers: request.headers }) : request;
|
|
32
|
+
};
|
|
33
|
+
}
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
/*
|
|
2
|
+
Copyright 2019 Google LLC
|
|
3
|
+
|
|
4
|
+
Use of this source code is governed by an MIT-style
|
|
5
|
+
license that can be found in the LICENSE file or at
|
|
6
|
+
https://opensource.org/licenses/MIT.
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
import type { PrecacheRouteOptions } from "../../types.js";
|
|
10
|
+
import { generateURLVariations } from "../../utils/generateURLVariations.js";
|
|
11
|
+
import { getSingletonPrecacheController } from "../singletonPrecacheController.js";
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* This function will take the request URL and manipulate it based on the
|
|
15
|
+
* configuration options.
|
|
16
|
+
*
|
|
17
|
+
* @param url
|
|
18
|
+
* @param options
|
|
19
|
+
* @returns Returns the URL in the cache that matches the request,
|
|
20
|
+
* if possible.
|
|
21
|
+
*
|
|
22
|
+
* @private
|
|
23
|
+
*/
|
|
24
|
+
export const getCacheKeyForURL = (url: string, options: PrecacheRouteOptions): string | undefined => {
|
|
25
|
+
const precacheController = getSingletonPrecacheController();
|
|
26
|
+
|
|
27
|
+
const urlsToCacheKeys = precacheController.getURLsToCacheKeys();
|
|
28
|
+
for (const possibleURL of generateURLVariations(url, options)) {
|
|
29
|
+
const possibleCacheKey = urlsToCacheKeys.get(possibleURL);
|
|
30
|
+
if (possibleCacheKey) {
|
|
31
|
+
return possibleCacheKey;
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
return undefined;
|
|
36
|
+
};
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
/*
|
|
2
|
+
Copyright 2018 Google LLC
|
|
3
|
+
|
|
4
|
+
Use of this source code is governed by an MIT-style
|
|
5
|
+
license that can be found in the LICENSE file or at
|
|
6
|
+
https://opensource.org/licenses/MIT.
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
import type { MapLikeObject } from "../../types.js";
|
|
10
|
+
import { type MessageKey, messages } from "./messages.js";
|
|
11
|
+
|
|
12
|
+
const fallback = (code: string, ...args: any[]) => {
|
|
13
|
+
let msg = code;
|
|
14
|
+
if (args.length > 0) {
|
|
15
|
+
msg += ` :: ${JSON.stringify(args)}`;
|
|
16
|
+
}
|
|
17
|
+
return msg;
|
|
18
|
+
};
|
|
19
|
+
|
|
20
|
+
const generatorFunction = (code: MessageKey, details: MapLikeObject = {}) => {
|
|
21
|
+
const message = messages[code];
|
|
22
|
+
if (!message) {
|
|
23
|
+
throw new Error(`Unable to find message for code '${code}'.`);
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
return message(details);
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
export const messageGenerator = process.env.NODE_ENV === "production" ? fallback : generatorFunction;
|