From 76805bc0fc65452ca830623aa810888f9c476a2b Mon Sep 17 00:00:00 2001 From: toasted-nutbread Date: Thu, 28 Dec 2023 00:48:33 -0500 Subject: API type safety updates (#457) * Update message handlers in SearchDisplayController * Update types * Updates * Updates * Simplify * Updates * Updates * Rename * Improve types * Improve types * Resolve TODOs --- types/ext/api.d.ts | 7 +-- types/ext/application.d.ts | 148 +++++++++++++++++++++++++++++++++++++++++++++ types/ext/extension.d.ts | 11 +--- types/ext/frontend.d.ts | 8 --- 4 files changed, 152 insertions(+), 22 deletions(-) create mode 100644 types/ext/application.d.ts (limited to 'types/ext') diff --git a/types/ext/api.d.ts b/types/ext/api.d.ts index ad3aa22c..46dfbdc2 100644 --- a/types/ext/api.d.ts +++ b/types/ext/api.d.ts @@ -31,6 +31,7 @@ import type * as Settings from './settings'; import type * as SettingsModifications from './settings-modifications'; import type * as Translation from './translation'; import type * as Translator from './translator'; +import type {ApiMessageNoFrameIdAny as ApplicationApiMessageNoFrameIdAny} from './application'; import type { ApiMap as BaseApiMap, ApiMapInit as BaseApiMapInit, @@ -220,15 +221,13 @@ type ApiSurface = { sendMessageToFrame: { params: { frameId: number; - action: string; - params?: Core.SerializableObject; + message: ApplicationApiMessageNoFrameIdAny; }; return: boolean; }; broadcastTab: { params: { - action: string; - params?: Core.SerializableObject; + message: ApplicationApiMessageNoFrameIdAny; }; return: boolean; }; diff --git a/types/ext/application.d.ts b/types/ext/application.d.ts new file mode 100644 index 00000000..ac594abc --- /dev/null +++ b/types/ext/application.d.ts @@ -0,0 +1,148 @@ +/* + * Copyright (C) 2023 Yomitan Authors + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +import type {TokenString} from './core'; +import type {SearchMode} from './display'; +import type {FrameEndpointReadyDetails, FrameEndpointConnectedDetails} from './frame-client'; +import type {DatabaseUpdateType, DatabaseUpdateCause} from './backend'; +import type { + ApiMap as BaseApiMap, + ApiHandler as BaseApiHandler, + ApiParams as BaseApiParams, + ApiNames as BaseApiNames, + ApiReturn as BaseApiReturn, +} from './api-map'; + +export type ApiSurface = { + searchDisplayControllerGetMode: { + params: void; + return: SearchMode; + }; + searchDisplayControllerSetMode: { + params: { + mode: SearchMode; + }; + return: void; + }; + searchDisplayControllerUpdateSearchQuery: { + params: { + text: string; + animate?: boolean; + }; + return: void; + }; + applicationReady: { + params: void; + return: void; + }; + applicationIsReady: { + params: void; + return: boolean; + }; + applicationBackendReady: { + params: void; + return: void; + }; + applicationGetUrl: { + params: void; + return: { + url: string; + }; + }; + applicationOptionsUpdated: { + params: { + source: string; + }; + return: void; + }; + applicationDatabaseUpdated: { + params: { + type: DatabaseUpdateType; + cause: DatabaseUpdateCause; + }; + return: void; + }; + applicationZoomChanged: { + params: { + oldZoomFactor: number; + newZoomFactor: number; + }; + return: void; + }; + frontendRequestReadyBroadcast: { + params: { + frameId: number; + }; + return: void; + }; + frontendSetAllVisibleOverride: { + params: { + value: boolean; + priority: number; + awaitFrame: boolean; + }; + return: TokenString; + }; + frontendClearAllVisibleOverride: { + params: { + token: TokenString; + }; + return: boolean; + }; + frontendReady: { + params: { + frameId: number; + }; + return: void; + }; + frameEndpointReady: { + params: FrameEndpointReadyDetails; + return: void; + }; + frameEndpointConnected: { + params: FrameEndpointConnectedDetails; + return: void; + }; +}; + +export type ApiParams = BaseApiParams; + +export type ApiNames = BaseApiNames; + +export type ApiMessageNoFrameId = ( + ApiParams extends void ? + {action: TName, params?: never} : + {action: TName, params: ApiParams} +); + +export type ApiMessage = ApiMessageNoFrameId & { + /** + * The origin frameId that sent this message. + * If sent from the backend, this value will be undefined. + */ + frameId?: number; +}; + +export type ApiMessageNoFrameIdAny = {[name in ApiNames]: ApiMessageNoFrameId}[ApiNames]; + +export type ApiMessageAny = {[name in ApiNames]: ApiMessage}[ApiNames]; + +export type ApiMap = BaseApiMap; + +export type ApiHandler = BaseApiHandler; + +export type ApiReturn = BaseApiReturn; diff --git a/types/ext/extension.d.ts b/types/ext/extension.d.ts index 1c86a4ca..5a244566 100644 --- a/types/ext/extension.d.ts +++ b/types/ext/extension.d.ts @@ -56,16 +56,7 @@ export type ContentOrigin = { frameId?: number; }; -export type ChromeRuntimeMessage = { - action: string; - params?: Core.SerializableObject; -}; - -export type ChromeRuntimeMessageWithFrameId = ChromeRuntimeMessage & { - frameId?: number; -}; - -export type ChromeRuntimeOnMessageCallback = ( +export type ChromeRuntimeOnMessageCallback = ( message: TMessage, sender: chrome.runtime.MessageSender, sendResponse: ChromeRuntimeMessageSendResponseFunction, diff --git a/types/ext/frontend.d.ts b/types/ext/frontend.d.ts index 73b24dc3..4cc8d03b 100644 --- a/types/ext/frontend.d.ts +++ b/types/ext/frontend.d.ts @@ -48,14 +48,6 @@ export type ConstructorDetails = { export type PageType = 'web' | 'popup' | 'search'; -export type FrontendRequestReadyBroadcastParams = { - frameId: number; -}; - export type GetPopupInfoResult = { popupId: string | null; }; - -export type FrontendReadyDetails = { - frameId: number; -}; -- cgit v1.2.3