@vkma.dev/bridge 0.0.0-stage → 0.2.1
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 +88 -2
- package/dist/bridge.d.ts +38 -0
- package/dist/bridge.d.ts.map +1 -0
- package/dist/bridge.js +384 -0
- package/dist/bridge.js.map +1 -0
- package/dist/deprecated.d.ts +47 -0
- package/dist/deprecated.d.ts.map +1 -0
- package/dist/index.d.ts +34 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +64 -0
- package/dist/index.js.map +1 -0
- package/dist/launch-params.d.ts +26 -0
- package/dist/launch-params.d.ts.map +1 -0
- package/dist/launch-params.js +103 -0
- package/dist/launch-params.js.map +1 -0
- package/dist/middleware.d.ts +25 -0
- package/dist/middleware.d.ts.map +1 -0
- package/dist/middleware.js +31 -0
- package/dist/middleware.js.map +1 -0
- package/dist/package.js +6 -0
- package/dist/package.js.map +1 -0
- package/dist/tap.d.ts +35 -0
- package/dist/tap.d.ts.map +1 -0
- package/dist/tap.js +39 -0
- package/dist/tap.js.map +1 -0
- package/dist/types.d.ts +214 -0
- package/dist/types.d.ts.map +1 -0
- package/package.json +61 -3
- package/src/bridge.ts +599 -0
- package/src/deprecated.ts +61 -0
- package/src/index.ts +130 -0
- package/src/launch-params.ts +131 -0
- package/src/middleware.ts +70 -0
- package/src/tap.ts +77 -0
- package/src/types.ts +271 -0
package/src/types.ts
ADDED
|
@@ -0,0 +1,271 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Bridge machinery types, built on top of the @vkma.dev/bridge-schema
|
|
3
|
+
* catalogue. The payload types and the three catalogue maps
|
|
4
|
+
* (`RequestPropsMap` / `ReceiveDataMap` / `ReceiveEventMap`) are re-exported
|
|
5
|
+
* from the schema package by the public entrypoint; everything here is the
|
|
6
|
+
* send/subscribe/event plumbing around them.
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
import type { ReceiveDataMap, ReceiveEventMap, RequestPropsMap } from "@vkma.dev/bridge-schema";
|
|
10
|
+
import type { BridgeTap, BridgeTapEvent } from "./tap.ts";
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* Name of a method that can be sent.
|
|
14
|
+
*/
|
|
15
|
+
export type AnyRequestMethodName = keyof RequestPropsMap;
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* Name of a method that can be received.
|
|
19
|
+
*/
|
|
20
|
+
export type AnyReceiveMethodName = keyof ReceiveDataMap;
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* Name of a method that can be only sent.
|
|
24
|
+
*/
|
|
25
|
+
export type AnyRequestOnlyMethodName = Exclude<AnyRequestMethodName, AnyReceiveMethodName>;
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* Name of a method that can be only received.
|
|
29
|
+
*/
|
|
30
|
+
export type AnyReceiveOnlyMethodName = Exclude<AnyReceiveMethodName, AnyRequestMethodName>;
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* Type of any method name.
|
|
34
|
+
*/
|
|
35
|
+
export type AnyMethodName = AnyRequestMethodName | AnyReceiveMethodName;
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* The name of the method that can be both sent and received.
|
|
39
|
+
*/
|
|
40
|
+
export type AnyIOMethodName = AnyRequestMethodName & AnyReceiveMethodName;
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* Getter of request properties of a method.
|
|
44
|
+
*/
|
|
45
|
+
export type RequestProps<M extends AnyRequestMethodName = AnyRequestMethodName> =
|
|
46
|
+
RequestPropsMap[M];
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* Getter of response data of a method.
|
|
50
|
+
*/
|
|
51
|
+
export type ReceiveData<M extends AnyReceiveMethodName> = ReceiveDataMap[M];
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* Property for matching a sent request with the received message.
|
|
55
|
+
*/
|
|
56
|
+
export type RequestIdProp = { request_id?: number | string };
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* Extra per-send controls on top of the classic two-argument call.
|
|
60
|
+
*/
|
|
61
|
+
export interface SendOptions {
|
|
62
|
+
/** Reject the promise if no response arrives within this many milliseconds. */
|
|
63
|
+
timeout?: number;
|
|
64
|
+
/** Reject the promise when the signal aborts. */
|
|
65
|
+
signal?: AbortSignal;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* Type of send function for methods that have props.
|
|
70
|
+
*
|
|
71
|
+
* @param method The method (event) name to send.
|
|
72
|
+
* @param props Method properties.
|
|
73
|
+
* @param options Timeout / abort controls.
|
|
74
|
+
* @returns The Promise object with response data.
|
|
75
|
+
*/
|
|
76
|
+
export type VKBridgeSend = <K extends AnyRequestMethodName>(
|
|
77
|
+
method: K,
|
|
78
|
+
props?: RequestProps<K> & RequestIdProp,
|
|
79
|
+
options?: SendOptions,
|
|
80
|
+
) => Promise<K extends AnyReceiveMethodName ? ReceiveData<K> : void>;
|
|
81
|
+
|
|
82
|
+
/**
|
|
83
|
+
* Name of the Result response event of a method (per the catalogue wiring).
|
|
84
|
+
*/
|
|
85
|
+
export type ResultResponseEventName<M extends AnyRequestMethodName> =
|
|
86
|
+
M extends keyof ReceiveEventMap ? ReceiveEventMap[M]["result"] : never;
|
|
87
|
+
|
|
88
|
+
/**
|
|
89
|
+
* Name of the Failed response event of a method (per the catalogue wiring).
|
|
90
|
+
*/
|
|
91
|
+
export type FailedResponseEventName<M extends AnyRequestMethodName> =
|
|
92
|
+
M extends keyof ReceiveEventMap ? ReceiveEventMap[M]["failed"] : never;
|
|
93
|
+
|
|
94
|
+
/**
|
|
95
|
+
* Client error data.
|
|
96
|
+
*/
|
|
97
|
+
export type ErrorDataClientError = {
|
|
98
|
+
error_code: number;
|
|
99
|
+
error_reason: string;
|
|
100
|
+
error_description?: string;
|
|
101
|
+
};
|
|
102
|
+
|
|
103
|
+
/**
|
|
104
|
+
* API error data.
|
|
105
|
+
*/
|
|
106
|
+
export type ErrorDataAPIError = {
|
|
107
|
+
error_code: number;
|
|
108
|
+
error_msg: string;
|
|
109
|
+
request_params: string[];
|
|
110
|
+
};
|
|
111
|
+
|
|
112
|
+
/**
|
|
113
|
+
* Auth error data.
|
|
114
|
+
*/
|
|
115
|
+
export type ErrorDataAuthError = {
|
|
116
|
+
error_code: number;
|
|
117
|
+
error_reason: string;
|
|
118
|
+
error_description?: string[];
|
|
119
|
+
};
|
|
120
|
+
|
|
121
|
+
/**
|
|
122
|
+
* Error data of a Failed response event.
|
|
123
|
+
*/
|
|
124
|
+
export type ErrorData =
|
|
125
|
+
| {
|
|
126
|
+
error_type: "client_error";
|
|
127
|
+
error_data: ErrorDataClientError;
|
|
128
|
+
request_id?: number | string;
|
|
129
|
+
}
|
|
130
|
+
| {
|
|
131
|
+
error_type: "api_error";
|
|
132
|
+
error_data: ErrorDataAPIError;
|
|
133
|
+
request_id?: number | string;
|
|
134
|
+
}
|
|
135
|
+
| {
|
|
136
|
+
error_type: "auth_error";
|
|
137
|
+
error_data: ErrorDataAuthError;
|
|
138
|
+
request_id?: number | string;
|
|
139
|
+
};
|
|
140
|
+
|
|
141
|
+
/**
|
|
142
|
+
* Generic event type for creating event types.
|
|
143
|
+
*/
|
|
144
|
+
export type VKBridgeEventBase<Type extends string, Data> = {
|
|
145
|
+
detail: {
|
|
146
|
+
type: Type;
|
|
147
|
+
data: Data;
|
|
148
|
+
};
|
|
149
|
+
};
|
|
150
|
+
|
|
151
|
+
/**
|
|
152
|
+
* Type of error event data.
|
|
153
|
+
*/
|
|
154
|
+
export type VKBridgeErrorEvent<M extends AnyReceiveMethodName> = VKBridgeEventBase<
|
|
155
|
+
M extends AnyRequestMethodName ? FailedResponseEventName<M> : never,
|
|
156
|
+
ErrorData
|
|
157
|
+
>;
|
|
158
|
+
|
|
159
|
+
/**
|
|
160
|
+
* Type of event that is a response to a request.
|
|
161
|
+
*/
|
|
162
|
+
export type VKBridgeIOEvent<M extends AnyIOMethodName> = {
|
|
163
|
+
[K in M]: VKBridgeEventBase<ResultResponseEventName<K>, ReceiveData<K> & RequestIdProp>;
|
|
164
|
+
}[M];
|
|
165
|
+
|
|
166
|
+
/**
|
|
167
|
+
* Type of event that is not a response to a request.
|
|
168
|
+
*/
|
|
169
|
+
export type VKBridgeReceiveOnlyEvent<M extends AnyReceiveOnlyMethodName> = {
|
|
170
|
+
[K in M]: VKBridgeEventBase<K, ReceiveData<K>>;
|
|
171
|
+
}[M];
|
|
172
|
+
|
|
173
|
+
/**
|
|
174
|
+
* Type of result event data.
|
|
175
|
+
*/
|
|
176
|
+
export type VKBridgeResultEvent<M extends AnyReceiveMethodName> = M extends AnyReceiveOnlyMethodName
|
|
177
|
+
? VKBridgeReceiveOnlyEvent<M>
|
|
178
|
+
: M extends AnyIOMethodName
|
|
179
|
+
? VKBridgeIOEvent<M>
|
|
180
|
+
: never;
|
|
181
|
+
|
|
182
|
+
/**
|
|
183
|
+
* VK Bridge event.
|
|
184
|
+
*/
|
|
185
|
+
export type VKBridgeEvent<M extends AnyReceiveMethodName> =
|
|
186
|
+
| VKBridgeErrorEvent<M>
|
|
187
|
+
| VKBridgeResultEvent<M>;
|
|
188
|
+
|
|
189
|
+
/**
|
|
190
|
+
* Type of function that can be subscribed to VK Bridge events.
|
|
191
|
+
*/
|
|
192
|
+
export type VKBridgeSubscribeHandler = (event: VKBridgeEvent<AnyReceiveMethodName>) => void;
|
|
193
|
+
|
|
194
|
+
/**
|
|
195
|
+
* VK Bridge interface — the classic drop-in surface.
|
|
196
|
+
*/
|
|
197
|
+
export interface VKBridge {
|
|
198
|
+
/**
|
|
199
|
+
* Sends an event to the runtime env and returns the Promise object with
|
|
200
|
+
* response data. In the case of Android/iOS application env is the
|
|
201
|
+
* application itself. In the case of the browser, the parent frame in
|
|
202
|
+
* which the event handlers is located.
|
|
203
|
+
*
|
|
204
|
+
* @param method The method (event) name to send.
|
|
205
|
+
* @param [props] Method properties.
|
|
206
|
+
* @param [options] Timeout / abort controls.
|
|
207
|
+
* @returns The Promise object with response data.
|
|
208
|
+
*/
|
|
209
|
+
send: VKBridgeSend;
|
|
210
|
+
|
|
211
|
+
/**
|
|
212
|
+
* @alias send
|
|
213
|
+
* @deprecated
|
|
214
|
+
*/
|
|
215
|
+
sendPromise: VKBridgeSend;
|
|
216
|
+
|
|
217
|
+
/**
|
|
218
|
+
* Adds an event listener. It will be called any time a data is received.
|
|
219
|
+
*
|
|
220
|
+
* @param listener A callback to be invoked on every event receive.
|
|
221
|
+
*/
|
|
222
|
+
subscribe: (listener: VKBridgeSubscribeHandler) => void;
|
|
223
|
+
|
|
224
|
+
/**
|
|
225
|
+
* Removes an event listener which has been subscribed for event listening.
|
|
226
|
+
*
|
|
227
|
+
* @param listener A callback to unsubscribe.
|
|
228
|
+
*/
|
|
229
|
+
unsubscribe: (listener: VKBridgeSubscribeHandler) => void;
|
|
230
|
+
|
|
231
|
+
/**
|
|
232
|
+
* Checks if a method is supported on runtime platform.
|
|
233
|
+
*
|
|
234
|
+
* @param method Method (event) name to check.
|
|
235
|
+
* @returns Result of checking.
|
|
236
|
+
* @deprecated This method is deprecated. Use supportsAsync instead.
|
|
237
|
+
*/
|
|
238
|
+
supports: <K extends AnyRequestMethodName>(method: K) => boolean;
|
|
239
|
+
|
|
240
|
+
/**
|
|
241
|
+
* Checks if a method is supported on runtime platform.
|
|
242
|
+
*
|
|
243
|
+
* @param method Method (event) name to check.
|
|
244
|
+
* @returns The Promise object with result of checking.
|
|
245
|
+
*/
|
|
246
|
+
supportsAsync: <K extends AnyRequestMethodName>(method: K) => Promise<boolean>;
|
|
247
|
+
|
|
248
|
+
/**
|
|
249
|
+
* Checks whether the runtime is a WebView.
|
|
250
|
+
*/
|
|
251
|
+
isWebView: () => boolean;
|
|
252
|
+
|
|
253
|
+
isIframe: () => boolean;
|
|
254
|
+
|
|
255
|
+
isEmbedded: () => boolean;
|
|
256
|
+
|
|
257
|
+
isStandalone: () => boolean;
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
/**
|
|
261
|
+
* The full instance surface: the classic VK Bridge plus the always-on tap
|
|
262
|
+
* and its subscription helpers.
|
|
263
|
+
*/
|
|
264
|
+
export interface VkmaBridge extends VKBridge {
|
|
265
|
+
/** The replay-buffered stream of sends, paired responses and events. */
|
|
266
|
+
tap: BridgeTap;
|
|
267
|
+
/** Subscribe to outgoing `send` calls. */
|
|
268
|
+
onSend: (listener: (event: BridgeTapEvent & { direction: "out" }) => void) => () => void;
|
|
269
|
+
/** Subscribe to everything incoming: paired responses and unsolicited events. */
|
|
270
|
+
onEvent: (listener: (event: BridgeTapEvent & { direction: "in" }) => void) => () => void;
|
|
271
|
+
}
|