diff options
author | toasted-nutbread <toasted-nutbread@users.noreply.github.com> | 2023-12-28 00:48:33 -0500 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-12-28 05:48:33 +0000 |
commit | 76805bc0fc65452ca830623aa810888f9c476a2b (patch) | |
tree | d91e257fd335c75dfca1a37784eb12769fbb5a66 /types | |
parent | fc2123a45b3ceacc2ec887d24e5e752dca59bb4f (diff) |
API type safety updates (#457)
* Update message handlers in SearchDisplayController
* Update types
* Updates
* Updates
* Simplify
* Updates
* Updates
* Rename
* Improve types
* Improve types
* Resolve TODOs
Diffstat (limited to 'types')
-rw-r--r-- | types/ext/api.d.ts | 7 | ||||
-rw-r--r-- | types/ext/application.d.ts | 148 | ||||
-rw-r--r-- | types/ext/extension.d.ts | 11 | ||||
-rw-r--r-- | types/ext/frontend.d.ts | 8 |
4 files changed, 152 insertions, 22 deletions
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 <https://www.gnu.org/licenses/>. + */ + +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<TName extends ApiNames> = BaseApiParams<ApiSurface[TName]>; + +export type ApiNames = BaseApiNames<ApiSurface>; + +export type ApiMessageNoFrameId<TName extends ApiNames> = ( + ApiParams<TName> extends void ? + {action: TName, params?: never} : + {action: TName, params: ApiParams<TName>} +); + +export type ApiMessage<TName extends ApiNames> = ApiMessageNoFrameId<TName> & { + /** + * 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<name>}[ApiNames]; + +export type ApiMessageAny = {[name in ApiNames]: ApiMessage<name>}[ApiNames]; + +export type ApiMap = BaseApiMap<ApiSurface>; + +export type ApiHandler<TName extends ApiNames> = BaseApiHandler<ApiSurface[TName]>; + +export type ApiReturn<TName extends ApiNames> = BaseApiReturn<ApiSurface[TName]>; 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<TMessage = ChromeRuntimeMessage> = ( +export type ChromeRuntimeOnMessageCallback<TMessage = unknown> = ( 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; -}; |