minip-bridge 1.0.0

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/.editorconfig ADDED
@@ -0,0 +1,12 @@
1
+ # EditorConfig is awesome: https://EditorConfig.org
2
+
3
+ # top-most EditorConfig file
4
+ root = true
5
+
6
+ [*]
7
+ indent_style = space
8
+ indent_size = 2
9
+ end_of_line = crlf
10
+ charset = utf-8
11
+ trim_trailing_whitespace = false
12
+ insert_final_newline = false
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025 Yosorable
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1 @@
1
+ # minip-bridge
package/package.json ADDED
@@ -0,0 +1,24 @@
1
+ {
2
+ "name": "minip-bridge",
3
+ "version": "1.0.0",
4
+ "description": "minip webview bridge",
5
+ "main": "index.ts",
6
+ "module": "./dist/index.mjs",
7
+ "types": "./dist/index.d.mts",
8
+ "scripts": {
9
+ "build": "tsup"
10
+ },
11
+ "keywords": [],
12
+ "author": "yosorable",
13
+ "license": "MIT",
14
+ "devDependencies": {
15
+ "eslint": "^9.19.0",
16
+ "prettier": "^3.4.2",
17
+ "tsup": "^8.3.6",
18
+ "typescript": "^5.7.3"
19
+ },
20
+ "repository": {
21
+ "type": "git",
22
+ "url": "https://github.com/Yosorable/minip-bridge"
23
+ }
24
+ }
package/src/api/app.ts ADDED
@@ -0,0 +1,29 @@
1
+ import jsBridge from "../bridge";
2
+ import { AppInfo, MResponse, MResponseWithData } from "../model";
3
+
4
+ export function closeApp(): Promise<MResponse> {
5
+ return jsBridge.callNative({
6
+ api: "closeApp",
7
+ });
8
+ }
9
+
10
+ export function showAppDetail(): Promise<MResponse> {
11
+ return jsBridge.callNative({
12
+ api: "showAppDetail",
13
+ });
14
+ }
15
+
16
+ export function installApp(url: string): Promise<MResponse> {
17
+ return jsBridge.callNative({
18
+ api: "installApp",
19
+ data: {
20
+ url,
21
+ },
22
+ });
23
+ }
24
+
25
+ export function getInstalledAppList(): Promise<MResponseWithData<AppInfo[]>> {
26
+ return jsBridge.callNative({
27
+ api: "getInstalledAppList",
28
+ });
29
+ }
@@ -0,0 +1,34 @@
1
+ import jsBridge from "../bridge";
2
+ import { MResponse, MResponseWithData } from "../model";
3
+
4
+ /**
5
+ *
6
+ * @param type medium as default
7
+ */
8
+ export function vibrate(
9
+ type?: "light" | "medium" | "heavy",
10
+ ): Promise<MResponse> {
11
+ return jsBridge.callNative({
12
+ api: "vibrate",
13
+ data: {
14
+ type,
15
+ },
16
+ });
17
+ }
18
+
19
+ export function getClipboardData(): Promise<MResponseWithData<string>> {
20
+ return jsBridge.callNative({
21
+ api: "getClipboardData",
22
+ });
23
+ }
24
+
25
+ export function setClipboardData(
26
+ data: string,
27
+ ): Promise<MResponseWithData<string>> {
28
+ return jsBridge.callNative({
29
+ api: "setClipboardData",
30
+ data: {
31
+ data,
32
+ },
33
+ });
34
+ }
@@ -0,0 +1,5 @@
1
+ export * from "./route";
2
+ export * from "./app";
3
+ export * from "./ui";
4
+ export * from "./kvstorage";
5
+ export * from "./device";
@@ -0,0 +1,60 @@
1
+ import jsBridge from "../bridge";
2
+ import { MResponse, MResponseWithData } from "../model";
3
+
4
+ // async methods
5
+
6
+ export function getKVStorage(key: string): Promise<MResponseWithData<string>> {
7
+ return jsBridge.callNative({
8
+ api: "getKVStorage",
9
+ data: { key },
10
+ });
11
+ }
12
+
13
+ export function setKVStorage(key: string, value: string): Promise<MResponse> {
14
+ return jsBridge.callNative({
15
+ api: "setKVStorage",
16
+ data: { key, value },
17
+ });
18
+ }
19
+
20
+ export function deleteKVStorage(key: string): Promise<MResponse> {
21
+ return jsBridge.callNative({
22
+ api: "deleteKVStorage",
23
+ data: { key },
24
+ });
25
+ }
26
+
27
+ export function clearKVStorage(): Promise<MResponse> {
28
+ return jsBridge.callNative({
29
+ api: "clearKVStorage",
30
+ });
31
+ }
32
+
33
+ // sync methods
34
+
35
+ export function getKVStorageSync(key: string): MResponseWithData<string> {
36
+ return jsBridge.callNativeSync({
37
+ api: "getKVStorageSync",
38
+ data: { key },
39
+ });
40
+ }
41
+
42
+ export function setKVStorageSync(key: string, value: string): MResponse {
43
+ return jsBridge.callNativeSync({
44
+ api: "setKVStorageSync",
45
+ data: { key, value },
46
+ });
47
+ }
48
+
49
+ export function deleteKVStorageSync(key: string): MResponse {
50
+ return jsBridge.callNativeSync({
51
+ api: "deleteKVStorageSync",
52
+ data: { key },
53
+ });
54
+ }
55
+
56
+ export function clearKVStorageSync(): MResponse {
57
+ return jsBridge.callNativeSync({
58
+ api: "clearKVStorageSync",
59
+ });
60
+ }
@@ -0,0 +1,38 @@
1
+ import jsBridge from "../bridge";
2
+ import { MResponse } from "../model";
3
+
4
+ export function navigateTo(data: {
5
+ page: string;
6
+ title?: string;
7
+ }): Promise<MResponse> {
8
+ return jsBridge.callNative({
9
+ api: "navigateTo",
10
+ data: data,
11
+ });
12
+ }
13
+
14
+ export function navigateBack(delta: number = 1): Promise<MResponse> {
15
+ return jsBridge.callNative({
16
+ api: "navigateBack",
17
+ data: {
18
+ delta,
19
+ },
20
+ });
21
+ }
22
+
23
+ export function redirectTo(data: {
24
+ page: string;
25
+ title?: string;
26
+ }): Promise<MResponse> {
27
+ return jsBridge.callNative({
28
+ api: "redirectTo",
29
+ data: data,
30
+ });
31
+ }
32
+
33
+ export function openWebsite(url: string): Promise<MResponse> {
34
+ return jsBridge.callNative({
35
+ api: "openWebsite",
36
+ data: { url },
37
+ });
38
+ }
package/src/api/ui.ts ADDED
@@ -0,0 +1,102 @@
1
+ import jsBridge from "../bridge";
2
+ import {
3
+ AlertConfig,
4
+ MResponse,
5
+ MResponseWithData,
6
+ ShowHUDRequest,
7
+ } from "../model";
8
+
9
+ // navigation bar
10
+
11
+ export function setNavigationBarTitle(title: string): Promise<MResponse> {
12
+ return jsBridge.callNative({
13
+ api: "setNavigationBarTitle",
14
+ data: {
15
+ title,
16
+ },
17
+ });
18
+ }
19
+
20
+ export function setNavigationBarColor(config: {
21
+ foregroundColor: string;
22
+ backgroundColor: string;
23
+ }): Promise<MResponse> {
24
+ return jsBridge.callNative({
25
+ api: "setNavigationBarColor",
26
+ data: config,
27
+ });
28
+ }
29
+
30
+ // pulldown refresh
31
+
32
+ export function enablePullDownRefresh(): Promise<MResponse> {
33
+ return jsBridge.callNative({
34
+ api: "enablePullDownRefresh",
35
+ });
36
+ }
37
+
38
+ export function disablePullDownRefresh(): Promise<MResponse> {
39
+ return jsBridge.callNative({
40
+ api: "disablePullDownRefresh",
41
+ });
42
+ }
43
+
44
+ export function onPullDownRefresh(callback: (e: Event) => any) {
45
+ window.addEventListener("pulldownrefresh", callback);
46
+ }
47
+
48
+ export function startPullDownRefresh(): Promise<MResponse> {
49
+ return jsBridge.callNative({
50
+ api: "startPullDownRefresh",
51
+ });
52
+ }
53
+
54
+ export function stopPullDownRefresh(): Promise<MResponse> {
55
+ return jsBridge.callNative({
56
+ api: "stopPullDownRefresh",
57
+ });
58
+ }
59
+
60
+ // HUD
61
+
62
+ export function showHUD(req: ShowHUDRequest): Promise<MResponse> {
63
+ return jsBridge.callNative({
64
+ api: "showHUD",
65
+ data: req,
66
+ });
67
+ }
68
+
69
+ export function hideHUD(): Promise<MResponse> {
70
+ return jsBridge.callNative({
71
+ api: "hideHUD",
72
+ });
73
+ }
74
+
75
+ // alert
76
+ export function showAlert(
77
+ config: AlertConfig,
78
+ ): Promise<MResponseWithData<string>> {
79
+ return jsBridge.callNative({
80
+ api: "showAlert",
81
+ data: config,
82
+ });
83
+ }
84
+
85
+ // media
86
+ export function previewImage(url: string): Promise<MResponse> {
87
+ return jsBridge.callNative({
88
+ api: "previewImage",
89
+ data: {
90
+ url,
91
+ },
92
+ });
93
+ }
94
+
95
+ export function previewVideo(url: string): Promise<MResponse> {
96
+ return jsBridge.callNative({
97
+ api: "previewVideo",
98
+ data: {
99
+ url,
100
+ },
101
+ });
102
+ }
@@ -0,0 +1,71 @@
1
+ import { MResponseStatusCode } from "../model";
2
+ import { MRequest } from "../model/request";
3
+
4
+ interface Callable {
5
+ postMessage: (data: string) => Promise<string>;
6
+ }
7
+
8
+ declare global {
9
+ interface Window {
10
+ webkit?: {
11
+ messageHandlers?: {
12
+ MinipNativeInteraction?: Callable;
13
+ };
14
+ };
15
+ }
16
+ }
17
+
18
+ let jsBridge: {
19
+ callNative: <T>(req?: MRequest<T>) => Promise<any>;
20
+ callNativeSync: <T>(req?: MRequest<T>) => any;
21
+ };
22
+
23
+ // iOS
24
+ if (
25
+ window.webkit &&
26
+ window.webkit.messageHandlers &&
27
+ window.webkit.messageHandlers.MinipNativeInteraction
28
+ ) {
29
+ const _callNative = window.webkit.messageHandlers.MinipNativeInteraction;
30
+ jsBridge = {
31
+ callNative(req) {
32
+ return _callNative
33
+ .postMessage(JSON.stringify(req))
34
+ .then((res) => JSON.parse(res))
35
+ .then((res) => {
36
+ if (res.code === MResponseStatusCode.SUCCESS) {
37
+ return res;
38
+ } else {
39
+ throw new Error(res.msg ?? "Unknown error, res: ");
40
+ }
41
+ });
42
+ },
43
+ callNativeSync(req) {
44
+ const res = prompt(JSON.stringify(req));
45
+ if (res) {
46
+ return JSON.parse(res);
47
+ }
48
+ return {
49
+ code: MResponseStatusCode.FAILED,
50
+ msg: "Unknown error",
51
+ };
52
+ },
53
+ };
54
+ } else {
55
+ // error
56
+ jsBridge = {
57
+ callNative() {
58
+ return new Promise((_, reject) => {
59
+ reject("Cannot find JavaScript Bridge!!!");
60
+ });
61
+ },
62
+ callNativeSync() {
63
+ return {
64
+ code: MResponseStatusCode.FAILED,
65
+ msg: "Cannot find JavaScript Bridge!!!",
66
+ };
67
+ },
68
+ };
69
+ }
70
+
71
+ export default jsBridge;
package/src/index.ts ADDED
@@ -0,0 +1,3 @@
1
+ export * from "./api";
2
+ export * from "./model";
3
+ export * from "./bridge";
@@ -0,0 +1,50 @@
1
+ export interface AppInfo {
2
+ name: string;
3
+ appId: string;
4
+ author?: string;
5
+ website?: string;
6
+ icon?: string;
7
+ version?: string; // v{x.x.x}, like v0.0.1
8
+ description?: string;
9
+ homepage: string;
10
+ title?: string; // homepage title
11
+ pages?: PageConfig[]; // unused
12
+ tabs?: TabConfig[];
13
+ navigationBarStatus?: string; // display (default), hidden
14
+ colorScheme?: string; // dark, light (default auto)
15
+
16
+ // can be overridden in PageConfig
17
+ backgroundColor?: string; // hex string
18
+ navigationBarColor?: string;
19
+ tintColor?: string; // hex string
20
+
21
+ // web server
22
+ webServerEnabled?: boolean;
23
+ // orientation
24
+ landscape?: boolean;
25
+
26
+ // file list
27
+ files?: File[];
28
+ }
29
+
30
+ interface PageConfig {
31
+ path: string;
32
+ title?: string;
33
+ scrollable?: boolean;
34
+
35
+ // override
36
+ backgroundColor?: string;
37
+ navigationBarColor?: string;
38
+ }
39
+
40
+ interface TabConfig {
41
+ path: string;
42
+ title: string;
43
+ systemImage: string;
44
+ }
45
+
46
+ interface File {
47
+ name: string;
48
+ path: string;
49
+ hash: string;
50
+ }
@@ -0,0 +1,3 @@
1
+ export * from "./request";
2
+ export * from "./response";
3
+ export * from "./appinfo";
@@ -0,0 +1,14 @@
1
+ export interface MRequestBase {
2
+ /**
3
+ * Api name.
4
+ */
5
+ api: string;
6
+ }
7
+
8
+ export interface MRequestWithData<T> extends MRequestBase {
9
+ data?: T;
10
+ }
11
+
12
+ export type MRequest<T> = MRequestBase | MRequestWithData<T>;
13
+
14
+ export * from "./ui";
@@ -0,0 +1,26 @@
1
+ export type HUDType = "success" | "error" | "progress" | "label";
2
+ export interface ShowHUDRequest {
3
+ type?: HUDType;
4
+ title?: string;
5
+ subTitle?: string;
6
+ /**
7
+ * ms
8
+ */
9
+ delay?: number;
10
+ }
11
+
12
+ /**
13
+ * @param title use key if not set
14
+ * @param key callback arg
15
+ */
16
+ export interface AlertAction {
17
+ title?: string;
18
+ style?: "cancel" | "destructive";
19
+ key: string;
20
+ }
21
+ export interface AlertConfig {
22
+ title?: string;
23
+ message?: string;
24
+ preferredStyle?: "alert" | "actionSheet";
25
+ actions: AlertAction[];
26
+ }
@@ -0,0 +1,20 @@
1
+ export enum MResponseStatusCode {
2
+ SUCCESS = 0,
3
+ FAILED = 7,
4
+ }
5
+
6
+ export interface MResponse {
7
+ /**
8
+ * Integer, 0 for succeeded, other for failed.
9
+ * Auto reject in promise when failed.
10
+ */
11
+ code: MResponseStatusCode;
12
+ /**
13
+ * Error msg or other info.
14
+ */
15
+ msg?: string;
16
+ }
17
+
18
+ export interface MResponseWithData<T> extends MResponse {
19
+ data: T;
20
+ }
package/tsconfig.json ADDED
@@ -0,0 +1,16 @@
1
+ {
2
+ "compilerOptions": {
3
+ "target": "ESNext",
4
+ "module": "ESNext",
5
+ "lib": ["ESNext", "DOM"],
6
+ "moduleResolution": "node",
7
+ "declaration": true,
8
+ "declarationMap": true,
9
+ "sourceMap": true,
10
+ "outDir": "dist",
11
+ "strict": true,
12
+ "esModuleInterop": true,
13
+ "skipLibCheck": true
14
+ },
15
+ "include": ["src"]
16
+ }
package/tsup.config.ts ADDED
@@ -0,0 +1,9 @@
1
+ import { defineConfig } from "tsup";
2
+
3
+ export default defineConfig({
4
+ entry: ["src/index.ts"],
5
+ format: ["esm"],
6
+ dts: true,
7
+ sourcemap: true,
8
+ clean: true,
9
+ });