Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Route/Handler TypeScript normalization #2548

Merged
merged 8 commits into from
Jun 30, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ class BroadcastCacheUpdate {
* @param {Request} options.request The request.
* @param {string} options.cacheName Name of the cache the responses belong
* to. This is included in the broadcast message.
* @param {Event} [options.event] event An optional event that triggered
* @param {Event} options.event event The event that triggered
* this possible cache update.
* @return {Promise} Resolves once the update is sent.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ class BroadcastUpdatePlugin implements WorkboxPlugin {
* @param {Response} [options.oldResponse] The previous cached value, if any.
* @param {Response} options.newResponse The new value in the cache.
* @param {Request} options.request The request that triggered the update.
* @param {Request} [options.event] The event that triggered the update.
* @param {Request} options.event The event that triggered the update.
*/
cacheDidUpdate: WorkboxPlugin['cacheDidUpdate'] = async (options) => {
dontWaitFor(this._broadcastUpdate.notifyIfUpdated(options));
Expand Down
89 changes: 56 additions & 33 deletions packages/workbox-core/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@ export type PluginState = MapLikeObject;
* Options passed to a `RouteMatchCallback` function.
*/
export interface RouteMatchCallbackOptions {
url: URL;
sameOrigin: boolean;
event: ExtendableEvent;
request: Request;
event?: ExtendableEvent;
sameOrigin: boolean;
url: URL;
}

/**
Expand All @@ -48,12 +48,23 @@ export interface RouteMatchCallback {
* Options passed to a `RouteHandlerCallback` function.
*/
export interface RouteHandlerCallbackOptions {
request: Request | string;
url?: URL;
event: ExtendableEvent;
request: Request;
url: URL;
params?: string[] | MapLikeObject;
event?: ExtendableEvent;
}

/**
* Options passed to a `ManualHandlerCallback` function.
*/
export interface ManualHandlerCallbackOptions {
philipwalton marked this conversation as resolved.
Show resolved Hide resolved
event: ExtendableEvent;
request: Request | string;
}

export type HandlerCallbackOptions =
philipwalton marked this conversation as resolved.
Show resolved Hide resolved
RouteHandlerCallbackOptions | ManualHandlerCallbackOptions;

/**
* The "handler" callback is invoked whenever a `Router` matches a URL/Request
* to a `Route` via its `RouteMatchCallback`. This handler callback should
Expand All @@ -66,6 +77,18 @@ export interface RouteHandlerCallback {
(options: RouteHandlerCallbackOptions): Promise<Response>;
}

/**
* The "handler" callback is invoked whenever a `Router` matches a URL/Request
* to a `Route` via its `RouteMatchCallback`. This handler callback should
* return a `Promise` that resolves with a `Response`.
*
* If a non-empty array or object is returned by the `RouteMatchCallback` it
* will be passed in as this handler's `options.params` argument.
*/
export interface ManualHandlerCallback {
(options: ManualHandlerCallbackOptions): Promise<Response>;
}

/**
* An object with a `handle` method of type `RouteHandlerCallback`.
*
Expand All @@ -85,7 +108,7 @@ export type RouteHandler = RouteHandlerCallback | RouteHandlerObject;

export interface HandlerWillStartCallbackParam {
request: Request;
event?: ExtendableEvent;
event: ExtendableEvent;
state?: PluginState;
}

Expand All @@ -95,10 +118,10 @@ export interface HandlerWillStartCallback {

export interface CacheDidUpdateCallbackParam {
cacheName: string;
oldResponse?: Response | null;
newResponse: Response;
request: Request;
event?: ExtendableEvent;
event: ExtendableEvent;
oldResponse?: Response | null;
state?: PluginState;
}

Expand All @@ -107,10 +130,10 @@ export interface CacheDidUpdateCallback {
}

export interface CacheKeyWillBeUsedCallbackParam {
request: Request;
mode: string;
request: Request;
event: ExtendableEvent;
params?: any;
event?: ExtendableEvent;
state?: PluginState;
}

Expand All @@ -119,9 +142,9 @@ export interface CacheKeyWillBeUsedCallback {
}

export interface CacheWillUpdateCallbackParam {
response: Response;
request: Request;
event?: ExtendableEvent;
response: Response;
event: ExtendableEvent;
state?: PluginState;
}

Expand All @@ -132,9 +155,9 @@ export interface CacheWillUpdateCallback {
export interface CachedResponseWillBeUsedCallbackParam {
cacheName: string;
request: Request;
matchOptions?: CacheQueryOptions;
cachedResponse?: Response;
event?: ExtendableEvent;
event: ExtendableEvent;
matchOptions?: CacheQueryOptions;
state?: PluginState;
}

Expand All @@ -143,10 +166,10 @@ export interface CachedResponseWillBeUsedCallback {
}

export interface FetchDidFailCallbackParam {
originalRequest: Request;
error: Error;
originalRequest: Request;
request: Request;
event?: ExtendableEvent;
event: ExtendableEvent;
state?: PluginState;
}

Expand All @@ -157,7 +180,7 @@ export interface FetchDidFailCallback {
export interface FetchDidSucceedCallbackParam {
request: Request;
response: Response;
event?: ExtendableEvent;
event: ExtendableEvent;
state?: PluginState;
}

Expand All @@ -167,7 +190,7 @@ export interface FetchDidSucceedCallback {

export interface RequestWillFetchCallbackParam {
request: Request;
event?: ExtendableEvent;
event: ExtendableEvent;
state?: PluginState;
}

Expand All @@ -178,7 +201,7 @@ export interface RequestWillFetchCallback {
export interface HandlerWillRespondCallbackParam {
request: Request;
response: Response;
event?: ExtendableEvent;
event: ExtendableEvent;
state?: PluginState;
}

Expand All @@ -188,8 +211,8 @@ export interface HandlerWillRespondCallback {

export interface HandlerDidRespondCallbackParam {
request: Request;
event: ExtendableEvent;
response?: Response;
event?: ExtendableEvent;
state?: PluginState;
}

Expand All @@ -199,9 +222,9 @@ export interface HandlerDidRespondCallback {

export interface HandlerDidCompleteCallbackParam {
request: Request;
response?: Response;
error?: Error;
event?: ExtendableEvent;
event: ExtendableEvent;
response?: Response;
state?: PluginState;
}

Expand All @@ -214,29 +237,29 @@ export interface HandlerDidCompleteCallback {
* cache operations.
*/
export interface WorkboxPlugin {
handlerWillStart?: HandlerWillStartCallback;
cacheDidUpdate?: CacheDidUpdateCallback;
cachedResponseWillBeUsed?: CachedResponseWillBeUsedCallback;
cacheKeyWillBeUsed?: CacheKeyWillBeUsedCallback;
cacheWillUpdate?: CacheWillUpdateCallback;
cachedResponseWillBeUsed?: CachedResponseWillBeUsedCallback;
fetchDidFail?: FetchDidFailCallback;
fetchDidSucceed?: FetchDidSucceedCallback;
requestWillFetch?: RequestWillFetchCallback;
handlerWillRespond?: HandlerWillRespondCallback;
handlerDidRespond?: HandlerDidRespondCallback;
handlerDidComplete?: HandlerDidCompleteCallback;
handlerDidRespond?: HandlerDidRespondCallback;
handlerWillRespond?: HandlerWillRespondCallback;
handlerWillStart?: HandlerWillStartCallback;
requestWillFetch?: RequestWillFetchCallback;
}

export interface WorkboxPluginCallbackParam {
handlerWillStart: HandlerWillStartCallbackParam;
cacheDidUpdate: CacheDidUpdateCallbackParam;
cachedResponseWillBeUsed: CachedResponseWillBeUsedCallbackParam;
cacheKeyWillBeUsed: CacheKeyWillBeUsedCallbackParam;
cacheWillUpdate: CacheWillUpdateCallbackParam;
cachedResponseWillBeUsed: CachedResponseWillBeUsedCallbackParam;
fetchDidFail: FetchDidFailCallbackParam;
fetchDidSucceed: FetchDidSucceedCallbackParam;
requestWillFetch: RequestWillFetchCallbackParam;
handlerWillRespond: HandlerWillRespondCallbackParam;
handlerDidRespond: HandlerDidRespondCallbackParam;
handlerDidComplete: HandlerDidCompleteCallbackParam;
handlerDidRespond: HandlerDidRespondCallbackParam;
handlerWillRespond: HandlerWillRespondCallbackParam;
handlerWillStart: HandlerWillStartCallbackParam;
requestWillFetch: RequestWillFetchCallbackParam;
}
10 changes: 5 additions & 5 deletions packages/workbox-precaching/src/PrecacheController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -235,15 +235,15 @@ class PrecacheController {
* install event.
*
* @param {Object} options
* @param {Event} [options.event] The install event (if needed).
* @param {Event} options.event The install event.
* @param {Array<Object>} [options.plugins] Plugins to be used for fetching
* and caching during install.
* @return {Promise<module:workbox-precaching.InstallResult>}
*/
async install({event, plugins}: {
event?: ExtendableEvent;
event: ExtendableEvent;
plugins?: WorkboxPlugin[];
} = {}) {
}) {
if (process.env.NODE_ENV !== 'production') {
if (plugins) {
assert!.isArray(plugins, {
Expand Down Expand Up @@ -339,16 +339,16 @@ class PrecacheController {
* @param {Object} options
* @param {string} options.cacheKey The string to use a cache key.
* @param {string} options.url The URL to fetch and cache.
* @param {Event} options.event The install event.
* @param {string} [options.cacheMode] The cache mode for the network request.
* @param {Event} [options.event] The install event (if passed).
* @param {string} [options.integrity] The value to use for the `integrity`
* field when making the request.
*/
async _addURLToCache({cacheKey, url, cacheMode, event, integrity}: {
cacheKey: string;
url: string;
cacheMode: "reload" | "default" | "no-store" | "no-cache" | "force-cache" | "only-if-cached" | undefined;
event?: ExtendableEvent;
event: ExtendableEvent;
integrity?: string;
}) {
const request = new Request(url, {
Expand Down
2 changes: 1 addition & 1 deletion packages/workbox-precaching/src/utils/cacheWrapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import '../_version.js';
interface MatchWrapperOptions {
cacheName: string;
request: Request;
event?: ExtendableEvent;
event: ExtendableEvent;
plugins?: WorkboxPlugin[];
matchOptions?: CacheQueryOptions;
}
Expand Down
2 changes: 1 addition & 1 deletion packages/workbox-precaching/src/utils/fetchWrapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import '../_version.js';

interface WrappedFetchOptions {
request: Request | string;
event?: ExtendableEvent;
event: ExtendableEvent;
plugins?: WorkboxPlugin[];
fetchOptions?: RequestInit;
}
Expand Down
10 changes: 6 additions & 4 deletions packages/workbox-routing/src/NavigationRoute.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@

import {assert} from 'workbox-core/_private/assert.js';
import {logger} from 'workbox-core/_private/logger.js';
import {RouteHandler, RouteMatchCallbackOptions} from 'workbox-core/types.js';

import {Route} from './Route.js';
import {Handler, MatchCallbackOptions} from './_types.js';

import './_version.js';

export interface NavigationRouteMatchOptions {
Expand Down Expand Up @@ -55,7 +57,7 @@ class NavigationRoute extends Route {
* match the URL's pathname and search parameter, the route will handle the
* request (assuming the denylist doesn't match).
*/
constructor(handler: Handler,
constructor(handler: RouteHandler,
{allowlist = [/./], denylist = []}: NavigationRouteMatchOptions = {}) {
if (process.env.NODE_ENV !== 'production') {
assert!.isArrayOfClass(allowlist, RegExp, {
Expand All @@ -72,7 +74,7 @@ class NavigationRoute extends Route {
});
}

super((options: MatchCallbackOptions) => this._match(options), handler);
super((options: RouteMatchCallbackOptions) => this._match(options), handler);

this._allowlist = allowlist;
this._denylist = denylist;
Expand All @@ -88,7 +90,7 @@ class NavigationRoute extends Route {
*
* @private
*/
private _match({url, request}: MatchCallbackOptions): boolean {
private _match({url, request}: RouteMatchCallbackOptions): boolean {
if (request && request.mode !== 'navigate') {
return false;
}
Expand Down
9 changes: 6 additions & 3 deletions packages/workbox-routing/src/RegExpRoute.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,12 @@

import {assert} from 'workbox-core/_private/assert.js';
import {logger} from 'workbox-core/_private/logger.js';
import {RouteHandler, RouteMatchCallback, RouteMatchCallbackOptions}
from 'workbox-core/types.js';

import {HTTPMethod} from './utils/constants.js';
import {Route} from './Route.js';
import {Handler, MatchCallbackOptions, MatchCallback} from './_types.js';

import './_version.js';


Expand Down Expand Up @@ -41,7 +44,7 @@ class RegExpRoute extends Route {
* @param {string} [method='GET'] The HTTP method to match the Route
* against.
*/
constructor(regExp: RegExp, handler: Handler, method?: HTTPMethod) {
constructor(regExp: RegExp, handler: RouteHandler, method?: HTTPMethod) {
if (process.env.NODE_ENV !== 'production') {
assert!.isInstance(regExp, RegExp, {
moduleName: 'workbox-routing',
Expand All @@ -51,7 +54,7 @@ class RegExpRoute extends Route {
});
}

const match: MatchCallback = ({url}: MatchCallbackOptions) => {
const match: RouteMatchCallback = ({url}: RouteMatchCallbackOptions) => {
const result = regExp.exec(url.href);

// Return immediately if there's no match.
Expand Down
11 changes: 6 additions & 5 deletions packages/workbox-routing/src/Route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
import {assert} from 'workbox-core/_private/assert.js';
import {HTTPMethod, defaultMethod, validMethods} from './utils/constants.js';
import {normalizeHandler} from './utils/normalizeHandler.js';
import {Handler, HandlerObject, MatchCallback} from './_types.js';
import {RouteHandler, RouteHandlerObject, RouteMatchCallback}
from 'workbox-core/types.js';
import './_version.js';


Expand All @@ -23,8 +24,8 @@ import './_version.js';
* @memberof module:workbox-routing
*/
class Route {
handler: HandlerObject;
match: MatchCallback;
handler: RouteHandlerObject;
match: RouteMatchCallback;
method: HTTPMethod;

/**
Expand All @@ -39,8 +40,8 @@ class Route {
* against.
*/
constructor(
match: MatchCallback,
handler: Handler,
match: RouteMatchCallback,
handler: RouteHandler,
method: HTTPMethod = defaultMethod) {
if (process.env.NODE_ENV !== 'production') {
assert!.isType(match, 'function', {
Expand Down
Loading