aboutsummaryrefslogtreecommitdiff
path: root/types
diff options
context:
space:
mode:
authortoasted-nutbread <toasted-nutbread@users.noreply.github.com>2023-12-22 20:27:10 -0500
committerGitHub <noreply@github.com>2023-12-23 01:27:10 +0000
commita72b53de011d35602c0f68577c30e28386046583 (patch)
tree0fd3f2e38b36b81c773bd518c99ce5f01cae32eb /types
parentcb4499fd8ab0322fa3ab706adfb46caf21c57eec (diff)
Type safety for TemplateRendererProxy (#428)
* Type safety for TemplateRendererProxy * Simplify
Diffstat (limited to 'types')
-rw-r--r--types/ext/template-renderer-frame-api.d.ts24
-rw-r--r--types/ext/template-renderer-proxy.d.ts102
2 files changed, 102 insertions, 24 deletions
diff --git a/types/ext/template-renderer-frame-api.d.ts b/types/ext/template-renderer-frame-api.d.ts
deleted file mode 100644
index e00a0711..00000000
--- a/types/ext/template-renderer-frame-api.d.ts
+++ /dev/null
@@ -1,24 +0,0 @@
-/*
- * 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 * as Core from './core';
-
-export type MessageData = {
- action: string;
- params: Core.SerializableObject;
- id: string;
-};
diff --git a/types/ext/template-renderer-proxy.d.ts b/types/ext/template-renderer-proxy.d.ts
new file mode 100644
index 00000000..74e95b54
--- /dev/null
+++ b/types/ext/template-renderer-proxy.d.ts
@@ -0,0 +1,102 @@
+/*
+ * 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 {Response} from 'core';
+import type {RenderMode, NoteData} from 'anki-templates';
+import type {CompositeRenderData, PartialOrCompositeRenderData, RenderMultiItem, RenderResult} from 'template-renderer';
+import type {
+ ApiMap as BaseApiMap,
+ ApiMapInit as BaseApiMapInit,
+ ApiHandler as BaseApiHandler,
+ ApiParams as BaseApiParams,
+ ApiNames as BaseApiNames,
+ ApiReturn as BaseApiReturn,
+ ApiReturnAny as BaseApiReturnAny,
+} from './api-map';
+
+// Frontend API
+
+type FrontendApiSurface = {
+ render: {
+ params: {
+ template: string;
+ data: PartialOrCompositeRenderData;
+ type: RenderMode;
+ };
+ return: RenderResult;
+ };
+ renderMulti: {
+ params: {
+ items: RenderMultiItem[];
+ };
+ return: Response<RenderResult>[];
+ };
+ getModifiedData: {
+ params: {
+ data: CompositeRenderData;
+ type: RenderMode;
+ };
+ return: NoteData;
+ };
+};
+
+type FrontendApiParams<TName extends FrontendApiNames> = BaseApiParams<FrontendApiSurface[TName]>;
+
+type FrontendApiNames = BaseApiNames<FrontendApiSurface>;
+
+type FrontendApiReturnAny = BaseApiReturnAny<FrontendApiSurface>;
+
+export type FrontendMessage<TName extends FrontendApiNames> = {
+ action: TName;
+ params: FrontendApiParams<TName>;
+ id: string;
+};
+
+export type FrontendMessageAny = FrontendMessage<FrontendApiNames>;
+
+export type FrontendApiReturn<TName extends FrontendApiNames> = BaseApiReturn<FrontendApiSurface[TName]>;
+
+export type FrontendApiMap = BaseApiMap<FrontendApiSurface>;
+
+export type FrontendApiMapInit = BaseApiMapInit<FrontendApiSurface>;
+
+export type FrontendApiHandler<TName extends FrontendApiNames> = BaseApiHandler<FrontendApiSurface[TName]>;
+
+// Backend API
+
+export type BackendApiSurface = {
+ ready: {
+ params: void;
+ return: void;
+ };
+ response: {
+ params: Response<FrontendApiReturnAny>;
+ return: void;
+ };
+};
+
+type BackendApiNames = BaseApiNames<BackendApiSurface>;
+
+type BackendApiParams<TName extends BackendApiNames> = BaseApiParams<BackendApiSurface[TName]>;
+
+export type BackendMessage<TName extends BackendApiNames> = {
+ action: TName;
+ params: BackendApiParams<TName>;
+ id: string | null;
+};
+
+export type BackendMessageAny = BackendMessage<BackendApiNames>;